Debian Codegraph @

3rd_party/range-v3/range/v3/view/counted.hpp

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 #ifndef RANGES_V3_VIEW_COUNTED_HPP
14 #define RANGES_V3_VIEW_COUNTED_HPP
15
16 #include <utility>
17
18 #include <range/v3/range_fwd.hpp>
19
20 #include <range/v3/iterator/concepts.hpp>
21 #include <range/v3/iterator/counted_iterator.hpp>
22 #include <range/v3/iterator/default_sentinel.hpp>
23 #include <range/v3/iterator/traits.hpp>
24 #include <range/v3/utility/static_const.hpp>
25 #include <range/v3/view/interface.hpp>
26 #include <range/v3/view/subrange.hpp>
27
28 #include <range/v3/detail/prologue.hpp>
29
30 namespace ranges
31 {
32 /// \addtogroup group-views
33 /// @{
34 template<typename I>
35 struct counted_view : view_interface<counted_view<I>, finite>
36 {
37 private:
38 friend range_access;
39 I it_;
40 iter_difference_t<I> n_;
41
42 public:
43 counted_view() = default;
44 counted_view(I it, iter_difference_t<I> n)
45 : it_(it)
46 , n_(n)
47 {
48 RANGES_EXPECT(0 <= n_);
49 }
50 counted_iterator<I> begin() const
51 {
52 return make_counted_iterator(it_, n_);
53 }
54 default_sentinel_t end() const
55 {
56 return {};
57 }
58 auto size() const
59 {
60 return static_cast<detail::iter_size_t<I>>(n_);
61 }
62 };
63
64 template<typename I>
65 RANGES_INLINE_VAR constexpr bool enable_borrowed_range<counted_view<I>> = true;
66
67 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
68 template<typename I>
69 counted_view(I, iter_difference_t<I>)
70 -> counted_view<I>;
71 #endif
72
73 namespace views
74 {
75 struct cpp20_counted_fn
76 {
77 template(typename I)(
78 requires input_or_output_iterator<I> AND (!random_access_iterator<I>)) //
79 subrange<counted_iterator<I>, default_sentinel_t> //
80 operator()(I it, iter_difference_t<I> n) const
81 {
82 return {make_counted_iterator(std::move(it), n), default_sentinel};
83 }
84 template(typename I)(
85 requires random_access_iterator<I>)
86 subrange<I> operator()(I it, iter_difference_t<I> n) const
87 {
88 return {it, it + n};
89 }
90 };
91
92 struct counted_fn
93 {
94 template(typename I)(
95 requires input_or_output_iterator<I> AND (!random_access_iterator<I>)) //
96 counted_view<I> operator()(I it, iter_difference_t<I> n) const
97 {
98 return {std::move(it), n};
99 }
100 template(typename I)(
101 requires random_access_iterator<I>)
102 subrange<I> operator()(I it, iter_difference_t<I> n) const
103 {
104 return {it, it + n};
105 }
106 };
107
108 /// \relates counted_fn
109 /// \ingroup group-views
110 RANGES_INLINE_VARIABLE(counted_fn, counted)
111 } // namespace views
112
113 namespace cpp20
114 {
115 namespace views
116 {
117 RANGES_INLINE_VARIABLE(ranges::views::cpp20_counted_fn, counted)
118 }
119 } // namespace cpp20
120 /// @}
121 } // namespace ranges
122
123 #include <range/v3/detail/epilogue.hpp>
124 #include <range/v3/detail/satisfy_boost_range.hpp>
125 RANGES_SATISFY_BOOST_RANGE(::ranges::counted_view)
126
127 #endif