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