Horizon
counted.hpp
Go to the documentation of this file.
1 // 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 
24 #include <range/v3/utility/static_const.hpp>
27 
28 #include <range/v3/detail/prologue.hpp>
29 
30 namespace ranges
31 {
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  {
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 
111  } // namespace views
112 
113  namespace cpp20
114  {
115  namespace views
116  {
118  }
119  } // namespace cpp20
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
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
Definition: counted.hpp:36
Definition: default_sentinel.hpp:26
Definition: subrange.hpp:196
Definition: interface.hpp:129
Definition: counted.hpp:93
Definition: counted.hpp:76