comparison src/std_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 // Miscellaneous C++ stdlib helper utilities
20
21 #pragma once
22
23 #include <cstdlib>
24 #include <iostream>
25 #include <iterator>
26 #include <fstream>
27 #include <map>
28 #include <sstream>
29 #include <stdexcept>
30 #include <string>
31
32 namespace stdx {
33
34 typedef unsigned int uint;
35
36 // Helper function for retrieving values from const std::maps
37 template<typename M>
38 const typename M::mapped_type& GetFromMap(const M& map,
39 const typename M::key_type& key) {
40 typename M::const_iterator i = map.find(key);
41 if (i == map.end()) {
42 throw std::runtime_error("GetFromMap: specified key not in map.");
43 }
44 return i->second;
45 }
46
47
48 template<typename T>
49 inline T RandomRange(T min, T max) {
50 return static_cast<T>(((double)rand() / (float)RAND_MAX) * (max - min) + min);
51 }
52
53 template<typename T>
54 inline T RandomRangeInt(T min, T max) {
55 return static_cast<T>(((double)rand() / (float)RAND_MAX)
56 * (max - min)
57 + min + 0.5);
58 }
59
60 template<typename T>
61 inline T Random(T maximum) {
62 return static_cast<T>(((double)rand() / (double)RAND_MAX) * maximum);
63 }
64
65 inline bool Random5050() {
66 return rand() > (RAND_MAX / 2);
67 }
68
69 inline double RandomCoefficient() {
70 return static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
71 }
72
73 inline bool Chance(double probability) {
74 return RandomCoefficient() < probability;
75 }
76
77 template<typename T>
78 class RandomGenerator {
79 T minimum_;
80 T maximum_;
81
82 public:
83 RandomGenerator(T minimum = 0, T maximum = 1)
84 : minimum_(minimum),
85 maximum_(maximum)
86 {}
87
88 T operator()() {
89 return RandomRange(minimum_, maximum_);
90 }
91 };
92
93
94 // clamps input T to range specified by U and V
95 template<typename T, typename U, typename V>
96 T Clamp(const T& value, const U& minimum, const V& maximum) {
97 return (value > maximum) ? maximum : (value < minimum) ? minimum : value;
98 }
99
100 // Function object that increments its internal value by the provided step size
101 template<typename T>
102 class Stepper {
103 T value_;
104 T step_size_;
105 public:
106 Stepper(const T& step_size, const T& initial_value = T())
107 : value_(initial_value),
108 step_size_(step_size)
109 {}
110
111 T operator()() {
112 T result = value_;
113 value_ += step_size_;
114 return result;
115 }
116 };
117
118 // Function object that returns true if value is less than internal value
119 template<typename T>
120 class LessThan {
121 T value_;
122 public:
123 LessThan(const T& value) : value_(value) {}
124 bool operator()(const T& other) { return other < value_; }
125 };
126
127 // writes a container to a stream
128 template<typename Container>
129 void DumpContainer(const Container& container,
130 const std::string& separator = "\n",
131 std::ostream& output = std::cout) {
132 typedef typename Container::value_type Value;
133 std::copy(container.begin(), container.end(),
134 std::ostream_iterator<Value>(output, separator.c_str()));
135 }
136
137 // saves a container to a specified file path
138 template<typename Container>
139 void SaveContainerToFile(const Container& container,
140 const std::string& file_path,
141 const std::string& separator = "\n") {
142 std::ofstream file(file_path.c_str());
143 DumpContainer(container, separator, file);
144 }
145
146 template<typename T>
147 void AppendToFile(const T& value_to_append,
148 const std::string& file_path,
149 const std::string& separator = "\n") {
150 std::ofstream file(file_path.c_str(),
151 std::ofstream::app | std::ofstream::out);
152 file << value_to_append << separator;
153 }
154
155 inline std::string LoadFile(const std::string& file_path) {
156 std::ifstream file(file_path.c_str());
157 std::stringstream buffer;
158 buffer << file.rdbuf();
159 return buffer.str();
160 }
161
162 } // stdx namespace