comparison src/boost_ex.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:add35537fdbb
1 // Copyright 2011, Ian Hobson.
2 //
3 // This file is part of gpsynth.
4 //
5 // gpsynth is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // gpsynth is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with gpsynth in the file COPYING.
17 // If not, see http://www.gnu.org/licenses/.
18
19 // Boost helper functions
20
21 #pragma once
22
23 #include "boost/foreach.hpp"
24 #include "boost/random/mersenne_twister.hpp"
25 #include "boost/random/normal_distribution.hpp"
26 #include "boost/random/variate_generator.hpp"
27 #include "boost/shared_array.hpp"
28 #include "boost/thread.hpp"
29
30 #include <cstddef>
31 #include <ctime>
32
33 // foreach macro
34 #define foreach BOOST_FOREACH
35 #define foreach_reverse BOOST_REVERSE_FOREACH
36
37 namespace bx {
38
39 // Sleep the current thread for a certain number of milliseconds
40 inline void SleepMs(int milliseconds) {
41 boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
42 }
43
44 // boost.random helpers
45 typedef boost::random::mt19937 EngineType;
46 typedef boost::variate_generator<EngineType, boost::normal_distribution<> >
47 GaussianGenerator;
48
49 class Random {
50 EngineType engine_;
51
52 public:
53 Random() : engine_(static_cast<unsigned int>(std::time(0))) {}
54 EngineType& Engine() { return engine_; }
55
56 static Random& Singleton() {
57 static Random random;
58 return random;
59 }
60 };
61
62 static GaussianGenerator MakeGaussianGenerator(double mean = 0.0,
63 double sigma = 1.0) {
64 boost::normal_distribution<> distribution(mean, sigma);
65 return GaussianGenerator(Random::Singleton().Engine(), distribution);
66 }
67
68 static EngineType& RandomEngine() {
69 return Random::Singleton().Engine();
70 }
71
72
73 } // bx namespace