Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[libdai.git] / include / dai / util.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_util_h
23 #define __defined_libdai_util_h
24
25
26 #include <vector>
27 #include <set>
28 #include <map>
29 #include <iostream>
30 #include <cstdio>
31 #include <boost/foreach.hpp>
32
33
34 #ifdef WINDOWS
35 #include <map>
36 #else
37 #include <tr1/unordered_map>
38 #endif
39
40
41 #define foreach BOOST_FOREACH
42
43
44 #ifdef WINDOWS
45 bool isnan( double x );
46 double atanh( double x );
47 double log1p( double x );
48 #endif
49
50
51 namespace dai {
52
53
54 #ifdef WINDOWS
55 template <typename T, typename U>
56 class hash_map : public std::map<T,U> {};
57 #else
58 template <typename T, typename U>
59 class hash_map : public std::tr1::unordered_map<T,U> {};
60 #endif
61
62
63 double toc();
64 void rnd_seed( size_t seed );
65 double rnd_uniform();
66 double rnd_stdnormal();
67 int rnd_int( int min, int max );
68
69
70 // Output a std::vector
71 template<class T>
72 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
73 os << "(";
74 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
75 os << (it != x.begin() ? ", " : "") << *it;
76 os << ")";
77 return os;
78 }
79
80
81 // Output a std::set
82 template<class T>
83 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
84 os << "{";
85 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
86 os << (it != x.begin() ? ", " : "") << *it;
87 os << "}";
88 return os;
89 }
90
91
92 /// Output a std::map
93 template<class T1, class T2>
94 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
95 os << "{";
96 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
97 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
98 os << "}";
99 return os;
100 }
101
102
103 /// Output a std::pair
104 template<class T1, class T2>
105 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
106 os << "(" << x.first << ", " << x.second << ")";
107 return os;
108 }
109
110
111 } // end of namespace dai
112
113
114 #endif