sparrow 0.3.0
Loading...
Searching...
No Matches
repeat_container.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <ranges>
18
20
21namespace sparrow
22{
23
24 template <typename T>
26 : public sparrow::iterator_base<repeat_view_iterator<T>, const T, std::random_access_iterator_tag>
27 {
28 public:
29
32
33 using iterator_category = std::random_access_iterator_tag;
34 using value_type = T;
35 using reference = typename base_type::reference;
36 using difference_type = typename base_type::difference_type;
37
38 constexpr repeat_view_iterator() = default;
39
45 constexpr repeat_view_iterator(const T& value, size_t index);
46
47 private:
48
49 [[nodiscard]] constexpr reference dereference() const noexcept;
50 constexpr void increment() noexcept;
51 constexpr void decrement() noexcept;
52 constexpr void advance(difference_type n) noexcept;
53 [[nodiscard]] constexpr difference_type distance_to(const self_type& rhs) const noexcept;
54 [[nodiscard]] constexpr bool equal(const self_type& rhs) const noexcept;
55 [[nodiscard]] constexpr bool less_than(const self_type& rhs) const noexcept;
56
57 const T* m_value = nullptr;
58 size_t m_index = 0;
59
60 friend class iterator_access;
61 };
62
66 template <typename T>
67 class repeat_view : public std::ranges::view_interface<repeat_view<T>>
68 {
69 public:
70
71 using value_type = T;
74
80 constexpr repeat_view(const T& value, size_t count) noexcept;
81
82 constexpr const_iterator begin() const noexcept;
83 constexpr const_iterator end() const noexcept;
84 constexpr const_iterator cbegin() const noexcept;
85 constexpr const_iterator cend() const noexcept;
86 [[nodiscard]] constexpr size_t size() const noexcept;
87
88 private:
89
90 T m_value;
91 size_t m_count;
92 };
93
94 template <typename T>
95 constexpr repeat_view_iterator<T>::repeat_view_iterator(const T& value, size_t index)
96 : m_value(&value)
97 , m_index(index)
98 {
99 }
100
101 template <typename T>
102 constexpr auto repeat_view_iterator<T>::dereference() const noexcept -> reference
103 {
104 return *m_value;
105 }
106
107 template <typename T>
108 constexpr void repeat_view_iterator<T>::increment() noexcept
109 {
110 ++m_index;
111 }
112
113 template <typename T>
114 constexpr void repeat_view_iterator<T>::decrement() noexcept
115 {
116 --m_index;
117 }
118
119 template <typename T>
120 constexpr void repeat_view_iterator<T>::advance(difference_type n) noexcept
121 {
122 m_index += n;
123 }
124
125 template <typename T>
126 constexpr auto repeat_view_iterator<T>::distance_to(const self_type& rhs) const noexcept -> difference_type
127 {
128 return static_cast<difference_type>(rhs.m_index - m_index);
129 }
130
131 template <typename T>
132 constexpr bool repeat_view_iterator<T>::equal(const self_type& rhs) const noexcept
133 {
134 return m_index == rhs.m_index;
135 }
136
137 template <typename T>
138 constexpr bool repeat_view_iterator<T>::less_than(const self_type& rhs) const noexcept
139 {
140 return m_index < rhs.m_index;
141 }
142
143 template <typename T>
144 constexpr repeat_view<T>::repeat_view(const T& value, size_t count) noexcept
145 : m_value(value)
146 , m_count(count)
147 {
148 }
149
150 template <typename T>
151 constexpr auto repeat_view<T>::begin() const noexcept -> const_iterator
152 {
153 return const_iterator(m_value, 0);
154 }
155
156 template <typename T>
157 constexpr auto repeat_view<T>::end() const noexcept -> const_iterator
158 {
159 return const_iterator(m_value, m_count);
160 }
161
162 template <typename T>
163 constexpr auto repeat_view<T>::cbegin() const noexcept -> const_iterator
164 {
165 return const_iterator(m_value, 0);
166 }
167
168 template <typename T>
169 constexpr auto repeat_view<T>::cend() const noexcept -> const_iterator
170 {
171 return const_iterator(m_value, m_count);
172 }
173
174 template <typename T>
175 [[nodiscard]] constexpr size_t repeat_view<T>::size() const noexcept
176 {
177 return m_count;
178 }
179
180}
sparrow::iterator_base< self_type, const T, std::random_access_iterator_tag > base_type
constexpr repeat_view_iterator(const T &value, size_t index)
Constructs a repeat_view_iterator.
repeat_view_iterator< T > self_type
std::random_access_iterator_tag iterator_category
typename base_type::reference reference
constexpr repeat_view_iterator()=default
typename base_type::difference_type difference_type
constexpr size_t size() const noexcept
repeat_view< value_type > self_type
constexpr repeat_view(const T &value, size_t count) noexcept
Constructs a repeat_view.
constexpr const_iterator begin() const noexcept
constexpr const_iterator cend() const noexcept
constexpr const_iterator cbegin() const noexcept
repeat_view_iterator< value_type > const_iterator
constexpr const_iterator end() const noexcept