VarSorter.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 "VarSorter.h"
#include "TermTranslator.h"
#include <algorithm>
class VarSorterCompare {
public:
VarSorterCompare(const VarNames& names):
_names(names) {
}
bool operator()(size_t a, size_t b) const {
return _names.getName(a) < _names.getName(b);
}
private:
void operator=(const VarSorterCompare&); // To make unaccessible.
const VarNames& _names;
};
VarSorter::VarSorter(const VarNames& names):
_names(names),
_bigTmpTerm(names.getVarCount()),
_tmpTerm(names.getVarCount()) {
_permutation.reserve(names.getVarCount());
for (size_t i = 0; i < names.getVarCount(); ++i)
_permutation.push_back(i);
sort(_permutation.begin(), _permutation.end(), VarSorterCompare(_names));
}
void VarSorter::getOrderedNames(VarNames& names) {
names.clear();
for (size_t i = 0; i < _permutation.size(); ++i)
names.addVar(_names.getName(_permutation[i]));
}
void VarSorter::permute(vector<mpz_class>& term) {
ASSERT(term.size() == _bigTmpTerm.size());
_bigTmpTerm.swap(term);
for (size_t i = 0; i < _permutation.size(); ++i)
mpz_swap(term[i].get_mpz_t(), _bigTmpTerm[_permutation[i]].get_mpz_t());
}
void VarSorter::permute(Exponent* term) {
_tmpTerm = term;
for (size_t i = 0; i < _permutation.size(); ++i)
std::swap(term[i], _tmpTerm[_permutation[i]]);
}
void VarSorter::permute(TermTranslator* translator) {
ASSERT(translator->getVarCount() == _permutation.size());
size_t varCount = _permutation.size();
vector<int> done(translator->getVarCount());
for (size_t var = 0; var < varCount; ++var) {
if (done[var])
continue;
size_t v = var;
while (true) {
done[v] = true;
size_t nextInCycle = _permutation[v];
if (done[nextInCycle])
break;
translator->swapVariables(v, nextInCycle);
v = nextInCycle;
}
}
}
TermTranslator.h
VarSorter.h
TermTranslator
TermTranslator handles translation between terms whose exponents are infinite precision integers and ...
Definition TermTranslator.h:41
TermTranslator::swapVariables
void swapVariables(size_t a, size_t b)
Definition TermTranslator.cpp:310
TermTranslator::getVarCount
size_t getVarCount() const
Definition TermTranslator.cpp:447
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
VarNames::addVar
bool addVar(const string &name)
Adds the variable and returns true if name is not already a variable.
Definition VarNames.cpp:44
VarNames::getVarCount
size_t getVarCount() const
Returns the current number of variables.
Definition VarNames.h:113
VarNames::clear
void clear()
Resets the number of variables to zero.
Definition VarNames.cpp:106
VarSorterCompare
Definition VarSorter.cpp:24
VarSorterCompare::operator=
void operator=(const VarSorterCompare &)
VarSorterCompare::_names
const VarNames & _names
Definition VarSorter.cpp:37
VarSorterCompare::operator()
bool operator()(size_t a, size_t b) const
Definition VarSorter.cpp:30
VarSorterCompare::VarSorterCompare
VarSorterCompare(const VarNames &names)
Definition VarSorter.cpp:26
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
Exponent
unsigned int Exponent
Definition stdinc.h:89
ASSERT
#define ASSERT(X)
Definition stdinc.h:86
VarSorter::_names
VarNames _names
Definition VarSorter.h:43
VarSorter::_bigTmpTerm
vector< mpz_class > _bigTmpTerm
Definition VarSorter.h:44
VarSorter::VarSorter
VarSorter(const VarNames &names)
Definition VarSorter.cpp:40
VarSorter::_permutation
vector< size_t > _permutation
Definition VarSorter.h:42
VarSorter::getOrderedNames
void getOrderedNames(VarNames &names)
Definition VarSorter.cpp:50
VarSorter::permute
void permute(vector< mpz_class > &term)
Definition VarSorter.cpp:56
VarSorter::_tmpTerm
Term _tmpTerm
Definition VarSorter.h:45