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