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