From f7ba86d3ed849ab879545a61b1c67cbc577cdc44 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Tue, 15 May 2012 19:41:14 +0100 Subject: [PATCH] kill Util::abort, s/vectorIndex/indexOf and s/isCarteesianProduct/isCartesianProduct --- packages/CLPBN/clpbn/bp/ConstraintTree.cpp | 29 ++++++++-------------- packages/CLPBN/clpbn/bp/ConstraintTree.h | 2 +- packages/CLPBN/clpbn/bp/FoveSolver.cpp | 4 +-- packages/CLPBN/clpbn/bp/Parfactor.cpp | 4 +-- packages/CLPBN/clpbn/bp/ProbFormula.cpp | 8 +----- packages/CLPBN/clpbn/bp/Util.cpp | 16 +++--------- packages/CLPBN/clpbn/bp/Util.h | 8 +++--- 7 files changed, 24 insertions(+), 47 deletions(-) diff --git a/packages/CLPBN/clpbn/bp/ConstraintTree.cpp b/packages/CLPBN/clpbn/bp/ConstraintTree.cpp index d854abb1b..46899b47b 100644 --- a/packages/CLPBN/clpbn/bp/ConstraintTree.cpp +++ b/packages/CLPBN/clpbn/bp/ConstraintTree.cpp @@ -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 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) { diff --git a/packages/CLPBN/clpbn/bp/ConstraintTree.h b/packages/CLPBN/clpbn/bp/ConstraintTree.h index 8e7283264..f1206ba0e 100644 --- a/packages/CLPBN/clpbn/bp/ConstraintTree.h +++ b/packages/CLPBN/clpbn/bp/ConstraintTree.h @@ -185,7 +185,7 @@ class ConstraintTree TinySet getConditionalCounts (const LogVarSet&); - bool isCarteesianProduct (const LogVarSet&); + bool isCartesianProduct (const LogVarSet&); std::pair split (const Tuple&); diff --git a/packages/CLPBN/clpbn/bp/FoveSolver.cpp b/packages/CLPBN/clpbn/bp/FoveSolver.cpp index 09da088e8..b3a1a24dc 100644 --- a/packages/CLPBN/clpbn/bp/FoveSolver.cpp +++ b/packages/CLPBN/clpbn/bp/FoveSolver.cpp @@ -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_); diff --git a/packages/CLPBN/clpbn/bp/Parfactor.cpp b/packages/CLPBN/clpbn/bp/Parfactor.cpp index c5ad13d14..50813694c 100644 --- a/packages/CLPBN/clpbn/bp/Parfactor.cpp +++ b/packages/CLPBN/clpbn/bp/Parfactor.cpp @@ -194,7 +194,7 @@ Parfactor::multiply (Parfactor& g) TFactor::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; diff --git a/packages/CLPBN/clpbn/bp/ProbFormula.cpp b/packages/CLPBN/clpbn/bp/ProbFormula.cpp index 827f255b7..09f555ebe 100644 --- a/packages/CLPBN/clpbn/bp/ProbFormula.cpp +++ b/packages/CLPBN/clpbn/bp/ProbFormula.cpp @@ -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); } diff --git a/packages/CLPBN/clpbn/bp/Util.cpp b/packages/CLPBN/clpbn/bp/Util.cpp index 6eb220779..083103ff8 100644 --- a/packages/CLPBN/clpbn/bp/Util.cpp +++ b/packages/CLPBN/clpbn/bp/Util.cpp @@ -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 (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) { diff --git a/packages/CLPBN/clpbn/bp/Util.h b/packages/CLPBN/clpbn/bp/Util.h index 72517fb6b..00656148f 100644 --- a/packages/CLPBN/clpbn/bp/Util.h +++ b/packages/CLPBN/clpbn/bp/Util.h @@ -34,7 +34,7 @@ template bool contains (const set&, const T&); template bool contains ( const unordered_map&, const K&); -template int vectorIndex (const vector&, const T&); +template int indexOf (const vector&, const T&); template std::string toString (const T&); @@ -78,8 +78,6 @@ vector 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,10 +141,10 @@ Util::contains (const unordered_map& m, const K& k) template int -Util::vectorIndex (const vector& v, const T& e) +Util::indexOf (const vector& v, const T& e) { int pos = std::distance (v.begin(), - std::find (v.begin(), v.end(), e)); + std::find (v.begin(), v.end(), e)); if (pos == (int)v.size()) { pos = -1; }