From aeb07d43a7389a2dad557489efe3244706d85708 Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Mon, 7 May 2012 17:48:15 +0100 Subject: [PATCH] add two more utility functions --- packages/CLPBN/clpbn/bp/Util.cpp | 12 ++++++++++-- packages/CLPBN/clpbn/bp/Util.h | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/CLPBN/clpbn/bp/Util.cpp b/packages/CLPBN/clpbn/bp/Util.cpp index 9f8f1955f..80b0dd755 100644 --- a/packages/CLPBN/clpbn/bp/Util.cpp +++ b/packages/CLPBN/clpbn/bp/Util.cpp @@ -56,8 +56,7 @@ stringToUnsigned (string str) ss << str; ss >> val; if (val < 0) { - cerr << "error: the value tried to read is negative" << endl; - abort(); + abort ("error: the value tried to read is negative"); } return static_cast (val); } @@ -297,6 +296,15 @@ setHorusFlag (string key, string value) +void +abort (string msg) +{ + 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 3b65c5141..92d95681f 100644 --- a/packages/CLPBN/clpbn/bp/Util.h +++ b/packages/CLPBN/clpbn/bp/Util.h @@ -34,6 +34,8 @@ template bool contains (const set&, const T&); template bool contains ( const unordered_map&, const K&); +template int vectorIndex (const vector&, const T&); + template std::string toString (const T&); template <> std::string toString (const bool&); @@ -76,6 +78,8 @@ vector getStateLines (const Vars&); bool setHorusFlag (string key, string value); +void abort (string msg); + void printHeader (string, std::ostream& os = std::cout); void printSubHeader (string, std::ostream& os = std::cout); @@ -138,6 +142,19 @@ Util::contains (const unordered_map& m, const K& k) +template int +Util::vectorIndex (const vector& v, const T& e) +{ + int pos = std::distance (v.begin(), + std::find (v.begin(), v.end(), e)); + if (pos == (int)v.size()) { + pos = -1; + } + return pos; +} + + + template std::string Util::toString (const T& t) {