forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
378 lines (308 loc) · 9.04 KB
/
array.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#pragma once
#include <bits/stdc++.h>
#include "common.h"
#include "random.h"
#include "printers.h"
namespace impl {
template<typename T>
class GenericArray : public ReprProxy<GenericArray<T>>, public std::vector<T> {
public:
typedef std::vector<T> Base;
using Base::Base;
GenericArray() {}
GenericArray(const GenericArray<T>&) = default;
GenericArray& operator=(const GenericArray<T>&) = default;
GenericArray(GenericArray<T>&&) = default;
GenericArray& operator=(GenericArray<T>&&) = default;
~GenericArray() {}
/* implicit */ GenericArray(const Base& base) :
Base(base)
{ }
// TODO(ifsmirnov): 'use' all methods and make inheritance private
using Base::at;
using Base::operator[];
using Base::size;
using Base::begin;
using Base::end;
using Base::insert;
using Base::clear;
using Base::erase;
template<typename F, typename ...Args>
static GenericArray<T> randomf(size_t size, F func, const Args& ... args);
template<typename F, typename ...Args>
static GenericArray<T> randomfUnique(size_t size, F func, const Args& ... args);
template<typename ...Args>
static GenericArray<T> random(size_t size, const Args& ... args);
template<typename ...Args>
static GenericArray<T> randomUnique(size_t size, const Args& ... args);
static GenericArray<T> id(size_t size, T start = T{});
GenericArray<T>& shuffle();
GenericArray<T> shuffled() const;
GenericArray<T>& reverse();
GenericArray<T> reversed() const;
GenericArray<T>& sort();
GenericArray<T> sorted() const;
template<typename Comp>
GenericArray<T>& sort(Comp&& comp) const;
template<typename Comp>
GenericArray<T> sorted(Comp&& comp);
GenericArray<T>& unique();
GenericArray<T> uniqued() const;
GenericArray<T> inverse() const;
template<typename Integer>
GenericArray<T> subseq(const std::vector<Integer>& indices) const;
template<typename Integer>
GenericArray<T> subseq(
const std::initializer_list<Integer>& indices) const;
const T& choice() const;
GenericArray<T> choice(size_t count) const;
GenericArray<T> choiceWithRepetition(size_t count) const;
GenericArray<T>& operator+=(const GenericArray<T>& other);
GenericArray<T> operator+(const GenericArray<T>& other) const;
GenericArray<T>& operator*=(int k);
GenericArray<T> operator*(int k) const;
};
template<typename T>
template<typename ...Args>
GenericArray<T> GenericArray<T>::random(size_t size, const Args& ... args) {
GenericArray<T> result(size);
for (T& x: result) {
x = rnd.tnext<T>(args...);
}
return result;
}
template<typename T>
template<typename F, typename ...Args>
GenericArray<T> GenericArray<T>::randomf(
size_t size,
F func,
const Args& ... args)
{
GenericArray<T> result(size);
for (T& x: result) {
x = func(args...);
}
return result;
}
namespace detail {
template<typename T, typename Enable = std::size_t>
struct DictContainer {
typedef std::set<T> type;
};
template<typename T>
struct DictContainer<T, typename std::hash<T>::result_type>
{
typedef std::unordered_set<T> type;
};
} // namespace detail
template<typename T>
template<typename F, typename ...Args>
GenericArray<T> GenericArray<T>::randomfUnique(
size_t size,
F func,
const Args& ... args)
{
typename detail::DictContainer<T>::type set;
GenericArray<T> result;
result.reserve(size);
size_t retries = (size + 10) * log(size + 10) * 2;
while (result.size() != size) {
if (--retries == 0) {
ensure(false, "There are not enough unique elements");
}
T t = func(args...);
if (!set.count(t)) {
set.insert(t);
result.push_back(t);
}
}
return result;
}
template<typename T>
template<typename ...Args>
GenericArray<T> GenericArray<T>::randomUnique(
size_t size, const Args& ... args)
{
return GenericArray<T>::randomfUnique(
size,
rnd.tnext<T, Args...>,
args...);
}
template<typename T>
GenericArray<T> GenericArray<T>::id(size_t size, T start) {
constexpr bool enable = std::is_integral<T>::value;
static_assert(enable, "Cannot call Array<T>::id with non-integral T");
if (enable) {
GenericArray<T> result(size);
std::iota(result.begin(), result.end(), start);
return result;
} else {
return {};
}
}
template<typename T>
GenericArray<T>& GenericArray<T>::shuffle() {
for (size_t i = 1; i < size(); ++i) {
std::swap(at(i), at(rnd.next(i + 1)));
}
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::shuffled() const {
auto res = *this;
res.shuffle();
return res;
}
template<typename T>
GenericArray<T>& GenericArray<T>::reverse() {
std::reverse(begin(), end());
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::reversed() const {
auto res = *this;
res.reverse();
return res;
}
template<typename T>
GenericArray<T>& GenericArray<T>::sort() {
std::sort(begin(), end());
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::sorted() const {
auto res = *this;
res.sort();
return res;
}
template<typename T>
template<typename Comp>
GenericArray<T>& GenericArray<T>::sort(Comp&& comp) const {
std::sort(begin(), end(), comp);
return *this;
}
template<typename T>
template<typename Comp>
GenericArray<T> GenericArray<T>::sorted(Comp&& comp) {
auto res = *this;
res.sort(comp);
return res;
}
template<typename T>
GenericArray<T>& GenericArray<T>::unique() {
erase(std::unique(begin(), end()), end());
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::uniqued() const {
auto res = *this;
res.unique();
return res;
}
template<typename T>
GenericArray<T> GenericArray<T>::inverse() const {
static_assert(
std::is_integral<T>::value,
"Can only take inverse permutation of integral array");
int n = size();
// sanity check
ensure(*max_element(begin(), end()) == n-1 &&
*min_element(begin(), end()) == 0,
"Trying to take inverse of the array which is not a permutation");
const static T NONE = static_cast<T>(-1);
GenericArray<T> result(n, NONE);
for (int i = 0; i < n; ++i) {
ensure(result[at(i)] == NONE,
"Trying to take inverse of the array which is not a permutation");
result[at(i)] = i;
}
return result;
}
template<typename T>
template<typename Integer>
GenericArray<T> GenericArray<T>::subseq(
const std::vector<Integer>& indices) const
{
GenericArray<T> result;
result.reserve(indices.size());
for (Integer idx: indices) {
result.push_back(at(idx));
}
return result;
}
// TODO(ifsmirnov): ever need to make it faster?
template<typename T>
template<typename Integer>
GenericArray<T> GenericArray<T>::subseq(
const std::initializer_list<Integer>& indices) const
{
return subseq(std::vector<T>(indices));
}
template<typename T>
const T& GenericArray<T>::choice() const {
return at(rnd.next(size()));
}
template<typename T>
GenericArray<T> GenericArray<T>::choice(size_t count) const {
ensure(count <= size());
size_t n = size();
std::unordered_map<size_t, size_t> used;
std::vector<size_t> res;
for (size_t i = 0; i < count; ++i) {
size_t oldValue = used.count(n-i-1) ? used[n-i-1] : n-i-1;
size_t index = rnd.tnext<size_t>(n-i);
res.push_back(used.count(index) ? used[index] : index);
used[index] = oldValue;
}
return subseq(res);
}
template<typename T>
GenericArray<T> GenericArray<T>::choiceWithRepetition(size_t count) const {
GenericArray<T> res(count);
for (T& t: res) {
t = choice();
}
return res;
}
template<typename T>
GenericArray<T>& GenericArray<T>::operator+=(const GenericArray<T>& other) {
insert(end(), other.begin(), other.end());
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::operator+(const GenericArray<T>& other) const {
GenericArray<T> copy(*this);
return copy += other;
}
template<typename T>
GenericArray<T>& GenericArray<T>::operator*=(int k) {
if (k == 0) {
clear();
return *this;
}
size_t size = this->size();
while (k-- > 1) {
insert(end(), begin(), begin() + size);
}
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::operator*(int k) const {
GenericArray<T> copy(*this);
return copy *= k;
}
} // namespace impl
template<typename T>
using TArray = impl::GenericArray<T>;
using Array = impl::GenericArray<int>;
using Array64 = impl::GenericArray<long long>;
using Arrayf = impl::GenericArray<double>;
using Arrayp = impl::GenericArray<std::pair<int, int>>;
template<typename T>
impl::GenericArray<T> makeArray(const std::vector<T>& values) {
return impl::GenericArray<T>(values);
}
template<typename T>
impl::GenericArray<T> makeArray(const std::initializer_list<T>& values) {
return impl::GenericArray<T>(values);
}