-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.h
243 lines (214 loc) · 8.33 KB
/
options.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#pragma once
#include <random>
#include <memory>
#include <stdint.h>
#include "string_converter.h"
#include "errors.h"
namespace calyx
{
/**
* @brief Class for dealing with various options relating to grammars. For example, whether to run grammars in 'loose' or 'strict'
* mode, and what the primary string converter is. This class provides its own random number generation API, and also
* can convert `std::string`s to `String_t`s.
*/
class Options : public StringConverter<String_t>
{
public:
static const bool DEFAULT_STRICT;
using RandomSource_t = std::mt19937;
/**
* @brief Construct a new Options object with a default random number generator
*
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);
/**
* @brief Construct a new Options object with a specified random seed
*
* @param seed The random seed to use
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(
std::uint32_t seed,
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);
/**
* @brief Construct a new Options object with a specific random number generator
*
* @param rng The random number generator to use
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(
RandomSource_t rng,
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);
Options(Options&& other) noexcept;
~Options() override = default;
Options(const Options& old) = delete;
Options& operator=(const Options& other) noexcept = delete;
Options& operator=(Options&& old) noexcept;
/**
* @brief Generated a random number with minimum and maximum bounds, and ignores errors.
*
* **Only use this when confident that errors will never be generated!**
*
* Max must be greater than 0 and min!
*
* @tparam T The integer type of the number to be generated.
* @param min The minimum bound of the random number (inclusive)
* @param max The maximum bound of the random number (exclusive)
* @return int Returns a random int between min (inclusive) and max (exclusive)
*/
template <typename T>
T randomInteger(T min, T max)
{
ErrorHolder errs;
return randomInteger(min, max, errs);
}
/**
* @brief Shuffles a vector of items, in place.
*
* @tparam T The type of the items in the vector
* @param items The vector to shuffle
*/
template <typename T>
void shuffle(std::vector<T>& items)
{
std::shuffle(items.begin(), items.end(), _rng);
}
/**
* @brief Generated a random number with a maximum bound and ignores errors.
*
* **Only use this when confident that errors will never be generated!**
*
* Max must be greater than 0!
*
* @tparam T The integer type of the number to be generated.
* @param max The maximum bound of the random number (exclusive)
* @return int Returns a random int between 0 (inclusive) and max (exclusive)
*/
template <typename T>
T randomInteger(T max)
{
ErrorHolder errs;
return randomInteger(max, errs);
}
/**
* @brief Generated a random number with minimum and maximum bounds
*
* Max must be greater than 0 and min.
*
* @tparam T The integer type of the number to be generated.
* @param min The minimum bound of the random number (inclusive)
* @param max The maximum bound of the random number (exclusive)
* @param errorHolder Reference to error holder
* @return int Returns a random int between min (inclusive) and max (exclusive)
*/
template <typename T>
T randomInteger(T min, T max, ErrorHolder& errorHolder)
{
static_assert(std::is_integral_v<T>, "T must be an integral (integer) type");
if (max <= 0)
{
String_t msg = _converter->fromString("Max bound must be positive");
errorHolder.setError(msg);
return 0;
}
if (max <= min)
{
String_t msg = _converter->fromString("Max bound must be greater than min!");
errorHolder.setError(msg);
return 0;
}
std::uniform_int_distribution distribution(min, max - 1);
return distribution(_rng);
}
/**
* @brief Generated a random number with a maximum bound
*
* Max must be greater than 0.
*
* @tparam T The integer type of the number to be generated.
* @param max The maximum bound of the random number (exclusive)
* @param errorHolder Reference to error holder
* @return int Returns a random int between 0 (inclusive) and max (exclusive)
*/
template <typename T>
T randomInteger(T max, ErrorHolder& errorHolder)
{
static_assert(std::is_integral_v<T>, "T must be an integral (integer) type");
if (max <= 0)
{
const String_t msg = _converter->fromString("Max bound must be positive");
errorHolder.setError(msg);
return 0;
}
std::uniform_int_distribution<T> distribution(0, max - 1);
return distribution(_rng);
}
/**
* @brief Generates a random number
*
* @tparam T The integer type of the number to be generated.
* @return int Returns a random int
*/
template <typename T>
T randomInteger()
{
static_assert(std::is_integral_v<T>, "T must be an integral (integer) type");
return _rng();
}
/**
* @brief Generates a random real number between 0 and 1.
*
* @tparam T The real type of the number to be generated. Must be float or double.
* @return double Returns a random real number between 0 and 1
*/
template <typename T>
T randomReal()
{
static_assert(std::is_floating_point_v<T>, "T must be a real number type (eg double or float)");
std::uniform_real_distribution distribution(
0.0, 1.0
);
return distribution(_rng);
}
/**
* @brief Check whether to use strict-rule checking for expansion of unknown rules
*
* @return true
* @return false
*/
bool isStrict() const;
/**
* @brief Converts a string-like object into a concrete, std::string.
*
* Delegates this functionality to {@link #_converter}.
*
* @param stringLike The string-like object to be converted
* @return const std::string The std::string equivalent of {@code stringLike}
*/
std::string toString(const String_t& stringLike) const override;
/**
* @brief Converts a concrete, std::string into a string-like object.
*
* Delegates this functionality to {@link #_converter}.
*
* @param stdString The std::string to be converted
* @return const String_t The String_t equivalent of the {@code stdString}
*/
String_t fromString(const std::string& stdString) const override;
private:
bool _strict;
RandomSource_t _rng;
std::unique_ptr<StringConverter> _converter;
};
}