ian@0: // Copyright 2011, Ian Hobson. ian@0: // ian@0: // This file is part of gpsynth. ian@0: // ian@0: // gpsynth is free software: you can redistribute it and/or modify ian@0: // it under the terms of the GNU General Public License as published by ian@0: // the Free Software Foundation, either version 3 of the License, or ian@0: // (at your option) any later version. ian@0: // ian@0: // gpsynth is distributed in the hope that it will be useful, ian@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ian@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ian@0: // GNU General Public License for more details. ian@0: // ian@0: // You should have received a copy of the GNU General Public License ian@0: // along with gpsynth in the file COPYING. ian@0: // If not, see http://www.gnu.org/licenses/. ian@0: ian@0: // Boost helper functions ian@0: ian@0: #pragma once ian@0: ian@0: #include "boost/foreach.hpp" ian@0: #include "boost/random/mersenne_twister.hpp" ian@0: #include "boost/random/normal_distribution.hpp" ian@0: #include "boost/random/variate_generator.hpp" ian@0: #include "boost/shared_array.hpp" ian@0: #include "boost/thread.hpp" ian@0: ian@0: #include ian@0: #include ian@0: ian@0: // foreach macro ian@0: #define foreach BOOST_FOREACH ian@0: #define foreach_reverse BOOST_REVERSE_FOREACH ian@0: ian@0: namespace bx { ian@0: ian@0: // Sleep the current thread for a certain number of milliseconds ian@0: inline void SleepMs(int milliseconds) { ian@0: boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); ian@0: } ian@0: ian@0: // boost.random helpers ian@0: typedef boost::random::mt19937 EngineType; ian@0: typedef boost::variate_generator > ian@0: GaussianGenerator; ian@0: ian@0: class Random { ian@0: EngineType engine_; ian@0: ian@0: public: ian@0: Random() : engine_(static_cast(std::time(0))) {} ian@0: EngineType& Engine() { return engine_; } ian@0: ian@0: static Random& Singleton() { ian@0: static Random random; ian@0: return random; ian@0: } ian@0: }; ian@0: ian@0: static GaussianGenerator MakeGaussianGenerator(double mean = 0.0, ian@0: double sigma = 1.0) { ian@0: boost::normal_distribution<> distribution(mean, sigma); ian@0: return GaussianGenerator(Random::Singleton().Engine(), distribution); ian@0: } ian@0: ian@0: static EngineType& RandomEngine() { ian@0: return Random::Singleton().Engine(); ian@0: } ian@0: ian@0: ian@0: } // bx namespace