File: interface.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 (515 lines) | stat: -rw-r--r-- 19,817 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
/// \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_VIEW_INTERFACE_HPP
#define RANGES_V3_VIEW_INTERFACE_HPP

#include <iosfwd>

#include <meta/meta.hpp>

#include <concepts/concepts.hpp>

#include <range/v3/range_fwd.hpp>

#include <range/v3/iterator/common_iterator.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/range/access.hpp>
#include <range/v3/range/concepts.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/range/traits.hpp>

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

#if defined(RANGES_WORKAROUND_GCC_91525)
#define CPP_template_gcc_workaround CPP_template_sfinae
#else
#define CPP_template_gcc_workaround template
#endif

namespace ranges
{
    /// \cond
    namespace detail
    {
        template<typename From, typename To = From>
        struct slice_bounds
        {
            From from;
            To to;
            template(typename F, typename T)(
                requires convertible_to<F, From> AND convertible_to<T, To>)
            constexpr slice_bounds(F f, T t)
              : from(static_cast<From>(f))
              , to(static_cast<To>(t))
            {}
        };

        template<typename Int>
        struct from_end_
        {
            Int dist_;

            constexpr explicit from_end_(Int dist)
              : dist_(dist)
            {}

            template(typename Other)(
                requires integer_like_<Other> AND explicitly_convertible_to<Other, Int>)
            constexpr operator from_end_<Other>() const
            {
                return from_end_<Other>{static_cast<Other>(dist_)};
            }
        };

        template<typename Rng>
        using from_end_of_t = from_end_<range_difference_t<Rng>>;

        // clang-format off
        /// \concept _can_empty_
        /// \brief The \c _can_empty_ concept
        template<typename Rng>
        CPP_requires(_can_empty_,
            requires(Rng & rng) //
            (
                ranges::empty(rng)
            ));
        /// \concept can_empty_
        /// \brief The \c can_empty_ concept
        template<typename Rng>
        CPP_concept can_empty_ = //
            CPP_requires_ref(detail::_can_empty_, Rng);
        // clang-format on

        template<cardinality C>
        RANGES_INLINE_VAR constexpr bool has_fixed_size_ = (C >= 0 || C == infinite);

        template<bool>
        struct dependent_
        {
            template<typename T>
            using invoke = T;
        };

        template<typename Stream, typename Rng>
        Stream & print_rng_(Stream & sout, Rng & rng)
        {
            sout << '[';
            auto it = ranges::begin(rng);
            auto const e = ranges::end(rng);
            if(it != e)
            {
                for(;;)
                {
                    sout << *it;
                    if(++it == e)
                        break;
                    sout << ',';
                }
            }
            sout << ']';
            return sout;
        }
    } // namespace detail
    /// \endcond

    /// \addtogroup group-views
    /// @{
    template<typename Derived, cardinality Cardinality /* = finite*/>
    struct view_interface : basic_view<Cardinality>
    {
    protected:
        template<bool B>
        using D = meta::invoke<detail::dependent_<B>, Derived>;

        constexpr Derived & derived() noexcept
        {
            CPP_assert(derived_from<Derived, view_interface>);
            return static_cast<Derived &>(*this);
        }
        /// \overload
        constexpr Derived const & derived() const noexcept
        {
            CPP_assert(derived_from<Derived, view_interface>);
            return static_cast<Derived const &>(*this);
        }

    public:
        view_interface() = default;
        view_interface(view_interface &&) = default;
        view_interface(view_interface const &) = default;
        view_interface & operator=(view_interface &&) = default;
        view_interface & operator=(view_interface const &) = default;
        /// \brief Test whether a range can be empty:
        CPP_member
        constexpr auto empty() const noexcept //
            -> CPP_ret(bool)(
                requires (detail::has_fixed_size_<Cardinality>))
        {
            return Cardinality == 0;
        }
        /// \overload
        template(bool True = true)(
            requires True AND (Cardinality < 0) AND (Cardinality != infinite) AND
                (!forward_range<D<True>>) AND sized_range<D<True>>)
        constexpr bool empty() //
            noexcept(noexcept(bool(ranges::size(std::declval<D<True> &>()) == 0)))
        {
            return ranges::size(derived()) == 0;
        }
        /// \overload
        template(bool True = true)(
            requires True AND (Cardinality < 0) AND (Cardinality != infinite) AND
                (!forward_range<D<True> const>) AND sized_range<D<True> const>)
        constexpr bool empty() const //
            noexcept(noexcept(bool(ranges::size(std::declval<D<True> const &>()) == 0)))
        {
            return ranges::size(derived()) == 0;
        }
        /// \overload
        template(bool True = true)(
            requires True AND (!detail::has_fixed_size_<Cardinality>) AND
                forward_range<D<True>>)
        constexpr bool empty() noexcept(
            noexcept(bool(ranges::begin(std::declval<D<True> &>()) ==
                          ranges::end(std::declval<D<True> &>()))))
        {
            return bool(ranges::begin(derived()) == ranges::end(derived()));
        }
        /// \overload
        template(bool True = true)(
            requires True AND (!detail::has_fixed_size_<Cardinality>) AND
                forward_range<D<True> const>)
        constexpr bool empty() const
            noexcept(noexcept(bool(ranges::begin(std::declval<D<True> const &>()) ==
                                   ranges::end(std::declval<D<True> const &>()))))
        {
            return bool(ranges::begin(derived()) == ranges::end(derived()));
        }
        CPP_template_gcc_workaround(bool True = true)(
            requires True && detail::can_empty_<D<True>>) // clang-format off
        constexpr explicit operator bool()
            noexcept(noexcept(ranges::empty(std::declval<D<True> &>())))
        {
            return !ranges::empty(derived());
        }
        // clang-format on
        /// \overload
        CPP_template_gcc_workaround(bool True = true)(
            requires True && detail::can_empty_<D<True> const>) // clang-format off
        constexpr explicit operator bool() const
            noexcept(noexcept(ranges::empty(std::declval<D<True> const &>())))
        {
            return !ranges::empty(derived());
        }
        // clang-format on
        /// If the size of the range is known at compile-time and finite,
        /// return it.
        template(bool True = true, int = 42)(
            requires True AND (Cardinality >= 0)) //
        static constexpr std::size_t size() noexcept
        {
            return static_cast<std::size_t>(Cardinality);
        }
        /// If `sized_sentinel_for<sentinel_t<Derived>, iterator_t<Derived>>` is
        /// satisfied, and if `Derived` is a `forward_range`, then return
        /// `end - begin` cast to an unsigned integer.
        template(bool True = true)(
            requires True AND (Cardinality < 0) AND
                sized_sentinel_for<sentinel_t<D<True>>, iterator_t<D<True>>> AND
                forward_range<D<True>>)
        constexpr detail::iter_size_t<iterator_t<D<True>>> size()
        {
            using size_type = detail::iter_size_t<iterator_t<D<True>>>;
            return static_cast<size_type>(derived().end() - derived().begin());
        }
        /// \overload
        template(bool True = true)(
            requires True AND (Cardinality < 0) AND
                sized_sentinel_for<sentinel_t<D<True> const>,
                                   iterator_t<D<True> const>> AND
                forward_range<D<True> const>)
        constexpr detail::iter_size_t<iterator_t<D<True>>> size() const //
        {
            using size_type = detail::iter_size_t<iterator_t<D<True>>>;
            return static_cast<size_type>(derived().end() - derived().begin());
        }
        /// Access the first element in a range:
        template(bool True = true)(
            requires True AND forward_range<D<True>>)
        constexpr range_reference_t<D<True>> front()
        {
            return *derived().begin();
        }
        /// \overload
        template(bool True = true)(
            requires True AND forward_range<D<True> const>)
        constexpr range_reference_t<D<True> const> front() const
        {
            return *derived().begin();
        }
        /// Access the last element in a range:
        template(bool True = true)(
            requires True AND common_range<D<True>> AND bidirectional_range<D<True>>)
        constexpr range_reference_t<D<True>> back()
        {
            return *prev(derived().end());
        }
        /// \overload
        template(bool True = true)(
            requires True AND common_range<D<True> const> AND
                bidirectional_range<D<True> const>)
        constexpr range_reference_t<D<True> const> back() const
        {
            return *prev(derived().end());
        }
        /// Simple indexing:
        template(bool True = true)(
            requires True AND random_access_range<D<True>>)
        constexpr range_reference_t<D<True>> operator[](range_difference_t<D<True>> n)
        {
            return derived().begin()[n];
        }
        /// \overload
        template(bool True = true)(
            requires True AND random_access_range<D<True> const>)
        constexpr range_reference_t<D<True> const> //
        operator[](range_difference_t<D<True>> n) const
        {
            return derived().begin()[n];
        }
        /// Returns a pointer to the block of memory
        /// containing the elements of a contiguous range:
        template(bool True = true)(
            requires True AND contiguous_iterator<iterator_t<D<True>>>)
        constexpr std::add_pointer_t<range_reference_t<D<True>>> data() //
        {
            return std::addressof(*ranges::begin(derived()));
        }
        /// \overload
        template(bool True = true)(
            requires True AND contiguous_iterator<iterator_t<D<True> const>>)
        constexpr std::add_pointer_t<range_reference_t<D<True> const>> data() const //
        {
            return std::addressof(*ranges::begin(derived()));
        }
        /// Returns a reference to the element at specified location pos, with bounds
        /// checking.
        template(bool True = true)(
            requires True AND random_access_range<D<True>> AND sized_range<D<True>>)
        constexpr range_reference_t<D<True>> at(range_difference_t<D<True>> n)
        {
            using size_type = range_size_t<Derived>;
            if(n < 0 || size_type(n) >= ranges::size(derived()))
            {
                throw std::out_of_range("view_interface::at");
            }
            return derived().begin()[n];
        }
        /// \overload
        template(bool True = true)(
            requires True AND random_access_range<D<True> const> AND
                sized_range<D<True> const>)
        constexpr range_reference_t<D<True> const> at(range_difference_t<D<True>> n) const
        {
            using size_type = range_size_t<Derived const>;
            if(n < 0 || size_type(n) >= ranges::size(derived()))
            {
                throw std::out_of_range("view_interface::at");
            }
            return derived().begin()[n];
        }
        /// Python-ic slicing:
        //      rng[{4,6}]
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> &>)
        constexpr auto
            operator[](detail::slice_bounds<range_difference_t<D<True>>> offs) &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> const &>)
        constexpr auto
            operator[](detail::slice_bounds<range_difference_t<D<True>>> offs) const &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True>>)
        constexpr auto
            operator[](detail::slice_bounds<range_difference_t<D<True>>> offs) &&
        {
            return Slice{}(detail::move(derived()), offs.from, offs.to);
        }
        //      rng[{4,end-2}]
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> &> AND sized_range<D<True> &>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> const &> AND
                sized_range<D<True> const &>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) const &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True>> AND sized_range<D<True>>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) &&
        {
            return Slice{}(detail::move(derived()), offs.from, offs.to);
        }
        //      rng[{end-4,end-2}]
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND (forward_range<D<True> &> ||
                              (input_range<D<True> &> && sized_range<D<True> &>))) //
        constexpr auto //
        operator[](detail::slice_bounds<detail::from_end_of_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND
            (forward_range<D<True> const &> ||
             (input_range<D<True> const &> && sized_range<D<True> const &>))) //
        constexpr auto //
        operator[](detail::slice_bounds<detail::from_end_of_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) const &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND
                (forward_range<D<True>> ||
                    (input_range<D<True>> && sized_range<D<True>>))) //
        constexpr auto //
        operator[](detail::slice_bounds<detail::from_end_of_t<D<True>>,
                                        detail::from_end_of_t<D<True>>> offs) &&
        {
            return Slice{}(detail::move(derived()), offs.from, offs.to);
        }
        //      rng[{4,end}]
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> &>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>, end_fn> offs) &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True> const &>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>, end_fn> offs) const &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND input_range<D<True>>)
        constexpr auto //
        operator[](detail::slice_bounds<range_difference_t<D<True>>, end_fn> offs) &&
        {
            return Slice{}(detail::move(derived()), offs.from, offs.to);
        }
        //      rng[{end-4,end}]
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND
                (forward_range<D<True> &> ||
                    (input_range<D<True> &> && sized_range<D<True> &>))) //
        constexpr auto //
        operator[](detail::slice_bounds<detail::from_end_of_t<D<True>>, end_fn> offs) &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND
                (forward_range<D<True> const &> ||
                    (input_range<D<True> const &> && sized_range<D<True> const &>))) //
        constexpr auto //
        operator[](
            detail::slice_bounds<detail::from_end_of_t<D<True>>, end_fn> offs) const &
        {
            return Slice{}(derived(), offs.from, offs.to);
        }
        /// \overload
        template(bool True = true, typename Slice = views::slice_fn)(
            requires True AND
                (forward_range<D<True>> ||
                    (input_range<D<True>> && sized_range<D<True>>))) //
        constexpr auto //
        operator[](detail::slice_bounds<detail::from_end_of_t<D<True>>, end_fn> offs) &&
        {
            return Slice{}(detail::move(derived()), offs.from, offs.to);
        }
    private:
#ifndef RANGES_V3_DISABLE_IO
        /// \brief Print a range to an ostream
        template<bool True = true>
        friend auto operator<<(std::ostream & sout, Derived const & rng)
            -> CPP_broken_friend_ret(std::ostream &)(
                requires True && input_range<D<True> const>)
        {
            return detail::print_rng_(sout, rng);
        }
        /// \overload
        template<bool True = true>
        friend auto operator<<(std::ostream & sout, Derived & rng)
            -> CPP_broken_friend_ret(std::ostream &)(
                requires True && (!range<D<True> const>) && input_range<D<True>>)
        {
            return detail::print_rng_(sout, rng);
        }
        /// \overload
        template<bool True = true>
        friend auto operator<<(std::ostream & sout, Derived && rng)
            -> CPP_broken_friend_ret(std::ostream &)(
                requires True && (!range<D<True> const>) && input_range<D<True>>)
        {
            return detail::print_rng_(sout, rng);
        }
#endif
    };
    namespace cpp20
    {
        template(typename Derived)(
            requires std::is_class<Derived>::value AND
                same_as<Derived, meta::_t<std::remove_cv<Derived>>>)
        using view_interface = ranges::view_interface<Derived, ranges::unknown>;
    }
    /// @}
} // namespace ranges

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

#endif