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
5 This file is part of libDAI.
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.
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.
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
24 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
25 /// \todo Improve documentation
28 #ifndef __defined_libdai_util_h
29 #define __defined_libdai_util_h
37 #include <boost/foreach.hpp>
44 #include <boost/tr1/unordered_map.hpp>
46 #include <tr1/unordered_map>
50 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
51 #define foreach BOOST_FOREACH
54 /// Real number (alias for double, which could be changed to long double if necessary)
59 /// Returns true if argument is NAN (Not A Number)
60 bool isnan( double x
);
62 /// Returns inverse hyperbolic tangent of argument
63 double atanh( double x
);
66 double log1p( double x
);
69 #define INFINITY (std::numeric_limits<double>::infinity())
77 /// hash_map is an alias for std::map.
78 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
80 template <typename T
, typename U
>
81 class hash_map
: public std::map
<T
,U
> {};
83 /// hash_map is an alias for std::tr1::unordered_map.
84 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
86 template <typename T
, typename U
>
87 class hash_map
: public std::tr1::unordered_map
<T
,U
> {};
91 /// Returns wall clock time in seconds
95 /// Sets the random seed
96 void rnd_seed( size_t seed
);
98 /// Returns a real number, distributed uniformly on [0,1)
101 /// Returns a real number from a standard-normal distribution
102 double rnd_stdnormal();
104 /// Returns a random integer in interval [min, max]
105 int rnd_int( int min
, int max
);
108 /// Writes a std::vector to a std::ostream
110 std::ostream
& operator << (std::ostream
& os
, const std::vector
<T
> & x
) {
112 for( typename
std::vector
<T
>::const_iterator it
= x
.begin(); it
!= x
.end(); it
++ )
113 os
<< (it
!= x
.begin() ? ", " : "") << *it
;
118 /// Writes a std::set to a std::ostream
120 std::ostream
& operator << (std::ostream
& os
, const std::set
<T
> & x
) {
122 for( typename
std::set
<T
>::const_iterator it
= x
.begin(); it
!= x
.end(); it
++ )
123 os
<< (it
!= x
.begin() ? ", " : "") << *it
;
128 /// Writes a std::map to a std::ostream
129 template<class T1
, class T2
>
130 std::ostream
& operator << (std::ostream
& os
, const std::map
<T1
,T2
> & x
) {
132 for( typename
std::map
<T1
,T2
>::const_iterator it
= x
.begin(); it
!= x
.end(); it
++ )
133 os
<< (it
!= x
.begin() ? ", " : "") << it
->first
<< "->" << it
->second
;
138 /// Writes a std::pair to a std::ostream
139 template<class T1
, class T2
>
140 std::ostream
& operator << (std::ostream
& os
, const std::pair
<T1
,T2
> & x
) {
141 os
<< "(" << x
.first
<< ", " << x
.second
<< ")";
146 /// Used to keep track of the progress made by iterative algorithms
147 class Diffs
: public std::vector
<double> {
151 std::vector
<double>::iterator _pos
;
152 std::vector
<double>::iterator _maxpos
;
155 Diffs(long maxsize
, double def
) : std::vector
<double>(), _maxsize(maxsize
), _def(def
) {
156 this->reserve(_maxsize
);
160 /// Returns maximum difference encountered
162 if( size() < _maxsize
)
167 /// Register new difference x
168 void push(double x
) {
169 if( size() < _maxsize
) {
173 if( *_maxpos
< back() ) {
184 if( _maxpos
== _pos
) {
186 _maxpos
= max_element(begin(),end());
194 /// Return maximum number of differences stored
195 size_t maxSize() { return _maxsize
; }
199 /// Split a string into tokens
200 void tokenizeString( const std::string
& s
,
201 std::vector
<std::string
>& outTokens
,
202 const std::string
& delim
="\t\n" );
205 } // end of namespace dai