SliceStrategyCommon.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 "SliceStrategyCommon.h"
#include "ElementDeleter.h"
#include "TaskEngine.h"
#include "Slice.h"
SliceStrategyCommon::SliceStrategyCommon(const SplitStrategy* splitStrategy):
_split(splitStrategy),
_useIndependence(true),
_useSimplification(true) {
ASSERT(splitStrategy != 0);
}
SliceStrategyCommon::~SliceStrategyCommon() {
// TODO: use ElementDeleter instead
while (!_sliceCache.empty()) {
delete _sliceCache.back();
_sliceCache.pop_back();
}
}
void SliceStrategyCommon::freeSlice(unique_ptr<Slice> slice) {
ASSERT(slice.get() != 0);
ASSERT(debugIsValidSlice(slice.get()));
slice->clearIdealAndSubtract(); // To preserve memory.
noThrowPushBack(_sliceCache, std::move(slice));
}
void SliceStrategyCommon::setUseIndependence(bool use) {
_useIndependence = use;
}
void SliceStrategyCommon::setUseSimplification(bool use) {
_useSimplification = use;
}
bool SliceStrategyCommon::simplify(Slice& slice) {
if (getUseSimplification())
return slice.simplify();
else if (_split->isLabelSplit()) {
// The label split code requires at least this simplification.
return slice.adjustMultiply();
}
return false;
}
unique_ptr<Slice> SliceStrategyCommon::newSlice() {
unique_ptr<Slice> slice;
if (!_sliceCache.empty()) {
slice.reset(_sliceCache.back());
_sliceCache.pop_back();
} else
slice = allocateSlice();
ASSERT(debugIsValidSlice(slice.get()));
return slice;
}
void SliceStrategyCommon::pivotSplit(unique_ptr<Slice> slice) {
ASSERT(slice.get() != 0);
_pivotTmp.reset(slice->getVarCount());
getPivot(_pivotTmp, *slice);
// Assert valid pivot.
ASSERT(_pivotTmp.getVarCount() == slice->getVarCount());
ASSERT(!_pivotTmp.isIdentity());
ASSERT(!slice->getIdeal().contains(_pivotTmp));
ASSERT(!slice->getSubtract().contains(_pivotTmp));
// Set slice2 to the inner slice.
unique_ptr<Slice> slice2 = newSlice();
*slice2 = *slice;
slice2->innerSlice(_pivotTmp);
simplify(*slice2);
// Set slice to the outer slice.
slice->outerSlice(_pivotTmp);
simplify(*slice);
// Process the smaller slice first to preserve memory.
if (slice2->getIdeal().getGeneratorCount() <
slice->getIdeal().getGeneratorCount()) {
// std::swap() may not work correctly on unique_ptr, so we have to
// do the swap by hand.
unique_ptr<Slice> tmp = std::move(slice2);
slice2 = std::move(slice);
slice = std::move(tmp);
}
_tasks.addTask(slice2.release());
_tasks.addTask(slice.release());
}
bool SliceStrategyCommon::getUseIndependence() const {
return _useIndependence;
}
bool SliceStrategyCommon::getUseSimplification() const {
return _useSimplification;
}
ElementDeleter.h
noThrowPushBack
void noThrowPushBack(Container &container, unique_ptr< Element > pointer)
Definition ElementDeleter.h:141
SliceStrategyCommon.h
Slice.h
TaskEngine.h
SliceStrategyCommon::setUseIndependence
virtual void setUseIndependence(bool use)
This method should only be called before calling run().
Definition SliceStrategyCommon.cpp:47
SliceStrategyCommon::getUseIndependence
bool getUseIndependence() const
Returns true if independence splits should be performed when possible.
Definition SliceStrategyCommon.cpp:113
SliceStrategyCommon::getPivot
virtual void getPivot(Term &pivot, Slice &slice)=0
Used by pivotSplit to obtain a pivot.
SliceStrategyCommon::getUseSimplification
bool getUseSimplification() const
Returns true if slices should be simplified.
Definition SliceStrategyCommon.cpp:117
SliceStrategyCommon::_split
const SplitStrategy * _split
Definition SliceStrategyCommon.h:80
SliceStrategyCommon::_sliceCache
vector< Slice * > _sliceCache
This is the cache maintained through newSlice and freeSlice.
Definition SliceStrategyCommon.h:95
SliceStrategyCommon::freeSlice
virtual void freeSlice(unique_ptr< Slice > slice)
It is allowed to delete returned slices directly, but it is better to use freeSlice.
Definition SliceStrategyCommon.cpp:39
SliceStrategyCommon::simplify
virtual bool simplify(Slice &slice)
Simplifies slice and returns true if it changed.
Definition SliceStrategyCommon.cpp:55
SliceStrategyCommon::_useIndependence
bool _useIndependence
Definition SliceStrategyCommon.h:87
SliceStrategyCommon::allocateSlice
virtual unique_ptr< Slice > allocateSlice()=0
Directly allocate a slice of the correct type using new.
SliceStrategyCommon::pivotSplit
virtual void pivotSplit(unique_ptr< Slice > slice)
Takes over ownership of slice.
Definition SliceStrategyCommon.cpp:77
SliceStrategyCommon::SliceStrategyCommon
SliceStrategyCommon(const SplitStrategy *splitStrategy)
Definition SliceStrategyCommon.cpp:24
SliceStrategyCommon::~SliceStrategyCommon
virtual ~SliceStrategyCommon()
Definition SliceStrategyCommon.cpp:31
SliceStrategyCommon::setUseSimplification
virtual void setUseSimplification(bool use)
This method should only be called before calling run().
Definition SliceStrategyCommon.cpp:51
SliceStrategyCommon::_useSimplification
bool _useSimplification
Definition SliceStrategyCommon.h:88
SliceStrategyCommon::debugIsValidSlice
virtual bool debugIsValidSlice(Slice *slice)=0
Check that this slice is valid for use with this strategy.
SliceStrategyCommon::_pivotTmp
Term _pivotTmp
Definition SliceStrategyCommon.h:97
SliceStrategyCommon::newSlice
unique_ptr< Slice > newSlice()
Returns a slice from the cache that freeSlice adds to, or allocate a new one using allocateSlice.
Definition SliceStrategyCommon.cpp:65
SliceStrategyCommon::_tasks
TaskEngine _tasks
This keeps track of pending tasks to process.
Definition SliceStrategyCommon.h:84
Slice
This class represents a slice, which is the central data structure of the Slice Algorithm.
Definition Slice.h:77
Slice::adjustMultiply
bool adjustMultiply()
Ensure that for each var, var appears to the first power in some generator of getIdeal().
Definition Slice.cpp:162
Slice::simplify
virtual bool simplify()
Simplifies this object such that it may become simpler without changing the content.
Definition Slice.cpp:204
SplitStrategy
A SplitStrategy is an implementation of a split selection strategy for the Slice Algorithm.
Definition SplitStrategy.h:30
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