MonosIOHandler.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "MonosIOHandler.h"
#include "Scanner.h"
#include "BigTermConsumer.h"
#include "DataType.h"
#include "IdealWriter.h"
#include "error.h"
#include "InputConsumer.h"
#include <cstdio>
namespace IO {
namespace Monos {
void writeRing(const VarNames& names, FILE* out);
}
namespace M = Monos;
class MonosIdealWriter : public IdealWriter {
public:
MonosIdealWriter(FILE* out): IdealWriter(out) {
}
private:
virtual void doWriteHeader(bool first) {
M::writeRing(getNames(), getFile());
fputc('[', getFile());
}
virtual void doWriteTerm(const Term& term,
const TermTranslator& translator,
bool first) {
fputs(first ? "\n " : ",\n ", getFile());
writeTermProduct(term, translator, getFile());
}
virtual void doWriteTerm(const vector<mpz_class>& term,
bool first) {
fputs(first ? "\n " : ",\n ", getFile());
writeTermProduct(term, getNames(), getFile());
}
virtual void doWriteFooter(bool wasZeroIdeal) {
fputs("\n];\n", getFile());
}
virtual void doWriteEmptyList() {
M::writeRing(getNames(), getFile());
}
};
MonosIOHandler::MonosIOHandler():
IOHandlerCommon(staticGetName(),
"Older format used by the program Monos.") {
registerInput(DataType::getMonomialIdealType());
registerInput(DataType::getMonomialIdealListType());
registerOutput(DataType::getMonomialIdealType());
registerOutput(DataType::getMonomialIdealListType());
}
const char* MonosIOHandler::staticGetName() {
return "monos";
}
BigTermConsumer* MonosIOHandler::doCreateIdealWriter(FILE* out) {
return new MonosIdealWriter(out);
}
void MonosIOHandler::doWriteTerm(const vector<mpz_class>& term,
const VarNames& names,
FILE* out) {
writeTermProduct(term, names, out);
}
void MonosIOHandler::doReadTerm(Scanner& in, InputConsumer& consumer) {
consumer.consumeTermProductNotation(in);
}
void MonosIOHandler::doReadRing(Scanner& in, VarNames& names) {
names.clear();
in.expect("vars");
if (!in.match(';')) {
do {
names.addVarSyntaxCheckUnique(in, in.readIdentifier());
} while (in.match(','));
in.expect(';');
}
}
bool MonosIOHandler::doPeekRing(Scanner& in) {
return in.peek('v');
}
void MonosIOHandler::doReadBareIdeal(Scanner& in, InputConsumer& consumer) {
consumer.beginIdeal();
in.expect('[');
if (!in.match(']')) {
do {
consumer.consumeTermProductNotation(in);
} while (in.match(','));
if (!in.match(']')) {
if (in.peekIdentifier())
in.expect('*');
else
in.expect(']');
}
}
in.expect(';');
consumer.endIdeal();
}
void M::writeRing(const VarNames& names, FILE* out) {
fputs("vars ", out);
const char* pre = "";
for (unsigned int i = 0; i < names.getVarCount(); ++i) {
fputs(pre, out);
fputs(names.getName(i).c_str(), out);
pre = ", ";
}
fputs(";\n", out);
}
}
BigTermConsumer.h
DataType.h
IdealWriter.h
InputConsumer.h
MonosIOHandler.h
Scanner.h
BigTermConsumer
Definition BigTermConsumer.h:29
DataType::getMonomialIdealListType
static const DataType & getMonomialIdealListType()
Returns the one and only instance for monomial ideal lists.
Definition DataType.cpp:54
DataType::getMonomialIdealType
static const DataType & getMonomialIdealType()
Returns the one and only instance for monomial ideals.
Definition DataType.cpp:45
IO::IOHandlerCommon::IOHandlerCommon
IOHandlerCommon(const char *formatName, const char *formatDescription)
Definition IOHandlerCommon.cpp:27
IO::IOHandlerImpl::registerInput
void registerInput(const DataType &type)
Specify that input of the argument type is supported.
Definition IOHandlerImpl.cpp:60
IO::IOHandlerImpl::registerOutput
void registerOutput(const DataType &type)
Specify that output of the argument type is supported.
Definition IOHandlerImpl.cpp:67
IO::IdealWriter::getFile
FILE * getFile()
Definition IdealWriter.h:43
IO::IdealWriter::IdealWriter
IdealWriter(FILE *out)
Definition IdealWriter.cpp:25
IO::IdealWriter::getNames
const VarNames & getNames()
Definition IdealWriter.h:44
IO::MonosIOHandler::doCreateIdealWriter
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)
Definition MonosIOHandler.cpp:81
IO::MonosIOHandler::doReadRing
virtual void doReadRing(Scanner &in, VarNames &names)
Definition MonosIOHandler.cpp:95
IO::MonosIOHandler::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition MonosIOHandler.cpp:85
IO::MonosIOHandler::MonosIOHandler
MonosIOHandler()
Definition MonosIOHandler.cpp:68
IO::MonosIOHandler::doReadBareIdeal
virtual void doReadBareIdeal(Scanner &in, InputConsumer &consumer)
Definition MonosIOHandler.cpp:110
IO::MonosIOHandler::staticGetName
static const char * staticGetName()
Definition MonosIOHandler.cpp:77
IO::MonosIOHandler::doReadTerm
virtual void doReadTerm(Scanner &in, InputConsumer &consumer)
Definition MonosIOHandler.cpp:91
IO::MonosIOHandler::doPeekRing
virtual bool doPeekRing(Scanner &in)
Definition MonosIOHandler.cpp:106
IO::MonosIdealWriter
Definition MonosIOHandler.cpp:35
IO::MonosIdealWriter::doWriteHeader
virtual void doWriteHeader(bool first)
Definition MonosIOHandler.cpp:41
IO::MonosIdealWriter::doWriteTerm
virtual void doWriteTerm(const Term &term, const TermTranslator &translator, bool first)
Definition MonosIOHandler.cpp:46
IO::MonosIdealWriter::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, bool first)
Definition MonosIOHandler.cpp:53
IO::MonosIdealWriter::doWriteEmptyList
virtual void doWriteEmptyList()
Definition MonosIOHandler.cpp:63
IO::MonosIdealWriter::MonosIdealWriter
MonosIdealWriter(FILE *out)
Definition MonosIOHandler.cpp:37
IO::MonosIdealWriter::doWriteFooter
virtual void doWriteFooter(bool wasZeroIdeal)
Definition MonosIOHandler.cpp:59
InputConsumer
Definition InputConsumer.h:30
InputConsumer::beginIdeal
void beginIdeal()
Start consuming an ideal.
Definition InputConsumer.cpp:50
InputConsumer::consumeTermProductNotation
void consumeTermProductNotation(Scanner &in)
Reads a term in a format like "a^4*b*c^2".
Definition InputConsumer.cpp:148
InputConsumer::endIdeal
void endIdeal()
Done reading an ideal.
Definition InputConsumer.cpp:191
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::peek
bool peek(char character)
Skips whitespace and returns true if the next character is equal to the parameter(s).
Definition Scanner.h:262
Scanner::expect
void expect(char expected)
Require the next character to be equal to expected.
Definition Scanner.h:231
Scanner::readIdentifier
const char * readIdentifier()
The returned string is only valid until the next method on this object gets called.
Definition Scanner.cpp:255
Scanner::peekIdentifier
bool peekIdentifier()
Skips whitespace and returns true if the next token is an identifier.
Definition Scanner.h:257
Scanner::match
bool match(char c)
Return true if the next character is c, and in that case skip past it.
Definition Scanner.h:215
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition TermTranslator.h:41
Term
Term represents a product of variables which does not include a coefficient.
Definition Term.h:49
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
VarNames::getVarCount
size_t getVarCount() const
Returns the current number of variables.
Definition VarNames.h:113
VarNames::getName
const string & getName(size_t index) const
The returned reference can become invalid next time addVar is called.
Definition VarNames.cpp:100
VarNames::clear
void clear()
Resets the number of variables to zero.
Definition VarNames.cpp:106
VarNames::addVarSyntaxCheckUnique
void addVarSyntaxCheckUnique(const Scanner &in, const string &name)
As addvar, except it reports a syntax error if name is already a variable.
Definition VarNames.cpp:68
error.h
IO::Monos
Definition MonosIOHandler.cpp:30
IO::Monos::writeRing
void writeRing(const VarNames &names, FILE *out)
Definition MonosIOHandler.cpp:130
IO
Definition CoCoA4IOHandler.cpp:33
IO::writeTermProduct
void writeTermProduct(const Term &term, const TermTranslator &translator, FILE *out)
Definition IOHandlerImpl.cpp:170
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...