CanonicalTermConsumer.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 "CanonicalTermConsumer.h"
#include "TermTranslator.h"
#include "IdealComparator.h"
#include "Term.h"
CanonicalTermConsumer::CanonicalTermConsumer(unique_ptr<TermConsumer> consumer,
size_t varCount,
TermTranslator* translator):
_varCount(varCount),
_storingList(false),
_ideals(),
_idealsDeleter(_ideals),
_consumer(std::move(consumer)),
_translator(translator) {
ASSERT(_consumer.get() != 0);
}
void CanonicalTermConsumer::consumeRing(const VarNames& names) {
_consumer->consumeRing(names);
}
void CanonicalTermConsumer::beginConsumingList() {
ASSERT(!_storingList);
ASSERT(_ideals.empty());
_storingList = true;
}
void CanonicalTermConsumer::beginConsuming() {
ASSERT(_storingList || _ideals.empty());
unique_ptr<Ideal> ideal(new Ideal(_varCount));
exceptionSafePushBack(_ideals, std::move(ideal));
}
void CanonicalTermConsumer::consume(const Term& term) {
ASSERT(term.getVarCount() == _varCount);
ASSERT(!_ideals.empty());
_ideals.back()->insert(term);
}
void CanonicalTermConsumer::doneConsuming() {
if (!_storingList) {
ASSERT(_ideals.size() == 1);
passLastIdeal();
ASSERT(_ideals.empty());
}
}
void CanonicalTermConsumer::doneConsumingList() {
ASSERT(_storingList);
vector<Ideal*>::iterator end = _ideals.end();
for (vector<Ideal*>::iterator it = _ideals.begin(); it != end; ++it)
canonicalizeIdeal(**it);
// We are sorting in reverse because we are processing the ideals from
// the back, so they get passed on in the correct order.
if (_translator == 0) {
IdealComparator comparator;
sort(_ideals.rbegin(), _ideals.rend(), comparator);
} else {
TranslatedIdealComparator comparator(*_translator);
sort(_ideals.rbegin(), _ideals.rend(), comparator);
}
_consumer->beginConsumingList();
while (!_ideals.empty())
passLastIdeal();
_consumer->doneConsumingList();
}
void CanonicalTermConsumer::passLastIdeal() {
ASSERT(!_ideals.empty());
ASSERT(_ideals.back() != 0);
unique_ptr<Ideal> ideal(_ideals.back());
_ideals.pop_back();
canonicalizeIdeal(*ideal);
_consumer->beginConsuming();
Term tmp(_varCount);
Ideal::const_iterator end = ideal->end();
for (Ideal::const_iterator it = ideal->begin(); it != end; ++it) {
tmp = *it;
_consumer->consume(tmp);
}
ideal.reset(0);
_consumer->doneConsuming();
}
void CanonicalTermConsumer::canonicalizeIdeal(Ideal& ideal) {
if (_translator == 0)
ideal.sortReverseLex();
else {
TranslatedReverseLexComparator comparator(*_translator);
sort(ideal.begin(), ideal.end(), comparator);
}
}
CanonicalTermConsumer.h
exceptionSafePushBack
void exceptionSafePushBack(Container &container, unique_ptr< Element > pointer)
Definition ElementDeleter.h:129
IdealComparator.h
TermTranslator.h
Term.h
CanonicalTermConsumer::_storingList
bool _storingList
Definition CanonicalTermConsumer.h:67
CanonicalTermConsumer::canonicalizeIdeal
void canonicalizeIdeal(Ideal &ideal)
Definition CanonicalTermConsumer.cpp:113
CanonicalTermConsumer::_consumer
unique_ptr< TermConsumer > _consumer
Definition CanonicalTermConsumer.h:70
CanonicalTermConsumer::consume
virtual void consume(const Term &term)
Consume a term.
Definition CanonicalTermConsumer.cpp:54
CanonicalTermConsumer::beginConsumingList
virtual void beginConsumingList()
This method is not required to be called.
Definition CanonicalTermConsumer.cpp:40
CanonicalTermConsumer::CanonicalTermConsumer
CanonicalTermConsumer(unique_ptr< TermConsumer > consumer, size_t varCount, TermTranslator *translator=0)
The translator, if non-null, is used to identify exponents that map to zero, which influences the sor...
Definition CanonicalTermConsumer.cpp:24
CanonicalTermConsumer::_translator
TermTranslator * _translator
Definition CanonicalTermConsumer.h:71
CanonicalTermConsumer::doneConsuming
virtual void doneConsuming()
Must be called once after each time beginConsuming has been called.
Definition CanonicalTermConsumer.cpp:61
CanonicalTermConsumer::beginConsuming
virtual void beginConsuming()
Tell the consumer to begin consuming an ideal.
Definition CanonicalTermConsumer.cpp:47
CanonicalTermConsumer::_idealsDeleter
ElementDeleter< vector< Ideal * > > _idealsDeleter
Definition CanonicalTermConsumer.h:69
CanonicalTermConsumer::passLastIdeal
void passLastIdeal()
Definition CanonicalTermConsumer.cpp:92
CanonicalTermConsumer::_ideals
vector< Ideal * > _ideals
Definition CanonicalTermConsumer.h:68
CanonicalTermConsumer::_varCount
size_t _varCount
Definition CanonicalTermConsumer.h:66
CanonicalTermConsumer::consumeRing
virtual void consumeRing(const VarNames &names)
Passes on the call immediately.
Definition CanonicalTermConsumer.cpp:36
CanonicalTermConsumer::doneConsumingList
virtual void doneConsumingList()
Must be called once after each time beginConsumingList has been called.
Definition CanonicalTermConsumer.cpp:69
IdealComparator
Definition IdealComparator.h:35
Ideal
Represents a monomial ideal with int exponents.
Definition Ideal.h:27
Ideal::const_iterator
Cont::const_iterator const_iterator
Definition Ideal.h:43
Ideal::sortReverseLex
void sortReverseLex()
Definition Ideal.cpp:510
Ideal::end
const_iterator end() const
Definition Ideal.h:49
Ideal::begin
const_iterator begin() const
Definition Ideal.h:48
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
Term::getVarCount
size_t getVarCount() const
Definition Term.h:85
TranslatedIdealComparator
Definition IdealComparator.h:43
TranslatedReverseLexComparator
A predicate that sorts according to reverse lexicographic order on the translated values of a term.
Definition TermTranslator.h:141
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
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