1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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
16namespace std {
17
18// constants
19inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
20
21template<class T>
22 concept integral-constant-like = // exposition only
23 is_integral_v<remove_cvref_t<decltype(T::value)>> &&
24 !is_same_v<bool, remove_cvref_t<decltype(T::value)>> &&
25 convertible_to<T, decltype(T::value)> &&
26 equality_comparable_with<T, decltype(T::value)> &&
27 bool_constant<T() == T::value>::value &&
28 bool_constant<static_cast<decltype(T::value)>(T()) == T::value>::value;
29
30template<class T>
31 constexpr size_t maybe-static-ext = dynamic_extent; // exposition only
32template<integral-constant-like T>
33 constexpr size_t maybe-static-ext<T> = {T::value};
34
35// [views.span], class template span
36template <class ElementType, size_t Extent = dynamic_extent>
37 class span;
38
39template<class ElementType, size_t Extent>
40 inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
41
42template<class ElementType, size_t Extent>
43 inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
44
45// [span.objectrep], views of object representation
46template <class ElementType, size_t Extent>
47 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
48 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
49
50template <class ElementType, size_t Extent>
51 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
52 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
53
54
55template <class ElementType, size_t Extent = dynamic_extent>
56class span {
57public:
58 // constants and types
59 using element_type = ElementType;
60 using value_type = remove_cv_t<ElementType>;
61 using size_type = size_t;
62 using difference_type = ptrdiff_t;
63 using pointer = element_type*;
64 using const_pointer = const element_type*;
65 using reference = element_type&;
66 using const_reference = const element_type&;
67 using iterator = implementation-defined;
68 using reverse_iterator = std::reverse_iterator<iterator>;
69 static constexpr size_type extent = Extent;
70
71 // [span.cons], span constructors, copy, assignment, and destructor
72 constexpr span() noexcept;
73 template <class It>
74 constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
75 template <class It, class End>
76 constexpr explicit(Extent != dynamic_extent) span(It first, End last);
77 template <size_t N>
78 constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
79 template <size_t N>
80 constexpr span(array<value_type, N>& arr) noexcept;
81 template <size_t N>
82 constexpr span(const array<value_type, N>& arr) noexcept;
83 template<class R>
84 constexpr explicit(Extent != dynamic_extent) span(R&& r);
85 constexpr span(const span& other) noexcept = default;
86 template <class OtherElementType, size_t OtherExtent>
87 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
88 constexpr span& operator=(const span& other) noexcept = default;
89
90 // [span.sub], span subviews
91 template <size_t Count>
92 constexpr span<element_type, Count> first() const;
93 template <size_t Count>
94 constexpr span<element_type, Count> last() const;
95 template <size_t Offset, size_t Count = dynamic_extent>
96 constexpr span<element_type, see below> subspan() const;
97
98 constexpr span<element_type, dynamic_extent> first(size_type count) const;
99 constexpr span<element_type, dynamic_extent> last(size_type count) const;
100 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
101
102 // [span.obs], span observers
103 constexpr size_type size() const noexcept;
104 constexpr size_type size_bytes() const noexcept;
105 [[nodiscard]] constexpr bool empty() const noexcept;
106
107 // [span.elem], span element access
108 constexpr reference operator[](size_type idx) const;
109 constexpr reference at(size_type idx) const; // since C++26
110 constexpr reference front() const;
111 constexpr reference back() const;
112 constexpr pointer data() const noexcept;
113
114 // [span.iterators], span iterator support
115 constexpr iterator begin() const noexcept;
116 constexpr iterator end() const noexcept;
117 constexpr reverse_iterator rbegin() const noexcept;
118 constexpr reverse_iterator rend() const noexcept;
119
120private:
121 pointer data_; // exposition only
122 size_type size_; // exposition only
123};
124
125template<class It, class EndOrSize>
126 span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>, maybe-static-ext<EndOrSize>>;
127
128template<class T, size_t N>
129 span(T (&)[N]) -> span<T, N>;
130
131template<class T, size_t N>
132 span(array<T, N>&) -> span<T, N>;
133
134template<class T, size_t N>
135 span(const array<T, N>&) -> span<const T, N>;
136
137template<class R>
138 span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
139
140} // namespace std
141
142*/
143
144#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
145# include <__cxx03/__config>
146#else
147# include <__assert>
148# include <__concepts/convertible_to.h>
149# include <__concepts/equality_comparable.h>
150# include <__config>
151# include <__cstddef/byte.h>
152# include <__cstddef/ptrdiff_t.h>
153# include <__cstddef/size_t.h>
154# include <__fwd/array.h>
155# include <__fwd/span.h>
156# include <__iterator/bounded_iter.h>
157# include <__iterator/concepts.h>
158# include <__iterator/iterator_traits.h>
159# include <__iterator/reverse_iterator.h>
160# include <__iterator/wrap_iter.h>
161# include <__memory/pointer_traits.h>
162# include <__ranges/concepts.h>
163# include <__ranges/data.h>
164# include <__ranges/enable_borrowed_range.h>
165# include <__ranges/enable_view.h>
166# include <__ranges/size.h>
167# include <__type_traits/integral_constant.h>
168# include <__type_traits/is_array.h>
169# include <__type_traits/is_const.h>
170# include <__type_traits/is_convertible.h>
171# include <__type_traits/is_integral.h>
172# include <__type_traits/is_same.h>
173# include <__type_traits/is_volatile.h>
174# include <__type_traits/remove_const.h>
175# include <__type_traits/remove_cv.h>
176# include <__type_traits/remove_cvref.h>
177# include <__type_traits/remove_reference.h>
178# include <__type_traits/type_identity.h>
179# include <__utility/forward.h>
180# include <stdexcept>
181# include <version>
182
183// standard-mandated includes
184
185// [iterator.range]
186# include <__iterator/access.h>
187# include <__iterator/data.h>
188# include <__iterator/empty.h>
189# include <__iterator/reverse_access.h>
190# include <__iterator/size.h>
191
192# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
193# pragma GCC system_header
194# endif
195
196_LIBCPP_PUSH_MACROS
197# include <__undef_macros>
198
199# if _LIBCPP_STD_VER >= 20
200
201_LIBCPP_BEGIN_NAMESPACE_STD
202
203template <class _Tp>
204struct __is_std_span : false_type {};
205
206template <class _Tp, size_t _Sz>
207struct __is_std_span<span<_Tp, _Sz>> : true_type {};
208
209template <class _Range, class _ElementType>
210concept __span_compatible_range =
211 !__is_std_span<remove_cvref_t<_Range>>::value && //
212 ranges::contiguous_range<_Range> && //
213 ranges::sized_range<_Range> && //
214 (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && //
215 !__is_std_array_v<remove_cvref_t<_Range>> && //
216 !is_array_v<remove_cvref_t<_Range>> && //
217 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>> (*)[], _ElementType (*)[]>;
218
219template <class _From, class _To>
220concept __span_array_convertible = is_convertible_v<_From (*)[], _To (*)[]>;
221
222template <class _It, class _Tp>
223concept __span_compatible_iterator =
224 contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;
225
226template <class _Sentinel, class _It>
227concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;
228
229template <typename _Tp, size_t _Extent>
230class span {
231public:
232 // constants and types
233 using element_type = _Tp;
234 using value_type = remove_cv_t<_Tp>;
235 using size_type = size_t;
236 using difference_type = ptrdiff_t;
237 using pointer = _Tp*;
238 using const_pointer = const _Tp*;
239 using reference = _Tp&;
240 using const_reference = const _Tp&;
241# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
242 using iterator = __bounded_iter<pointer>;
243# else
244 using iterator = __wrap_iter<pointer>;
245# endif
246 using reverse_iterator = std::reverse_iterator<iterator>;
247
248 static constexpr size_type extent = _Extent;
249
250 // [span.cons], span constructors, copy, assignment, and destructor
251 template <size_t _Sz = _Extent>
252 requires(_Sz == 0)
253 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr} {}
254
255 constexpr span(const span&) noexcept = default;
256 constexpr span& operator=(const span&) noexcept = default;
257
258 template <__span_compatible_iterator<element_type> _It>
259 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, size_type __count) : __data_{std::to_address(__first)} {
260 (void)__count;
261 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
262 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__count == 0 || std::to_address(__first) != nullptr,
263 "passed nullptr with non-zero length in span's constructor (iterator, len)");
264 }
265
266 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
267 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, _End __last) : __data_{std::to_address(__first)} {
268 // [span.cons]/10
269 // Throws: When and what last - first throws.
270 [[maybe_unused]] auto __dist = __last - __first;
271 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)");
272 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
273 __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent");
274 }
275
276 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {}
277
278 template <__span_array_convertible<element_type> _OtherElementType>
279 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
280
281 template <class _OtherElementType>
282 requires __span_array_convertible<const _OtherElementType, element_type>
283 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept
284 : __data_{__arr.data()} {}
285
286 template <__span_compatible_range<element_type> _Range>
287 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} {
288 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
289 }
290
291 template <__span_array_convertible<element_type> _OtherElementType>
292 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _Extent>& __other) noexcept
293 : __data_{__other.data()} {}
294
295 template <__span_array_convertible<element_type> _OtherElementType>
296 _LIBCPP_HIDE_FROM_ABI constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
297 : __data_{__other.data()} {
298 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __other.size(), "size mismatch in span's constructor (other span)");
299 }
300
301 template <size_t _Count>
302 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept {
303 static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range");
304 return span<element_type, _Count>{data(), _Count};
305 }
306
307 template <size_t _Count>
308 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept {
309 static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range");
310 return span<element_type, _Count>{data() + size() - _Count, _Count};
311 }
312
313 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
314 first(size_type __count) const noexcept {
315 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::first(count): count out of range");
316 return {data(), __count};
317 }
318
319 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
320 last(size_type __count) const noexcept {
321 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T, N>::last(count): count out of range");
322 return {data() + size() - __count, __count};
323 }
324
325 template <size_t _Offset, size_t _Count = dynamic_extent>
326 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto subspan() const noexcept
327 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> {
328 static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range");
329 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset,
330 "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range");
331
332 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
333 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
334 }
335
336 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
337 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept {
338 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T, N>::subspan(offset, count): offset out of range");
339 if (__count == dynamic_extent)
340 return {data() + __offset, size() - __offset};
341 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
342 __count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range");
343 return {data() + __offset, __count};
344 }
345
346 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return _Extent; }
347 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept {
348 return _Extent * sizeof(element_type);
349 }
350 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return _Extent == 0; }
351
352 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept {
353 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T, N>::operator[](index): index out of range");
354 return __data_[__idx];
355 }
356
357# if _LIBCPP_STD_VER >= 26
358 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
359 if (__index >= size())
360 std::__throw_out_of_range(msg: "span");
361 return __data_[__index];
362 }
363# endif
364
365 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
366 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::front() on empty span");
367 return __data_[0];
368 }
369
370 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept {
371 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::back() on empty span");
372 return __data_[size() - 1];
373 }
374
375 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; }
376
377 // [span.iter], span iterator support
378 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
379# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
380 return std::__make_bounded_iter(data(), data(), data() + size());
381# else
382 return iterator(data());
383# endif
384 }
385 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
386# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
387 return std::__make_bounded_iter(data() + size(), data(), data() + size());
388# else
389 return iterator(data() + size());
390# endif
391 }
392 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept {
393 return reverse_iterator(end());
394 }
395 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept {
396 return reverse_iterator(begin());
397 }
398
399 _LIBCPP_HIDE_FROM_ABI span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept {
400 return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte*>(data()), size_bytes()};
401 }
402
403 _LIBCPP_HIDE_FROM_ABI span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept {
404 return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte*>(data()), size_bytes()};
405 }
406
407private:
408 pointer __data_;
409};
410
411template <typename _Tp>
412class span<_Tp, dynamic_extent> {
413public:
414 // constants and types
415 using element_type = _Tp;
416 using value_type = remove_cv_t<_Tp>;
417 using size_type = size_t;
418 using difference_type = ptrdiff_t;
419 using pointer = _Tp*;
420 using const_pointer = const _Tp*;
421 using reference = _Tp&;
422 using const_reference = const _Tp&;
423# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
424 using iterator = __bounded_iter<pointer>;
425# else
426 using iterator = __wrap_iter<pointer>;
427# endif
428 using reverse_iterator = std::reverse_iterator<iterator>;
429
430 static constexpr size_type extent = dynamic_extent;
431
432 // [span.cons], span constructors, copy, assignment, and destructor
433 _LIBCPP_HIDE_FROM_ABI constexpr span() noexcept : __data_{nullptr}, __size_{0} {}
434
435 constexpr span(const span&) noexcept = default;
436 constexpr span& operator=(const span&) noexcept = default;
437
438 template <__span_compatible_iterator<element_type> _It>
439 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, size_type __count)
440 : __data_{std::to_address(__first)}, __size_{__count} {
441 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__count == 0 || std::to_address(__first) != nullptr,
442 "passed nullptr with non-zero length in span's constructor (iterator, len)");
443 }
444
445 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
446 _LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, _End __last)
447 : __data_(std::to_address(__first)), __size_(__last - __first) {
448 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
449 }
450
451 template <size_t _Sz>
452 _LIBCPP_HIDE_FROM_ABI constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept
453 : __data_{__arr}, __size_{_Sz} {}
454
455 template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
456 _LIBCPP_HIDE_FROM_ABI constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept
457 : __data_{__arr.data()}, __size_{_Sz} {}
458
459 template <class _OtherElementType, size_t _Sz>
460 requires __span_array_convertible<const _OtherElementType, element_type>
461 _LIBCPP_HIDE_FROM_ABI constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept
462 : __data_{__arr.data()}, __size_{_Sz} {}
463
464 template <__span_compatible_range<element_type> _Range>
465 _LIBCPP_HIDE_FROM_ABI constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {}
466
467 template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
468 _LIBCPP_HIDE_FROM_ABI constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept
469 : __data_{__other.data()}, __size_{__other.size()} {}
470
471 template <size_t _Count>
472 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> first() const noexcept {
473 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::first<Count>(): Count out of range");
474 return span<element_type, _Count>{data(), _Count};
475 }
476
477 template <size_t _Count>
478 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> last() const noexcept {
479 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count <= size(), "span<T>::last<Count>(): Count out of range");
480 return span<element_type, _Count>{data() + size() - _Count, _Count};
481 }
482
483 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
484 first(size_type __count) const noexcept {
485 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::first(count): count out of range");
486 return {data(), __count};
487 }
488
489 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, dynamic_extent>
490 last(size_type __count) const noexcept {
491 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__count <= size(), "span<T>::last(count): count out of range");
492 return {data() + size() - __count, __count};
493 }
494
495 template <size_t _Offset, size_t _Count = dynamic_extent>
496 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr span<element_type, _Count> subspan() const noexcept {
497 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range");
498 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Count == dynamic_extent || _Count <= size() - _Offset,
499 "span<T>::subspan<Offset, Count>(): Offset + Count out of range");
500 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
501 }
502
503 [[nodiscard]] constexpr span<element_type, dynamic_extent> _LIBCPP_HIDE_FROM_ABI
504 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept {
505 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__offset <= size(), "span<T>::subspan(offset, count): offset out of range");
506 if (__count == dynamic_extent)
507 return {data() + __offset, size() - __offset};
508 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
509 __count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range");
510 return {data() + __offset, __count};
511 }
512
513 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept { return __size_; }
514 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size_bytes() const noexcept {
515 return __size_ * sizeof(element_type);
516 }
517 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept { return __size_ == 0; }
518
519 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](size_type __idx) const noexcept {
520 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__idx < size(), "span<T>::operator[](index): index out of range");
521 return __data_[__idx];
522 }
523
524# if _LIBCPP_STD_VER >= 26
525 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
526 if (__index >= size())
527 std::__throw_out_of_range(msg: "span");
528 return __data_[__index];
529 }
530# endif
531
532 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
533 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::front() on empty span");
534 return __data_[0];
535 }
536
537 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reference back() const noexcept {
538 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::back() on empty span");
539 return __data_[size() - 1];
540 }
541
542 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr pointer data() const noexcept { return __data_; }
543
544 // [span.iter], span iterator support
545 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() const noexcept {
546# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
547 return std::__make_bounded_iter(data(), data(), data() + size());
548# else
549 return iterator(data());
550# endif
551 }
552 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() const noexcept {
553# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
554 return std::__make_bounded_iter(data() + size(), data(), data() + size());
555# else
556 return iterator(data() + size());
557# endif
558 }
559 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rbegin() const noexcept {
560 return reverse_iterator(end());
561 }
562 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator rend() const noexcept {
563 return reverse_iterator(begin());
564 }
565
566 _LIBCPP_HIDE_FROM_ABI span<const byte, dynamic_extent> __as_bytes() const noexcept {
567 return {reinterpret_cast<const byte*>(data()), size_bytes()};
568 }
569
570 _LIBCPP_HIDE_FROM_ABI span<byte, dynamic_extent> __as_writable_bytes() const noexcept {
571 return {reinterpret_cast<byte*>(data()), size_bytes()};
572 }
573
574private:
575 pointer __data_;
576 size_type __size_;
577};
578
579template <class _Tp, size_t _Extent>
580inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
581
582template <class _ElementType, size_t _Extent>
583inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
584
585// as_bytes & as_writable_bytes
586template <class _Tp, size_t _Extent>
587 requires(!is_volatile_v<_Tp>)
588[[nodiscard]] _LIBCPP_HIDE_FROM_ABI auto as_bytes(span<_Tp, _Extent> __s) noexcept {
589 return __s.__as_bytes();
590}
591
592template <class _Tp, size_t _Extent>
593 requires(!is_const_v<_Tp> && !is_volatile_v<_Tp>)
594[[nodiscard]] _LIBCPP_HIDE_FROM_ABI auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept {
595 return __s.__as_writable_bytes();
596}
597
598template <class _Tp>
599concept __integral_constant_like =
600 is_integral_v<remove_cvref_t<decltype(_Tp::value)>> && !is_same_v<bool, remove_cvref_t<decltype(_Tp::value)>> &&
601 convertible_to<_Tp, decltype(_Tp::value)> && equality_comparable_with<_Tp, decltype(_Tp::value)> &&
602 bool_constant<_Tp() == _Tp::value>::value &&
603 bool_constant<static_cast<decltype(_Tp::value)>(_Tp()) == _Tp::value>::value;
604
605template <class _Tp>
606inline constexpr size_t __maybe_static_ext = dynamic_extent;
607
608template <__integral_constant_like _Tp>
609inline constexpr size_t __maybe_static_ext<_Tp> = {_Tp::value};
610
611template <contiguous_iterator _It, class _EndOrSize>
612span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>, __maybe_static_ext<_EndOrSize>>;
613
614template <class _Tp, size_t _Sz>
615span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
616
617template <class _Tp, size_t _Sz>
618span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
619
620template <class _Tp, size_t _Sz>
621span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
622
623template <ranges::contiguous_range _Range>
624span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
625
626_LIBCPP_END_NAMESPACE_STD
627
628# endif // _LIBCPP_STD_VER >= 20
629
630_LIBCPP_POP_MACROS
631
632# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
633# include <array>
634# include <concepts>
635# include <functional>
636# include <iterator>
637# include <type_traits>
638# endif
639#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
640
641#endif // _LIBCPP_SPAN
642