-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunderscore.h
364 lines (292 loc) · 9.52 KB
/
underscore.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
#ifndef UNDERSCORE_H
#define UNDERSCORE_H
#include <algorithm>
#include <numeric>
#include <random>
#include <map>
namespace _ {
namespace util {
template<typename T>
class HasSize {
private:
template<typename U>
static auto check(U* p) -> decltype(p->size(), int());
template<typename>
static void check(...);
public:
static const bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
};
template<typename T>
class HasPushBack {
private:
template<typename U>
static auto check(U* p) -> decltype(p->push_back(std::declval<typename U::value_type>()), int());
template<typename>
static void check(...);
public:
static const bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
};
template<typename T>
class HasInsert {
private:
template<typename U>
static auto check(U* p) -> decltype(p->insert(std::declval<typename U::value_type>()), int());
template<typename>
static void check(...);
public:
static const bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
};
template<typename T>
class HasInsertAfter {
private:
template<typename U>
static auto check(U* p) -> decltype(p->insert_after(p->begin(), std::declval<typename U::value_type>()), int()); // begin is just for checking
template<typename>
static void check(...);
public:
static const bool value = !std::is_void<decltype(check<T>(nullptr))>::value;
};
template<typename T, typename U>
typename std::enable_if<HasPushBack<T>::value, void>::type
add(T& c, U&& v) {
c.push_back(std::forward<U>(v));
}
template<typename T, typename U>
typename std::enable_if<HasInsert<T>::value, void>::type
add(T& c, U&& v) {
c.insert(std::forward<U>(v));
}
template<typename T, typename U>
typename std::enable_if<HasInsertAfter<T>::value, void>::type
add(T& c, U&& v) {
// get to the end of the list, which is O(N) and not fast at all
auto before_end = c.before_begin();
for (auto& _ : c) {
++before_end;
}
c.insert_after(before_end, std::forward<U>(v));
}
} // namespace util
template<typename Collection, typename Function>
void
each(Collection& obj, Function iterator) {
std::for_each(std::begin(obj), std::end(obj), iterator);
}
template<template<typename ...T>
class RetCollection = std::vector,
typename Collection,
typename Function>
auto
map(const Collection& obj, Function iterator)
-> RetCollection<typename std::decay<decltype(iterator(*std::begin(obj)))>::type> {
using R = typename std::decay<decltype(iterator(*std::begin(obj)))>::type;
RetCollection<R> result;
for (auto& i : obj) {
util::add(result, iterator(i));
}
return result;
}
template<typename Collection, typename Function, typename Memo>
Memo
reduce(const Collection& obj, Function iterator, Memo memo) {
return std::accumulate(std::begin(obj), std::end(obj), memo, iterator);
}
template<typename Collection, typename Function, typename Memo>
Memo
reduceRight(const Collection& obj, Function iterator, Memo memo) {
for (typename Collection::const_reverse_iterator it = obj.rbegin(); it != obj.rend(); ++it) {
memo = iterator(memo, *it);
}
return memo;
}
template<typename T, size_t N, typename Function, typename Memo>
Memo
reduceRight(T (&obj)[N], Function iterator, Memo memo) {
for (int i = N-1; i >= 0; --i) {
memo = iterator(memo, obj[i]);
}
return memo;
}
template<typename Collection, typename Function>
auto
find(Collection& obj, Function iterator)
-> decltype(std::begin(obj)) {
return std::find_if(std::begin(obj), std::end(obj), iterator);
}
template<typename Collection, typename Function>
Collection
filter(const Collection& obj, Function iterator) {
Collection result;
for (auto& i : obj) {
if (iterator(i)) {
util::add(result, i);
}
}
return result;
}
template<typename Collection, typename Function>
Collection
reject(const Collection& obj, Function iterator) {
Collection result;
for (auto& i : obj) {
if (!iterator(i)) {
util::add(result, i);
}
}
return result;
}
template<typename Collection, typename Function>
bool
every(const Collection& obj, Function iterator) {
return std::all_of(std::begin(obj), std::end(obj), iterator);
}
template<typename Collection, typename Function>
bool
some(const Collection& obj, Function iterator) {
return std::any_of(std::begin(obj), std::end(obj), iterator);
}
template<typename Collection, typename U>
bool
contains(const Collection& obj, const U& value) {
return std::find(std::begin(obj), std::end(obj), value) != std::end(obj);
}
template<typename Collection, typename Function, typename... Argument>
auto
invoke(Collection& obj, Function method, Argument&&... args)
-> typename std::enable_if<std::is_void<decltype(((*std::begin(obj)).*method)(args...))>::value, void>::type {
for (auto& i : obj) {
(i.*method)(std::forward<Argument>(args)...);
}
}
template<template<typename ...T>
class RetCollection = std::vector,
typename Collection,
typename Function,
typename... Argument>
auto
invoke(Collection& obj, Function method, Argument&&... args)
-> typename std::enable_if<!std::is_void<decltype(((*std::begin(obj)).*method)(args...))>::value,
RetCollection<typename std::decay<decltype(((*std::begin(obj)).*method)(args...))>::type>>::type {
using R = typename std::decay<decltype(((*std::begin(obj)).*method)(args...))>::type;
RetCollection<R> result;
for (auto& i : obj) {
util::add(result, (i.*method)(std::forward<Argument>(args)...));
}
return result;
}
template<template<typename ...T>
class RetCollection = std::vector,
typename Collection,
typename Function>
auto
pluck(const Collection& obj, Function member)
-> RetCollection<typename std::decay<decltype((*std::begin(obj)).*member)>::type> {
using R = typename std::decay<decltype((*std::begin(obj)).*member)>::type;
RetCollection<R> result;
for (auto& i : obj) {
util::add(result, i.*member);
}
return result;
}
template<typename Collection>
auto
max(Collection& obj)
-> decltype(std::begin(obj)) {
return std::max_element(std::begin(obj), std::end(obj));
}
template<typename Collection, typename Function>
auto
max(Collection& obj, Function iterator)
-> decltype(std::begin(obj)) {
using R = typename std::decay<decltype(*std::begin(obj))>::type;
return std::max_element(std::begin(obj), std::end(obj), [&](const R& a, const R& b) {
return iterator(a) < iterator(b);
});
}
template<typename Collection>
auto
min(Collection& obj)
-> decltype(std::begin(obj)) {
return std::min_element(std::begin(obj), std::end(obj));
}
template<typename Collection, typename Function>
auto
min(Collection& obj, Function iterator)
-> decltype(std::begin(obj)) {
using R = typename std::decay<decltype(*std::begin(obj))>::type;
return std::min_element(std::begin(obj), std::end(obj), [&](const R& a, const R& b) {
return iterator(a) < iterator(b);
});
}
template<typename Collection, typename Function>
Collection
sortBy(const Collection& obj, Function iterator) {
Collection result = obj;
using R = typename Collection::value_type;
std::sort(std::begin(result), std::end(result), [&](const R& a, const R& b) {
return iterator(a) < iterator(b);
});
return result;
}
template<typename Collection, typename Function>
auto
groupBy(const Collection& obj, Function iterator)
-> std::multimap<typename std::decay<decltype(iterator(*std::begin(obj)))>::type,
typename std::decay<decltype(*std::begin(obj))>::type> {
using R = std::multimap<typename std::decay<decltype(iterator(*std::begin(obj)))>::type,
typename std::decay<decltype(*std::begin(obj))>::type>;
R result;
for (auto& i : obj) {
result.insert(typename R::value_type(iterator(i), i));
}
return result;
}
template<typename Collection, typename Function>
auto
countBy(const Collection& obj, Function iterator)
-> std::map<typename std::decay<decltype(iterator(*std::begin(obj)))>::type, std::size_t> {
using R = std::map<typename std::decay<decltype(iterator(*std::begin(obj)))>::type, std::size_t>;
R result;
for (auto& i : obj) {
auto&& v = iterator(i);
typename R::iterator it = result.find(v);
if (it == result.end()) {
result.insert(typename R::value_type(std::forward<decltype(v)>(v), 1));
} else {
++it->second;
}
}
return result;
}
template<typename Collection>
Collection
shuffle(const Collection& obj) {
Collection result = obj;
std::random_device rd;
std::shuffle(std::begin(result), std::end(result), std::mt19937(rd()));
return result;
}
template<template<typename ...T>
class RetCollection = std::vector,
typename Collection>
auto
toArray(const Collection& obj)
-> RetCollection<typename std::decay<decltype(*std::begin(obj))>::type> {
return RetCollection<typename std::decay<decltype(*std::begin(obj))>::type>(std::begin(obj), std::end(obj));
}
template<typename Collection>
typename std::enable_if<util::HasSize<Collection>::value, size_t>::type
size(const Collection& obj) {
return obj.size();
}
template<typename Collection>
typename std::enable_if<!util::HasSize<Collection>::value, size_t>::type
size(const Collection& obj) {
size_t s = 0;
for (auto& _ : obj) {
++s;
}
return s;
}
} // namespace _
#endif // UNDERSCORE_H