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: // Miscellaneous C++ stdlib helper utilities ian@0: ian@0: #pragma once ian@0: ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: ian@0: namespace stdx { ian@0: ian@0: typedef unsigned int uint; ian@0: ian@0: // Helper function for retrieving values from const std::maps ian@0: template ian@0: const typename M::mapped_type& GetFromMap(const M& map, ian@0: const typename M::key_type& key) { ian@0: typename M::const_iterator i = map.find(key); ian@0: if (i == map.end()) { ian@0: throw std::runtime_error("GetFromMap: specified key not in map."); ian@0: } ian@0: return i->second; ian@0: } ian@0: ian@0: ian@0: template ian@0: inline T RandomRange(T min, T max) { ian@0: return static_cast(((double)rand() / (float)RAND_MAX) * (max - min) + min); ian@0: } ian@0: ian@0: template ian@0: inline T RandomRangeInt(T min, T max) { ian@0: return static_cast(((double)rand() / (float)RAND_MAX) ian@0: * (max - min) ian@0: + min + 0.5); ian@0: } ian@0: ian@0: template ian@0: inline T Random(T maximum) { ian@0: return static_cast(((double)rand() / (double)RAND_MAX) * maximum); ian@0: } ian@0: ian@0: inline bool Random5050() { ian@0: return rand() > (RAND_MAX / 2); ian@0: } ian@0: ian@0: inline double RandomCoefficient() { ian@0: return static_cast(rand()) / static_cast(RAND_MAX); ian@0: } ian@0: ian@0: inline bool Chance(double probability) { ian@0: return RandomCoefficient() < probability; ian@0: } ian@0: ian@0: template ian@0: class RandomGenerator { ian@0: T minimum_; ian@0: T maximum_; ian@0: ian@0: public: ian@0: RandomGenerator(T minimum = 0, T maximum = 1) ian@0: : minimum_(minimum), ian@0: maximum_(maximum) ian@0: {} ian@0: ian@0: T operator()() { ian@0: return RandomRange(minimum_, maximum_); ian@0: } ian@0: }; ian@0: ian@0: ian@0: // clamps input T to range specified by U and V ian@0: template ian@0: T Clamp(const T& value, const U& minimum, const V& maximum) { ian@0: return (value > maximum) ? maximum : (value < minimum) ? minimum : value; ian@0: } ian@0: ian@0: // Function object that increments its internal value by the provided step size ian@0: template ian@0: class Stepper { ian@0: T value_; ian@0: T step_size_; ian@0: public: ian@0: Stepper(const T& step_size, const T& initial_value = T()) ian@0: : value_(initial_value), ian@0: step_size_(step_size) ian@0: {} ian@0: ian@0: T operator()() { ian@0: T result = value_; ian@0: value_ += step_size_; ian@0: return result; ian@0: } ian@0: }; ian@0: ian@0: // Function object that returns true if value is less than internal value ian@0: template ian@0: class LessThan { ian@0: T value_; ian@0: public: ian@0: LessThan(const T& value) : value_(value) {} ian@0: bool operator()(const T& other) { return other < value_; } ian@0: }; ian@0: ian@0: // writes a container to a stream ian@0: template ian@0: void DumpContainer(const Container& container, ian@0: const std::string& separator = "\n", ian@0: std::ostream& output = std::cout) { ian@0: typedef typename Container::value_type Value; ian@0: std::copy(container.begin(), container.end(), ian@0: std::ostream_iterator(output, separator.c_str())); ian@0: } ian@0: ian@0: // saves a container to a specified file path ian@0: template ian@0: void SaveContainerToFile(const Container& container, ian@0: const std::string& file_path, ian@0: const std::string& separator = "\n") { ian@0: std::ofstream file(file_path.c_str()); ian@0: DumpContainer(container, separator, file); ian@0: } ian@0: ian@0: template ian@0: void AppendToFile(const T& value_to_append, ian@0: const std::string& file_path, ian@0: const std::string& separator = "\n") { ian@0: std::ofstream file(file_path.c_str(), ian@0: std::ofstream::app | std::ofstream::out); ian@0: file << value_to_append << separator; ian@0: } ian@0: ian@0: inline std::string LoadFile(const std::string& file_path) { ian@0: std::ifstream file(file_path.c_str()); ian@0: std::stringstream buffer; ian@0: buffer << file.rdbuf(); ian@0: return buffer.str(); ian@0: } ian@0: ian@0: } // stdx namespace