-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkirito.h
403 lines (338 loc) · 9.96 KB
/
kirito.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#ifndef KIRITO_H_
#define KIRITO_H_
#include <cstdint>
#include <vector>
#include <algorithm>
namespace kirito {
using std::random_access_iterator_tag;
using std::enable_if_t;
using std::is_integral;
using std::is_same;
struct EmptySlot {};
struct OpenSlot {};
#ifdef DEBUG
#include <iostream>
using std::ostream;
inline ostream& operator<<(ostream& os, const EmptySlot& e) {
return os << "EmptySlot";
}
inline ostream& operator<<(ostream& os, const OpenSlot& o) {
return os << "OpenSlot";
}
#endif // DEBUG
template <typename Offset, typename Container>
inline auto getStart(Offset o, Container &c) {
if constexpr (is_same<Offset, EmptySlot>::value) {
return c.begin();
} else {
if (o < 0) {
return c.end() + o;
} else {
return c.begin() + o;
}
}
}
template <typename Offset, typename Container>
inline auto getStop(Offset o, Container &c) {
if constexpr (
is_same<Offset, EmptySlot>::value ||
is_same<Offset, OpenSlot>::value
) {
return c.end();
} else {
if (o < 0) {
return c.end() + o;
} else {
return c.begin() + o;
}
}
}
template <typename A, typename B, typename C, bool V = false>
struct Index {
const A a;
const B b;
const C c;
constexpr Index(A a, B b, C c) : a(a), b(b), c(c) {}
};
template <typename A, typename B, bool V>
struct IndexPartial : public Index<A, B, OpenSlot, V> {
constexpr IndexPartial(A a, B b) : Index<A, B, OpenSlot, V>(a, b, {}) {}
template <typename C, typename = enable_if_t<is_integral<C>::value>>
constexpr Index<A, B, C, V> operator()(C c) const {
return {Index<A, B, OpenSlot, V>::a, Index<A, B, OpenSlot, V>::b, c};
}
};
template <typename A, bool V>
struct IndexStart : public Index<A, OpenSlot, OpenSlot, V> {
constexpr IndexStart(A a) : Index<A, OpenSlot, OpenSlot, V>(a, {}, {}) {}
template <typename B, typename = enable_if_t<is_integral<B>::value>>
constexpr IndexPartial<A, B, V> operator()(B b) const {
return {Index<A, OpenSlot, OpenSlot, V>::a, b};
}
constexpr IndexPartial<A, EmptySlot, V> operator()() const {
return {Index<A, OpenSlot, OpenSlot, V>::a, {}};
}
};
template <bool V>
struct IndexBuilder {
constexpr IndexStart<EmptySlot, V> operator()() const { return {{}}; }
template <typename A, typename = enable_if_t<is_integral<A>::value>>
constexpr IndexStart<A, V> operator()(A a) const { return {a}; }
};
constexpr static IndexBuilder<false> I;
constexpr static IndexBuilder<true> V;
template <
typename BaseContainer,
typename T = typename BaseContainer::value_type
>
class FancyContainer : public BaseContainer {
public:
using typename BaseContainer::iterator;
using BaseContainer::begin;
using BaseContainer::end;
using BaseContainer::operator[];
constexpr FancyContainer(const BaseContainer &b) : BaseContainer(b) {}
FancyContainer(BaseContainer &&b) : BaseContainer(std::move(b)) {}
using BaseContainer::BaseContainer;
private:
template <typename Step>
FancyContainer copySlice(iterator start, iterator stop, Step step) {
BaseContainer rv;
if (step > 0) {
for (auto iter = start; iter < stop; iter += step) {
rv.push_back(*iter);
}
} else if (step < 0) {
for (auto iter = start; iter > stop; iter += step) {
rv.push_back(*iter);
}
}
return rv;
}
public:
template <typename A, typename B, typename C>
FancyContainer operator[](Index<A, B, C, false> ndx) {
if constexpr (is_same<C, OpenSlot>::value) {
return copySlice(
getStart(ndx.a, *this),
getStop(ndx.b, *this),
1);
} else {
iterator start;
iterator stop;
if constexpr (is_same<A, EmptySlot>::value) {
if (ndx.c < 0) start = end() - 1;
else start = begin();
} else {
start = getStart(ndx.a, *this);
}
if constexpr (is_same<B, EmptySlot>::value) {
if (ndx.c < 0) stop = begin() - 1;
else stop = end();
} else {
stop = getStop(ndx.b, *this);
}
return copySlice(start, stop, ndx.c);
}
}
template <
typename C,
typename difference_type = typename iterator::difference_type
>
class view_iterator :
public std::iterator<random_access_iterator_tag, T> {
iterator iter;
C step;
public:
view_iterator(const iterator &iter, C step) :
iter(iter), step(step) {}
view_iterator(iterator &&iter, C step) :
iter(std::move(iter)), step(step) {}
view_iterator(const view_iterator &iter, C step) :
iter(iter.iter), step(step * iter.step) {}
view_iterator(view_iterator &&iter, C step) :
iter(std::move(iter.iter)), step(step * iter.step) {}
view_iterator() = default;
view_iterator(const view_iterator &other) = default;
view_iterator(view_iterator &&other) = default;
view_iterator reverse() const {
return {iter, -step};
}
view_iterator& operator=(const view_iterator &rhs) {
iter = rhs.iter;
step = rhs.step;
}
view_iterator& operator+=(difference_type rhs) {
return {iter += (rhs * step), step};
}
view_iterator& operator-=(difference_type rhs) {
return {iter -= (rhs * step), step};
}
T& operator[](difference_type rhs) const {
return iter[rhs * step];
}
view_iterator& operator++() {
iter += step;
return *this;
}
view_iterator& operator++(int) {
view_iterator temp(*this);
iter += step;
return temp;
}
view_iterator& operator--() {
iter -= step;
return *this;
}
view_iterator& operator--(int) {
view_iterator temp(*this);
iter -= step;
return temp;
}
difference_type operator-(const view_iterator& rhs) const {
return (iter - rhs.iter) / step;
}
view_iterator operator+(difference_type rhs) const {
return {iter + (rhs * step), step};
}
view_iterator operator-(difference_type rhs) const {
return {iter - (rhs * step), step};
}
bool operator<(const view_iterator &rhs) const {
if (step < 0) return iter > rhs.iter;
else return iter < rhs.iter;
}
bool operator>(const view_iterator &rhs) const {
if (step < 0) return iter < rhs.iter;
else return iter > rhs.iter;
}
bool operator<=(const view_iterator &rhs) const {
if (step < 0) return iter >= rhs.iter;
else return iter <= rhs.iter;
}
bool operator>=(const view_iterator &rhs) const {
if (step < 0) return iter <= rhs.iter;
else return iter >= rhs.iter;
}
bool operator==(const view_iterator &rhs) const {
return iter == rhs.iter;
}
bool operator!=(const view_iterator &rhs) const {
return iter != rhs.iter;
}
T& operator*() { return *iter; }
const T& operator*() const { return *iter; }
T* operator->() { return iter; }
const T* operator->() const { return iter; }
};
template <typename C>
class View {
view_iterator<C> start;
view_iterator<C> stop;
public:
view_iterator<C> begin() { return start; }
view_iterator<C> end() { return stop; }
view_iterator<C> rbegin() { return (stop).reverse() + 1; }
view_iterator<C> rend() { return (start).reverse() + 1; }
const view_iterator<C> cbegin() const { return start; }
const view_iterator<C> cend() const { return stop; }
const view_iterator<C> crbegin() const {
return (stop).reverse() + 1;
}
const view_iterator<C> crend() const {
return (start).reverse() + 1;
}
T& front() { return start[0]; }
const T& front() const { return start[0]; }
T& back() { return start[size() - 1]; }
const T& back() const { return start[size() - 1]; }
T& operator[](C at) { return start[at]; }
const T &operator[](C at) const { return start[at]; }
T& at(C at) { return start[at]; }
const T at(C at) const { return start[at]; }
size_t size() const { return cend() - cbegin(); }
View(const view_iterator<C> &start, const view_iterator<C> &stop) :
start(start), stop(stop) {}
View(const iterator &start, const iterator &stop, C step) :
start(start, step), stop(stop, step) {}
View(const view_iterator<C> &start, const view_iterator<C> &stop,
C step) :
start(start, step), stop(stop, step) {}
void swap(View &other) {
auto temp = start;
start = other.start;
other.start = temp;
temp = stop;
stop = other.stop;
other.stop = temp;
}
operator BaseContainer() {
BaseContainer rv;
for (auto iter = begin(); iter < end(); iter++) {
rv.push_back(*iter);
}
return rv;
}
operator FancyContainer() {
FancyContainer rv;
for (auto iter = begin(); iter < end(); iter++) {
rv.push_back(*iter);
}
return rv;
}
template <typename A, typename B, typename NDXC,
typename = enable_if_t<is_same<NDXC, OpenSlot>::value>>
View operator[](Index<A, B, NDXC, true> ndx) {
return {getStart(ndx.a, *this), getStop(ndx.b, *this), 1};
}
template <typename A, typename B,
typename = enable_if_t<!is_same<C, OpenSlot>::value>>
View operator[](Index<A, B, C, true> ndx) {
view_iterator<C> start;
view_iterator<C> stop;
if constexpr (is_same<A, EmptySlot>::value) {
if (ndx.c < 0) start = end() - 1;
else start = begin();
} else {
start = getStart(ndx.a, *this);
}
if constexpr (is_same<B, EmptySlot>::value) {
if (ndx.c < 0) stop = begin() - 1;
else stop = end();
} else {
stop = getStop(ndx.b, *this);
}
auto comp = (start - stop) % ndx.c;
if (comp) comp += ndx.c;
return {start, stop + comp, ndx.c};
}
};
template <typename A, typename B, typename C,
typename = enable_if_t<!is_same<C, OpenSlot>::value>>
View<C> operator[](Index<A, B, C, true> ndx) {
iterator start;
iterator stop;
if constexpr (is_same<A, EmptySlot>::value) {
if (ndx.c < 0) start = end() - 1;
else start = begin();
} else {
start = getStart(ndx.a, *this);
}
if constexpr (is_same<B, EmptySlot>::value) {
if (ndx.c < 0) stop = begin() - 1;
else stop = end();
} else {
stop = getStop(ndx.b, *this);
}
auto comp = (start - stop) % ndx.c;
if (comp) comp += ndx.c;
return {start, stop + comp, ndx.c};
}
template <typename A, typename B, typename C,
typename = enable_if_t<is_same<C, OpenSlot>::value>>
View<int> operator[](Index<A, B, C, true> ndx) {
return {getStart(ndx.a, *this), getStop(ndx.b, *this), 1};
}
};
} // namespace kirito
#endif // KIRITO_H_