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 <algorithm>
00028
00029
00030 #if defined(WINDOWS)
00031 #include <boost/tr1/unordered_map.hpp>
00032 #elif defined(CYGWIN)
00033 #include <boost/tr1/unordered_map.hpp>
00034 #elif defined(MACOSX)
00035 #include <boost/tr1/unordered_map.hpp>
00036 #else
00037 #include <tr1/unordered_map>
00038 #endif
00039
00040
00042 #define foreach BOOST_FOREACH
00043
00044 #ifdef DAI_DEBUG
00046
00051 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
00053 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
00054 #else
00055 #define DAI_PV(x) do {} while(0)
00056 #define DAI_DMSG(str) do {} while(0)
00057 #endif
00058
00060 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
00061
00062
00063 #ifdef WINDOWS
00065 bool isnan( double x );
00066
00068 double atanh( double x );
00069
00071 double log1p( double x );
00072
00074 #define INFINITY (std::numeric_limits<Real>::infinity())
00075 #endif
00076
00077
00078 namespace dai {
00079
00080
00082 typedef double Real;
00083
00085 inline Real log( Real x ) {
00086 return std::log(x);
00087 }
00088
00090 inline Real log0( Real x ) {
00091 return x ? std::log(x) : 0;
00092 }
00093
00095 inline Real exp( Real x ) {
00096 return std::exp(x);
00097 }
00098
00100 Real max( const std::vector<Real> &v );
00101
00102
00104
00106 template <typename T, typename U, typename H = boost::hash<T> >
00107 class hash_map : public std::tr1::unordered_map<T,U,H> {};
00108
00109
00111 double toc();
00112
00113
00115 template<class T>
00116 inline T abs( const T &t ) {
00117 return (t < 0) ? (-t) : t;
00118 }
00119
00120
00122 void rnd_seed( size_t seed );
00123
00125 Real rnd_uniform();
00126
00128 Real rnd_stdnormal();
00129
00131 int rnd_int( int min, int max );
00132
00134 inline int rnd( int n ) {
00135 return rnd_int( 0, n-1 );
00136 }
00137
00138
00140 template<class T>
00141 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
00142 os << "(";
00143 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00144 os << (it != x.begin() ? ", " : "") << *it;
00145 os << ")";
00146 return os;
00147 }
00148
00150 template<class T>
00151 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
00152 os << "{";
00153 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
00154 os << (it != x.begin() ? ", " : "") << *it;
00155 os << "}";
00156 return os;
00157 }
00158
00160 template<class T1, class T2>
00161 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
00162 os << "{";
00163 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
00164 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
00165 os << "}";
00166 return os;
00167 }
00168
00170 template<class T1, class T2>
00171 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
00172 os << "(" << x.first << ", " << x.second << ")";
00173 return os;
00174 }
00175
00177 template<class T>
00178 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
00179 std::vector<T> w;
00180 w.reserve( u.size() + v.size() );
00181 for( size_t i = 0; i < u.size(); i++ )
00182 w.push_back( u[i] );
00183 for( size_t i = 0; i < v.size(); i++ )
00184 w.push_back( v[i] );
00185 return w;
00186 }
00187
00189 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
00190
00191
00192 }
00193
00194
00195 #endif