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