forked from intel/cpp-std-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan.hpp
More file actions
287 lines (245 loc) · 10.6 KB
/
span.hpp
File metadata and controls
287 lines (245 loc) · 10.6 KB
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
#pragma once
#include <stdx/bit.hpp>
#include <stdx/compiler.hpp>
#include <stdx/iterator.hpp>
#include <stdx/memory.hpp>
#include <stdx/type_traits.hpp>
#include <algorithm>
#include <array>
#include <cstddef>
#include <iterator>
#include <limits>
#include <type_traits>
// NOLINTBEGIN(modernize-use-constraints)
namespace stdx {
inline namespace v1 {
constexpr static auto dynamic_extent = std::numeric_limits<std::size_t>::max();
namespace detail {
template <typename T, std::size_t N> struct span_base {
constexpr span_base() = default;
template <typename It, typename SizeOrEnd>
constexpr explicit span_base(It, SizeOrEnd) {}
constexpr static std::integral_constant<std::size_t, N> size{};
constexpr static std::integral_constant<std::size_t, N * sizeof(T)>
size_bytes{};
constexpr static std::bool_constant<N == 0> empty{};
};
template <typename T> class span_base<T, dynamic_extent> {
std::size_t sz{};
public:
constexpr span_base() = default;
template <typename It, typename SizeOrEnd,
std::enable_if_t<std::is_integral_v<SizeOrEnd>, int> = 0>
constexpr span_base(It, SizeOrEnd count)
: sz{static_cast<std::size_t>(count)} {}
template <typename It, typename SizeOrEnd,
std::enable_if_t<not std::is_integral_v<SizeOrEnd>, int> = 0>
constexpr span_base(It first, SizeOrEnd last)
: sz{static_cast<std::size_t>(std::distance(first, last))} {}
[[nodiscard]] constexpr auto size() const noexcept -> std::size_t {
return sz;
}
[[nodiscard]] constexpr auto size_bytes() const noexcept -> std::size_t {
return sz * sizeof(T);
}
[[nodiscard]] constexpr auto empty() const noexcept -> bool {
return sz == 0u;
}
};
} // namespace detail
template <typename T, std::size_t Extent = dynamic_extent>
class span : public detail::span_base<T, Extent> {
template <typename> constexpr static inline auto dependent_extent = Extent;
using base_t = detail::span_base<T, Extent>;
T *ptr{};
public:
using element_type = T;
using value_type = stdx::remove_cvref_t<T>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T *;
using const_pointer = T const *;
using reference = T &;
using const_reference = T const &;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr static inline auto extent = Extent;
constexpr span() = default;
template <typename It, typename SizeOrEnd,
std::enable_if_t<dependent_extent<It> != dynamic_extent, int> = 0>
explicit constexpr span(It first, SizeOrEnd)
: ptr{stdx::to_address(first)} {}
template <typename It, typename SizeOrEnd,
std::enable_if_t<dependent_extent<It> == dynamic_extent, int> = 0>
constexpr span(It first, SizeOrEnd sore)
: base_t{first, sore}, ptr{stdx::to_address(first)} {}
template <typename U, std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(std::array<U, N> &arr LIFETIMEBOUND) noexcept
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}
template <typename U, std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(std::array<U, N> const &arr LIFETIMEBOUND) noexcept
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}
// NOLINTNEXTLINE(*-avoid-c-arrays)
template <std::size_t N> using arr_t = type_identity_t<element_type> (&)[N];
template <std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(arr_t<N> arr LIFETIMEBOUND) noexcept
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}
template <typename R,
std::enable_if_t<dependent_extent<R> != dynamic_extent, int> = 0>
explicit constexpr span(R &&r)
: ptr{stdx::to_address(std::begin(std::forward<R>(r)))} {}
template <typename R,
std::enable_if_t<dependent_extent<R> == dynamic_extent, int> = 0>
explicit constexpr span(R &&r)
: base_t{std::begin(std::forward<R>(r)), std::end(std::forward<R>(r))},
ptr{stdx::to_address(std::begin(std::forward<R>(r)))} {}
template <class U, std::size_t N,
std::enable_if_t<dependent_extent<U> != dynamic_extent and
N == dynamic_extent,
int> = 0>
explicit constexpr span(span<U, N> const &s) noexcept : ptr{s.data()} {}
template <class U, std::size_t N,
std::enable_if_t<dependent_extent<U> == dynamic_extent or
N != dynamic_extent,
int> = 0>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(span<U, N> const &s) noexcept
: base_t{s.data(), s.size()}, ptr{s.data()} {}
[[nodiscard]] constexpr auto data() const noexcept -> pointer {
return ptr;
}
[[nodiscard]] constexpr auto begin() const noexcept -> iterator {
return ptr;
}
[[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator {
return ptr;
}
[[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator {
return std::reverse_iterator{end()};
}
[[nodiscard]] constexpr auto crbegin() const noexcept
-> const_reverse_iterator {
return std::reverse_iterator{cend()};
}
[[nodiscard]] constexpr auto end() const noexcept -> iterator {
return ptr + this->size();
}
[[nodiscard]] constexpr auto cend() const noexcept -> const_iterator {
return ptr + this->size();
}
[[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator {
return std::reverse_iterator{begin()};
}
[[nodiscard]] constexpr auto crend() const noexcept
-> const_reverse_iterator {
return std::reverse_iterator{cbegin()};
}
[[nodiscard]] constexpr auto front() const -> reference { return *begin(); }
[[nodiscard]] constexpr auto back() const -> reference {
return *std::prev(end());
}
[[nodiscard]] constexpr auto operator[](size_type idx) const -> reference {
return data()[idx];
}
template <std::size_t Count>
[[nodiscard]] constexpr auto first() const -> span<element_type, Count> {
static_assert(Count <= Extent, "first cannot form a larger span!");
return span<element_type, Count>{ptr, Count};
}
[[nodiscard]] constexpr auto first(size_type count) const
-> span<element_type, dynamic_extent> {
return {ptr, count};
}
template <std::size_t Count>
[[nodiscard]] constexpr auto last() const -> span<element_type, Count> {
static_assert(Count <= Extent, "last cannot form a larger span!");
return span<element_type, Count>{ptr + this->size() - Count, Count};
}
[[nodiscard]] constexpr auto last(size_type count) const
-> span<element_type, dynamic_extent> {
return {ptr + this->size() - count, count};
}
template <std::size_t Offset, std::size_t Count = dynamic_extent>
[[nodiscard]] constexpr auto subspan() const {
if constexpr (Count != dynamic_extent) {
static_assert(Offset <= Extent,
"subspan cannot start beyond span!");
static_assert(Count <= Extent,
"subspan cannot be longer than span!");
static_assert(Offset <= Extent - Count,
"subspan cannot end beyond span!");
return span<element_type, Count>{ptr + Offset, Count};
} else if constexpr (Extent != dynamic_extent) {
static_assert(Offset <= Extent,
"subspan cannot start beyond span!");
return span<element_type, Extent - Offset>{ptr + Offset,
Extent - Offset};
} else {
return span<element_type, dynamic_extent>{ptr + Offset,
this->size() - Offset};
}
}
[[nodiscard]] constexpr auto subspan(size_type Offset,
size_type Count = dynamic_extent) const
-> span<element_type, dynamic_extent> {
return {ptr + Offset, std::min(Count, this->size() - Offset)};
}
};
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
template <class T, std::size_t N> auto as_bytes(span<T, N> s) noexcept {
if constexpr (N == dynamic_extent) {
return span{reinterpret_cast<std::byte const *>(s.data()),
s.size_bytes()};
} else {
constexpr auto size = s.size_bytes();
return span<std::byte const, size>{
reinterpret_cast<std::byte const *>(s.data()), size};
}
}
template <class T, std::size_t N,
std::enable_if_t<not std::is_const_v<T>, int> = 0>
auto as_writable_bytes(span<T, N> s) noexcept {
if constexpr (N == dynamic_extent) {
return span{reinterpret_cast<std::byte *>(s.data()), s.size_bytes()};
} else {
constexpr auto size = s.size_bytes();
return span<std::byte, size>{reinterpret_cast<std::byte *>(s.data()),
size};
}
}
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
namespace detail {
template <typename T> using iter_reference_t = decltype(*std::declval<T &>());
template <typename T>
using iterator_t = decltype(std::begin(std::declval<T &>()));
template <typename R> using range_reference_t = iter_reference_t<iterator_t<R>>;
} // namespace detail
template <typename It, typename EndOrSize>
span(It, EndOrSize)
-> span<std::remove_reference_t<detail::iter_reference_t<It>>>;
// NOLINTNEXTLINE(*-avoid-c-arrays)
template <typename T, std::size_t N> span(T (&)[N]) -> span<T, N>;
template <typename T, std::size_t N> span(std::array<T, N> &) -> span<T, N>;
template <typename T, std::size_t N>
span(std::array<T, N> const &) -> span<T const, N>;
template <typename R>
span(R &&) -> span<std::remove_reference_t<detail::range_reference_t<R>>>;
template <typename T, std::size_t N>
constexpr auto ct_capacity_v<span<T, N>> = N;
} // namespace v1
} // namespace stdx
// NOLINTEND(modernize-use-constraints)