CountingIOHandler.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 "CountingIOHandler.h"
#include "BigTermConsumer.h"
#include "CoefBigTermConsumer.h"
#include "DataType.h"
#include "BigIdeal.h"
#include "BigPolynomial.h"
namespace IO {
namespace {
class CountingConsumer :
public BigTermConsumer, public CoefBigTermConsumer {
public:
CountingConsumer(FILE* out):
_termCount(0),
_out(out) {
}
virtual ~CountingConsumer() {
// Destructors must not throw exceptions. As far as I know,
// bad_alloc is the only thing that could be thrown from
// gmp_fprintf.
}
virtual void consumeRing(const VarNames& names) {
}
virtual void beginConsuming() {
_termCount = 0;
}
virtual void consume(const Term& term, const TermTranslator& translator) {
++_termCount;
}
virtual void consume(const vector<mpz_class>& term) {
++_termCount;
}
virtual void consume(const BigIdeal& ideal) {
beginConsuming();
_termCount += ideal.getGeneratorCount();
doneConsuming();
}
virtual void consume(const mpz_class& coef, const Term& term) {
++_termCount;
}
virtual void consume(const mpz_class& coef,
const Term& term,
const TermTranslator& translator) {
++_termCount;
}
virtual void consume(const mpz_class& coef,
const vector<mpz_class>& term) {
++_termCount;
}
virtual void consume(const BigPolynomial& poly) {
beginConsuming();
_termCount += poly.getTermCount();
doneConsuming();
}
virtual void doneConsuming() {
gmp_fprintf(_out, "%Zd\n", _termCount.get_mpz_t());
}
private:
mpz_class _termCount;
FILE* _out;
};
}
CountingIOHandler::CountingIOHandler():
IOHandlerImpl(staticGetName(), "Writes the number of output terms.") {
registerOutput(DataType::getMonomialIdealType());
registerOutput(DataType::getPolynomialType());
registerOutput(DataType::getMonomialIdealListType());
}
const char* CountingIOHandler::staticGetName() {
return "count";
}
BigTermConsumer* CountingIOHandler::doCreateIdealWriter(FILE* out) {
return new CountingConsumer(out);
}
CoefBigTermConsumer* CountingIOHandler::doCreatePolynomialWriter(FILE* out) {
return new CountingConsumer(out);
}
void CountingIOHandler::doWriteTerm(const vector<mpz_class>& term,
const VarNames& names,
FILE* out) {
fputc('1', out);
}
}
BigIdeal.h
BigPolynomial.h
BigTermConsumer.h
CoefBigTermConsumer.h
CountingIOHandler.h
DataType.h
BigIdeal::getGeneratorCount
size_t getGeneratorCount() const
Definition BigIdeal.h:144
BigPolynomial::getTermCount
size_t getTermCount() const
Definition BigPolynomial.cpp:34
BigTermConsumer
Definition BigTermConsumer.h:29
CoefBigTermConsumer
Definition CoefBigTermConsumer.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
DataType::getPolynomialType
static const DataType & getPolynomialType()
Returns the one and only instance for polynomials.
Definition DataType.cpp:50
IO::CountingIOHandler::doCreatePolynomialWriter
virtual CoefBigTermConsumer * doCreatePolynomialWriter(FILE *out)
Definition CountingIOHandler.cpp:116
IO::CountingIOHandler::doCreateIdealWriter
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)
Definition CountingIOHandler.cpp:112
IO::CountingIOHandler::staticGetName
static const char * staticGetName()
Definition CountingIOHandler.cpp:108
IO::CountingIOHandler::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition CountingIOHandler.cpp:120
IO::CountingIOHandler::CountingIOHandler
CountingIOHandler()
Definition CountingIOHandler.cpp:101
IO::IOHandlerImpl::IOHandlerImpl
IOHandlerImpl(const char *formatName, const char *formatDescription)
Definition IOHandlerImpl.cpp:36
IO::IOHandlerImpl::registerOutput
void registerOutput(const DataType &type)
Specify that output of the argument type is supported.
Definition IOHandlerImpl.cpp:67
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
IO
Definition CoCoA4IOHandler.cpp:33
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...