1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
#ifndef AL_SPAN_H
#define AL_SPAN_H
#include <array>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include "almalloc.h"
#include "altraits.h"
namespace al {
inline constexpr size_t dynamic_extent{static_cast<size_t>(-1)};
template<typename T, size_t E=dynamic_extent>
class span;
namespace detail_ {
template<typename T>
struct is_span_ : std::false_type { };
template<typename T, size_t E>
struct is_span_<span<T,E>> : std::true_type { };
template<typename T>
inline constexpr bool is_span_v = is_span_<std::remove_cv_t<T>>::value;
template<typename T>
struct is_std_array_ : std::false_type { };
template<typename T, size_t N>
struct is_std_array_<std::array<T,N>> : std::true_type { };
template<typename T>
inline constexpr bool is_std_array_v = is_std_array_<std::remove_cv_t<T>>::value;
template<typename T, typename = void>
inline constexpr bool has_size_and_data = false;
template<typename T>
inline constexpr bool has_size_and_data<T,
std::void_t<decltype(std::size(std::declval<T>())),decltype(std::data(std::declval<T>()))>>
= true;
template<typename C>
inline constexpr bool is_valid_container_type = !is_span_v<C> && !is_std_array_v<C>
&& !std::is_array<C>::value && has_size_and_data<C>;
template<typename T, typename U>
inline constexpr bool is_array_compatible = std::is_convertible<T(*)[],U(*)[]>::value; /* NOLINT(*-avoid-c-arrays) */
template<typename C, typename T>
inline constexpr bool is_valid_container = is_valid_container_type<C>
&& is_array_compatible<std::remove_pointer_t<decltype(std::data(std::declval<C&>()))>,T>;
} // namespace detail_
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true
template<typename T, size_t E>
class span {
public:
using element_type = T;
using value_type = std::remove_cv_t<T>;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr size_t extent{E};
template<bool is0=(extent == 0), REQUIRES(is0)>
constexpr span() noexcept { }
template<typename U>
constexpr explicit span(U iter, index_type) : mData{::al::to_address(iter)} { }
template<typename U, typename V, REQUIRES(!std::is_convertible<V,size_t>::value)>
constexpr explicit span(U first, V) : mData{::al::to_address(first)}
{ }
constexpr span(type_identity_t<element_type> (&arr)[E]) noexcept /* NOLINT(*-avoid-c-arrays) */
: span{std::data(arr), std::size(arr)}
{ }
constexpr span(std::array<value_type,E> &arr) noexcept
: span{std::data(arr), std::size(arr)}
{ }
template<typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,E> &arr) noexcept
: span{std::data(arr), std::size(arr)}
{ }
template<typename U, REQUIRES(detail_::is_valid_container<U, element_type>)>
constexpr explicit span(U&& cont) : span{std::data(cont), std::size(cont)} { }
template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
&& detail_::is_array_compatible<U,element_type> && N == dynamic_extent)>
constexpr explicit span(const span<U,N> &span_) noexcept
: span{std::data(span_), std::size(span_)}
{ }
template<typename U, index_type N, REQUIRES(!std::is_same<element_type,U>::value
&& detail_::is_array_compatible<U,element_type> && N == extent)>
constexpr span(const span<U,N> &span_) noexcept : span{std::data(span_), std::size(span_)} { }
constexpr span(const span&) noexcept = default;
constexpr span& operator=(const span &rhs) noexcept = default;
[[nodiscard]] constexpr auto front() const -> reference { return mData[0]; }
[[nodiscard]] constexpr auto back() const -> reference { return mData[E-1]; }
[[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; }
[[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; }
[[nodiscard]] constexpr auto size() const noexcept -> index_type { return E; }
[[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return E * sizeof(value_type); }
[[nodiscard]] constexpr auto empty() const noexcept -> bool { return E == 0; }
[[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; }
[[nodiscard]] constexpr auto end() const noexcept -> iterator { return mData+E; }
[[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; }
[[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mData+E; }
[[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; }
[[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; }
[[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator
{ return const_reverse_iterator{cend()}; }
[[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator
{ return const_reverse_iterator{cbegin()}; }
template<size_t C>
[[nodiscard]] constexpr auto first() const -> span<element_type,C>
{
static_assert(E >= C, "New size exceeds original capacity");
return span<element_type,C>{mData, C};
}
template<size_t C>
[[nodiscard]] constexpr auto last() const -> span<element_type,C>
{
static_assert(E >= C, "New size exceeds original capacity");
return span<element_type,C>{mData+(E-C), C};
}
template<size_t O, size_t C>
[[nodiscard]] constexpr auto subspan() const -> std::enable_if_t<C!=dynamic_extent,span<element_type,C>>
{
static_assert(E >= O, "Offset exceeds extent");
static_assert(E-O >= C, "New size exceeds original capacity");
return span<element_type,C>{mData+O, C};
}
template<size_t O, size_t C=dynamic_extent>
[[nodiscard]] constexpr auto subspan() const -> std::enable_if_t<C==dynamic_extent,span<element_type,E-O>>
{
static_assert(E >= O, "Offset exceeds extent");
return span<element_type,E-O>{mData+O, E-O};
}
/* NOTE: Can't declare objects of a specialized template class prior to
* defining the specialization. As a result, these methods need to be
* defined later.
*/
[[nodiscard]] constexpr auto first(size_t count) const -> span<element_type,dynamic_extent>;
[[nodiscard]] constexpr auto last(size_t count) const -> span<element_type,dynamic_extent>;
[[nodiscard]] constexpr auto subspan(size_t offset,
size_t count=dynamic_extent) const -> span<element_type,dynamic_extent>;
private:
pointer mData{nullptr};
};
template<typename T>
class span<T,dynamic_extent> {
public:
using element_type = T;
using value_type = std::remove_cv_t<T>;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr size_t extent{dynamic_extent};
constexpr span() noexcept = default;
template<typename U>
constexpr span(U iter, index_type count) : mData{::al::to_address(iter)}, mDataLength{count}
{ }
template<typename U, typename V, REQUIRES(!std::is_convertible<V,size_t>::value)>
constexpr span(U first, V last) : span{::al::to_address(first), static_cast<size_t>(last - first)}
{ }
template<size_t N>
constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept /* NOLINT(*-avoid-c-arrays) */
: span{std::data(arr), std::size(arr)}
{ }
template<size_t N>
constexpr span(std::array<value_type,N> &arr) noexcept
: span{std::data(arr), std::size(arr)}
{ }
template<size_t N, typename U=T, REQUIRES(std::is_const<U>::value)>
constexpr span(const std::array<value_type,N> &arr) noexcept
: span{std::data(arr), std::size(arr)}
{ }
template<typename U, REQUIRES(detail_::is_valid_container<U, element_type>)>
constexpr span(U&& cont) : span{std::data(cont), std::size(cont)} { }
template<typename U, size_t N, REQUIRES((!std::is_same<element_type,U>::value || extent != N)
&& detail_::is_array_compatible<U,element_type>)>
constexpr span(const span<U,N> &span_) noexcept : span{std::data(span_), std::size(span_)} { }
constexpr span(const span&) noexcept = default;
constexpr span& operator=(const span &rhs) noexcept = default;
[[nodiscard]] constexpr auto front() const -> reference { return mData[0]; }
[[nodiscard]] constexpr auto back() const -> reference { return mData[mDataLength-1]; }
[[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; }
[[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; }
[[nodiscard]] constexpr auto size() const noexcept -> index_type { return mDataLength; }
[[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return mDataLength * sizeof(value_type); }
[[nodiscard]] constexpr auto empty() const noexcept -> bool { return mDataLength == 0; }
[[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; }
[[nodiscard]] constexpr auto end() const noexcept -> iterator { return mData+mDataLength; }
[[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; }
[[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mData+mDataLength; }
[[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; }
[[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; }
[[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator
{ return const_reverse_iterator{cend()}; }
[[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator
{ return const_reverse_iterator{cbegin()}; }
template<size_t C>
[[nodiscard]] constexpr auto first() const -> span<element_type,C>
{
if(C > mDataLength)
throw std::out_of_range{"Subspan count out of range"};
return span<element_type,C>{mData, C};
}
[[nodiscard]] constexpr auto first(size_t count) const -> span
{
if(count > mDataLength)
throw std::out_of_range{"Subspan count out of range"};
return span{mData, count};
}
template<size_t C>
[[nodiscard]] constexpr auto last() const -> span<element_type,C>
{
if(C > mDataLength)
throw std::out_of_range{"Subspan count out of range"};
return span<element_type,C>{mData+mDataLength-C, C};
}
[[nodiscard]] constexpr auto last(size_t count) const -> span
{
if(count > mDataLength)
throw std::out_of_range{"Subspan count out of range"};
return span{mData+mDataLength-count, count};
}
template<size_t O, size_t C>
[[nodiscard]] constexpr auto subspan() const -> std::enable_if_t<C!=dynamic_extent,span<element_type,C>>
{
if(O > mDataLength)
throw std::out_of_range{"Subspan offset out of range"};
if(C > mDataLength-O)
throw std::out_of_range{"Subspan length out of range"};
return span<element_type,C>{mData+O, C};
}
template<size_t O, size_t C=dynamic_extent>
[[nodiscard]] constexpr auto subspan() const -> std::enable_if_t<C==dynamic_extent,span<element_type,C>>
{
if(O > mDataLength)
throw std::out_of_range{"Subspan offset out of range"};
return span<element_type,C>{mData+O, mDataLength-O};
}
[[nodiscard]] constexpr auto subspan(size_t offset, size_t count=dynamic_extent) const -> span
{
if(offset > mDataLength)
throw std::out_of_range{"Subspan offset out of range"};
if(count != dynamic_extent)
{
if(count > mDataLength-offset)
throw std::out_of_range{"Subspan length out of range"};
return span{mData+offset, count};
}
return span{mData+offset, mDataLength-offset};
}
private:
pointer mData{nullptr};
index_type mDataLength{0};
};
template<typename T, size_t E>
[[nodiscard]] constexpr inline auto span<T,E>::first(size_t count) const -> span<element_type,dynamic_extent>
{
if(count > size())
throw std::out_of_range{"Subspan count out of range"};
return span<element_type>{mData, count};
}
template<typename T, size_t E>
[[nodiscard]] constexpr inline auto span<T,E>::last(size_t count) const -> span<element_type,dynamic_extent>
{
if(count > size())
throw std::out_of_range{"Subspan count out of range"};
return span<element_type>{mData+size()-count, count};
}
template<typename T, size_t E>
[[nodiscard]] constexpr inline auto span<T,E>::subspan(size_t offset, size_t count) const
-> span<element_type,dynamic_extent>
{
if(offset > size())
throw std::out_of_range{"Subspan offset out of range"};
if(count != dynamic_extent)
{
if(count > size()-offset)
throw std::out_of_range{"Subspan length out of range"};
return span{mData+offset, count};
}
return span{mData+offset, size()-offset};
}
template<typename T, typename EndOrSize>
span(T, EndOrSize) -> span<std::remove_reference_t<decltype(*std::declval<T&>())>>;
template<typename T, std::size_t N>
span(T (&)[N]) -> span<T, N>; /* NOLINT(*-avoid-c-arrays) */
template<typename T, std::size_t N>
span(std::array<T, N>&) -> span<T, N>;
template<typename T, std::size_t N>
span(const std::array<T, N>&) -> span<const T, N>;
template<typename C, REQUIRES(detail_::is_valid_container_type<C>)>
span(C&&) -> span<std::remove_pointer_t<decltype(std::data(std::declval<C&>()))>>;
#undef REQUIRES
} // namespace al
#endif /* AL_SPAN_H */
|