IOFacade.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 "IOFacade.h"
#include "IOHandler.h"
#include "fourti2.h"
#include "BigIdeal.h"
#include "fplllIO.h"
#include "Scanner.h"
#include "ElementDeleter.h"
#include "error.h"
#include "FrobbyStringStream.h"
#include "BigTermRecorder.h"
#include "CoefBigTermConsumer.h"
#include "CoefBigTermRecorder.h"
#include "SatBinomIdeal.h"
#include "SatBinomRecorder.h"
#include "InputConsumer.h"
#include "SquareFreeIdeal.h"
#include "RawSquareFreeIdeal.h"
#include <iterator>
IOFacade::IOFacade(bool printActions):
Facade(printActions) {
}
bool IOFacade::isValidMonomialIdealFormat(const string& format) {
beginAction("Validating monomial ideal format name.");
bool valid = true;
try {
(void)createIOHandler(format);
} catch (const UnknownNameException&) {
valid = false;
}
endAction();
return valid;
}
void IOFacade::readSatBinomIdeal(Scanner& in, SatBinomConsumer& consumer) {
beginAction("Reading saturated binomial ideal.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
handler->readSatBinomIdeal(in, consumer);
endAction();
}
void IOFacade::readSatBinomIdeal(Scanner& in, SatBinomIdeal& ideal) {
beginAction("Reading saturated binomial ideal.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
ideal.clear();
SatBinomRecorder recorder(ideal);
handler->readSatBinomIdeal(in, recorder);
endAction();
}
void IOFacade::readIdeal(Scanner& in, BigTermConsumer& consumer) {
beginAction("Reading monomial ideal.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
InputConsumer middleman;
handler->readIdeal(in, middleman);
// todo: find a way to generate the input as it comes in rather than
// storing it and only then letting it go on.
ASSERT(!middleman.empty());
consumer.consume(middleman.releaseBigIdeal());
ASSERT(middleman.empty());
endAction();
}
void IOFacade::readIdeal(Scanner& in, BigIdeal& ideal) {
beginAction("Reading monomial ideal.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
InputConsumer recorder;
handler->readIdeal(in, recorder);
ASSERT(!recorder.empty());
ideal.swap(*(recorder.releaseBigIdeal()));
ASSERT(recorder.empty());
endAction();
}
void IOFacade::readSquareFreeIdeal(Scanner& in, SquareFreeIdeal& ideal) {
beginAction("Reading square free ideal.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
InputConsumer consumer;
consumer.requireSquareFree();
handler->readIdeal(in, consumer);
ASSERT(!consumer.empty());
ideal.swap(*consumer.releaseSquareFreeIdeal());
ASSERT(consumer.empty());
endAction();
}
void IOFacade::readIdeals(Scanner& in,
vector<BigIdeal*>& ideals,
VarNames& names) {
beginAction("Reading monomial ideals.");
// To make it clear what needs to be deleted in case of an exception.
ASSERT(ideals.empty());
ElementDeleter<vector<BigIdeal*> > idealsDeleter(ideals);
unique_ptr<IOHandler> handler(in.createIOHandler());
//BigTermRecorder recorder;
InputConsumer recorder;
handler->readIdeals(in, recorder);
names = recorder.getRing();
while (!recorder.empty())
exceptionSafePushBack(ideals, recorder.releaseBigIdeal());
idealsDeleter.release();
endAction();
}
void IOFacade::writeIdeal(const BigIdeal& ideal,
IOHandler* handler,
FILE* out) {
ASSERT(handler != 0);
beginAction("Writing monomial ideal.");
handler->createIdealWriter(out)->consume(ideal);
endAction();
}
void IOFacade::writeIdeals(const vector<BigIdeal*>& ideals,
const VarNames& names,
IOHandler* handler,
FILE* out) {
ASSERT(handler != 0);
beginAction("Writing monomial ideals.");
{
unique_ptr<BigTermConsumer> consumer = handler->createIdealWriter(out);
consumer->beginConsumingList();
consumer->consumeRing(names);
for (vector<BigIdeal*>::const_iterator it = ideals.begin();
it != ideals.end(); ++it)
consumer->consume(**it);
consumer->doneConsumingList();
}
endAction();
}
void IOFacade::readPolynomial(Scanner& in, BigPolynomial& polynomial) {
beginAction("Reading polynomial.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
CoefBigTermRecorder recorder(&polynomial);
handler->readPolynomial(in, recorder);
endAction();
}
void IOFacade::writePolynomial(const BigPolynomial& polynomial,
IOHandler* handler,
FILE* out) {
ASSERT(handler != 0);
ASSERT(out != 0);
beginAction("Writing polynomial.");
handler->createPolynomialWriter(out)->consume(polynomial);
endAction();
}
void IOFacade::writeTerm(const vector<mpz_class>& term,
const VarNames& names,
IOHandler* handler,
FILE* out) {
beginAction("Writing monomial.");
handler->writeTerm(term, names, out);
endAction();
}
bool IOFacade::readAlexanderDualInstance
(Scanner& in, BigIdeal& ideal, vector<mpz_class>& term) {
beginAction("Reading Alexander dual input.");
unique_ptr<IOHandler> handler(in.createIOHandler());
ASSERT(handler.get() != 0);
InputConsumer recorder;
handler->readIdeal(in, recorder);
// TODO: return value instead of this copy.
ASSERT(!recorder.empty());
ideal = *(recorder.releaseBigIdeal());
ASSERT(recorder.empty());
bool pointSpecified = false;
if (handler->hasMoreInput(in)) {
handler->readTerm(in, ideal.getNames(), term);
pointSpecified = true;
}
endAction();
return pointSpecified;
}
void IOFacade::readVector
(Scanner& in, vector<mpz_class>& v, size_t integerCount) {
beginAction("Reading vector.");
v.resize(integerCount);
for (size_t i = 0; i < integerCount; ++i)
in.readInteger(v[i]);
endAction();
}
void IOFacade::
readFrobeniusInstance(Scanner& in, vector<mpz_class>& instance) {
beginAction("Reading Frobenius instance.");
::readFrobeniusInstance(in, instance);
endAction();
}
void IOFacade::readFrobeniusInstanceWithGrobnerBasis
(Scanner& in, BigIdeal& ideal, vector<mpz_class>& instance) {
beginAction("Reading frobenius instance with Grobner basis.");
fourti2::readGrobnerBasis(in, ideal);
::readFrobeniusInstance(in, instance);
if (instance.size() != ideal.getVarCount() + 1) {
if (instance.empty())
reportSyntaxError
(in, "The Grobner basis is not followed by a Frobenius instance.");
else {
// Note that we add one since the first entry of the rows encoding
// the Grobner basis is chopped off.
FrobbyStringStream errorMsg;
errorMsg << "The Grobner basis has "
<< ideal.getVarCount() + 1
<< " entries, and the Frobenius instance should then also have "
<< ideal.getVarCount() + 1
<< " entries, but in fact it has "
<< instance.size()
<< " entries.";
reportSyntaxError(in, errorMsg);
}
}
endAction();
}
void IOFacade::
writeFrobeniusInstance(FILE* out, vector<mpz_class>& instance) {
beginAction("Writing Frobenius instance.");
for (size_t i = 0; i < instance.size(); ++i) {
if (i > 0)
fputc(' ', out);
gmp_fprintf(out, "%Zd", instance[i].get_mpz_t());
}
fputc('\n', out);
endAction();
}
bool IOFacade::isValidLatticeFormat(const string& format) {
beginAction("Validating lattice format name.");
bool valid = (format == "4ti2" || format == "fplll");
endAction();
return valid;
}
void IOFacade::
readLattice(Scanner& in, BigIdeal& ideal) {
beginAction("Reading lattice basis.");
if (in.getFormat() == "4ti2")
fourti2::readLatticeBasis(in, ideal);
else if (in.getFormat() == "fplll")
fplll::readLatticeBasis(in, ideal);
else {
ASSERT(false);
}
endAction();
}
void IOFacade::
writeLattice(FILE* out, const BigIdeal& ideal, const string& format) {
beginAction("Writing lattice basis.");
if (format == "4ti2")
fourti2::writeLatticeBasis(out, ideal);
else if (format == "fplll")
fplll::writeLatticeBasis(out, ideal);
else {
ASSERT(false);
}
endAction();
}
BigIdeal.h
BigTermRecorder.h
CoefBigTermConsumer.h
CoefBigTermRecorder.h
ElementDeleter.h
exceptionSafePushBack
void exceptionSafePushBack(Container &container, unique_ptr< Element > pointer)
Definition ElementDeleter.h:129
FrobbyStringStream.h
IOFacade.h
createIOHandler
unique_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
Definition IOHandler.cpp:145
IOHandler.h
InputConsumer.h
RawSquareFreeIdeal.h
SatBinomIdeal.h
SatBinomRecorder.h
Scanner.h
SquareFreeIdeal.h
BigIdeal
Definition BigIdeal.h:27
BigIdeal::swap
void swap(BigIdeal &ideal)
Definition BigIdeal.cpp:305
BigIdeal::getVarCount
size_t getVarCount() const
Definition BigIdeal.h:148
BigIdeal::getNames
const VarNames & getNames() const
Definition BigIdeal.cpp:253
BigPolynomial
Definition BigPolynomial.h:27
BigTermConsumer
Definition BigTermConsumer.h:29
BigTermConsumer::consume
virtual void consume(const vector< mpz_class > &term)=0
CoefBigTermRecorder
Definition CoefBigTermRecorder.h:26
ElementDeleter
Definition ElementDeleter.h:50
ElementDeleter::release
void release()
Definition ElementDeleter.h:65
Facade::beginAction
void beginAction(const char *message)
Prints message to standard error if printing is turned on, and records the time when the action start...
Definition Facade.cpp:38
Facade::endAction
void endAction()
Prints to standard error the time since the last call to beginAction.
Definition Facade.cpp:51
Facade::Facade
Facade(bool printActions)
Constructs a facade that prints out what it is doing if printActions is true.
Definition Facade.cpp:20
FrobbyStringStream
A replacement for stringstream.
Definition FrobbyStringStream.h:26
IOFacade::isValidLatticeFormat
bool isValidLatticeFormat(const string &format)
Definition IOFacade.cpp:318
IOFacade::writeFrobeniusInstance
void writeFrobeniusInstance(FILE *out, vector< mpz_class > &instance)
Definition IOFacade.cpp:305
IOFacade::readFrobeniusInstance
void readFrobeniusInstance(Scanner &in, vector< mpz_class > &instance)
Definition IOFacade.cpp:267
IOFacade::writePolynomial
void writePolynomial(const BigPolynomial &polynomial, IOHandler *handler, FILE *out)
Definition IOFacade.cpp:205
IOFacade::isValidMonomialIdealFormat
bool isValidMonomialIdealFormat(const string &format)
Definition IOFacade.cpp:42
IOFacade::readPolynomial
void readPolynomial(Scanner &in, BigPolynomial &polynomial)
Definition IOFacade.cpp:192
IOFacade::readFrobeniusInstanceWithGrobnerBasis
void readFrobeniusInstanceWithGrobnerBasis(Scanner &in, BigIdeal &ideal, vector< mpz_class > &instance)
Definition IOFacade.cpp:276
IOFacade::writeIdeal
void writeIdeal(const BigIdeal &ideal, IOHandler *handler, FILE *out)
Definition IOFacade.cpp:156
IOFacade::writeTerm
void writeTerm(const vector< mpz_class > &term, const VarNames &names, IOHandler *handler, FILE *out)
Definition IOFacade.cpp:218
IOFacade::readIdeals
void readIdeals(Scanner &in, vector< BigIdeal * > &ideals, VarNames &names)
Insert the ideals that are read into the parameter ideals.
Definition IOFacade.cpp:132
IOFacade::readLattice
void readLattice(Scanner &in, BigIdeal &ideal)
Definition IOFacade.cpp:329
IOFacade::readIdeal
void readIdeal(Scanner &in, BigTermConsumer &consumer)
Read an ideal from in and feed it to consumer.
Definition IOFacade.cpp:81
IOFacade::readSquareFreeIdeal
void readSquareFreeIdeal(Scanner &in, SquareFreeIdeal &ideal)
Read a square free ideal from in and place it in the parameter ideal.
Definition IOFacade.cpp:116
IOFacade::readSatBinomIdeal
void readSatBinomIdeal(Scanner &in, SatBinomConsumer &consumer)
Read a saturated binomial ideal from in and feed it to consumer.
Definition IOFacade.cpp:57
IOFacade::writeIdeals
void writeIdeals(const vector< BigIdeal * > &ideals, const VarNames &names, IOHandler *handler, FILE *out)
Definition IOFacade.cpp:168
IOFacade::IOFacade
IOFacade(bool printActions)
Definition IOFacade.cpp:38
IOFacade::writeLattice
void writeLattice(FILE *out, const BigIdeal &ideal, const string &format)
Definition IOFacade.cpp:344
IOFacade::readVector
void readVector(Scanner &in, vector< mpz_class > &v, size_t integerCount)
Definition IOFacade.cpp:256
IOFacade::readAlexanderDualInstance
bool readAlexanderDualInstance(Scanner &in, BigIdeal &ideal, vector< mpz_class > &term)
Definition IOFacade.cpp:230
IOHandler
An IOHandler implements input and output for some format in such a way that client code does not need...
Definition IOHandler.h:41
IOHandler::writeTerm
void writeTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition IOHandler.cpp:72
IOHandler::createIdealWriter
unique_ptr< BigTermConsumer > createIdealWriter(FILE *out)
Definition IOHandler.cpp:89
IOHandler::createPolynomialWriter
unique_ptr< CoefBigTermConsumer > createPolynomialWriter(FILE *out)
Definition IOHandler.cpp:110
InputConsumer
Definition InputConsumer.h:30
InputConsumer::requireSquareFree
void requireSquareFree()
Definition InputConsumer.cpp:45
InputConsumer::empty
bool empty() const
Returns true if there are ideals stored.
Definition InputConsumer.h:74
InputConsumer::releaseBigIdeal
unique_ptr< BigIdeal > releaseBigIdeal()
Returns the least recently read ideal that has not been released.
Definition InputConsumer.cpp:209
InputConsumer::getRing
const VarNames & getRing() const
Returns the current ring.
Definition InputConsumer.h:91
InputConsumer::releaseSquareFreeIdeal
unique_ptr< SquareFreeIdeal > releaseSquareFreeIdeal()
Returns the least recently read ideal that has not been released.
Definition InputConsumer.cpp:218
SatBinomConsumer
Definition SatBinomConsumer.h:25
SatBinomIdeal
Represents a saturated binomial ideal.
Definition SatBinomIdeal.h:28
SatBinomIdeal::clear
void clear()
Definition SatBinomIdeal.cpp:86
SatBinomRecorder
SatBinomRecorder records all consumed saturated binomials into a passed-in ideal.
Definition SatBinomRecorder.h:27
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::createIOHandler
unique_ptr< IOHandler > createIOHandler() const
Definition Scanner.cpp:42
Scanner::getFormat
const string & getFormat() const
Definition Scanner.h:61
Scanner::readInteger
void readInteger(mpz_class &integer)
Read an arbitrary-precision integer.
Definition Scanner.h:238
SquareFreeIdeal
Definition SquareFreeIdeal.h:24
SquareFreeIdeal::swap
void swap(SquareFreeIdeal &ideal)
Definition SquareFreeIdeal.cpp:95
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
reportSyntaxError
void reportSyntaxError(const Scanner &scanner, const string &errorMsg)
Definition error.cpp:44
error.h
fourti2.h
fplllIO.h
fourti2::readGrobnerBasis
void readGrobnerBasis(Scanner &scanner, BigIdeal &basis)
Definition fourti2.cpp:44
fourti2::readLatticeBasis
void readLatticeBasis(Scanner &scanner, BigIdeal &basis)
Definition fourti2.cpp:63
fourti2::writeLatticeBasis
void writeLatticeBasis(FILE *out, const BigIdeal &basis)
Definition fourti2.cpp:80
fplll::readLatticeBasis
void readLatticeBasis(Scanner &scanner, BigIdeal &basis)
Definition fplllIO.cpp:26
fplll::writeLatticeBasis
void writeLatticeBasis(FILE *out, const BigIdeal &basis)
Definition fplllIO.cpp:62
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86