[Peter Gober] libDAI now compiles out-of-the-box under Cygwin
[libdai.git] / include / dai / util.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_util_h
29 #define __defined_libdai_util_h
30
31
32 #include <vector>
33 #include <set>
34 #include <map>
35 #include <iostream>
36 #include <cstdio>
37 #include <boost/foreach.hpp>
38 #include <algorithm>
39
40
41 #if defined(WINDOWS)
42 #include <map>
43 #elif defined(CYGWIN)
44 #include <boost/tr1/unordered_map.hpp>
45 #else
46 #include <tr1/unordered_map>
47 #endif
48
49
50 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
51 #define foreach BOOST_FOREACH
52
53
54 /// Real number (alias for double, which could be changed to long double if necessary)
55 typedef double Real;
56
57
58 #ifdef WINDOWS
59 /// Returns true if argument is NAN (Not A Number)
60 bool isnan( double x );
61
62 /// Returns inverse hyperbolic tangent of argument
63 double atanh( double x );
64
65 /// Returns log(1+x)
66 double log1p( double x );
67 #endif
68
69
70 namespace dai {
71
72
73 #ifdef WINDOWS
74 /// hash_map is an alias for std::map.
75 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
76 */
77 template <typename T, typename U>
78 class hash_map : public std::map<T,U> {};
79 #else
80 /// hash_map is an alias for std::tr1::unordered_map.
81 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
82 */
83 template <typename T, typename U>
84 class hash_map : public std::tr1::unordered_map<T,U> {};
85 #endif
86
87
88 /// Returns the time in seconds
89 double toc();
90
91
92 /// Sets the random seed
93 void rnd_seed( size_t seed );
94
95 /// Returns a real number, distributed uniformly on [0,1)
96 double rnd_uniform();
97
98 /// Returns a real number from a standard-normal distribution
99 double rnd_stdnormal();
100
101 /// Returns a random integer in interval [min, max]
102 int rnd_int( int min, int max );
103
104
105 /// Writes a std::vector to a std::ostream
106 template<class T>
107 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
108 os << "(";
109 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
110 os << (it != x.begin() ? ", " : "") << *it;
111 os << ")";
112 return os;
113 }
114
115 /// Writes a std::set to a std::ostream
116 template<class T>
117 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
118 os << "{";
119 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
120 os << (it != x.begin() ? ", " : "") << *it;
121 os << "}";
122 return os;
123 }
124
125 /// Writes a std::map to a std::ostream
126 template<class T1, class T2>
127 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
128 os << "{";
129 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
130 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
131 os << "}";
132 return os;
133 }
134
135 /// Writes a std::pair to a std::ostream
136 template<class T1, class T2>
137 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
138 os << "(" << x.first << ", " << x.second << ")";
139 return os;
140 }
141
142
143 /// Used to keep track of the progress made by iterative algorithms
144 class Diffs : public std::vector<double> {
145 private:
146 size_t _maxsize;
147 double _def;
148 std::vector<double>::iterator _pos;
149 std::vector<double>::iterator _maxpos;
150 public:
151 /// Constructor
152 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
153 this->reserve(_maxsize);
154 _pos = begin();
155 _maxpos = begin();
156 }
157 /// Returns maximum difference encountered
158 double maxDiff() {
159 if( size() < _maxsize )
160 return _def;
161 else
162 return( *_maxpos );
163 }
164 /// Register new difference x
165 void push(double x) {
166 if( size() < _maxsize ) {
167 push_back(x);
168 _pos = end();
169 if( size() > 1 ) {
170 if( *_maxpos < back() ) {
171 _maxpos = end();
172 _maxpos--;
173 }
174 } else {
175 _maxpos = begin();
176 }
177 }
178 else {
179 if( _pos == end() )
180 _pos = begin();
181 if( _maxpos == _pos ) {
182 *_pos++ = x;
183 _maxpos = max_element(begin(),end());
184 } else {
185 if( x > *_maxpos )
186 _maxpos = _pos;
187 *_pos++ = x;
188 }
189 }
190 }
191 /// Return maximum number of differences stored
192 size_t maxSize() { return _maxsize; }
193 };
194
195
196 } // end of namespace dai
197
198
199 #endif