sparrow 2.3.1
C++20 idiomatic APIs for the Apache Arrow Columnar Format
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 <functional>
18#include <ranges>
19#include <type_traits>
20#include <utility>
21
23
24namespace sparrow
25{
26 namespace detail
27 {
28 template <typename T>
30 {
31 using value_type = T;
32 using storage_type = T;
34
35 [[nodiscard]] static constexpr const value_type& get(const storage_type& value) noexcept
36 {
37 return value;
38 }
39 };
40
41 template <typename T>
43 {
44 using value_type = std::remove_reference_t<T>;
45 using storage_type = std::reference_wrapper<value_type>;
47
48 [[nodiscard]] static constexpr const value_type& get(const storage_type& value) noexcept
49 {
50 return value.get();
51 }
52 };
53
54 }
55
56 template <typename T>
58 : public sparrow::iterator_base<repeat_view_iterator<T>, const T, std::random_access_iterator_tag>
59 {
60 public:
61
64
65 using iterator_category = std::random_access_iterator_tag;
66 using value_type = T;
67 using reference = typename base_type::reference;
68 using difference_type = typename base_type::difference_type;
69
70 constexpr repeat_view_iterator() = default;
71
77 constexpr repeat_view_iterator(const T& value, size_t index);
78
79 private:
80
81 [[nodiscard]] constexpr reference dereference() const noexcept;
82 constexpr void increment() noexcept;
83 constexpr void decrement() noexcept;
84 constexpr void advance(difference_type n) noexcept;
85 [[nodiscard]] constexpr difference_type distance_to(const self_type& rhs) const noexcept;
86 [[nodiscard]] constexpr bool equal(const self_type& rhs) const noexcept;
87 [[nodiscard]] constexpr bool less_than(const self_type& rhs) const noexcept;
88
89 const T* m_value = nullptr;
90 size_t m_index = 0;
91
92 friend class iterator_access;
93 };
94
98 template <typename T>
99 class repeat_view : public std::ranges::view_interface<repeat_view<T>>
100 {
101 public:
102
107
113 constexpr repeat_view(typename storage_traits::constructor_reference value, size_t count) noexcept(
114 std::is_nothrow_constructible_v<typename storage_traits::storage_type, typename storage_traits::constructor_reference>
115 );
116
117 constexpr repeat_view(value_type&& value, size_t count) noexcept(
118 std::is_nothrow_move_constructible_v<value_type>
119 )
120 requires(!std::is_reference_v<T>);
121
122 constexpr const_iterator begin() const noexcept;
123 constexpr const_iterator end() const noexcept;
124 constexpr const_iterator cbegin() const noexcept;
125 constexpr const_iterator cend() const noexcept;
126 [[nodiscard]] constexpr size_t size() const noexcept;
127
128 private:
129
130 [[nodiscard]] constexpr const value_type& stored_value() const noexcept;
131
132 typename storage_traits::storage_type m_value;
133 size_t m_count = 0;
134 };
135
136 template <typename T>
137 repeat_view(T&, size_t) -> repeat_view<T&>;
138
139 template <typename T>
140 repeat_view(T&&, size_t) -> repeat_view<std::remove_cvref_t<T>>;
141
142 template <typename T>
143 constexpr repeat_view_iterator<T>::repeat_view_iterator(const T& value, size_t index)
144 : m_value(&value)
145 , m_index(index)
146 {
147 }
148
149 template <typename T>
150 constexpr auto repeat_view_iterator<T>::dereference() const noexcept -> reference
151 {
152 return *m_value;
153 }
154
155 template <typename T>
156 constexpr void repeat_view_iterator<T>::increment() noexcept
157 {
158 ++m_index;
159 }
160
161 template <typename T>
162 constexpr void repeat_view_iterator<T>::decrement() noexcept
163 {
164 --m_index;
165 }
166
167 template <typename T>
168 constexpr void repeat_view_iterator<T>::advance(difference_type n) noexcept
169 {
170 m_index += n;
171 }
172
173 template <typename T>
174 constexpr auto repeat_view_iterator<T>::distance_to(const self_type& rhs) const noexcept -> difference_type
175 {
176 return static_cast<difference_type>(rhs.m_index - m_index);
177 }
178
179 template <typename T>
180 constexpr bool repeat_view_iterator<T>::equal(const self_type& rhs) const noexcept
181 {
182 return m_index == rhs.m_index;
183 }
184
185 template <typename T>
186 constexpr bool repeat_view_iterator<T>::less_than(const self_type& rhs) const noexcept
187 {
188 return m_index < rhs.m_index;
189 }
190
191 template <typename T>
192 constexpr repeat_view<T>::repeat_view(typename storage_traits::constructor_reference value, size_t count) noexcept(
193 std::is_nothrow_constructible_v<typename storage_traits::storage_type, typename storage_traits::constructor_reference>
194 )
195 : m_value(value)
196 , m_count(count)
197 {
198 }
199
200 template <typename T>
201 constexpr repeat_view<T>::repeat_view(value_type&& value, size_t count) noexcept(
202 std::is_nothrow_move_constructible_v<value_type>
203 )
204 requires(!std::is_reference_v<T>)
205 : m_value(std::move(value))
206 , m_count(count)
207 {
208 }
209
210 template <typename T>
211 [[nodiscard]] constexpr const typename repeat_view<T>::value_type&
212 repeat_view<T>::stored_value() const noexcept
213 {
214 return storage_traits::get(m_value);
215 }
216
217 template <typename T>
218 constexpr auto repeat_view<T>::begin() const noexcept -> const_iterator
219 {
220 return const_iterator(stored_value(), 0);
221 }
222
223 template <typename T>
224 constexpr auto repeat_view<T>::end() const noexcept -> const_iterator
225 {
226 return const_iterator(stored_value(), m_count);
227 }
228
229 template <typename T>
230 constexpr auto repeat_view<T>::cbegin() const noexcept -> const_iterator
231 {
232 return const_iterator(stored_value(), 0);
233 }
234
235 template <typename T>
236 constexpr auto repeat_view<T>::cend() const noexcept -> const_iterator
237 {
238 return const_iterator(stored_value(), m_count);
239 }
240
241 template <typename T>
242 [[nodiscard]] constexpr size_t repeat_view<T>::size() const noexcept
243 {
244 return m_count;
245 }
246
247}
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
A view that repeats a value a given number of times.
constexpr size_t size() const noexcept
typename storage_traits::value_type value_type
constexpr const_iterator begin() const noexcept
detail::repeat_view_storage_traits< T > storage_traits
constexpr repeat_view(typename storage_traits::constructor_reference value, size_t count) noexcept(std::is_nothrow_constructible_v< typename storage_traits::storage_type, typename storage_traits::constructor_reference >)
Constructs a repeat_view.
constexpr const_iterator cend() const noexcept
constexpr const_iterator cbegin() const noexcept
repeat_view< T > self_type
repeat_view_iterator< value_type > const_iterator
constexpr const_iterator end() const noexcept
repeat_view(T &, size_t) -> repeat_view< T & >
Extensions to the C++ standard library.
std::reference_wrapper< value_type > storage_type
static constexpr const value_type & get(const storage_type &value) noexcept
static constexpr const value_type & get(const storage_type &value) noexcept