File indexing completed on 2025-01-18 10:09:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef RANGES_V3_VIEW_COUNTED_HPP
0014 #define RANGES_V3_VIEW_COUNTED_HPP
0015
0016 #include <utility>
0017
0018 #include <range/v3/range_fwd.hpp>
0019
0020 #include <range/v3/iterator/concepts.hpp>
0021 #include <range/v3/iterator/counted_iterator.hpp>
0022 #include <range/v3/iterator/default_sentinel.hpp>
0023 #include <range/v3/iterator/traits.hpp>
0024 #include <range/v3/utility/static_const.hpp>
0025 #include <range/v3/view/interface.hpp>
0026 #include <range/v3/view/subrange.hpp>
0027
0028 #include <range/v3/detail/prologue.hpp>
0029
0030 namespace ranges
0031 {
0032
0033
0034 template<typename I>
0035 struct counted_view : view_interface<counted_view<I>, finite>
0036 {
0037 private:
0038 friend range_access;
0039 I it_;
0040 iter_difference_t<I> n_;
0041
0042 public:
0043 counted_view() = default;
0044 counted_view(I it, iter_difference_t<I> n)
0045 : it_(it)
0046 , n_(n)
0047 {
0048 RANGES_EXPECT(0 <= n_);
0049 }
0050 counted_iterator<I> begin() const
0051 {
0052 return make_counted_iterator(it_, n_);
0053 }
0054 default_sentinel_t end() const
0055 {
0056 return {};
0057 }
0058 auto size() const
0059 {
0060 return static_cast<detail::iter_size_t<I>>(n_);
0061 }
0062 };
0063
0064 template<typename I>
0065 RANGES_INLINE_VAR constexpr bool enable_borrowed_range<counted_view<I>> = true;
0066
0067 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
0068 template<typename I>
0069 counted_view(I, iter_difference_t<I>)
0070 -> counted_view<I>;
0071 #endif
0072
0073 namespace views
0074 {
0075 struct cpp20_counted_fn
0076 {
0077 template(typename I)(
0078 requires input_or_output_iterator<I> AND (!random_access_iterator<I>))
0079 subrange<counted_iterator<I>, default_sentinel_t>
0080 operator()(I it, iter_difference_t<I> n) const
0081 {
0082 return {make_counted_iterator(std::move(it), n), default_sentinel};
0083 }
0084 template(typename I)(
0085 requires random_access_iterator<I>)
0086 subrange<I> operator()(I it, iter_difference_t<I> n) const
0087 {
0088 return {it, it + n};
0089 }
0090 };
0091
0092 struct counted_fn
0093 {
0094 template(typename I)(
0095 requires input_or_output_iterator<I> AND (!random_access_iterator<I>))
0096 counted_view<I> operator()(I it, iter_difference_t<I> n) const
0097 {
0098 return {std::move(it), n};
0099 }
0100 template(typename I)(
0101 requires random_access_iterator<I>)
0102 subrange<I> operator()(I it, iter_difference_t<I> n) const
0103 {
0104 return {it, it + n};
0105 }
0106 };
0107
0108
0109
0110 RANGES_INLINE_VARIABLE(counted_fn, counted)
0111 }
0112
0113 namespace cpp20
0114 {
0115 namespace views
0116 {
0117 RANGES_INLINE_VARIABLE(ranges::views::cpp20_counted_fn, counted)
0118 }
0119 }
0120
0121 }
0122
0123 #include <range/v3/detail/epilogue.hpp>
0124 #include <range/v3/detail/satisfy_boost_range.hpp>
0125 RANGES_SATISFY_BOOST_RANGE(::ranges::counted_view)
0126
0127 #endif