00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00014
00015
00016 #ifndef __defined_libdai_util_h
00017 #define __defined_libdai_util_h
00018
00019
00020 #include <string>
00021 #include <vector>
00022 #include <set>
00023 #include <map>
00024 #include <iostream>
00025 #include <boost/foreach.hpp>
00026 #include <boost/functional/hash.hpp>
00027 #include <boost/lexical_cast.hpp>
00028 #include <algorithm>
00029 #include <cerrno>
00030
00031 #include <dai/exceptions.h>
00032
00033
00034 #if defined(WINDOWS)
00035 #include <boost/tr1/unordered_map.hpp>
00036 #elif defined(CYGWIN)
00037 #include <boost/tr1/unordered_map.hpp>
00038 #elif defined(MACOSX)
00039 #include <boost/tr1/unordered_map.hpp>
00040 #else
00041 #include <tr1/unordered_map>
00042 #endif
00043
00044
00046 #define foreach BOOST_FOREACH
00047
00048 #ifdef DAI_DEBUG
00050
00055 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
00057 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
00058 #else
00059 #define DAI_PV(x) do {} while(0)
00060 #define DAI_DMSG(str) do {} while(0)
00061 #endif
00062
00064 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
00065
00066
00067 #ifdef WINDOWS
00069 double atanh( double x );
00070
00072 double log1p( double x );
00073
00075 #define INFINITY (std::numeric_limits<Real>::infinity())
00076 #endif
00077
00078
00079 namespace dai {
00080
00081
00083 typedef double Real;
00084
00086 bool isnan( Real x );
00087
00089 inline Real log( Real x ) {
00090 return std::log(x);
00091 }
00092
00094 inline Real log0( Real x ) {
00095 return x ? std::log(x) : 0;
00096 }
00097
00099 inline Real exp( Real x ) {
00100 return std::exp(x);
00101 }
00102
00104 inline Real pow( Real x, Real y ) {
00105 errno = 0;
00106 Real result = std::pow(x, y);
00107 DAI_DEBASSERT( errno == 0 );
00108 return result;
00109 }
00110
00111
00113
00115 template <typename T, typename U, typename H = boost::hash<T> >
00116 class hash_map : public std::tr1::unordered_map<T,U,H> {};
00117
00118
00120 double toc();
00121
00122
00124 template<class T>
00125 inline T abs( const T &t ) {
00126 return (t < 0) ? (-t) : t;
00127 }
00128
00129
00131 void rnd_seed( size_t seed );
00132
00134 Real rnd_uniform();
00135
00137 Real rnd_stdnormal();
00138
00140 int rnd_int( int min, int max );
00141
00143 inline int rnd( int n ) {
00144 return rnd_int( 0, n-1 );
00145 }
00146
00147
00149 template<class T>
00150 std::string toString( const T& x ) {
00151 return boost::lexical_cast<std::string>(x);
00152 }
00153
00154
00156 template<class T>
00157 T fromString( const std::string& x ) {
00158 return boost::lexical_cast<T>(x);
00159 }
00160
00161
00163 template<class T>
00164 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
00165 os << "(";
00166 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00167 os << (it != x.begin() ? ", " : "") << *it;
00168 os << ")";
00169 return os;
00170 }
00171
00173 template<class T>
00174 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
00175 os << "{";
00176 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00177 os << (it != x.begin() ? ", " : "") << *it;
00178 os << "}";
00179 return os;
00180 }
00181
00183 template<class T1, class T2>
00184 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
00185 os << "{";
00186 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
00187 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
00188 os << "}";
00189 return os;
00190 }
00191
00193 template<class T1, class T2>
00194 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
00195 os << "(" << x.first << ", " << x.second << ")";
00196 return os;
00197 }
00198
00200 template<class T>
00201 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
00202 std::vector<T> w;
00203 w.reserve( u.size() + v.size() );
00204 for( size_t i = 0; i < u.size(); i++ )
00205 w.push_back( u[i] );
00206 for( size_t i = 0; i < v.size(); i++ )
00207 w.push_back( v[i] );
00208 return w;
00209 }
00210
00212
00217 std::vector<std::string> tokenizeString( const std::string& s, bool singleDelim, const std::string& delim="\t\n" );
00218
00219
00221
00223 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
00224
00225
00227
00231 typedef enum { NORMPROB, NORMLINF } ProbNormType;
00232
00234
00241 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } ProbDistType;
00242
00243
00244 }
00245
00246
00247 #endif