File: move_iterators.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 (453 lines) | stat: -rw-r--r-- 13,671 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
/// \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_MOVE_ITERATORS_HPP
#define RANGES_V3_ITERATOR_MOVE_ITERATORS_HPP

#include <cstddef>
#include <utility>

#include <range/v3/range_fwd.hpp>

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

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

namespace ranges
{
    /// \addtogroup group-iterator
    /// @{
    template<typename I>
    struct move_iterator
    {
    private:
        CPP_assert(input_iterator<I>);
        I current_ = I{};

    public:
        using iterator_type = I;
        using difference_type = iter_difference_t<I>;
        using value_type = iter_value_t<I>;
        using iterator_category = std::input_iterator_tag;
        using reference = iter_rvalue_reference_t<I>;

        constexpr move_iterator() = default;
        explicit move_iterator(I i)
          : current_(i)
        {}
        template(typename O)(
            requires convertible_to<O, I>)
        move_iterator(move_iterator<O> const & i)
          : current_(i.base())
        {}
        template(typename O)(
            requires convertible_to<O, I>)
        move_iterator & operator=(move_iterator<O> const & i)
        {
            current_ = i.base();
            return *this;
        }
        I base() const
        {
            return current_;
        }
        // clang-format off
        auto CPP_auto_fun(operator*)()(const)
        (
            return iter_move(current_)
        )
            // clang-format on
            move_iterator &
            operator++()
        {
            ++current_;
            return *this;
        }
        CPP_member
        auto operator++(int) //
            -> CPP_ret(void)(
                requires (!forward_iterator<I>))
        {
            ++current_;
        }
        CPP_member
        auto operator++(int) //
            -> CPP_ret(move_iterator)(
                requires forward_iterator<I>)
        {
            return move_iterator(current_++);
        }
        CPP_member
        auto operator--() //
            -> CPP_ret(move_iterator &)(
                requires forward_iterator<I>)
        {
            --current_;
            return *this;
        }
        CPP_member
        auto operator--(int) //
            -> CPP_ret(move_iterator)(
                requires bidirectional_iterator<I>)
        {
            return move_iterator(current_--);
        }
        CPP_member
        auto operator+(difference_type n) const //
            -> CPP_ret(move_iterator)(
                requires random_access_iterator<I>)
        {
            return move_iterator(current_ + n);
        }
        CPP_member
        auto operator+=(difference_type n)
            -> CPP_ret(move_iterator &)(
                requires random_access_iterator<I>)
        {
            current_ += n;
            return *this;
        }
        CPP_member
        auto operator-(difference_type n) const //
            -> CPP_ret(move_iterator)(
                requires random_access_iterator<I>)
        {
            return move_iterator(current_ - n);
        }
        CPP_member
        auto operator-=(difference_type n) //
            -> CPP_ret(move_iterator &)(
                requires random_access_iterator<I>)
        {
            current_ -= n;
            return *this;
        }
        CPP_member
        auto operator[](difference_type n) const //
            -> CPP_ret(reference)(
                requires random_access_iterator<I>)
        {
            return iter_move(current_ + n);
        }

        template<typename I2>
        friend auto operator==(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires equality_comparable_with<I, I2>)
        {
            return x.base() == y.base();
        }
        template<typename I2>
        friend auto operator!=(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires equality_comparable_with<I, I2>)
        {
            return !(x == y);
        }
        template<typename I2>
        friend auto operator<(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires totally_ordered_with<I, I2>)
        {
            return x.base() < y.base();
        }
        template<typename I2>
        friend auto operator<=(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires totally_ordered_with<I, I2>)
        {
            return !(y < x);
        }
        template<typename I2>
        friend auto operator>(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires totally_ordered_with<I, I2>)
        {
            return y < x;
        }
        template<typename I2>
        friend auto operator>=(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(bool)(
                requires totally_ordered_with<I, I2>)
        {
            return !(x < y);
        }

        template<typename I2>
        friend auto operator-(move_iterator const & x, move_iterator<I2> const & y)
            -> CPP_broken_friend_ret(iter_difference_t<I2>)(
                requires sized_sentinel_for<I, I2>)
        {
            return x.base() - y.base();
        }
        CPP_broken_friend_member
        friend auto operator+(iter_difference_t<I> n,
                              move_iterator const & x)
            -> CPP_broken_friend_ret(move_iterator)(
                requires random_access_iterator<I>)
        {
            return x + n;
        }
    };

    struct make_move_iterator_fn
    {
        template(typename I)(
            requires input_iterator<I>)
        constexpr move_iterator<I> operator()(I it) const
        {
            return move_iterator<I>{detail::move(it)};
        }
    };

    RANGES_INLINE_VARIABLE(make_move_iterator_fn, make_move_iterator)

    template<typename S>
    struct move_sentinel
    {
    private:
        S sent_;

    public:
        constexpr move_sentinel()
          : sent_{}
        {}
        constexpr explicit move_sentinel(S s)
          : sent_(detail::move(s))
        {}
        template(typename OS)(
            requires convertible_to<OS, S>)
        constexpr explicit move_sentinel(move_sentinel<OS> const & that)
          : sent_(that.base())
        {}
        template(typename OS)(
            requires convertible_to<OS, S>)
        move_sentinel & operator=(move_sentinel<OS> const & that)
        {
            sent_ = that.base();
            return *this;
        }
        S base() const
        {
            return sent_;
        }

        template<typename I>
        friend auto operator==(move_iterator<I> const & i, move_sentinel const & s)
            -> CPP_broken_friend_ret(bool)(
                requires sentinel_for<S, I>)
        {
            return i.base() == s.base();
        }
        template<typename I>
        friend auto operator==(move_sentinel const & s, move_iterator<I> const & i)
            -> CPP_broken_friend_ret(bool)(
                requires sentinel_for<S, I>)
        {
            return s.base() == i.base();
        }
        template<typename I>
        friend auto operator!=(move_iterator<I> const & i, move_sentinel const & s)
            -> CPP_broken_friend_ret(bool)(
                requires sentinel_for<S, I>)
        {
            return i.base() != s.base();
        }
        template<typename I>
        friend auto operator!=(move_sentinel const & s, move_iterator<I> const & i)
            -> CPP_broken_friend_ret(bool)(
                requires sentinel_for<S, I>)
        {
            return s.base() != i.base();
        }
    };

    struct make_move_sentinel_fn
    {
        template(typename I)(
            requires input_iterator<I>)
        constexpr move_iterator<I> operator()(I i) const
        {
            return move_iterator<I>{detail::move(i)};
        }

        template(typename S)(
            requires semiregular<S> AND (!input_iterator<S>)) //
        constexpr move_sentinel<S> operator()(S s) const
        {
            return move_sentinel<S>{detail::move(s)};
        }
    };

    RANGES_INLINE_VARIABLE(make_move_sentinel_fn, make_move_sentinel)

    /// \cond
    namespace detail
    {
        template<typename I, bool IsReadable>
        struct move_into_cursor_types_
        {};

        template<typename I>
        struct move_into_cursor_types_<I, true>
        {
            using value_type = iter_value_t<I>;
            using single_pass = meta::bool_<(bool)single_pass_iterator_<I>>;
        };

        template<typename I>
        using move_into_cursor_types =
            move_into_cursor_types_<I, (bool)indirectly_readable<I>>;

        template<typename I>
        struct move_into_cursor : move_into_cursor_types<I>
        {
        private:
            friend range_access;
            struct mixin : basic_mixin<move_into_cursor>
            {
                mixin() = default;
                #ifndef _MSC_VER
                using basic_mixin<move_into_cursor>::basic_mixin;
                #else
                constexpr explicit mixin(move_into_cursor && cur)
                  : basic_mixin<move_into_cursor>(static_cast<move_into_cursor &&>(cur))
                {}
                constexpr explicit mixin(move_into_cursor const & cur)
                  : basic_mixin<move_into_cursor>(cur)
                {}
                #endif
                explicit mixin(I it)
                  : mixin{move_into_cursor{std::move(it)}}
                {}
                I base() const
                {
                    return this->get().it_;
                }
            };

            I it_ = I();

            explicit move_into_cursor(I it)
              : it_(std::move(it))
            {}
            void next()
            {
                ++it_;
            }
            template(typename T)(
                requires indirectly_writable<I, aux::move_t<T>>)
            void write(T && t) noexcept(noexcept(*it_ = std::move(t)))
            {
                *it_ = std::move(t);
            }
            template(typename T)(
                requires indirectly_writable<I, aux::move_t<T>>)
            void write(T && t) const noexcept(noexcept(*it_ = std::move(t)))
            {
                *it_ = std::move(t);
            }
            CPP_member
            auto read() const noexcept(noexcept(*std::declval<I const &>()))
                -> CPP_ret(iter_reference_t<I>)(
                    requires indirectly_readable<I>)
            {
                return *it_;
            }
            CPP_member
            auto equal(move_into_cursor const & that) const //
                -> CPP_ret(bool)(
                    requires input_iterator<I>)
            {
                return it_ == that.it_;
            }
            CPP_member
            auto prev() //
                -> CPP_ret(void)(
                    requires bidirectional_iterator<I>)
            {
                --it_;
            }
            CPP_member
            auto advance(iter_difference_t<I> n) //
                -> CPP_ret(void)(
                    requires random_access_iterator<I>)
            {
                it_ += n;
            }
            CPP_member
            auto distance_to(move_into_cursor const & that) const //
                -> CPP_ret(iter_difference_t<I>)(
                    requires sized_sentinel_for<I, I>)
            {
                return that.it_ - it_;
            }
            template(typename II = I const)(
                requires same_as<I const, II> AND indirectly_readable<II>)
            constexpr iter_rvalue_reference_t<II> move() const //
                noexcept(has_nothrow_iter_move_v<II>)
            {
                return iter_move(it_);
            }

        public:
            constexpr move_into_cursor() = default;
        };
    } // namespace detail
    /// \endcond

    struct move_into_fn
    {
        template<typename I>
        constexpr move_into_iterator<I> operator()(I it) const
        {
            return move_into_iterator<I>{std::move(it)};
        }
    };

    /// \sa `move_into_fn`
    RANGES_INLINE_VARIABLE(move_into_fn, move_into)

    namespace cpp20
    {
        using ranges::make_move_iterator;
        using ranges::move_iterator;
        using ranges::move_sentinel;
    } // namespace cpp20
    /// @}
} // namespace ranges

/// \cond
RANGES_DIAGNOSTIC_PUSH
RANGES_DIAGNOSTIC_IGNORE_MISMATCHED_TAGS

namespace std
{
    template<typename I>
    struct iterator_traits<::ranges::move_iterator<I>>
    {
        using iterator_category = std::input_iterator_tag;
        using difference_type = typename ::ranges::move_iterator<I>::difference_type;
        using value_type = typename ::ranges::move_iterator<I>::value_type;
        using reference = typename ::ranges::move_iterator<I>::reference;
        using pointer = meta::_t<std::add_pointer<reference>>;
    };
} // namespace std

RANGES_DIAGNOSTIC_POP
/// \endcond

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

#endif // RANGES_V3_ITERATOR_MOVE_ITERATORS_HPP