• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    ~span() noexcept = default;
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 front() const;
97    constexpr reference back() const;
98    constexpr pointer data() const noexcept;
99
100    // [span.iterators], span iterator support
101    constexpr iterator begin() const noexcept;
102    constexpr iterator end() const noexcept;
103    constexpr reverse_iterator rbegin() const noexcept;
104    constexpr reverse_iterator rend() const noexcept;
105
106private:
107    pointer data_;    // exposition only
108    size_type size_;  // exposition only
109};
110
111template<class It, class EndOrSize>
112    span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
113
114template<class T, size_t N>
115    span(T (&)[N]) -> span<T, N>;
116
117template<class T, size_t N>
118    span(array<T, N>&) -> span<T, N>;
119
120template<class T, size_t N>
121    span(const array<T, N>&) -> span<const T, N>;
122
123template<class R>
124    span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
125
126} // namespace std
127
128*/
129
130#include <__assert> // all public C++ headers provide the assertion handler
131#include <__config>
132#include <__debug>
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 <limits>
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 = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;
201
202template <class _Sentinel, class _It>
203concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;
204
205template <typename _Tp, size_t _Extent>
206class _LIBCPP_TEMPLATE_VIS span {
207public:
208//  constants and types
209    using element_type           = _Tp;
210    using value_type             = remove_cv_t<_Tp>;
211    using size_type              = size_t;
212    using difference_type        = ptrdiff_t;
213    using pointer                = _Tp *;
214    using const_pointer          = const _Tp *;
215    using reference              = _Tp &;
216    using const_reference        = const _Tp &;
217#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
218    using iterator               = __bounded_iter<pointer>;
219#else
220    using iterator               = __wrap_iter<pointer>;
221#endif
222    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
223
224    static constexpr size_type extent = _Extent;
225
226// [span.cons], span constructors, copy, assignment, and destructor
227    template <size_t _Sz = _Extent> requires(_Sz == 0)
228    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data_{nullptr} {}
229
230    constexpr span           (const span&) noexcept = default;
231    constexpr span& operator=(const span&) noexcept = default;
232
233    template <__span_compatible_iterator<element_type> _It>
234    _LIBCPP_INLINE_VISIBILITY
235    constexpr explicit span(_It __first, size_type __count)
236        : __data_{_VSTD::to_address(__first)} {
237      (void)__count;
238      _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
239    }
240
241    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
242    _LIBCPP_INLINE_VISIBILITY
243    constexpr explicit span(_It __first, _End __last) : __data_{_VSTD::to_address(__first)} {
244      // [span.cons]/10
245      // Throws: When and what last - first throws.
246      [[maybe_unused]] auto __dist = __last - __first;
247      _LIBCPP_ASSERT(__dist >= 0, "invalid range in span's constructor (iterator, sentinel)");
248      _LIBCPP_ASSERT(
249          __dist == _Extent, "invalid range in span's constructor (iterator, sentinel): last - first != extent");
250    }
251
252    _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {}
253
254    template <__span_array_convertible<element_type> _OtherElementType>
255    _LIBCPP_INLINE_VISIBILITY
256    constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
257
258    template <class _OtherElementType>
259        requires __span_array_convertible<const _OtherElementType, element_type>
260    _LIBCPP_INLINE_VISIBILITY
261    constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {}
262
263    template <__span_compatible_range<element_type> _Range>
264    _LIBCPP_INLINE_VISIBILITY
265    constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} {
266      _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
267    }
268
269    template <__span_array_convertible<element_type> _OtherElementType>
270    _LIBCPP_INLINE_VISIBILITY
271        constexpr span(const span<_OtherElementType, _Extent>& __other)
272        : __data_{__other.data()} {}
273
274    template <__span_array_convertible<element_type> _OtherElementType>
275    _LIBCPP_INLINE_VISIBILITY
276        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
277        : __data_{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
278
279
280//  ~span() noexcept = default;
281
282    template <size_t _Count>
283    _LIBCPP_INLINE_VISIBILITY
284    constexpr span<element_type, _Count> first() const noexcept
285    {
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_INLINE_VISIBILITY
292    constexpr span<element_type, _Count> last() const noexcept
293    {
294        static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range");
295        return span<element_type, _Count>{data() + size() - _Count, _Count};
296    }
297
298    _LIBCPP_INLINE_VISIBILITY
299    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
300    {
301        _LIBCPP_ASSERT(__count <= size(), "span<T, N>::first(count): count out of range");
302        return {data(), __count};
303    }
304
305    _LIBCPP_INLINE_VISIBILITY
306    constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
307    {
308        _LIBCPP_ASSERT(__count <= size(), "span<T, N>::last(count): count out of range");
309        return {data() + size() - __count, __count};
310    }
311
312    template <size_t _Offset, size_t _Count = dynamic_extent>
313    _LIBCPP_INLINE_VISIBILITY
314    constexpr auto subspan() const noexcept
315        -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
316    {
317        static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range");
318        static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range");
319
320        using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
321        return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
322    }
323
324
325    _LIBCPP_INLINE_VISIBILITY
326    constexpr span<element_type, dynamic_extent>
327       subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
328    {
329        _LIBCPP_ASSERT(__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(__count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range");
333        return {data() + __offset, __count};
334    }
335
336    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return _Extent; }
337    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return _Extent * sizeof(element_type); }
338    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
339
340    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
341    {
342        _LIBCPP_ASSERT(__idx < size(), "span<T, N>::operator[](index): index out of range");
343        return __data_[__idx];
344    }
345
346    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
347    {
348        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
349        return __data_[0];
350    }
351
352    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
353    {
354        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
355        return __data_[size()-1];
356    }
357
358    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data_; }
359
360// [span.iter], span iterator support
361    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
362#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
363        return std::__make_bounded_iter(data(), data(), data() + size());
364#else
365        return iterator(this, data());
366#endif
367    }
368    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
369#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
370        return std::__make_bounded_iter(data() + size(), data(), data() + size());
371#else
372        return iterator(this, data() + size());
373#endif
374    }
375    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
376    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
377
378    _LIBCPP_INLINE_VISIBILITY 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_INLINE_VISIBILITY 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_DEBUG_ITERATOR_BOUNDS_CHECKING
402    using iterator               = __bounded_iter<pointer>;
403#else
404    using iterator               = __wrap_iter<pointer>;
405#endif
406    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
407
408    static constexpr size_type extent = dynamic_extent;
409
410// [span.cons], span constructors, copy, assignment, and destructor
411    _LIBCPP_INLINE_VISIBILITY 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_INLINE_VISIBILITY
418    constexpr span(_It __first, size_type __count)
419        : __data_{_VSTD::to_address(__first)}, __size_{__count} {}
420
421    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
422    _LIBCPP_INLINE_VISIBILITY constexpr span(_It __first, _End __last)
423        : __data_(_VSTD::to_address(__first)), __size_(__last - __first) {
424      _LIBCPP_ASSERT(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
425    }
426
427    template <size_t _Sz>
428    _LIBCPP_INLINE_VISIBILITY
429    constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data_{__arr}, __size_{_Sz} {}
430
431    template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
432    _LIBCPP_INLINE_VISIBILITY
433    constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {}
434
435    template <class _OtherElementType, size_t _Sz>
436        requires __span_array_convertible<const _OtherElementType, element_type>
437    _LIBCPP_INLINE_VISIBILITY
438    constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {}
439
440    template <__span_compatible_range<element_type> _Range>
441    _LIBCPP_INLINE_VISIBILITY
442    constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {}
443
444    template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
445    _LIBCPP_INLINE_VISIBILITY
446        constexpr span(const span<_OtherElementType, _OtherExtent>& __other)  noexcept
447        : __data_{__other.data()}, __size_{__other.size()} {}
448
449//    ~span() noexcept = default;
450
451    template <size_t _Count>
452    _LIBCPP_INLINE_VISIBILITY
453    constexpr span<element_type, _Count> first() const noexcept
454    {
455        _LIBCPP_ASSERT(_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(_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(__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(__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(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range");
486        _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "span<T>::subspan<Offset, Count>(): Offset + Count out of range");
487        return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
488    }
489
490    constexpr span<element_type, dynamic_extent>
491    _LIBCPP_INLINE_VISIBILITY
492    subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
493    {
494        _LIBCPP_ASSERT(__offset <= size(), "span<T>::subspan(offset, count): offset out of range");
495        if (__count == dynamic_extent)
496            return {data() + __offset, size() - __offset};
497        _LIBCPP_ASSERT(__count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range");
498        return {data() + __offset, __count};
499    }
500
501    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return __size_; }
502    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return __size_ * sizeof(element_type); }
503    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size_ == 0; }
504
505    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
506    {
507        _LIBCPP_ASSERT(__idx < size(), "span<T>::operator[](index): index out of range");
508        return __data_[__idx];
509    }
510
511    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
512    {
513        _LIBCPP_ASSERT(!empty(), "span<T>::front() on empty span");
514        return __data_[0];
515    }
516
517    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
518    {
519        _LIBCPP_ASSERT(!empty(), "span<T>::back() on empty span");
520        return __data_[size()-1];
521    }
522
523
524    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data_; }
525
526// [span.iter], span iterator support
527    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
528#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
529        return std::__make_bounded_iter(data(), data(), data() + size());
530#else
531        return iterator(this, data());
532#endif
533    }
534    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
535#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING
536        return std::__make_bounded_iter(data() + size(), data(), data() + size());
537#else
538        return iterator(this, data() + size());
539#endif
540    }
541    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
542    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
543
544    _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
545    { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
546
547    _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
548    { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
549
550private:
551    pointer   __data_;
552    size_type __size_;
553};
554
555template <class _Tp, size_t _Extent>
556inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
557
558template <class _ElementType, size_t _Extent>
559inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
560
561//  as_bytes & as_writable_bytes
562template <class _Tp, size_t _Extent>
563_LIBCPP_INLINE_VISIBILITY
564auto as_bytes(span<_Tp, _Extent> __s) noexcept
565{ return __s.__as_bytes(); }
566
567template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>)
568_LIBCPP_INLINE_VISIBILITY
569auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
570{ return __s.__as_writable_bytes(); }
571
572#if _LIBCPP_STD_VER >= 20
573template<contiguous_iterator _It, class _EndOrSize>
574    span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
575#endif // _LIBCPP_STD_VER >= 20
576
577template<class _Tp, size_t _Sz>
578    span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
579
580template<class _Tp, size_t _Sz>
581    span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
582
583template<class _Tp, size_t _Sz>
584    span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
585
586template<ranges::contiguous_range _Range>
587    span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
588
589#endif // _LIBCPP_STD_VER >= 20
590
591_LIBCPP_END_NAMESPACE_STD
592
593_LIBCPP_POP_MACROS
594
595#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
596#  include <concepts>
597#  include <functional>
598#  include <iterator>
599#  include <type_traits>
600#endif
601
602#endif // _LIBCPP_SPAN
603