kill Util::abort, s/vectorIndex/indexOf and s/isCarteesianProduct/isCartesianProduct

This commit is contained in:
Tiago Gomes 2012-05-15 19:41:14 +01:00
parent db0d2c9772
commit f7ba86d3ed
7 changed files with 24 additions and 47 deletions

View File

@ -242,11 +242,9 @@ void
ConstraintTree::moveToTop (const LogVars& lvs)
{
for (unsigned i = 0; i < lvs.size(); i++) {
LogVars::iterator it =
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
for (unsigned j = pos; j > i; j--) {
int pos = Util::indexOf (logVars_, lvs[i]);
assert (pos != -1);
for (int j = pos; j > (int)i; j--) {
swapLogVar (logVars_[j-1]);
}
}
@ -261,9 +259,9 @@ ConstraintTree::moveToBottom (const LogVars& lvs)
LogVars::iterator it =
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
unsigned stop = logVars_.size() - (lvs.size() - i - 1);
for (unsigned j = pos; j < stop - 1; j++) {
int pos = Util::indexOf (logVars_, lvs[i]);
int stop = logVars_.size() - (lvs.size() - i - 1);
for (int j = pos; j < stop - 1; j++) {
swapLogVar (logVars_[j]);
}
}
@ -328,10 +326,7 @@ ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
unsigned
ConstraintTree::getLevel (LogVar X) const
{
LogVars::const_iterator it =
std::find (logVars_.begin(), logVars_.end(), X);
assert (it != logVars_.end());
unsigned level = std::distance (logVars_.begin(), it);
unsigned level = Util::indexOf (logVars_, X);
level += 1; // root is in level 0, first logVar is in level 1
return level;
}
@ -471,7 +466,7 @@ ConstraintTree::tupleSet (const LogVars& originalLvs)
vector<int> indexes;
indexes.reserve (originalLvs.size());
for (unsigned i = 0; i < originalLvs.size(); i++) {
indexes.push_back (Util::vectorIndex (uniqueLvs, originalLvs[i]));
indexes.push_back (Util::indexOf (uniqueLvs, originalLvs[i]));
}
Tuples tuples2;
tuples2.reserve (tuples.size());
@ -618,7 +613,7 @@ ConstraintTree::getConditionalCounts (const LogVarSet& Ys)
bool
ConstraintTree::isCarteesianProduct (const LogVarSet& Xs)
ConstraintTree::isCartesianProduct (const LogVarSet& Xs)
{
assert (logVarSet_.contains (Xs));
if (Xs.size() <= 1) {
@ -979,10 +974,8 @@ ConstraintTree::appendOnBottom (CTNode* n, const CTChilds& childs)
void
ConstraintTree::swapLogVar (LogVar X)
{
LogVars::iterator it;
it = std::find (logVars_.begin(),logVars_.end(), X);
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
int pos = Util::indexOf (logVars_, X);
assert (pos != -1);
const CTNodes& nodes = getNodesAtLevel (pos);
for (CTNodes::const_iterator nodeIt = nodes.begin();
nodeIt != nodes.end(); ++ nodeIt) {

View File

@ -185,7 +185,7 @@ class ConstraintTree
TinySet<unsigned> getConditionalCounts (const LogVarSet&);
bool isCarteesianProduct (const LogVarSet&);
bool isCartesianProduct (const LogVarSet&);
std::pair<ConstraintTree*, ConstraintTree*> split (const Tuple&);

View File

@ -346,7 +346,7 @@ CountingOperator::getLogCost (void)
cost += size * HistogramSet::nrHistograms (counts[i], range);
}
unsigned group = (*pfIter_)->argument (fIdx).group();
int lvIndex = Util::vectorIndex (
int lvIndex = Util::indexOf (
(*pfIter_)->argument (fIdx).logVars(), X_);
assert (lvIndex != -1);
ParfactorList::iterator pfIter = pfList_.begin();
@ -379,7 +379,7 @@ CountingOperator::apply (void)
Parfactors pfs = FoveSolver::countNormalize (pf, X_);
for (unsigned i = 0; i < pfs.size(); i++) {
unsigned condCount = pfs[i]->constr()->getConditionalCount (X_);
bool cartProduct = pfs[i]->constr()->isCarteesianProduct (
bool cartProduct = pfs[i]->constr()->isCartesianProduct (
pfs[i]->countedLogVars() | X_);
if (condCount > 1 && cartProduct) {
pfs[i]->countConvert (X_);

View File

@ -194,7 +194,7 @@ Parfactor::multiply (Parfactor& g)
TFactor<ProbFormula>::multiply (g);
constr_->join (g.constr(), true);
simplifyGrounds();
assert (constr_->isCarteesianProduct (countedLogVars()));
assert (constr_->isCartesianProduct (countedLogVars()));
}
@ -215,7 +215,7 @@ Parfactor::canCountConvert (LogVar X)
if (constr_->getConditionalCount (X) == 1) {
return false;
}
if (constr_->isCarteesianProduct (countedLogVars() | X) == false) {
if (constr_->isCartesianProduct (countedLogVars() | X) == false) {
return false;
}
return true;

View File

@ -32,13 +32,7 @@ ProbFormula::contains (LogVarSet s) const
int
ProbFormula::indexOf (LogVar X) const
{
int pos = std::distance (
logVars_.begin(),
std::find (logVars_.begin(), logVars_.end(), X));
if (pos == (int)logVars_.size()) {
pos = -1;
}
return pos;
return Util::indexOf (logVars_, X);
}

View File

@ -55,7 +55,10 @@ stringToUnsigned (string str)
stringstream ss;
ss << str;
ss >> val;
abort ("error: the value tried to read is negative", val < 0);
if (val < 0) {
cerr << "error: the readed number is negative" << endl;
abort();
}
return static_cast<unsigned> (val);
}
@ -294,17 +297,6 @@ setHorusFlag (string key, string value)
void
abort (string msg, bool b)
{
if (b) {
cerr << msg << endl;
std::abort();
}
}
void
printHeader (string header, std::ostream& os)
{

View File

@ -34,7 +34,7 @@ template <typename T> bool contains (const set<T>&, const T&);
template <typename K, typename V> bool contains (
const unordered_map<K, V>&, const K&);
template <typename T> int vectorIndex (const vector<T>&, const T&);
template <typename T> int indexOf (const vector<T>&, const T&);
template <typename T> std::string toString (const T&);
@ -78,8 +78,6 @@ vector<string> getStateLines (const Vars&);
bool setHorusFlag (string key, string value);
void abort (string msg, bool = true);
void printHeader (string, std::ostream& os = std::cout);
void printSubHeader (string, std::ostream& os = std::cout);
@ -143,7 +141,7 @@ Util::contains (const unordered_map<K, V>& m, const K& k)
template <typename T> int
Util::vectorIndex (const vector<T>& v, const T& e)
Util::indexOf (const vector<T>& v, const T& e)
{
int pos = std::distance (v.begin(),
std::find (v.begin(), v.end(), e));