aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/polyfill_ranges.h
blob: 512dbcbcb787c40a6cfc2eba188a62df2286d56e (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

//
// TODO: remove this file when ranges are supported by all compilation targets
//

#pragma once

#include <algorithm>
#include <utility>
#include <version>

#ifndef __cpp_lib_ranges

namespace std {
namespace ranges {

template <typename T>
concept range = requires(T& t) {
                    begin(t);
                    end(t);
                };

template <typename T>
concept input_range = range<T>;

template <typename T>
concept output_range = range<T>;

template <range R>
using range_difference_t = ptrdiff_t;

//
// find, find_if, find_if_not
//

struct find_fn {
    template <typename Iterator, typename T, typename Proj = std::identity>
    constexpr Iterator operator()(Iterator first, Iterator last, const T& value,
                                  Proj proj = {}) const {
        for (; first != last; ++first) {
            if (std::invoke(proj, *first) == value) {
                return first;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename T, typename Proj = std::identity>
    constexpr ranges::iterator_t<R> operator()(R&& r, const T& value, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), value, std::ref(proj));
    }
};

struct find_if_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr Iterator operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        for (; first != last; ++first) {
            if (std::invoke(pred, std::invoke(proj, *first))) {
                return first;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr ranges::iterator_t<R> operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

struct find_if_not_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr Iterator operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        for (; first != last; ++first) {
            if (!std::invoke(pred, std::invoke(proj, *first))) {
                return first;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr ranges::iterator_t<R> operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

inline constexpr find_fn find;
inline constexpr find_if_fn find_if;
inline constexpr find_if_not_fn find_if_not;

//
// any_of, all_of, none_of
//

struct all_of_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        return ranges::find_if_not(first, last, std::ref(pred), std::ref(proj)) == last;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

struct any_of_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) != last;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

struct none_of_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        return ranges::find_if(first, last, std::ref(pred), std::ref(proj)) == last;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

inline constexpr any_of_fn any_of;
inline constexpr all_of_fn all_of;
inline constexpr none_of_fn none_of;

//
// count, count_if
//

struct count_fn {
    template <typename Iterator, typename T, typename Proj = std::identity>
    constexpr ptrdiff_t operator()(Iterator first, Iterator last, const T& value,
                                   Proj proj = {}) const {
        ptrdiff_t counter = 0;
        for (; first != last; ++first)
            if (std::invoke(proj, *first) == value)
                ++counter;
        return counter;
    }

    template <ranges::input_range R, typename T, typename Proj = std::identity>
    constexpr ptrdiff_t operator()(R&& r, const T& value, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), value, std::ref(proj));
    }
};

struct count_if_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred>
    constexpr ptrdiff_t operator()(Iterator first, Iterator last, Pred pred, Proj proj = {}) const {
        ptrdiff_t counter = 0;
        for (; first != last; ++first)
            if (std::invoke(pred, std::invoke(proj, *first)))
                ++counter;
        return counter;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Pred>
    constexpr ptrdiff_t operator()(R&& r, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

inline constexpr count_fn count;
inline constexpr count_if_fn count_if;

//
// transform
//

struct transform_fn {
    template <typename InputIterator, typename OutputIterator, typename F,
              typename Proj = std::identity>
    constexpr void operator()(InputIterator first1, InputIterator last1, OutputIterator result,
                              F op, Proj proj = {}) const {
        for (; first1 != last1; ++first1, (void)++result) {
            *result = std::invoke(op, std::invoke(proj, *first1));
        }
    }

    template <ranges::input_range R, typename OutputIterator, typename F,
              typename Proj = std::identity>
    constexpr void operator()(R&& r, OutputIterator result, F op, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), result, std::ref(op), std::ref(proj));
    }
};

inline constexpr transform_fn transform;

//
// sort
//

struct sort_fn {
    template <typename Iterator, typename Comp = ranges::less, typename Proj = std::identity>
    constexpr void operator()(Iterator first, Iterator last, Comp comp = {}, Proj proj = {}) const {
        if (first == last)
            return;

        Iterator last_iter = ranges::next(first, last);
        std::sort(first, last_iter,
                  [&](auto& lhs, auto& rhs) { return comp(proj(lhs), proj(rhs)); });
    }

    template <ranges::input_range R, typename Comp = ranges::less, typename Proj = std::identity>
    constexpr void operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(comp), std::move(proj));
    }
};

inline constexpr sort_fn sort;

//
// fill
//

struct fill_fn {
    template <typename T, typename OutputIterator>
    constexpr OutputIterator operator()(OutputIterator first, OutputIterator last,
                                        const T& value) const {
        while (first != last) {
            *first++ = value;
        }

        return first;
    }

    template <typename T, ranges::output_range R>
    constexpr ranges::iterator_t<R> operator()(R&& r, const T& value) const {
        return operator()(ranges::begin(r), ranges::end(r), value);
    }
};

inline constexpr fill_fn fill;

//
// for_each
//

struct for_each_fn {
    template <typename Iterator, typename Proj = std::identity, typename Fun>
    constexpr void operator()(Iterator first, Iterator last, Fun f, Proj proj = {}) const {
        for (; first != last; ++first) {
            std::invoke(f, std::invoke(proj, *first));
        }
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Fun>
    constexpr void operator()(R&& r, Fun f, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(f), std::ref(proj));
    }
};

inline constexpr for_each_fn for_each;

//
// min_element, max_element
//

struct min_element_fn {
    template <typename Iterator, typename Proj = std::identity, typename Comp = ranges::less>
    constexpr Iterator operator()(Iterator first, Iterator last, Comp comp = {},
                                  Proj proj = {}) const {
        if (first == last) {
            return last;
        }

        auto smallest = first;
        ++first;
        for (; first != last; ++first) {
            if (!std::invoke(comp, std::invoke(proj, *smallest), std::invoke(proj, *first))) {
                smallest = first;
            }
        }
        return smallest;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Comp = ranges::less>
    constexpr ranges::iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
    }
};

struct max_element_fn {
    template <typename Iterator, typename Proj = std::identity, typename Comp = ranges::less>
    constexpr Iterator operator()(Iterator first, Iterator last, Comp comp = {},
                                  Proj proj = {}) const {
        if (first == last) {
            return last;
        }

        auto largest = first;
        ++first;
        for (; first != last; ++first) {
            if (std::invoke(comp, std::invoke(proj, *largest), std::invoke(proj, *first))) {
                largest = first;
            }
        }
        return largest;
    }

    template <ranges::input_range R, typename Proj = std::identity, typename Comp = ranges::less>
    constexpr ranges::iterator_t<R> operator()(R&& r, Comp comp = {}, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(comp), std::ref(proj));
    }
};

inline constexpr min_element_fn min_element;
inline constexpr max_element_fn max_element;

//
// replace, replace_if
//

struct replace_fn {
    template <typename Iterator, typename T1, typename T2, typename Proj = std::identity>
    constexpr Iterator operator()(Iterator first, Iterator last, const T1& old_value,
                                  const T2& new_value, Proj proj = {}) const {
        for (; first != last; ++first) {
            if (old_value == std::invoke(proj, *first)) {
                *first = new_value;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename T1, typename T2, typename Proj = std::identity>
    constexpr ranges::iterator_t<R> operator()(R&& r, const T1& old_value, const T2& new_value,
                                               Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), old_value, new_value, std::move(proj));
    }
};

struct replace_if_fn {
    template <typename Iterator, typename T, typename Proj = std::identity, typename Pred>
    constexpr Iterator operator()(Iterator first, Iterator last, Pred pred, const T& new_value,
                                  Proj proj = {}) const {
        for (; first != last; ++first) {
            if (!!std::invoke(pred, std::invoke(proj, *first))) {
                *first = new_value;
            }
        }
        return std::move(first);
    }

    template <ranges::input_range R, typename T, typename Proj = std::identity, typename Pred>
    constexpr ranges::iterator_t<R> operator()(R&& r, Pred pred, const T& new_value,
                                               Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(pred), new_value,
                          std::move(proj));
    }
};

inline constexpr replace_fn replace;
inline constexpr replace_if_fn replace_if;

//
// copy, copy_if
//

struct copy_fn {
    template <typename InputIterator, typename OutputIterator>
    constexpr void operator()(InputIterator first, InputIterator last,
                              OutputIterator result) const {
        for (; first != last; ++first, (void)++result) {
            *result = *first;
        }
    }

    template <ranges::input_range R, typename OutputIterator>
    constexpr void operator()(R&& r, OutputIterator result) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(result));
    }
};

struct copy_if_fn {
    template <typename InputIterator, typename OutputIterator, typename Proj = std::identity,
              typename Pred>
    constexpr void operator()(InputIterator first, InputIterator last, OutputIterator result,
                              Pred pred, Proj proj = {}) const {
        for (; first != last; ++first) {
            if (std::invoke(pred, std::invoke(proj, *first))) {
                *result = *first;
                ++result;
            }
        }
    }

    template <ranges::input_range R, typename OutputIterator, typename Proj = std::identity,
              typename Pred>
    constexpr void operator()(R&& r, OutputIterator result, Pred pred, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(result), std::ref(pred),
                          std::ref(proj));
    }
};

inline constexpr copy_fn copy;
inline constexpr copy_if_fn copy_if;

//
// generate
//

struct generate_fn {
    template <typename Iterator, typename F>
    constexpr Iterator operator()(Iterator first, Iterator last, F gen) const {
        for (; first != last; *first = std::invoke(gen), ++first)
            ;
        return first;
    }

    template <typename R, std::copy_constructible F>
        requires std::invocable<F&> && ranges::output_range<R>
    constexpr ranges::iterator_t<R> operator()(R&& r, F gen) const {
        return operator()(ranges::begin(r), ranges::end(r), std::move(gen));
    }
};

inline constexpr generate_fn generate;

//
// lower_bound, upper_bound
//

struct lower_bound_fn {
    template <typename Iterator, typename T, typename Proj = std::identity,
              typename Comp = ranges::less>
    constexpr Iterator operator()(Iterator first, Iterator last, const T& value, Comp comp = {},
                                  Proj proj = {}) const {
        Iterator it;
        std::ptrdiff_t _count, _step;
        _count = std::distance(first, last);

        while (_count > 0) {
            it = first;
            _step = _count / 2;
            ranges::advance(it, _step, last);
            if (comp(std::invoke(proj, *it), value)) {
                first = ++it;
                _count -= _step + 1;
            } else {
                _count = _step;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename T, typename Proj = std::identity,
              typename Comp = ranges::less>
    constexpr ranges::iterator_t<R> operator()(R&& r, const T& value, Comp comp = {},
                                               Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), value, std::ref(comp), std::ref(proj));
    }
};

struct upper_bound_fn {
    template <typename Iterator, typename T, typename Proj = std::identity,
              typename Comp = ranges::less>
    constexpr Iterator operator()(Iterator first, Iterator last, const T& value, Comp comp = {},
                                  Proj proj = {}) const {
        Iterator it;
        std::ptrdiff_t _count, _step;
        _count = std::distance(first, last);

        while (_count > 0) {
            it = first;
            _step = _count / 2;
            ranges::advance(it, _step, last);
            if (!comp(value, std::invoke(proj, *it))) {
                first = ++it;
                _count -= _step + 1;
            } else {
                _count = _step;
            }
        }
        return first;
    }

    template <ranges::input_range R, typename T, typename Proj = std::identity,
              typename Comp = ranges::less>
    constexpr ranges::iterator_t<R> operator()(R&& r, const T& value, Comp comp = {},
                                               Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), value, std::ref(comp), std::ref(proj));
    }
};

inline constexpr lower_bound_fn lower_bound;
inline constexpr upper_bound_fn upper_bound;

//
// adjacent_find
//

struct adjacent_find_fn {
    template <typename Iterator, typename Proj = std::identity, typename Pred = ranges::equal_to>
    constexpr Iterator operator()(Iterator first, Iterator last, Pred pred = {},
                                  Proj proj = {}) const {
        if (first == last)
            return first;
        auto _next = ranges::next(first);
        for (; _next != last; ++_next, ++first)
            if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *_next)))
                return first;
        return _next;
    }

    template <ranges::input_range R, typename Proj = std::identity,
              typename Pred = ranges::equal_to>
    constexpr ranges::iterator_t<R> operator()(R&& r, Pred pred = {}, Proj proj = {}) const {
        return operator()(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
    }
};

inline constexpr adjacent_find_fn adjacent_find;

} // namespace ranges
} // namespace std

#endif