sparrow
2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Toggle main menu visibility
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
22
#include "
sparrow/utils/iterator.hpp
"
23
24
namespace
sparrow
25
{
26
namespace
detail
27
{
28
template
<
typename
T>
29
struct
repeat_view_storage_traits
30
{
31
using
value_type
= T;
32
using
storage_type
= T;
33
using
constructor_reference
=
const
value_type
&;
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>
42
struct
repeat_view_storage_traits
<T&>
43
{
44
using
value_type
= std::remove_reference_t<T>;
45
using
storage_type
= std::reference_wrapper<value_type>;
46
using
constructor_reference
=
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>
57
class
repeat_view_iterator
58
:
public
sparrow::iterator_base
<repeat_view_iterator<T>, const T, std::random_access_iterator_tag>
59
{
60
public
:
61
62
using
self_type
=
repeat_view_iterator<T>
;
63
using
base_type
=
sparrow::iterator_base<self_type, const T, std::random_access_iterator_tag>
;
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
103
using
storage_traits
=
detail::repeat_view_storage_traits<T>
;
104
using
value_type
=
typename
storage_traits::value_type
;
105
using
self_type
=
repeat_view<T>
;
106
using
const_iterator
=
repeat_view_iterator<value_type>
;
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
Definition
iterator.hpp:381
sparrow::repeat_view_iterator
Definition
repeat_container.hpp:59
sparrow::repeat_view_iterator::base_type
sparrow::iterator_base< self_type, const T, std::random_access_iterator_tag > base_type
Definition
repeat_container.hpp:63
sparrow::repeat_view_iterator::repeat_view_iterator
constexpr repeat_view_iterator(const T &value, size_t index)
Constructs a repeat_view_iterator.
Definition
repeat_container.hpp:143
sparrow::repeat_view_iterator::self_type
repeat_view_iterator< T > self_type
Definition
repeat_container.hpp:62
sparrow::repeat_view_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition
repeat_container.hpp:65
sparrow::repeat_view_iterator::value_type
T value_type
Definition
repeat_container.hpp:66
sparrow::repeat_view_iterator< value_type >::iterator_access
friend class iterator_access
Definition
repeat_container.hpp:92
sparrow::repeat_view_iterator::reference
typename base_type::reference reference
Definition
repeat_container.hpp:67
sparrow::repeat_view_iterator::repeat_view_iterator
constexpr repeat_view_iterator()=default
sparrow::repeat_view_iterator::difference_type
typename base_type::difference_type difference_type
Definition
repeat_container.hpp:68
sparrow::repeat_view
A view that repeats a value a given number of times.
Definition
repeat_container.hpp:100
sparrow::repeat_view::size
constexpr size_t size() const noexcept
Definition
repeat_container.hpp:242
sparrow::repeat_view::value_type
typename storage_traits::value_type value_type
Definition
repeat_container.hpp:104
sparrow::repeat_view::begin
constexpr const_iterator begin() const noexcept
Definition
repeat_container.hpp:218
sparrow::repeat_view::storage_traits
detail::repeat_view_storage_traits< T > storage_traits
Definition
repeat_container.hpp:103
sparrow::repeat_view::repeat_view
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.
Definition
repeat_container.hpp:192
sparrow::repeat_view::cend
constexpr const_iterator cend() const noexcept
Definition
repeat_container.hpp:236
sparrow::repeat_view::cbegin
constexpr const_iterator cbegin() const noexcept
Definition
repeat_container.hpp:230
sparrow::repeat_view::self_type
repeat_view< T > self_type
Definition
repeat_container.hpp:105
sparrow::repeat_view::const_iterator
repeat_view_iterator< value_type > const_iterator
Definition
repeat_container.hpp:106
sparrow::repeat_view::end
constexpr const_iterator end() const noexcept
Definition
repeat_container.hpp:224
iterator.hpp
sparrow::detail
Definition
dynamic_bitset.hpp:391
sparrow::ranges
Definition
ranges.hpp:88
sparrow
Definition
array.hpp:21
sparrow::repeat_view
repeat_view(T &, size_t) -> repeat_view< T & >
std
Extensions to the C++ standard library.
Definition
float16_t.hpp:1916
sparrow::detail::repeat_view_storage_traits< T & >::constructor_reference
value_type & constructor_reference
Definition
repeat_container.hpp:46
sparrow::detail::repeat_view_storage_traits< T & >::value_type
std::remove_reference_t< T > value_type
Definition
repeat_container.hpp:44
sparrow::detail::repeat_view_storage_traits< T & >::storage_type
std::reference_wrapper< value_type > storage_type
Definition
repeat_container.hpp:45
sparrow::detail::repeat_view_storage_traits< T & >::get
static constexpr const value_type & get(const storage_type &value) noexcept
Definition
repeat_container.hpp:48
sparrow::detail::repeat_view_storage_traits
Definition
repeat_container.hpp:30
sparrow::detail::repeat_view_storage_traits::value_type
T value_type
Definition
repeat_container.hpp:31
sparrow::detail::repeat_view_storage_traits::get
static constexpr const value_type & get(const storage_type &value) noexcept
Definition
repeat_container.hpp:35
sparrow::detail::repeat_view_storage_traits::storage_type
T storage_type
Definition
repeat_container.hpp:32
sparrow::detail::repeat_view_storage_traits::constructor_reference
const value_type & constructor_reference
Definition
repeat_container.hpp:33
sparrow
utils
repeat_container.hpp
Generated by
1.17.0