Horizon
Loading...
Searching...
No Matches
repeat_n.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2013-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
14#ifndef RANGES_V3_VIEW_REPEAT_N_HPP
15#define RANGES_V3_VIEW_REPEAT_N_HPP
16
17#include <utility>
18
20
24#include <range/v3/utility/static_const.hpp>
26
27#include <range/v3/detail/prologue.hpp>
28
29namespace ranges
30{
33
34 // Ordinarily, a view shouldn't contain its elements. This is so that copying
35 // and assigning ranges is O(1), and also so that in the event of element
36 // mutation, all the copies of the range see the mutation the same way. The
37 // repeat_n_view *does* own its lone element, though. This is OK because:
38 // - O(N) copying is fine when N==1 as it is in this case, and
39 // - The element is immutable, so there is no potential for incorrect
40 // semantics.
41 template<typename Val>
42 struct repeat_n_view : view_facade<repeat_n_view<Val>, finite>
43 {
44 private:
45 friend range_access;
46 semiregular_box_t<Val> value_;
47 std::ptrdiff_t n_;
48
49 struct cursor
50 {
51 private:
52 Val const * value_;
53 std::ptrdiff_t n_;
54
55 public:
56 cursor() = default;
57 cursor(Val const & value, std::ptrdiff_t n)
58 : value_(std::addressof(value))
59 , n_(n)
60 {}
61 Val const & read() const
62 {
63 return *value_;
64 }
65 constexpr bool equal(default_sentinel_t) const
66 {
67 return 0 == n_;
68 }
69 bool equal(cursor const & that) const
70 {
71 return n_ == that.n_;
72 }
73 void next()
74 {
75 RANGES_EXPECT(0 != n_);
76 --n_;
77 }
78 void prev()
79 {
80 ++n_;
81 }
82 void advance(std::ptrdiff_t n)
83 {
84 n_ -= n;
85 }
86 std::ptrdiff_t distance_to(cursor const & that) const
87 {
88 return n_ - that.n_;
89 }
90 };
91 cursor begin_cursor() const
92 {
93 return {value_, n_};
94 }
95
96 public:
97 repeat_n_view() = default;
98 constexpr repeat_n_view(Val value, std::ptrdiff_t n)
99 : value_(detail::move(value))
100 , n_((RANGES_EXPECT(0 <= n), n))
101 {}
102 constexpr std::size_t size() const
103 {
104 return static_cast<std::size_t>(n_);
105 }
106 };
107
108 namespace views
109 {
111 {
112 template(typename Val)(
113 requires copy_constructible<Val>)
114 repeat_n_view<Val> operator()(Val value, std::ptrdiff_t n) const
115 {
116 return repeat_n_view<Val>{std::move(value), n};
117 }
118 };
119
122 RANGES_INLINE_VARIABLE(repeat_n_fn, repeat_n)
123 } // namespace views
125} // namespace ranges
126
127#include <range/v3/detail/epilogue.hpp>
128#include <range/v3/detail/satisfy_boost_range.hpp>
129RANGES_SATISFY_BOOST_RANGE(::ranges::repeat_n_view)
130
131#endif
Definition default_sentinel.hpp:26
Definition repeat_n.hpp:43
A utility for constructing a view from a (derived) type that implements begin and end cursors.
Definition facade.hpp:66
Definition repeat_n.hpp:111