150
|
1 // -*- C++ -*-
|
|
2 //===------------------------------ span ---------------------------------===//
|
|
3 //
|
|
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
5 // See https://llvm.org/LICENSE.txt for license information.
|
|
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
7 //
|
|
8 //===---------------------------------------------------------------------===//
|
|
9
|
|
10 #ifndef _LIBCPP_SPAN
|
|
11 #define _LIBCPP_SPAN
|
|
12
|
|
13 /*
|
|
14 span synopsis
|
|
15
|
|
16 namespace std {
|
|
17
|
|
18 // constants
|
|
19 inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
|
|
20
|
|
21 // [views.span], class template span
|
|
22 template <class ElementType, size_t Extent = dynamic_extent>
|
|
23 class span;
|
|
24
|
|
25 // [span.objectrep], views of object representation
|
|
26 template <class ElementType, size_t Extent>
|
|
27 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
|
|
28 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
|
|
29
|
|
30 template <class ElementType, size_t Extent>
|
|
31 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
|
|
32 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
|
|
33
|
|
34
|
|
35 namespace std {
|
|
36 template <class ElementType, size_t Extent = dynamic_extent>
|
|
37 class span {
|
|
38 public:
|
|
39 // constants and types
|
|
40 using element_type = ElementType;
|
|
41 using value_type = remove_cv_t<ElementType>;
|
|
42 using size_type = size_t;
|
|
43 using difference_type = ptrdiff_t;
|
|
44 using pointer = element_type*;
|
|
45 using const_pointer = const element_type*;
|
|
46 using reference = element_type&;
|
|
47 using const_reference = const element_type&;
|
|
48 using iterator = implementation-defined;
|
|
49 using reverse_iterator = std::reverse_iterator<iterator>;
|
|
50 static constexpr size_type extent = Extent;
|
|
51
|
|
52 // [span.cons], span constructors, copy, assignment, and destructor
|
|
53 constexpr span() noexcept;
|
173
|
54 constexpr explicit(Extent != dynamic_extent) span(pointer ptr, size_type count);
|
|
55 constexpr explicit(Extent != dynamic_extent) span(pointer firstElem, pointer lastElem);
|
150
|
56 template <size_t N>
|
|
57 constexpr span(element_type (&arr)[N]) noexcept;
|
|
58 template <size_t N>
|
|
59 constexpr span(array<value_type, N>& arr) noexcept;
|
|
60 template <size_t N>
|
|
61 constexpr span(const array<value_type, N>& arr) noexcept;
|
|
62 template <class Container>
|
173
|
63 constexpr explicit(Extent != dynamic_extent) span(Container& cont);
|
150
|
64 template <class Container>
|
173
|
65 constexpr explicit(Extent != dynamic_extent) span(const Container& cont);
|
150
|
66 constexpr span(const span& other) noexcept = default;
|
|
67 template <class OtherElementType, size_t OtherExtent>
|
173
|
68 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
|
150
|
69 ~span() noexcept = default;
|
|
70 constexpr span& operator=(const span& other) noexcept = default;
|
|
71
|
|
72 // [span.sub], span subviews
|
|
73 template <size_t Count>
|
|
74 constexpr span<element_type, Count> first() const;
|
|
75 template <size_t Count>
|
|
76 constexpr span<element_type, Count> last() const;
|
|
77 template <size_t Offset, size_t Count = dynamic_extent>
|
|
78 constexpr span<element_type, see below> subspan() const;
|
|
79
|
|
80 constexpr span<element_type, dynamic_extent> first(size_type count) const;
|
|
81 constexpr span<element_type, dynamic_extent> last(size_type count) const;
|
|
82 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
|
|
83
|
|
84 // [span.obs], span observers
|
|
85 constexpr size_type size() const noexcept;
|
|
86 constexpr size_type size_bytes() const noexcept;
|
|
87 constexpr bool empty() const noexcept;
|
|
88
|
|
89 // [span.elem], span element access
|
|
90 constexpr reference operator[](size_type idx) const;
|
|
91 constexpr reference front() const;
|
|
92 constexpr reference back() const;
|
|
93 constexpr pointer data() const noexcept;
|
|
94
|
|
95 // [span.iterators], span iterator support
|
|
96 constexpr iterator begin() const noexcept;
|
|
97 constexpr iterator end() const noexcept;
|
|
98 constexpr reverse_iterator rbegin() const noexcept;
|
|
99 constexpr reverse_iterator rend() const noexcept;
|
|
100
|
|
101 private:
|
|
102 pointer data_; // exposition only
|
|
103 size_type size_; // exposition only
|
|
104 };
|
|
105
|
|
106 template<class T, size_t N>
|
|
107 span(T (&)[N]) -> span<T, N>;
|
|
108
|
|
109 template<class T, size_t N>
|
|
110 span(array<T, N>&) -> span<T, N>;
|
|
111
|
|
112 template<class T, size_t N>
|
|
113 span(const array<T, N>&) -> span<const T, N>;
|
|
114
|
|
115 template<class Container>
|
|
116 span(Container&) -> span<typename Container::value_type>;
|
|
117
|
|
118 template<class Container>
|
|
119 span(const Container&) -> span<const typename Container::value_type>;
|
|
120
|
|
121 } // namespace std
|
|
122
|
|
123 */
|
|
124
|
|
125 #include <__config>
|
|
126 #include <array> // for array
|
|
127 #include <cstddef> // for byte
|
|
128 #include <iterator> // for iterators
|
|
129 #include <type_traits> // for remove_cv, etc
|
|
130
|
|
131 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
132 #pragma GCC system_header
|
|
133 #endif
|
|
134
|
|
135 _LIBCPP_BEGIN_NAMESPACE_STD
|
|
136
|
|
137 #if _LIBCPP_STD_VER > 17
|
|
138
|
|
139 inline constexpr size_t dynamic_extent = (numeric_limits<size_t>::max)();
|
|
140 template <typename _Tp, size_t _Extent = dynamic_extent> class span;
|
|
141
|
|
142
|
|
143 template <class _Tp>
|
|
144 struct __is_span_impl : public false_type {};
|
|
145
|
|
146 template <class _Tp, size_t _Extent>
|
|
147 struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
|
|
148
|
|
149 template <class _Tp>
|
|
150 struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
|
|
151
|
|
152 template <class _Tp>
|
|
153 struct __is_std_array_impl : public false_type {};
|
|
154
|
|
155 template <class _Tp, size_t _Sz>
|
|
156 struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
|
|
157
|
|
158 template <class _Tp>
|
|
159 struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
|
|
160
|
|
161 template <class _Tp, class _ElementType, class = void>
|
|
162 struct __is_span_compatible_container : public false_type {};
|
|
163
|
|
164 template <class _Tp, class _ElementType>
|
|
165 struct __is_span_compatible_container<_Tp, _ElementType,
|
|
166 void_t<
|
|
167 // is not a specialization of span
|
|
168 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
|
|
169 // is not a specialization of array
|
|
170 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
|
|
171 // is_array_v<Container> is false,
|
|
172 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
|
|
173 // data(cont) and size(cont) are well formed
|
|
174 decltype(data(declval<_Tp>())),
|
|
175 decltype(size(declval<_Tp>())),
|
|
176 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
|
|
177 typename enable_if<
|
|
178 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
|
|
179 _ElementType(*)[]>,
|
|
180 nullptr_t>::type
|
|
181 >>
|
|
182 : public true_type {};
|
|
183
|
|
184
|
|
185 template <typename _Tp, size_t _Extent>
|
|
186 class _LIBCPP_TEMPLATE_VIS span {
|
|
187 public:
|
|
188 // constants and types
|
|
189 using element_type = _Tp;
|
|
190 using value_type = remove_cv_t<_Tp>;
|
|
191 using size_type = size_t;
|
|
192 using difference_type = ptrdiff_t;
|
|
193 using pointer = _Tp *;
|
|
194 using const_pointer = const _Tp *;
|
|
195 using reference = _Tp &;
|
|
196 using const_reference = const _Tp &;
|
|
197 using iterator = __wrap_iter<pointer>;
|
|
198 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
|
|
199
|
|
200 static constexpr size_type extent = _Extent;
|
|
201
|
|
202 // [span.cons], span constructors, copy, assignment, and destructor
|
173
|
203 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
|
|
204 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
|
150
|
205
|
|
206 constexpr span (const span&) noexcept = default;
|
|
207 constexpr span& operator=(const span&) noexcept = default;
|
|
208
|
173
|
209 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
|
150
|
210 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
|
173
|
211 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
|
150
|
212 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
|
|
213
|
|
214 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
|
173
|
215
|
|
216 template <class _OtherElementType,
|
|
217 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
|
|
218 _LIBCPP_INLINE_VISIBILITY
|
|
219 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
|
|
220
|
|
221 template <class _OtherElementType,
|
|
222 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
|
|
223 _LIBCPP_INLINE_VISIBILITY
|
|
224 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
|
|
225
|
|
226 template <class _Container>
|
|
227 _LIBCPP_INLINE_VISIBILITY
|
|
228 constexpr explicit span( _Container& __c,
|
|
229 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
|
|
230 : __data{_VSTD::data(__c)} {
|
|
231 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
|
|
232 }
|
|
233
|
|
234 template <class _Container>
|
|
235 _LIBCPP_INLINE_VISIBILITY
|
|
236 constexpr explicit span(const _Container& __c,
|
|
237 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
|
|
238 : __data{_VSTD::data(__c)} {
|
|
239 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
|
|
240 }
|
150
|
241
|
|
242 template <class _OtherElementType>
|
|
243 _LIBCPP_INLINE_VISIBILITY
|
|
244 constexpr span(const span<_OtherElementType, _Extent>& __other,
|
|
245 enable_if_t<
|
|
246 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
|
|
247 nullptr_t> = nullptr)
|
|
248 : __data{__other.data()} {}
|
|
249
|
|
250 template <class _OtherElementType>
|
|
251 _LIBCPP_INLINE_VISIBILITY
|
173
|
252 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
|
150
|
253 enable_if_t<
|
|
254 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
|
|
255 nullptr_t> = nullptr) noexcept
|
|
256 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
|
|
257
|
|
258
|
|
259 // ~span() noexcept = default;
|
|
260
|
|
261 template <size_t _Count>
|
|
262 _LIBCPP_INLINE_VISIBILITY
|
|
263 constexpr span<element_type, _Count> first() const noexcept
|
|
264 {
|
|
265 static_assert(_Count <= _Extent, "Count out of range in span::first()");
|
173
|
266 return span<element_type, _Count>{data(), _Count};
|
150
|
267 }
|
|
268
|
|
269 template <size_t _Count>
|
|
270 _LIBCPP_INLINE_VISIBILITY
|
|
271 constexpr span<element_type, _Count> last() const noexcept
|
|
272 {
|
|
273 static_assert(_Count <= _Extent, "Count out of range in span::last()");
|
173
|
274 return span<element_type, _Count>{data() + size() - _Count, _Count};
|
150
|
275 }
|
|
276
|
|
277 _LIBCPP_INLINE_VISIBILITY
|
|
278 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
|
|
279 {
|
|
280 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
|
|
281 return {data(), __count};
|
|
282 }
|
|
283
|
|
284 _LIBCPP_INLINE_VISIBILITY
|
|
285 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
|
|
286 {
|
|
287 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
|
|
288 return {data() + size() - __count, __count};
|
|
289 }
|
|
290
|
|
291 template <size_t _Offset, size_t _Count = dynamic_extent>
|
|
292 _LIBCPP_INLINE_VISIBILITY
|
|
293 constexpr auto subspan() const noexcept
|
|
294 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
|
|
295 {
|
|
296 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
|
|
297 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
|
173
|
298
|
|
299 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
|
|
300 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
|
150
|
301 }
|
|
302
|
|
303
|
|
304 _LIBCPP_INLINE_VISIBILITY
|
|
305 constexpr span<element_type, dynamic_extent>
|
|
306 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
|
|
307 {
|
|
308 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
|
|
309 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
|
|
310 if (__count == dynamic_extent)
|
|
311 return {data() + __offset, size() - __offset};
|
|
312 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
|
|
313 return {data() + __offset, __count};
|
|
314 }
|
|
315
|
|
316 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
|
|
317 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
|
|
318 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
|
|
319
|
|
320 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
|
|
321 {
|
|
322 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
|
|
323 return __data[__idx];
|
|
324 }
|
|
325
|
|
326 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
|
|
327 {
|
173
|
328 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
|
150
|
329 return __data[0];
|
|
330 }
|
|
331
|
|
332 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
|
|
333 {
|
173
|
334 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
|
150
|
335 return __data[size()-1];
|
|
336 }
|
|
337
|
|
338 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
|
|
339
|
|
340 // [span.iter], span iterator support
|
|
341 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
|
|
342 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
|
|
343 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
|
|
344 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
|
|
345
|
|
346 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
|
173
|
347 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
|
150
|
348
|
|
349 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
|
173
|
350 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
|
150
|
351
|
|
352 private:
|
|
353 pointer __data;
|
|
354
|
|
355 };
|
|
356
|
|
357
|
|
358 template <typename _Tp>
|
|
359 class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
|
|
360 private:
|
|
361
|
|
362 public:
|
|
363 // constants and types
|
|
364 using element_type = _Tp;
|
|
365 using value_type = remove_cv_t<_Tp>;
|
|
366 using size_type = size_t;
|
|
367 using difference_type = ptrdiff_t;
|
|
368 using pointer = _Tp *;
|
|
369 using const_pointer = const _Tp *;
|
|
370 using reference = _Tp &;
|
|
371 using const_reference = const _Tp &;
|
|
372 using iterator = __wrap_iter<pointer>;
|
|
373 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
|
|
374
|
|
375 static constexpr size_type extent = dynamic_extent;
|
|
376
|
|
377 // [span.cons], span constructors, copy, assignment, and destructor
|
|
378 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
|
|
379
|
|
380 constexpr span (const span&) noexcept = default;
|
|
381 constexpr span& operator=(const span&) noexcept = default;
|
|
382
|
|
383 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
|
|
384 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{static_cast<size_t>(distance(__f, __l))} {}
|
|
385
|
|
386 template <size_t _Sz>
|
|
387 _LIBCPP_INLINE_VISIBILITY
|
|
388 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
|
|
389
|
173
|
390 template <class _OtherElementType, size_t _Sz,
|
|
391 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
|
150
|
392 _LIBCPP_INLINE_VISIBILITY
|
173
|
393 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
|
150
|
394
|
173
|
395 template <class _OtherElementType, size_t _Sz,
|
|
396 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
|
150
|
397 _LIBCPP_INLINE_VISIBILITY
|
173
|
398 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
|
150
|
399
|
|
400 template <class _Container>
|
|
401 _LIBCPP_INLINE_VISIBILITY
|
|
402 constexpr span( _Container& __c,
|
|
403 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
|
|
404 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
|
|
405
|
|
406 template <class _Container>
|
|
407 _LIBCPP_INLINE_VISIBILITY
|
|
408 constexpr span(const _Container& __c,
|
|
409 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
|
|
410 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
|
|
411
|
|
412
|
|
413 template <class _OtherElementType, size_t _OtherExtent>
|
|
414 _LIBCPP_INLINE_VISIBILITY
|
|
415 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
|
|
416 enable_if_t<
|
|
417 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
|
|
418 nullptr_t> = nullptr) noexcept
|
|
419 : __data{__other.data()}, __size{__other.size()} {}
|
|
420
|
|
421 // ~span() noexcept = default;
|
|
422
|
|
423 template <size_t _Count>
|
|
424 _LIBCPP_INLINE_VISIBILITY
|
|
425 constexpr span<element_type, _Count> first() const noexcept
|
|
426 {
|
|
427 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
|
173
|
428 return span<element_type, _Count>{data(), _Count};
|
150
|
429 }
|
|
430
|
|
431 template <size_t _Count>
|
|
432 _LIBCPP_INLINE_VISIBILITY
|
|
433 constexpr span<element_type, _Count> last() const noexcept
|
|
434 {
|
|
435 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
|
173
|
436 return span<element_type, _Count>{data() + size() - _Count, _Count};
|
150
|
437 }
|
|
438
|
|
439 _LIBCPP_INLINE_VISIBILITY
|
|
440 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
|
|
441 {
|
|
442 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
|
|
443 return {data(), __count};
|
|
444 }
|
|
445
|
|
446 _LIBCPP_INLINE_VISIBILITY
|
|
447 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
|
|
448 {
|
|
449 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
|
|
450 return {data() + size() - __count, __count};
|
|
451 }
|
|
452
|
|
453 template <size_t _Offset, size_t _Count = dynamic_extent>
|
|
454 _LIBCPP_INLINE_VISIBILITY
|
|
455 constexpr span<element_type, _Count> subspan() const noexcept
|
|
456 {
|
|
457 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
|
|
458 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
|
173
|
459 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
|
150
|
460 }
|
|
461
|
|
462 constexpr span<element_type, dynamic_extent>
|
|
463 _LIBCPP_INLINE_VISIBILITY
|
|
464 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
|
|
465 {
|
|
466 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
|
|
467 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
|
|
468 if (__count == dynamic_extent)
|
|
469 return {data() + __offset, size() - __offset};
|
|
470 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
|
|
471 return {data() + __offset, __count};
|
|
472 }
|
|
473
|
|
474 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
|
|
475 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
|
|
476 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
|
|
477
|
|
478 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
|
|
479 {
|
|
480 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
|
|
481 return __data[__idx];
|
|
482 }
|
|
483
|
|
484 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
|
|
485 {
|
|
486 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
|
|
487 return __data[0];
|
|
488 }
|
|
489
|
|
490 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
|
|
491 {
|
|
492 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
|
|
493 return __data[size()-1];
|
|
494 }
|
|
495
|
|
496
|
|
497 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
|
|
498
|
|
499 // [span.iter], span iterator support
|
|
500 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
|
|
501 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
|
|
502 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
|
|
503 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
|
|
504
|
|
505 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
|
|
506 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
|
|
507
|
|
508 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
|
|
509 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
|
|
510
|
|
511 private:
|
|
512 pointer __data;
|
|
513 size_type __size;
|
|
514 };
|
|
515
|
|
516 // as_bytes & as_writable_bytes
|
|
517 template <class _Tp, size_t _Extent>
|
|
518 _LIBCPP_INLINE_VISIBILITY
|
|
519 auto as_bytes(span<_Tp, _Extent> __s) noexcept
|
|
520 -> decltype(__s.__as_bytes())
|
|
521 { return __s.__as_bytes(); }
|
|
522
|
|
523 template <class _Tp, size_t _Extent>
|
|
524 _LIBCPP_INLINE_VISIBILITY
|
|
525 auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
|
|
526 -> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
|
|
527 { return __s.__as_writable_bytes(); }
|
|
528
|
|
529 // Deduction guides
|
|
530 template<class _Tp, size_t _Sz>
|
|
531 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
|
|
532
|
|
533 template<class _Tp, size_t _Sz>
|
|
534 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
|
|
535
|
|
536 template<class _Tp, size_t _Sz>
|
|
537 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
|
|
538
|
|
539 template<class _Container>
|
|
540 span(_Container&) -> span<typename _Container::value_type>;
|
|
541
|
|
542 template<class _Container>
|
|
543 span(const _Container&) -> span<const typename _Container::value_type>;
|
|
544
|
|
545 #endif // _LIBCPP_STD_VER > 17
|
|
546
|
|
547 _LIBCPP_END_NAMESPACE_STD
|
|
548
|
|
549 #endif // _LIBCPP_SPAN
|