-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGenerator.cpp
289 lines (251 loc) · 9.15 KB
/
Generator.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <boost/lexical_cast.hpp>
#include "Generator.h"
#include <cmath>
#include <numeric>
#include <stdexcept>
#include <iostream>
using namespace std;
namespace {
const string octal{"01234567"}; // o
const string digits{"0123456789"}; // d
const string hex_lower{"0123456789abcdef"}; // h
const string hex_upper{"0123456789ABCDEF"}; // H
const string alpha_lower{"abcdefghijklmnopqrstuvwxyz"}; // a
const string alpha_upper{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; // A
const string alphanumeric_lower{"0123456789abcdefghijklmnopqrstuvwxyz"}; // n
const string alphanumeric_upper{"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; // N
const string alphanumeric{"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}; // b
const string* get_domain(char element) {
switch (element) {
case 'o':
return &octal;
case 'd':
return &digits;
case 'h':
return &hex_lower;
case 'H':
return &hex_upper;
case 'a':
return &alpha_lower;
case 'A':
return &alpha_upper;
case 'n':
return &alphanumeric_lower;
case 'N':
return &alphanumeric_upper;
case 'b':
return &alphanumeric;
default:
throw runtime_error{ string{"Unknown implicit range element: "} +element };
}
}
std::optional<Pattern> parse_next_pattern(const string& input, size_t start) {
Pattern pattern;
pattern.type = Pattern::Type::Implicit;
pattern.start = input.find('{', start);
if (pattern.start == string::npos) {
if (input.find('}', start) != string::npos) throw runtime_error{ "Unmatched closing brace found (})." };
return nullopt;
}
pattern.end = input.find('}', pattern.start + 1);
if (pattern.end == string::npos)
throw runtime_error{ "Unmatched open brace at " + to_string(pattern.start) };
if (pattern.end == pattern.start + 1) {
pattern.type = Pattern::Type::Continuation;
return pattern;
}
for (auto element_iter = input.begin() + pattern.start + 1;
element_iter < input.begin() + pattern.end;
++element_iter) {
const auto element = *element_iter;
if (element == ':') { pattern.type = Pattern::Type::Explicit; }
else {
auto& target = pattern.type == Pattern::Type::Implicit ? pattern.tokens.first : pattern.tokens.second;
target.push_back(element);
}
}
return move(pattern);
}
}
ImplicitRange::ImplicitRange(const string& pattern, bool leading_zeros)
: leading_zeros {leading_zeros} {
for (auto element : pattern) {
domains.emplace_back(get_domain(element));
digits.emplace_back(0);
}
}
void ImplicitRange::reset() { }
double ImplicitRange::log_size() const {
return std::accumulate(domains.begin(), domains.end(), double{0},
[](const auto& a, const auto& b) {
return a + log(b->size());
});
}
size_t ImplicitRange::size() const {
return std::accumulate(domains.begin(), domains.end(), size_t{1},
[](const auto& a, const auto& b) {
auto size = b->size();
auto new_result = a * size;
if (size != 0 && new_result / size != a) throw overflow_error{"Range size too large for size_t. Use logarithm."};
return new_result;
});
}
bool ImplicitRange::increment_return_carry(size_t pivot) {
++digits[pivot];
if (digits[pivot] < domains[pivot]->size()) return false;
digits[pivot] = 0;
return true;
}
bool ImplicitRange::increment_return_carry() {
auto pivot = digits.size() - 1;
while (increment_return_carry(pivot)) {
if (pivot == 0) return true;
--pivot;
}
return false;
}
string ImplicitRange::get_current() const {
string result;
auto is_leading_zero{true};
for (size_t i{}; i < digits.size(); i++) {
const auto digit = digits[i];
is_leading_zero &= !leading_zeros && digit == 0 && i != digits.size() - 1;
if (is_leading_zero) continue;
const auto& domain = *domains[i];
const auto element = domain[digit];
result += element;
}
return result;
}
ExplicitRange::ExplicitRange(size_t start, size_t end) : start{start}, end{end} {
if (end < start) throw runtime_error{ "End of pattern cannot be less than start." };
current = start;
}
void ExplicitRange::reset() { current = start; }
double ExplicitRange::log_size() const { return log(size()); }
size_t ExplicitRange::size() const { return end - start + 1; }
bool ExplicitRange::increment_return_carry() { return current++ == end; }
string ExplicitRange::get_current() const { return to_string(current); }
UriGenerator::UriGenerator(const string& input, bool lead_zero, bool is_telescoping) : is_complete{false} {
size_t index{};
while (auto pattern = parse_next_pattern(input, index)) {
const auto literal_length = pattern->start - index;
literal_tokens.emplace_back(input.substr(index, literal_length));
switch (pattern->type) {
case Pattern::Type::Explicit:
size_t start, end;
try { start = boost::lexical_cast<size_t>(pattern->tokens.first); }
catch (boost::bad_lexical_cast) { throw runtime_error{ "Unable to parse pattern " + pattern->tokens.first }; }
try { end = boost::lexical_cast<size_t>(pattern->tokens.second); }
catch (boost::bad_lexical_cast) { throw runtime_error{ "Unable to parse pattern " + pattern->tokens.second }; }
ranges.emplace_back(make_unique<ExplicitRange>(start, end));
break;
case Pattern::Type::Implicit:
if (is_telescoping) {
ranges.emplace_back(make_unique<TelescopingRange>(pattern->tokens.first, lead_zero));
}
else {
ranges.emplace_back(make_unique<ImplicitRange>(pattern->tokens.first, lead_zero));
}
break;
case Pattern::Type::Continuation:
if (ranges.size() == 0) throw runtime_error{ "Cannot start with a continuation pattern {}." };
ranges.emplace_back(make_unique<ContinuationRange>(*ranges.back()));
break;
default:
throw runtime_error{ "Unknown range type encountered." };
}
index = pattern->end + 1;
}
literal_tokens.emplace_back(input.substr(index));
}
std::optional<string> StdinGenerator::next() {
if (is_complete) return nullopt;
string next_line;
if (getline(cin, next_line)) return next_line;
is_complete = true;
return nullopt;
}
std::optional<string> UriGenerator::next() {
if (is_complete) return nullopt;
string result;
for (size_t i{}; i < ranges.size(); i++) {
result.append(literal_tokens[i]);
result.append(ranges[i]->get_current());
}
result.append(literal_tokens.back());
increment_ranges();
return result;
}
void UriGenerator::increment_ranges() {
if (ranges.empty()) {
is_complete = true;
return;
}
auto pivot = ranges.size() - 1;
while (ranges[pivot]->increment_return_carry()) {
ranges[pivot]->reset();
if (pivot == 0) {
is_complete = true;
return;
}
--pivot;
}
}
size_t UriGenerator::get_range_size() const {
return std::accumulate(ranges.begin(), ranges.end(),
size_t{1},
[](const auto& a, const auto& b) { return a * b->size(); });
}
double UriGenerator::get_log_range_size() const {
return log(std::accumulate(ranges.begin(), ranges.end(),
double{},
[](const auto& a, const auto& b) { return a + exp(b->log_size()); }));
}
TelescopingRange::TelescopingRange(const string& pattern_template, bool lead_zero) : index{} {
for (size_t index{}; index < pattern_template.size(); index++) {
const auto start = pattern_template.size() - index - 1;
const auto length = index + 1;
ranges.emplace_back(ImplicitRange{pattern_template.substr(start, length), lead_zero});
}
}
bool TelescopingRange::increment_return_carry() {
if (ranges[index].increment_return_carry()) {
++index;
if (ranges.size() == index) return true;
}
return false;
}
string TelescopingRange::get_current() const {
return ranges[index].get_current();
}
size_t TelescopingRange::size() const {
return std::accumulate(ranges.begin(), ranges.end(),
size_t{},
[](const auto& a, const auto& b) { return a + b.size(); });
}
double TelescopingRange::log_size() const {
return std::accumulate(ranges.begin(), ranges.end(),
double{},
[](const auto& a, const auto& b) { return a + b.log_size(); });
}
void TelescopingRange::reset() {
for (auto& range : ranges) {
range.reset();
}
index = 0;
}
ContinuationRange::ContinuationRange(const Range& target) : target{target} { }
bool ContinuationRange::increment_return_carry() {
return true;
}
string ContinuationRange::get_current() const {
return target.get_current();
}
size_t ContinuationRange::size() const {
return 1;
}
double ContinuationRange::log_size() const {
return 0;
}
void ContinuationRange::reset() { }