ian@0
|
1 // Copyright 2011, Ian Hobson.
|
ian@0
|
2 //
|
ian@0
|
3 // This file is part of gpsynth.
|
ian@0
|
4 //
|
ian@0
|
5 // gpsynth is free software: you can redistribute it and/or modify
|
ian@0
|
6 // it under the terms of the GNU General Public License as published by
|
ian@0
|
7 // the Free Software Foundation, either version 3 of the License, or
|
ian@0
|
8 // (at your option) any later version.
|
ian@0
|
9 //
|
ian@0
|
10 // gpsynth is distributed in the hope that it will be useful,
|
ian@0
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
ian@0
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
ian@0
|
13 // GNU General Public License for more details.
|
ian@0
|
14 //
|
ian@0
|
15 // You should have received a copy of the GNU General Public License
|
ian@0
|
16 // along with gpsynth in the file COPYING.
|
ian@0
|
17 // If not, see http://www.gnu.org/licenses/.
|
ian@0
|
18
|
ian@0
|
19 // Miscellaneous C++ stdlib helper utilities
|
ian@0
|
20
|
ian@0
|
21 #pragma once
|
ian@0
|
22
|
ian@0
|
23 #include <cstdlib>
|
ian@0
|
24 #include <iostream>
|
ian@0
|
25 #include <iterator>
|
ian@0
|
26 #include <fstream>
|
ian@0
|
27 #include <map>
|
ian@0
|
28 #include <sstream>
|
ian@0
|
29 #include <stdexcept>
|
ian@0
|
30 #include <string>
|
ian@0
|
31
|
ian@0
|
32 namespace stdx {
|
ian@0
|
33
|
ian@0
|
34 typedef unsigned int uint;
|
ian@0
|
35
|
ian@0
|
36 // Helper function for retrieving values from const std::maps
|
ian@0
|
37 template<typename M>
|
ian@0
|
38 const typename M::mapped_type& GetFromMap(const M& map,
|
ian@0
|
39 const typename M::key_type& key) {
|
ian@0
|
40 typename M::const_iterator i = map.find(key);
|
ian@0
|
41 if (i == map.end()) {
|
ian@0
|
42 throw std::runtime_error("GetFromMap: specified key not in map.");
|
ian@0
|
43 }
|
ian@0
|
44 return i->second;
|
ian@0
|
45 }
|
ian@0
|
46
|
ian@0
|
47
|
ian@0
|
48 template<typename T>
|
ian@0
|
49 inline T RandomRange(T min, T max) {
|
ian@0
|
50 return static_cast<T>(((double)rand() / (float)RAND_MAX) * (max - min) + min);
|
ian@0
|
51 }
|
ian@0
|
52
|
ian@0
|
53 template<typename T>
|
ian@0
|
54 inline T RandomRangeInt(T min, T max) {
|
ian@0
|
55 return static_cast<T>(((double)rand() / (float)RAND_MAX)
|
ian@0
|
56 * (max - min)
|
ian@0
|
57 + min + 0.5);
|
ian@0
|
58 }
|
ian@0
|
59
|
ian@0
|
60 template<typename T>
|
ian@0
|
61 inline T Random(T maximum) {
|
ian@0
|
62 return static_cast<T>(((double)rand() / (double)RAND_MAX) * maximum);
|
ian@0
|
63 }
|
ian@0
|
64
|
ian@0
|
65 inline bool Random5050() {
|
ian@0
|
66 return rand() > (RAND_MAX / 2);
|
ian@0
|
67 }
|
ian@0
|
68
|
ian@0
|
69 inline double RandomCoefficient() {
|
ian@0
|
70 return static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
|
ian@0
|
71 }
|
ian@0
|
72
|
ian@0
|
73 inline bool Chance(double probability) {
|
ian@0
|
74 return RandomCoefficient() < probability;
|
ian@0
|
75 }
|
ian@0
|
76
|
ian@0
|
77 template<typename T>
|
ian@0
|
78 class RandomGenerator {
|
ian@0
|
79 T minimum_;
|
ian@0
|
80 T maximum_;
|
ian@0
|
81
|
ian@0
|
82 public:
|
ian@0
|
83 RandomGenerator(T minimum = 0, T maximum = 1)
|
ian@0
|
84 : minimum_(minimum),
|
ian@0
|
85 maximum_(maximum)
|
ian@0
|
86 {}
|
ian@0
|
87
|
ian@0
|
88 T operator()() {
|
ian@0
|
89 return RandomRange(minimum_, maximum_);
|
ian@0
|
90 }
|
ian@0
|
91 };
|
ian@0
|
92
|
ian@0
|
93
|
ian@0
|
94 // clamps input T to range specified by U and V
|
ian@0
|
95 template<typename T, typename U, typename V>
|
ian@0
|
96 T Clamp(const T& value, const U& minimum, const V& maximum) {
|
ian@0
|
97 return (value > maximum) ? maximum : (value < minimum) ? minimum : value;
|
ian@0
|
98 }
|
ian@0
|
99
|
ian@0
|
100 // Function object that increments its internal value by the provided step size
|
ian@0
|
101 template<typename T>
|
ian@0
|
102 class Stepper {
|
ian@0
|
103 T value_;
|
ian@0
|
104 T step_size_;
|
ian@0
|
105 public:
|
ian@0
|
106 Stepper(const T& step_size, const T& initial_value = T())
|
ian@0
|
107 : value_(initial_value),
|
ian@0
|
108 step_size_(step_size)
|
ian@0
|
109 {}
|
ian@0
|
110
|
ian@0
|
111 T operator()() {
|
ian@0
|
112 T result = value_;
|
ian@0
|
113 value_ += step_size_;
|
ian@0
|
114 return result;
|
ian@0
|
115 }
|
ian@0
|
116 };
|
ian@0
|
117
|
ian@0
|
118 // Function object that returns true if value is less than internal value
|
ian@0
|
119 template<typename T>
|
ian@0
|
120 class LessThan {
|
ian@0
|
121 T value_;
|
ian@0
|
122 public:
|
ian@0
|
123 LessThan(const T& value) : value_(value) {}
|
ian@0
|
124 bool operator()(const T& other) { return other < value_; }
|
ian@0
|
125 };
|
ian@0
|
126
|
ian@0
|
127 // writes a container to a stream
|
ian@0
|
128 template<typename Container>
|
ian@0
|
129 void DumpContainer(const Container& container,
|
ian@0
|
130 const std::string& separator = "\n",
|
ian@0
|
131 std::ostream& output = std::cout) {
|
ian@0
|
132 typedef typename Container::value_type Value;
|
ian@0
|
133 std::copy(container.begin(), container.end(),
|
ian@0
|
134 std::ostream_iterator<Value>(output, separator.c_str()));
|
ian@0
|
135 }
|
ian@0
|
136
|
ian@0
|
137 // saves a container to a specified file path
|
ian@0
|
138 template<typename Container>
|
ian@0
|
139 void SaveContainerToFile(const Container& container,
|
ian@0
|
140 const std::string& file_path,
|
ian@0
|
141 const std::string& separator = "\n") {
|
ian@0
|
142 std::ofstream file(file_path.c_str());
|
ian@0
|
143 DumpContainer(container, separator, file);
|
ian@0
|
144 }
|
ian@0
|
145
|
ian@0
|
146 template<typename T>
|
ian@0
|
147 void AppendToFile(const T& value_to_append,
|
ian@0
|
148 const std::string& file_path,
|
ian@0
|
149 const std::string& separator = "\n") {
|
ian@0
|
150 std::ofstream file(file_path.c_str(),
|
ian@0
|
151 std::ofstream::app | std::ofstream::out);
|
ian@0
|
152 file << value_to_append << separator;
|
ian@0
|
153 }
|
ian@0
|
154
|
ian@0
|
155 inline std::string LoadFile(const std::string& file_path) {
|
ian@0
|
156 std::ifstream file(file_path.c_str());
|
ian@0
|
157 std::stringstream buffer;
|
ian@0
|
158 buffer << file.rdbuf();
|
ian@0
|
159 return buffer.str();
|
ian@0
|
160 }
|
ian@0
|
161
|
ian@0
|
162 } // stdx namespace
|