File: common_iterator.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 (462 lines) | stat: -rw-r--r-- 15,956 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
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//  Copyright Casey Carter 2016
//
//  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_COMMON_ITERATOR_HPP
#define RANGES_V3_ITERATOR_COMMON_ITERATOR_HPP

#include <cstdint>
#include <iterator>
#include <type_traits>

#include <meta/meta.hpp>

#include <concepts/concepts.hpp>

#include <range/v3/range_fwd.hpp>

#include <range/v3/detail/variant.hpp>
#include <range/v3/iterator/basic_iterator.hpp>
#include <range/v3/iterator/concepts.hpp>
#include <range/v3/utility/common_tuple.hpp>

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

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

    /// \cond
    namespace detail
    {
        template<typename I, typename S>
        variant<I, S> & cidata(common_iterator<I, S> & that)
        {
            return that.data_;
        }

        template<typename I, typename S>
        variant<I, S> const & cidata(common_iterator<I, S> const & that)
        {
            return that.data_;
        }
    } // namespace detail

#if RANGES_BROKEN_CPO_LOOKUP
    namespace _common_iterator_
    {
        struct adl_hook
        {};
    } // namespace _common_iterator_
#endif
    /// \endcond

    template<typename I, typename S>
    struct common_iterator
#if RANGES_BROKEN_CPO_LOOKUP
      : private _common_iterator_::adl_hook
#endif
    {
    private:
        CPP_assert(input_or_output_iterator<I>);
        CPP_assert(sentinel_for<S, I>);
        CPP_assert(!same_as<I, S>);
        variant<I, S> data_;

        friend variant<I, S> & detail::cidata<>(common_iterator<I, S> &);
        friend variant<I, S> const & detail::cidata<>(common_iterator<I, S> const &);
        struct emplace_fn
        {
            variant<I, S> * data_;
            template<typename T, std::size_t N>
            void operator()(indexed_element<T, N> t) const
            {
                ranges::emplace<N>(*data_, t.get());
            }
        };
        struct arrow_proxy_
        {
        private:
            friend common_iterator;
            iter_value_t<I> keep_;
            arrow_proxy_(iter_reference_t<I> && x)
              : keep_(std::move(x))
            {}

        public:
            const iter_value_t<I> * operator->() const noexcept
            {
                return std::addressof(keep_);
            }
        };
        template<typename T>
        static T * operator_arrow_(T * p, int) noexcept
        {
            return p;
        }
        template<typename J, typename = detail::iter_arrow_t<J const>>
        static J operator_arrow_(J const & j, int) noexcept(noexcept(J(j)))
        {
            return j;
        }
        template(typename J, typename R = iter_reference_t<J>)(
            requires std::is_reference<R>::value) //
        static meta::_t<std::add_pointer<R>> operator_arrow_(J const & j, long) noexcept
        {
            auto && r = *j;
            return std::addressof(r);
        }
        template(typename J, typename V = iter_value_t<J>)(
            requires constructible_from<V, iter_reference_t<J>>)
        static arrow_proxy_ operator_arrow_(J const & j, ...) noexcept(noexcept(V(V(*j))))
        {
            return arrow_proxy_(*j);
        }

    public:
        using difference_type = iter_difference_t<I>;

        common_iterator() = default;
        common_iterator(I i)
          : data_(emplaced_index<0>, std::move(i))
        {}
        common_iterator(S s)
          : data_(emplaced_index<1>, std::move(s))
        {}
        template(typename I2, typename S2)(
            requires convertible_to<I2, I> AND convertible_to<S2, S>)
        common_iterator(common_iterator<I2, S2> const & that)
          : data_(detail::variant_core_access::make_empty<I, S>())
        {
            detail::cidata(that).visit_i(emplace_fn{&data_});
        }
        template(typename I2, typename S2)(
            requires convertible_to<I2, I> AND convertible_to<S2, S>)
        common_iterator & operator=(common_iterator<I2, S2> const & that)
        {
            detail::cidata(that).visit_i(emplace_fn{&data_});
            return *this;
        }
        iter_reference_t<I> operator*() //
            noexcept(noexcept(iter_reference_t<I>(*std::declval<I &>())))
        {
            return *ranges::get<0>(data_);
        }
        CPP_member
        auto operator*() const //
            noexcept(noexcept(iter_reference_t<I>(*std::declval<I const &>())))
            -> CPP_ret(iter_reference_t<I>)(
                requires indirectly_readable<I const>)
        {
            return *ranges::get<0>(data_);
        }
        template(typename J = I)(
            requires indirectly_readable<J>)
        auto operator->() const //
            noexcept(
                noexcept(common_iterator::operator_arrow_(std::declval<I const &>(), 42)))
            -> decltype(common_iterator::operator_arrow_(std::declval<J const &>(), 42))
        {
            return common_iterator::operator_arrow_(ranges::get<0>(data_), 42);
        }
        common_iterator & operator++()
        {
            ++ranges::get<0>(data_);
            return *this;
        }
#ifdef RANGES_WORKAROUND_MSVC_677925
        template(typename I2 = I)(
            requires (!forward_iterator<I2>)) //
        auto operator++(int) //
            -> decltype(std::declval<I2 &>()++)
        {
            return ranges::get<0>(data_)++;
        }
#else  // ^^^ workaround ^^^ / vvv no workaround vvv
        CPP_member
        auto operator++(int) //
            -> CPP_ret(decltype(std::declval<I &>()++))(
                requires (!forward_iterator<I>))
        {
            return ranges::get<0>(data_)++;
        }
#endif // RANGES_WORKAROUND_MSVC_677925
        CPP_member
        auto operator++(int) //
            -> CPP_ret(common_iterator)(
                requires forward_iterator<I>)
        {
            return common_iterator(ranges::get<0>(data_)++);
        }

#if !RANGES_BROKEN_CPO_LOOKUP
        template<typename I_ = I>
        friend constexpr auto iter_move(common_iterator const & i) //
            noexcept(detail::has_nothrow_iter_move_v<I>)
            -> CPP_broken_friend_ret(iter_rvalue_reference_t<I>)(
                requires input_iterator<I_>)
        {
            return ranges::iter_move(ranges::get<0>(detail::cidata(i)));
        }
        template<typename I2, typename S2>
        friend auto iter_swap(
            common_iterator const & x,
            common_iterator<I2, S2> const &
                y) noexcept(is_nothrow_indirectly_swappable<I, I2>::value)
            -> CPP_broken_friend_ret(void)(
                requires indirectly_swappable<I2, I>)
        {
            return ranges::iter_swap(ranges::get<0>(detail::cidata(x)),
                                     ranges::get<0>(detail::cidata(y)));
        }
#endif
    };

    /// \cond
#if RANGES_BROKEN_CPO_LOOKUP
    namespace _common_iterator_
    {
        template<typename I, typename S>
        constexpr auto iter_move(common_iterator<I, S> const & i) noexcept(
            detail::has_nothrow_iter_move_v<I>)
            -> CPP_broken_friend_ret(iter_rvalue_reference_t<I>)(
                requires input_iterator<I>)
        {
            return ranges::iter_move(ranges::get<0>(detail::cidata(i)));
        }
        template<typename I1, typename S1, typename I2, typename S2>
        auto iter_swap(common_iterator<I1, S1> const & x,
                       common_iterator<I2, S2> const & y) //
            noexcept(is_nothrow_indirectly_swappable<I1, I2>::value)
                -> CPP_broken_friend_ret(void)(
                    requires indirectly_swappable<I1, I2>)
        {
            return ranges::iter_swap(ranges::get<0>(detail::cidata(x)),
                                     ranges::get<0>(detail::cidata(y)));
        }
    } // namespace _common_iterator_
#endif
    /// \endcond

    template(typename I1, typename I2, typename S1, typename S2)(
        requires sentinel_for<S1, I2> AND sentinel_for<S2, I1> AND
        (!equality_comparable_with<I1, I2>)) //
    bool operator==(common_iterator<I1, S1> const & x, common_iterator<I2, S2> const & y)
    {
        return detail::cidata(x).index() == 1u ? (detail::cidata(y).index() == 1u ||
                                                  ranges::get<0>(detail::cidata(y)) ==
                                                      ranges::get<1>(detail::cidata(x)))
                                               : (detail::cidata(y).index() != 1u ||
                                                  ranges::get<0>(detail::cidata(x)) ==
                                                      ranges::get<1>(detail::cidata(y)));
    }

    template(typename I1, typename I2, typename S1, typename S2)(
        requires sentinel_for<S1, I2> AND sentinel_for<S2, I1> AND
            equality_comparable_with<I1, I2>)
    bool operator==(common_iterator<I1, S1> const & x, common_iterator<I2, S2> const & y)
    {
        return detail::cidata(x).index() == 1u
                   ? (detail::cidata(y).index() == 1u ||
                      ranges::get<0>(detail::cidata(y)) ==
                          ranges::get<1>(detail::cidata(x)))
                   : (detail::cidata(y).index() == 1u
                          ? ranges::get<0>(detail::cidata(x)) ==
                                ranges::get<1>(detail::cidata(y))
                          : ranges::get<0>(detail::cidata(x)) ==
                                ranges::get<0>(detail::cidata(y)));
    }

    template(typename I1, typename I2, typename S1, typename S2)(
        requires sentinel_for<S1, I2> AND sentinel_for<S2, I1>)
    bool operator!=(common_iterator<I1, S1> const & x, common_iterator<I2, S2> const & y)
    {
        return !(x == y);
    }

    template(typename I1, typename I2, typename S1, typename S2)(
        requires sized_sentinel_for<I1, I2> AND sized_sentinel_for<S1, I2> AND
            sized_sentinel_for<S2, I1>)
    iter_difference_t<I2> operator-(common_iterator<I1, S1> const & x,
                                    common_iterator<I2, S2> const & y)
    {
        return detail::cidata(x).index() == 1u
                   ? (detail::cidata(y).index() == 1u
                          ? 0
                          : ranges::get<1>(detail::cidata(x)) -
                                ranges::get<0>(detail::cidata(y)))
                   : (detail::cidata(y).index() == 1u
                          ? ranges::get<0>(detail::cidata(x)) -
                                ranges::get<1>(detail::cidata(y))
                          : ranges::get<0>(detail::cidata(x)) -
                                ranges::get<0>(detail::cidata(y)));
    }

    template<typename I, typename S>
    struct indirectly_readable_traits<common_iterator<I, S>>
      : meta::if_c<
        (bool)indirectly_readable<I>,
        indirectly_readable_traits<I>,
        meta::nil_>
    {};

    /// \cond
    namespace detail
    {
        template<typename I>
        auto demote_common_iter_cat(...) -> nil_;
        template<typename I>
        auto demote_common_iter_cat(long)
            -> with_iterator_category<std::input_iterator_tag>;
        template(typename I)(
            requires derived_from<typename std::iterator_traits<I>::iterator_category,
                                      std::forward_iterator_tag>)
        auto demote_common_iter_cat(int)
            -> with_iterator_category<std::forward_iterator_tag>;

        template<typename I, bool = (bool)input_iterator<I>>
        struct common_iterator_std_traits : decltype(detail::demote_common_iter_cat<I>(0))
        {
            using difference_type = iter_difference_t<I>;
            using value_type = iter_value_t<I>;
            using reference = iter_reference_t<I>;
            using pointer = detail::iter_pointer_t<I>;
            using iterator_concept =
                meta::conditional_t<(bool)forward_iterator<I>, std::forward_iterator_tag,
                          std::input_iterator_tag>;
        };

        template<typename I>
        struct common_iterator_std_traits<I, false>
        {
            using difference_type = iter_difference_t<I>;
            using value_type = void;
            using reference = void;
            using pointer = void;
            using iterator_category = std::output_iterator_tag;
        };

        // An iterator adaptor that demotes a user-defined difference_type to
        // std::intmax_t, for use when constructing containers from such
        // iterators.
        template<typename I>
        struct cpp17_iterator_cursor
        {
        private:
            friend range_access;
            I it_;
            struct mixin : basic_mixin<cpp17_iterator_cursor>
            {
                mixin() = default;
                #ifndef _MSC_VER
                using basic_mixin<cpp17_iterator_cursor>::basic_mixin;
                #else
                constexpr explicit mixin(cpp17_iterator_cursor && cur)
                  : basic_mixin<cpp17_iterator_cursor>(
                        static_cast<cpp17_iterator_cursor &&>(cur))
                {}
                constexpr explicit mixin(cpp17_iterator_cursor const & cur)
                  : basic_mixin<cpp17_iterator_cursor>(cur)
                {}
                #endif
                explicit mixin(I it)
                  : mixin{cpp17_iterator_cursor{std::move(it)}}
                {}
                I base() const
                {
                    return this->get().it_;
                }
            };

        public:
            using single_pass = meta::bool_<!forward_iterator<I>>;
            using difference_type = std::ptrdiff_t;
            using value_type = iter_value_t<I>;

            cpp17_iterator_cursor() = default;
            constexpr explicit cpp17_iterator_cursor(I i)
              : it_(static_cast<I &&>(i))
            {}

            I arrow() const
            {
                return it_;
            }
            decltype(auto) read()
            {
                return *it_;
            }
            decltype(auto) read() const
            {
                return *it_;
            }
            void next()
            {
                ++it_;
            }
            bool equal(cpp17_iterator_cursor const & that) const
            {
                return it_ == that.it_;
            }
            CPP_member
            auto prev() //
                -> CPP_ret(void)(
                    requires bidirectional_iterator<I>)
            {
                --it_;
            }
            CPP_member
            auto advance(std::ptrdiff_t n) //
                -> CPP_ret(void)(
                    requires random_access_iterator<I>)
            {
                it_ += static_cast<iter_difference_t<I>>(n);
            }
            CPP_member
            auto distance_to(cpp17_iterator_cursor const & that) //
                -> CPP_ret(std::ptrdiff_t)(
                    requires random_access_iterator<I>)
            {
                auto d = that.it_ - it_;
                RANGES_EXPECT(d <= PTRDIFF_MAX);
                return static_cast<std::ptrdiff_t>(d);
            }
        };
    } // namespace detail
    /// \endcond

    namespace cpp20
    {
        using ranges::common_iterator;
    }
    /// @}
} // namespace ranges

/// \cond
RANGES_DIAGNOSTIC_PUSH
RANGES_DIAGNOSTIC_IGNORE_MISMATCHED_TAGS

namespace std
{
    template<typename I, typename S>
    struct iterator_traits<::ranges::common_iterator<I, S>>
      : ::ranges::detail::common_iterator_std_traits<I>
    {};
} // namespace std

RANGES_DIAGNOSTIC_POP
/// \endcond

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

#endif