From f3ca7b2dfc96b1a9378d47889f28f646ebf28429 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Mon, 28 May 2012 16:57:45 +0100 Subject: [PATCH] add elementsToString and move things around a bit --- packages/CLPBN/horus/Util.h | 255 +++++++++++++++++++----------------- 1 file changed, 134 insertions(+), 121 deletions(-) diff --git a/packages/CLPBN/horus/Util.h b/packages/CLPBN/horus/Util.h index c0e1f0bc1..e224cd9e3 100644 --- a/packages/CLPBN/horus/Util.h +++ b/packages/CLPBN/horus/Util.h @@ -19,106 +19,11 @@ using namespace std; -template -void operator+=(std::vector& v, double val) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind1st (plus(), val)); -} - - - -template -void operator-=(std::vector& v, double val) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind1st (minus(), val)); -} - - - -template -void operator*=(std::vector& v, double val) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind1st (multiplies(), val)); -} - - - -template -void operator/=(std::vector& v, double val) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind1st (divides(), val)); -} - - - -template -void operator+=(std::vector& a, const std::vector& b) -{ - assert (a.size() == b.size()); - std::transform (a.begin(), a.end(), b.begin(), a.begin(), - plus()); -} - - - -template -void operator-=(std::vector& a, const std::vector& b) -{ - assert (a.size() == b.size()); - std::transform (a.begin(), a.end(), b.begin(), a.begin(), - minus()); -} - - - -template -void operator*=(std::vector& a, const std::vector& b) -{ - assert (a.size() == b.size()); - std::transform (a.begin(), a.end(), b.begin(), a.begin(), - multiplies()); -} - - - -template -void operator/=(std::vector& a, const std::vector& b) -{ - assert (a.size() == b.size()); - std::transform (a.begin(), a.end(), b.begin(), a.begin(), - divides()); -} - - - -template -void operator^=(std::vector& v, double exp) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind2nd (ptr_fun (std::pow), exp)); -} - - - -template -void operator^=(std::vector& v, int iexp) -{ - std::transform (v.begin(), v.end(), v.begin(), - std::bind2nd (ptr_fun (std::pow), iexp)); -} - - - namespace { const double NEG_INF = -numeric_limits::infinity(); }; - namespace Util { template void addToVector (vector&, const vector&); @@ -136,14 +41,17 @@ template bool contains ( template size_t indexOf (const vector&, const T&); -template std::string toString (const T&); - -template <> std::string toString (const bool&); - template void log (vector&); template void exp (vector&); +template string elementsToString ( + const vector& v, string sep = " "); + +template std::string toString (const T&); + +template <> std::string toString (const bool&); + double logSum (double, double); void add (Params&, const Params&, unsigned); @@ -245,28 +153,6 @@ Util::indexOf (const vector& v, const T& e) -template std::string -Util::toString (const T& t) -{ - std::stringstream ss; - ss << t; - return ss.str(); -} - - - -template -std::ostream& operator << (std::ostream& os, const vector& v) -{ - os << "[" ; - for (size_t i = 0; i < v.size(); i++) { - os << ((i != 0) ? ", " : "") << v[i]; - } - os << "]" ; - return os; -} - - template void Util::log (vector& v) { @@ -283,6 +169,28 @@ Util::exp (vector& v) +template string +Util::elementsToString (const vector& v, string sep) +{ + stringstream ss; + for (size_t i = 0; i < v.size(); i++) { + ss << ((i != 0) ? sep : "") << v[i]; + } + return ss.str(); +} + + + +template std::string +Util::toString (const T& t) +{ + std::stringstream ss; + ss << t; + return ss.str(); +} + + + inline double Util::logSum (double x, double y) { @@ -382,5 +290,110 @@ void pow (Params&, double); }; + + +template +void operator+=(std::vector& v, double val) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind1st (plus(), val)); +} + + + +template +void operator-=(std::vector& v, double val) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind1st (minus(), val)); +} + + + +template +void operator*=(std::vector& v, double val) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind1st (multiplies(), val)); +} + + + +template +void operator/=(std::vector& v, double val) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind1st (divides(), val)); +} + + + +template +void operator+=(std::vector& a, const std::vector& b) +{ + assert (a.size() == b.size()); + std::transform (a.begin(), a.end(), b.begin(), a.begin(), + plus()); +} + + + +template +void operator-=(std::vector& a, const std::vector& b) +{ + assert (a.size() == b.size()); + std::transform (a.begin(), a.end(), b.begin(), a.begin(), + minus()); +} + + + +template +void operator*=(std::vector& a, const std::vector& b) +{ + assert (a.size() == b.size()); + std::transform (a.begin(), a.end(), b.begin(), a.begin(), + multiplies()); +} + + + +template +void operator/=(std::vector& a, const std::vector& b) +{ + assert (a.size() == b.size()); + std::transform (a.begin(), a.end(), b.begin(), a.begin(), + divides()); +} + + + +template +void operator^=(std::vector& v, double exp) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind2nd (ptr_fun (std::pow), exp)); +} + + + +template +void operator^=(std::vector& v, int iexp) +{ + std::transform (v.begin(), v.end(), v.begin(), + std::bind2nd (ptr_fun (std::pow), iexp)); +} + + + +template +std::ostream& operator << (std::ostream& os, const vector& v) +{ + os << "[" ; + os << Util::elementsToString (v, ", "); + os << "]" ; + return os; +} + #endif // HORUS_UTIL_H