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