Replaced the standard assert() macro by DAI_ASSERT
[libdai.git] / include / dai / util.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
14 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_util_h
18 #define __defined_libdai_util_h
19
20
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <map>
25 #include <iostream>
26 #include <boost/foreach.hpp>
27 #include <boost/functional/hash.hpp>
28 #include <algorithm>
29
30
31 #if defined(WINDOWS)
32 #include <map>
33 #elif defined(CYGWIN)
34 #include <boost/tr1/unordered_map.hpp>
35 #else
36 #include <tr1/unordered_map>
37 #endif
38
39
40 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
41 #define foreach BOOST_FOREACH
42
43 #ifdef DAI_DEBUG
44 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
45 /**
46 * Useful debugging macro to see what your code is doing.
47 * Example: \code DAI_PV(3+4) \endcode
48 * Output: \code 3+4= 7 \endcode
49 */
50 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
51 /// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
52 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
53 #else
54 #define DAI_PV(x) do {} while(0)
55 #define DAI_DMSG(str) do {} while(0)
56 #endif
57
58 /// Produces accessor and mutator methods according to the common pattern.
59 /** Example:
60 * \code DAI_ACCMUT(size_t& maxIter(), { return props.maxiter; }); \endcode
61 * \todo At the moment, only the mutator appears in doxygen documentation.
62 */
63 #define DAI_ACCMUT(x,y) \
64 x y; \
65 const x const y;
66
67 /// Macro to give error message \a stmt if props.verbose>=\a n
68 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { cerr << stmt; }
69
70
71 /// Real number (alias for double, which could be changed to long double if necessary)
72 typedef double Real;
73
74
75 #ifdef WINDOWS
76 /// Returns true if argument is NAN (Not A Number)
77 bool isnan( double x );
78
79 /// Returns inverse hyperbolic tangent of argument
80 double atanh( double x );
81
82 /// Returns log(1+x)
83 double log1p( double x );
84
85 /// Define INFINITY
86 #define INFINITY (std::numeric_limits<double>::infinity())
87 #endif
88
89
90 namespace dai {
91
92
93 #ifdef WINDOWS
94 /// hash_map is an alias for std::map.
95 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
96 */
97 template <typename T, typename U, typename H = boost::hash<T> >
98 class hash_map : public std::map<T,U> {};
99 #else
100 /// hash_map is an alias for std::tr1::unordered_map.
101 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
102 */
103 template <typename T, typename U, typename H = boost::hash<T> >
104 class hash_map : public std::tr1::unordered_map<T,U,H> {};
105 #endif
106
107
108 /// Returns wall clock time in seconds
109 double toc();
110
111
112 /// Sets the random seed
113 void rnd_seed( size_t seed );
114
115 /// Returns a real number, distributed uniformly on [0,1)
116 double rnd_uniform();
117
118 /// Returns a real number from a standard-normal distribution
119 double rnd_stdnormal();
120
121 /// Returns a random integer in interval [min, max]
122 int rnd_int( int min, int max );
123
124 /// Returns a random integer in the half-open interval \f$[0,n)\f$
125 inline int rnd( int n) {
126 return rnd_int( 0, n-1 );
127 }
128
129
130 /// Writes a std::vector to a std::ostream
131 template<class T>
132 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
133 os << "(";
134 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
135 os << (it != x.begin() ? ", " : "") << *it;
136 os << ")";
137 return os;
138 }
139
140 /// Writes a std::set to a std::ostream
141 template<class T>
142 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
143 os << "{";
144 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
145 os << (it != x.begin() ? ", " : "") << *it;
146 os << "}";
147 return os;
148 }
149
150 /// Writes a std::map to a std::ostream
151 template<class T1, class T2>
152 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
153 os << "{";
154 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
155 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
156 os << "}";
157 return os;
158 }
159
160 /// Writes a std::pair to a std::ostream
161 template<class T1, class T2>
162 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
163 os << "(" << x.first << ", " << x.second << ")";
164 return os;
165 }
166
167 /// Concatenate two vectors
168 template<class T>
169 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
170 std::vector<T> w;
171 w.reserve( u.size() + v.size() );
172 for( size_t i = 0; i < u.size(); i++ )
173 w.push_back( u[i] );
174 for( size_t i = 0; i < v.size(); i++ )
175 w.push_back( v[i] );
176 return w;
177 }
178
179 /// Split a string into tokens
180 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
181
182 /// Used to keep track of the progress made by iterative algorithms
183 class Diffs : public std::vector<double> {
184 private:
185 size_t _maxsize;
186 double _def;
187 std::vector<double>::iterator _pos;
188 std::vector<double>::iterator _maxpos;
189 public:
190 /// Constructor
191 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
192 this->reserve(_maxsize);
193 _pos = begin();
194 _maxpos = begin();
195 }
196 /// Returns maximum difference encountered
197 double maxDiff() {
198 if( size() < _maxsize )
199 return _def;
200 else
201 return( *_maxpos );
202 }
203 /// Register new difference x
204 void push(double x) {
205 if( size() < _maxsize ) {
206 push_back(x);
207 _pos = end();
208 if( size() > 1 ) {
209 if( *_maxpos < back() ) {
210 _maxpos = end();
211 _maxpos--;
212 }
213 } else {
214 _maxpos = begin();
215 }
216 } else {
217 if( _pos == end() )
218 _pos = begin();
219 if( _maxpos == _pos ) {
220 *_pos++ = x;
221 _maxpos = max_element(begin(),end());
222 } else {
223 if( x > *_maxpos )
224 _maxpos = _pos;
225 *_pos++ = x;
226 }
227 }
228 }
229 /// Return maximum number of differences stored
230 size_t maxSize() { return _maxsize; }
231 };
232
233
234 } // end of namespace dai
235
236
237 #endif