File: operations.hpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (650 lines) | stat: -rw-r--r-- 21,680 bytes parent folder | download | duplicates (6)
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_V3_ITERATOR_OPERATIONS_HPP
#define RANGES_V3_ITERATOR_OPERATIONS_HPP

#include <type_traits>
#include <utility>

#include <range/v3/range_fwd.hpp>

#include <range/v3/iterator/concepts.hpp>
#include <range/v3/iterator/traits.hpp>
#include <range/v3/range/concepts.hpp>

#include <range/v3/detail/prologue.hpp>

namespace ranges
{
    /// \addtogroup group-iterator
    /// @{

    /// \cond
    template<typename I>
        // requires input_or_output_iterator<I>
    struct counted_iterator;
    /// \endcond

    struct advance_fn
    {
#if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
        template(typename I)(
            requires input_or_output_iterator<I>)
        constexpr void operator()(I & i, iter_difference_t<I> n) const
        // [[expects: n >= 0 || bidirectional_iterator<I>]]
        {
            if constexpr(random_access_iterator<I>)
            {
                i += n;
            }
            else
            {
                if constexpr(bidirectional_iterator<I>)
                    for(; 0 > n; ++n)
                        --i;
                RANGES_EXPECT(0 <= n);
                for(; 0 < n; --n)
                    ++i;
            }
        }

        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr void operator()(I & i, S bound) const
        // [[expects axiom: reachable(i, bound)]]
        {
            if constexpr(assignable_from<I &, S>)
            {
                i = std::move(bound);
            }
            else if constexpr(sized_sentinel_for<S, I>)
            {
                iter_difference_t<I> d = bound - i;
                RANGES_EXPECT(0 <= d);
                (*this)(i, d);
            }
            else
                while(i != bound)
                    ++i;
        }

        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr iter_difference_t<I> //
        operator()(I & i, iter_difference_t<I> n, S bound) const
        // [[expects axiom: 0 == n ||
        //     (0 < n && reachable(i, bound)) ||
        //     (0 > n && same_as<I, S> && bidirectional_iterator<I> && reachable(bound,
        //     i))]]
        {
            if constexpr(sized_sentinel_for<S, I>)
            {
                if(0 == n)
                    return 0;
                const auto d = bound - i;
                if constexpr(bidirectional_iterator<I> && same_as<I, S>)
                {
                    RANGES_EXPECT(0 <= n ? 0 <= d : 0 >= d);
                    if(0 <= n ? d <= n : d >= n)
                    {
                        i = std::move(bound);
                        return n - d;
                    }
                }
                else
                {
                    RANGES_EXPECT(0 <= n && 0 <= d);
                    if(d <= n)
                    {
                        (*this)(i, std::move(bound));
                        return n - d;
                    }
                }
                (*this)(i, n);
                return 0;
            }
            else
            {
                if constexpr(bidirectional_iterator<I> && same_as<I, S>)
                {
                    if(0 > n)
                    {
                        do
                        {
                            --i;
                            ++n;
                        } while(0 != n && i != bound);
                        return n;
                    }
                }
                RANGES_EXPECT(0 <= n);
                while(0 != n && i != bound)
                {
                    ++i;
                    --n;
                }
                return n;
            }
        }
#else
    private:
        template<typename I>
        static constexpr void n_(I & i, iter_difference_t<I> n, std::input_iterator_tag);
        template<typename I>
        static constexpr void n_(I & i, iter_difference_t<I> n,
                                 std::bidirectional_iterator_tag);
        template<typename I>
        static constexpr void n_(I & i, iter_difference_t<I> n,
                                 std::random_access_iterator_tag);
        template<typename I, typename S>
        static constexpr void to_impl_(I & i, S s, sentinel_tag);
        template<typename I, typename S>
        static constexpr void to_impl_(I & i, S s, sized_sentinel_tag);
        template<typename I, typename S>
        static constexpr void to_(I & i, S s, std::true_type); // assignable
        template<typename I, typename S>
        static constexpr void to_(I & i, S s, std::false_type); // !assignable
        template<typename I, typename S>
        static constexpr iter_difference_t<I> bounded_(I & it, iter_difference_t<I> n,
                                                       S bound, sentinel_tag,
                                                       std::input_iterator_tag);
        template<typename I>
        static constexpr iter_difference_t<I> bounded_(I & it, iter_difference_t<I> n,
                                                       I bound, sentinel_tag,
                                                       std::bidirectional_iterator_tag);
        template<typename I, typename S, typename Concept>
        static constexpr iter_difference_t<I> bounded_(I & it, iter_difference_t<I> n,
                                                       S bound, sized_sentinel_tag,
                                                       Concept);

    public:
        // Advance a certain number of steps:
        template(typename I)(
            requires input_or_output_iterator<I>)
        constexpr void operator()(I & i, iter_difference_t<I> n) const
        {
            advance_fn::n_(i, n, iterator_tag_of<I>{});
        }
        // Advance to a certain position:
        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr void operator()(I & i, S s) const
        {
            advance_fn::to_(
                i, static_cast<S &&>(s), meta::bool_<assignable_from<I &, S>>());
        }
        // Advance a certain number of times, with a bound:
        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr iter_difference_t<I> //
        operator()(I & it, iter_difference_t<I> n, S bound) const
        {
            return advance_fn::bounded_(it,
                                        n,
                                        static_cast<S &&>(bound),
                                        sentinel_tag_of<S, I>(),
                                        iterator_tag_of<I>());
        }
#endif

        template(typename I)(
            requires input_or_output_iterator<I>)
        constexpr void operator()(counted_iterator<I> & i, iter_difference_t<I> n) const;
    };

    /// \sa `advance_fn`
    RANGES_INLINE_VARIABLE(advance_fn, advance)

#if RANGES_CXX_IF_CONSTEXPR < RANGES_CXX_IF_CONSTEXPR_17
    template<typename I>
    constexpr void advance_fn::n_(I & i, iter_difference_t<I> n, std::input_iterator_tag)
    {
        RANGES_EXPECT(n >= 0);
        for(; n > 0; --n)
            ++i;
    }
    template<typename I>
    constexpr void advance_fn::n_(I & i, iter_difference_t<I> n,
                                  std::bidirectional_iterator_tag)
    {
        if(n > 0)
            for(; n > 0; --n)
                ++i;
        else
            for(; n < 0; ++n)
                --i;
    }
    template<typename I>
    constexpr void advance_fn::n_(I & i, iter_difference_t<I> n,
                                  std::random_access_iterator_tag)
    {
        i += n;
    }
    template<typename I, typename S>
    constexpr void advance_fn::to_impl_(I & i, S s, sentinel_tag)
    {
        while(i != s)
            ++i;
    }
    template<typename I, typename S>
    constexpr void advance_fn::to_impl_(I & i, S s, sized_sentinel_tag)
    {
        iter_difference_t<I> d = s - i;
        RANGES_EXPECT(0 <= d);
        advance(i, d);
    }
    // Advance to a certain position:
    template<typename I, typename S>
    constexpr void advance_fn::to_(I & i, S s, std::true_type)
    {
        i = static_cast<S &&>(s);
    }
    template<typename I, typename S>
    constexpr void advance_fn::to_(I & i, S s, std::false_type)
    {
        advance_fn::to_impl_(i, static_cast<S &&>(s), sentinel_tag_of<S, I>());
    }
    template<typename I, typename S>
    constexpr iter_difference_t<I> advance_fn::bounded_(I & it, iter_difference_t<I> n,
                                                        S bound, sentinel_tag,
                                                        std::input_iterator_tag)
    {
        RANGES_EXPECT(0 <= n);
        for(; 0 != n && it != bound; --n)
            ++it;
        return n;
    }
    template<typename I>
    constexpr iter_difference_t<I> advance_fn::bounded_(I & it, iter_difference_t<I> n,
                                                        I bound, sentinel_tag,
                                                        std::bidirectional_iterator_tag)
    {
        if(0 <= n)
            for(; 0 != n && it != bound; --n)
                ++it;
        else
            for(; 0 != n && it != bound; ++n)
                --it;
        return n;
    }
    template<typename I, typename S, typename Concept>
    constexpr iter_difference_t<I> advance_fn::bounded_(I & it, iter_difference_t<I> n,
                                                        S bound, sized_sentinel_tag,
                                                        Concept)
    {
        RANGES_EXPECT(((bool)same_as<I, S> || 0 <= n));
        if(n == 0)
            return 0;
        iter_difference_t<I> d = bound - it;
        RANGES_EXPECT(0 <= n ? 0 <= d : 0 >= d);
        if(0 <= n ? n >= d : n <= d)
        {
            advance(it, static_cast<S &&>(bound));
            return n - d;
        }
        advance(it, n);
        return 0;
    }
#endif

    struct next_fn
    {
        template(typename I)(
            requires input_or_output_iterator<I>)
        constexpr I operator()(I it) const
        {
            return ++it;
        }
        template(typename I)(
            requires input_or_output_iterator<I>)
        constexpr I operator()(I it, iter_difference_t<I> n) const
        {
            advance(it, n);
            return it;
        }
        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr I operator()(I it, S s) const
        {
            advance(it, static_cast<S &&>(s));
            return it;
        }
        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr I operator()(I it, iter_difference_t<I> n, S bound) const
        {
            advance(it, n, static_cast<S &&>(bound));
            return it;
        }
    };

    /// \sa `next_fn`
    RANGES_INLINE_VARIABLE(next_fn, next)

    struct prev_fn
    {
        template(typename I)(
            requires bidirectional_iterator<I>)
        constexpr I operator()(I it) const
        {
            return --it;
        }
        template(typename I)(
            requires bidirectional_iterator<I>)
        constexpr I operator()(I it, iter_difference_t<I> n) const
        {
            advance(it, -n);
            return it;
        }
        template(typename I)(
            requires bidirectional_iterator<I>)
        constexpr I operator()(I it, iter_difference_t<I> n, I bound) const
        {
            advance(it, -n, static_cast<I &&>(bound));
            return it;
        }
    };

    /// \sa `prev_fn`
    RANGES_INLINE_VARIABLE(prev_fn, prev)

    struct iter_enumerate_fn
    {
    private:
        template(typename I, typename S)(
            requires (!sized_sentinel_for<I, I>)) //
        static constexpr std::pair<iter_difference_t<I>, I> //
        impl_i(I first, S last, sentinel_tag)
        {
            iter_difference_t<I> d = 0;
            for(; first != last; ++first)
                ++d;
            return {d, first};
        }
        template(typename I, typename S)(
            requires sized_sentinel_for<I, I>)
        static constexpr std::pair<iter_difference_t<I>, I> //
        impl_i(I first, S end_, sentinel_tag)
        {
            I last = ranges::next(first, end_);
            auto n = static_cast<iter_difference_t<I>>(last - first);
            RANGES_EXPECT(((bool)same_as<I, S> || 0 <= n));
            return {n, last};
        }
        template<typename I, typename S>
        static constexpr std::pair<iter_difference_t<I>, I> //
        impl_i(I first, S last, sized_sentinel_tag)
        {
            auto n = static_cast<iter_difference_t<I>>(last - first);
            RANGES_EXPECT(((bool)same_as<I, S> || 0 <= n));
            return {n, ranges::next(first, last)};
        }

    public:
        template(typename I, typename S)(
            requires sentinel_for<S, I>)
        constexpr std::pair<iter_difference_t<I>, I> operator()(I first, S last) const
        {
            return iter_enumerate_fn::impl_i(static_cast<I &&>(first),
                                             static_cast<S &&>(last),
                                             sentinel_tag_of<S, I>());
        }
    };

    /// \sa `iter_enumerate_fn`
    RANGES_INLINE_VARIABLE(iter_enumerate_fn, iter_enumerate)

    struct iter_distance_fn
    {
    private:
        template<typename I, typename S>
        static constexpr iter_difference_t<I> impl_i(I first, S last, sentinel_tag)
        {
            return iter_enumerate(static_cast<I &&>(first), static_cast<S &&>(last))
                .first;
        }
        template<typename I, typename S>
        static constexpr iter_difference_t<I> impl_i(I first, S last, sized_sentinel_tag)
        {
            auto n = static_cast<iter_difference_t<I>>(last - first);
            RANGES_EXPECT(((bool)same_as<I, S> || 0 <= n));
            return n;
        }

    public:
        template(typename I, typename S)(
            requires input_or_output_iterator<I> AND sentinel_for<S, I>)
        constexpr iter_difference_t<I> operator()(I first, S last) const
        {
            return iter_distance_fn::impl_i(static_cast<I &&>(first),
                                            static_cast<S &&>(last),
                                            sentinel_tag_of<S, I>());
        }
    };

    /// \sa `iter_distance_fn`
    RANGES_INLINE_VARIABLE(iter_distance_fn, iter_distance)

    struct iter_distance_compare_fn
    {
    private:
        template<typename I, typename S>
        static constexpr int impl_i(I first, S last, iter_difference_t<I> n, sentinel_tag)
        {
            if(n < 0)
                return 1;
            for(; n > 0; --n, ++first)
            {
                if(first == last)
                    return -1;
            }
            return first == last ? 0 : 1;
        }
        template<typename I, typename S>
        static constexpr int impl_i(I first, S last, iter_difference_t<I> n,
                                    sized_sentinel_tag)
        {
            iter_difference_t<I> dist = last - first;
            if(n < dist)
                return 1;
            if(dist < n)
                return -1;
            return 0;
        }

    public:
        template(typename I, typename S)(
            requires input_iterator<I> AND sentinel_for<S, I>)
        constexpr int operator()(I first, S last, iter_difference_t<I> n) const
        {
            return iter_distance_compare_fn::impl_i(static_cast<I &&>(first),
                                                    static_cast<S &&>(last),
                                                    n,
                                                    sentinel_tag_of<S, I>());
        }
    };

    /// \sa `iter_distance_compare_fn`
    RANGES_INLINE_VARIABLE(iter_distance_compare_fn, iter_distance_compare)

    // Like distance(b,e), but guaranteed to be O(1)
    struct iter_size_fn
    {
        template(typename I, typename S)(
            requires sized_sentinel_for<S, I>)
        constexpr meta::_t<std::make_unsigned<iter_difference_t<I>>> //
        operator()(I const & first, S last) const
        {
            using size_type = meta::_t<std::make_unsigned<iter_difference_t<I>>>;
            iter_difference_t<I> n = last - first;
            RANGES_EXPECT(0 <= n);
            return static_cast<size_type>(n);
        }
    };

    /// \sa `iter_size_fn`
    RANGES_INLINE_VARIABLE(iter_size_fn, iter_size)

    /// \cond
    namespace adl_uncounted_recounted_detail
    {
        template<typename I>
        constexpr I uncounted(I i)
        {
            return i;
        }

        template<typename I>
        constexpr I recounted(I const &, I i, iter_difference_t<I>)
        {
            return i;
        }

        struct uncounted_fn
        {
            template<typename I>
            constexpr auto operator()(I i) const -> decltype(uncounted((I &&) i))
            {
                return uncounted((I &&) i);
            }
        };

        struct recounted_fn
        {
            template<typename I, typename J>
            constexpr auto operator()(I i, J j, iter_difference_t<J> n) const
                -> decltype(recounted((I &&) i, (J &&) j, n))
            {
                return recounted((I &&) i, (J &&) j, n);
            }
        };
    } // namespace adl_uncounted_recounted_detail
    /// \endcond

    RANGES_INLINE_VARIABLE(adl_uncounted_recounted_detail::uncounted_fn, uncounted)
    RANGES_INLINE_VARIABLE(adl_uncounted_recounted_detail::recounted_fn, recounted)

    struct enumerate_fn : iter_enumerate_fn
    {
    private:
        template<typename Rng>
        static constexpr std::pair<range_difference_t<Rng>, iterator_t<Rng>> impl_r(
            Rng & rng, range_tag, range_tag)
        {
            return iter_enumerate(begin(rng), end(rng));
        }
        template<typename Rng>
        static constexpr std::pair<range_difference_t<Rng>, iterator_t<Rng>> impl_r(
            Rng & rng, common_range_tag, sized_range_tag)
        {
            return {static_cast<range_difference_t<Rng>>(size(rng)), end(rng)};
        }

    public:
        using iter_enumerate_fn::operator();

        template(typename Rng)(
            requires range<Rng>)
        constexpr std::pair<range_difference_t<Rng>, iterator_t<Rng>> operator()(Rng && rng) const
        {
            // Better not be trying to compute the distance of an infinite range:
            RANGES_EXPECT(!is_infinite<Rng>::value);
            return enumerate_fn::impl_r(
                rng, common_range_tag_of<Rng>(), sized_range_tag_of<Rng>());
        }
    };

    /// \sa `enumerate_fn`
    RANGES_INLINE_VARIABLE(enumerate_fn, enumerate)

    struct distance_fn : iter_distance_fn
    {
    private:
        template<typename Rng>
        static range_difference_t<Rng> impl_r(Rng & rng, range_tag)
        {
            return enumerate(rng).first;
        }
        template<typename Rng>
        static constexpr range_difference_t<Rng> impl_r(Rng & rng, sized_range_tag)
        {
            return static_cast<range_difference_t<Rng>>(size(rng));
        }

    public:
        using iter_distance_fn::operator();

        template(typename Rng)(
            requires range<Rng>)
        constexpr range_difference_t<Rng> operator()(Rng && rng) const
        {
            // Better not be trying to compute the distance of an infinite range:
            RANGES_EXPECT(!is_infinite<Rng>::value);
            return distance_fn::impl_r(rng, sized_range_tag_of<Rng>());
        }
    };

    /// \sa `distance_fn`
    RANGES_INLINE_VARIABLE(distance_fn, distance)

    // The interface of distance_compare is taken from Util.listLengthCmp in the GHC API.
    struct distance_compare_fn : iter_distance_compare_fn
    {
    private:
        template<typename Rng>
        static constexpr int impl_r(Rng & rng, range_difference_t<Rng> n, range_tag)
        {
            // Infinite ranges are always compared to be larger than a finite number.
            return is_infinite<Rng>::value
                       ? 1
                       : iter_distance_compare(begin(rng), end(rng), n);
        }
        template<typename Rng>
        static constexpr int impl_r(Rng & rng, range_difference_t<Rng> n, sized_range_tag)
        {
            auto dist = distance(rng); // O(1) since rng is a sized_range
            if(dist > n)
                return 1;
            else if(dist < n)
                return -1;
            else
                return 0;
        }

    public:
        using iter_distance_compare_fn::operator();

        template(typename Rng)(
            requires range<Rng>)
        constexpr int operator()(Rng && rng, range_difference_t<Rng> n) const
        {
            return distance_compare_fn::impl_r(rng, n, sized_range_tag_of<Rng>());
        }
    };

    /// \sa `distance_compare_fn`
    RANGES_INLINE_VARIABLE(distance_compare_fn, distance_compare)

    namespace cpp20
    {
        using ranges::advance;
        using ranges::distance;
        using ranges::next;
        using ranges::prev;
    } // namespace cpp20
    /// @}
} // namespace ranges

#include <range/v3/detail/epilogue.hpp>

#endif // RANGES_V3_ITERATOR_OPERATIONS_HPP