}
}
+
RowVector::RowVector(const std::vector< ArithVar >& variables,
const std::vector< Rational >& coefficients,
std::vector<uint32_t>& counts):
for(NonZeroIterator i=beginNonZero(), end=endNonZero(); i != end; ++i){
++d_rowCount[getArithVar(*i)];
+ addArithVar(d_contains, getArithVar(*i));
}
Assert(isSorted(d_entries, true));
Assert(noZeroCoefficients(d_entries));
}
+void RowVector::addArithVar(ArithVarContainsSet& contains, ArithVar v){
+ if(v >= contains.size()){
+ contains.resize(v+1, false);
+ }
+ contains[v] = true;
+}
+
+void RowVector::removeArithVar(ArithVarContainsSet& contains, ArithVar v){
+ Assert(v < contains.size());
+ Assert(contains[v]);
+ contains[v] = false;
+}
+
void RowVector::merge(VarCoeffArray& arr,
+ ArithVarContainsSet& contains,
const VarCoeffArray& other,
const Rational& c,
std::vector<uint32_t>& counts){
}else if(getArithVar(*curr1) > getArithVar(*curr2)){
++counts[getArithVar(*curr2)];
+ addArithVar(contains, getArithVar(*curr2));
arr.push_back( make_pair(getArithVar(*curr2), c * getCoefficient(*curr2)));
++curr2;
}else{
arr.push_back(make_pair(getArithVar(*curr1), res));
}else{
+ removeArithVar(contains, getArithVar(*curr2));
--counts[getArithVar(*curr2)];
}
++curr1;
while(curr2 != end2){
++counts[getArithVar(*curr2)];
+ addArithVar(contains, getArithVar(*curr2));
+
arr.push_back(make_pair(getArithVar(*curr2), c * getCoefficient(*curr2)));
++curr2;
}
void RowVector::addRowTimesConstant(const Rational& c, const RowVector& other){
Assert(c != 0);
- merge(d_entries, other.d_entries, c, d_rowCount);
+ merge(d_entries, d_contains, other.d_entries, c, d_rowCount);
}
void RowVector::printRow(){
VarCoeffArray justBasic;
justBasic.push_back(make_pair(basic, Rational(-1)));
- merge(d_entries,justBasic, Rational(1), d_rowCount);
+ merge(d_entries, d_contains, justBasic, Rational(1), d_rowCount);
Assert(wellFormed());
}
typedef std::vector<VarCoeffPair> VarCoeffArray;
typedef VarCoeffArray::const_iterator NonZeroIterator;
+ typedef std::vector<bool> ArithVarContainsSet;
+
/**
* Let c be -1 if strictlySorted is true and c be 0 otherwise.
* isSorted(arr, strictlySorted) is then equivalent to
*/
static bool isSorted(const VarCoeffArray& arr, bool strictlySorted);
- /**
- * noZeroCoefficients(arr) is equivalent to
- * 0 != getCoefficient(arr[i]) for all i.
- */
- static bool noZeroCoefficients(const VarCoeffArray& arr);
-
/**
* Zips together an array of variables and coefficients and appends
* it to the end of an output vector.
const std::vector< Rational >& coefficients,
VarCoeffArray& output);
- static void merge(VarCoeffArray& arr, const VarCoeffArray& other, const Rational& c, std::vector<uint32_t>& count);
-
+ static void merge(VarCoeffArray& arr,
+ ArithVarContainsSet& contains,
+ const VarCoeffArray& other,
+ const Rational& c,
+ std::vector<uint32_t>& count);
protected:
+ /**
+ * Debugging code.
+ * noZeroCoefficients(arr) is equivalent to
+ * 0 != getCoefficient(arr[i]) for all i.
+ */
+ static bool noZeroCoefficients(const VarCoeffArray& arr);
+
/**
* Invariants:
* - isSorted(d_entries, true)
*/
VarCoeffArray d_entries;
+ /**
+ * Invariants:
+ * - This set is the same as the set maintained in d_entries.
+ */
+ ArithVarContainsSet d_contains;
+
std::vector<uint32_t>& d_rowCount;
NonZeroIterator lower_bound(ArithVar x_j) const{
/** Returns true if the variable is in the row. */
bool has(ArithVar x_j) const{
+ if(x_j >= d_contains.size()){
+ return false;
+ }else{
+ return d_contains[x_j];
+ }
+ }
+
+private:
+ /** Debugging code. */
+ bool hasInEntries(ArithVar x_j) const {
return std::binary_search(d_entries.begin(), d_entries.end(), make_pair(x_j,0), cmp);
}
+public:
/**
* Returns the coefficient of a variable in the row.
*/
const Rational& lookup(ArithVar x_j) const{
Assert(has(x_j));
+ Assert(hasInEntries(x_j));
NonZeroIterator lb = lower_bound(x_j);
return getCoefficient(*lb);
}
void addRowTimesConstant(const Rational& c, const RowVector& other);
void printRow();
+
+protected:
+ /**
+ * Adds v to d_contains.
+ * This may resize d_contains.
+ */
+ static void addArithVar(ArithVarContainsSet& contains, ArithVar v);
+
+ /** Removes v from d_contains. */
+ static void removeArithVar(ArithVarContainsSet& contains, ArithVar v);
+
}; /* class RowVector */
/**
return d_basic;
}
+ /** Return true if x is in the row and is not the basic variable. */
+ bool hasNonBasic(ArithVar x) const {
+ if(x == basic()){
+ return false;
+ }else{
+ return has(x);
+ }
+ }
+
void pivot(ArithVar x_j);
void substitute(const ReducedRowVector& other);