forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
287 lines (231 loc) · 6.78 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
#pragma once
#include <bits/stdc++.h>
#include "common.h"
#include "random.h"
#include "repr.h"
namespace impl {
template<typename T>
class GenericArray : public Repr<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;
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;
GenericArray<T> inverse() const;
// TODO(ifsmirnov): think about naming
GenericArray<T>& add(T value);
GenericArray<T> added(T value) 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;
};
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.next(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 ...Args>
GenericArray<T> GenericArray<T>::randomUnique(
size_t size, const Args& ... args)
{
typename detail::DictContainer<T>::type set;
std::cerr << "using " << typeid(set).name() << std::endl;
GenericArray<T> result;
result.reserve(size);
while (result.size() != size) {
T t = rnd.next(args...);
if (!set.count(t)) {
set.insert(t);
result.push_back(t);
}
}
return result;
}
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>
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>
GenericArray<T>& GenericArray<T>::add(T value) {
for (T& x: *this) {
x += value;
}
return *this;
}
template<typename T>
GenericArray<T> GenericArray<T>::added(T value) const {
auto res = *this;
res.add(value);
return res;
}
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<T, 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.next(static_cast<size_t>(n-i));
res.push_back(used.count(index) ? used[index] : index);
used[index] = oldValue;
}
return subseq(res);
}
// not sure if it would be needed ever
/*
template<typename T>
std::ostream& operator<<(std::ostream& out, const GenericArray<T>& array) {
return out << repr(array);
}
*/
typedef GenericArray<int> Array;
typedef GenericArray<long long> Array64;
typedef GenericArray<double> Arrayf;
} // namespace impl
using impl::GenericArray;
using impl::Array;
using impl::Array64;
using impl::Arrayf;
template<typename T>
using GenericArray2 = GenericArray<GenericArray<T>>;
using Array2 = GenericArray<Array>;
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);
}