xref: /llvm-project-15.0.7/libcxx/include/span (revision cf6a7c19)
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/concepts.h>
135#include <__iterator/iterator_traits.h>
136#include <__iterator/wrap_iter.h>
137#include <__memory/pointer_traits.h>
138#include <__ranges/concepts.h>
139#include <__ranges/data.h>
140#include <__ranges/enable_borrowed_range.h>
141#include <__ranges/enable_view.h>
142#include <__ranges/size.h>
143#include <__utility/forward.h>
144#include <array>        // for array
145#include <cstddef>      // for byte
146#include <iterator> // TODO: Remove this include
147#include <limits>
148#include <type_traits>  // for remove_cv, etc
149#include <version>
150
151#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
152#  pragma GCC system_header
153#endif
154
155_LIBCPP_PUSH_MACROS
156#include <__undef_macros>
157
158_LIBCPP_BEGIN_NAMESPACE_STD
159
160#if _LIBCPP_STD_VER > 17
161
162template <class _Tp>
163struct __is_std_array : false_type {};
164
165template <class _Tp, size_t _Sz>
166struct __is_std_array<array<_Tp, _Sz>> : true_type {};
167
168template <class _Tp>
169struct __is_std_span : false_type {};
170
171template <class _Tp, size_t _Sz>
172struct __is_std_span<span<_Tp, _Sz>> : true_type {};
173
174#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
175// This is a temporary workaround until we ship <ranges> -- we've unfortunately been
176// shipping <span> before its API was finalized, and we used to provide a constructor
177// from container types that had the requirements below. To avoid breaking code that
178// has started relying on the range-based constructor until we ship all of <ranges>,
179// we emulate the constructor requirements like this.
180template <class _Range, class _ElementType>
181concept __span_compatible_range =
182  !__is_std_span<remove_cvref_t<_Range>>::value  &&
183  !__is_std_array<remove_cvref_t<_Range>>::value &&
184  !is_array_v<remove_cvref_t<_Range>> &&
185  requires (_Range&& __r) {
186      data(std::forward<_Range>(__r));
187      size(std::forward<_Range>(__r));
188  } &&
189  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
190#else
191template <class _Range, class _ElementType>
192concept __span_compatible_range =
193  ranges::contiguous_range<_Range> &&
194  ranges::sized_range<_Range> &&
195  (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
196  !__is_std_span<remove_cvref_t<_Range>>::value  &&
197  !__is_std_array<remove_cvref_t<_Range>>::value &&
198  !is_array_v<remove_cvref_t<_Range>> &&
199  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
200#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
201
202#if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
203#   define _LIBCPP_SPAN_USE_POINTER_ITERATOR
204#endif
205
206template <typename _Tp, size_t _Extent>
207class _LIBCPP_TEMPLATE_VIS span {
208public:
209//  constants and types
210    using element_type           = _Tp;
211    using value_type             = remove_cv_t<_Tp>;
212    using size_type              = size_t;
213    using difference_type        = ptrdiff_t;
214    using pointer                = _Tp *;
215    using const_pointer          = const _Tp *;
216    using reference              = _Tp &;
217    using const_reference        = const _Tp &;
218#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
219    using iterator               = pointer;
220#else
221    using iterator               = __wrap_iter<pointer>;
222#endif
223    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
224
225    static constexpr size_type extent = _Extent;
226
227// [span.cons], span constructors, copy, assignment, and destructor
228    template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
229    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
230
231    constexpr span           (const span&) noexcept = default;
232    constexpr span& operator=(const span&) noexcept = default;
233
234    template <class _It,
235              enable_if_t<contiguous_iterator<_It> &&
236                              is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
237                          nullptr_t> = nullptr>
238    _LIBCPP_INLINE_VISIBILITY
239    constexpr explicit span(_It __first, size_type __count)
240        : __data{_VSTD::to_address(__first)} {
241      (void)__count;
242      _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
243    }
244
245    template <
246        class _It, class _End,
247        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
248                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
249                    nullptr_t> = nullptr>
250    _LIBCPP_INLINE_VISIBILITY
251    constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
252      (void)__last;
253      _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
254      _LIBCPP_ASSERT(__last - __first == _Extent,
255                     "invalid range in span's constructor (iterator, sentinel): last - first != extent");
256    }
257
258    _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
259
260    template <class _OtherElementType,
261              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
262    _LIBCPP_INLINE_VISIBILITY
263    constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
264
265    template <class _OtherElementType,
266              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
267    _LIBCPP_INLINE_VISIBILITY
268    constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
269
270#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
271    template <class _Container>
272        requires __span_compatible_range<_Container, element_type>
273    _LIBCPP_INLINE_VISIBILITY
274    constexpr explicit span(_Container& __c) : __data{std::data(__c)} {
275      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
276    }
277    template <class _Container>
278        requires __span_compatible_range<const _Container, element_type>
279    _LIBCPP_INLINE_VISIBILITY
280    constexpr explicit span(const _Container& __c) : __data{std::data(__c)} {
281      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
282    }
283#else
284    template <__span_compatible_range<element_type> _Range>
285    _LIBCPP_INLINE_VISIBILITY
286    constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
287      _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
288    }
289#endif
290
291    template <class _OtherElementType>
292    _LIBCPP_INLINE_VISIBILITY
293        constexpr span(const span<_OtherElementType, _Extent>& __other,
294                       enable_if_t<
295                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
296                          nullptr_t> = nullptr)
297        : __data{__other.data()} {}
298
299    template <class _OtherElementType>
300    _LIBCPP_INLINE_VISIBILITY
301        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
302                       enable_if_t<
303                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
304                          nullptr_t> = nullptr) noexcept
305        : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
306
307
308//  ~span() noexcept = default;
309
310    template <size_t _Count>
311    _LIBCPP_INLINE_VISIBILITY
312    constexpr span<element_type, _Count> first() const noexcept
313    {
314        static_assert(_Count <= _Extent, "Count out of range in span::first()");
315        return span<element_type, _Count>{data(), _Count};
316    }
317
318    template <size_t _Count>
319    _LIBCPP_INLINE_VISIBILITY
320    constexpr span<element_type, _Count> last() const noexcept
321    {
322        static_assert(_Count <= _Extent, "Count out of range in span::last()");
323        return span<element_type, _Count>{data() + size() - _Count, _Count};
324    }
325
326    _LIBCPP_INLINE_VISIBILITY
327    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
328    {
329        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
330        return {data(), __count};
331    }
332
333    _LIBCPP_INLINE_VISIBILITY
334    constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
335    {
336        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
337        return {data() + size() - __count, __count};
338    }
339
340    template <size_t _Offset, size_t _Count = dynamic_extent>
341    _LIBCPP_INLINE_VISIBILITY
342    constexpr auto subspan() const noexcept
343        -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
344    {
345        static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
346        static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
347
348        using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
349        return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
350    }
351
352
353    _LIBCPP_INLINE_VISIBILITY
354    constexpr span<element_type, dynamic_extent>
355       subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
356    {
357        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
358        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
359        if (__count == dynamic_extent)
360            return {data() + __offset, size() - __offset};
361        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
362        return {data() + __offset, __count};
363    }
364
365    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return _Extent; }
366    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return _Extent * sizeof(element_type); }
367    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
368
369    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
370    {
371        _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
372        return __data[__idx];
373    }
374
375    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
376    {
377        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
378        return __data[0];
379    }
380
381    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
382    {
383        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
384        return __data[size()-1];
385    }
386
387    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
388
389// [span.iter], span iterator support
390    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
391#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
392        return iterator(data());
393#else
394        return iterator(this, data());
395#endif
396    }
397    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
398#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
399        return iterator(data() + size());
400#else
401        return iterator(this, data() + size());
402#endif
403    }
404    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
405    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
406
407    _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
408    { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
409
410    _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
411    { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
412
413private:
414    pointer    __data;
415
416};
417
418
419template <typename _Tp>
420class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
421private:
422
423public:
424//  constants and types
425    using element_type           = _Tp;
426    using value_type             = remove_cv_t<_Tp>;
427    using size_type              = size_t;
428    using difference_type        = ptrdiff_t;
429    using pointer                = _Tp *;
430    using const_pointer          = const _Tp *;
431    using reference              = _Tp &;
432    using const_reference        = const _Tp &;
433#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
434    using iterator               = pointer;
435#else
436    using iterator               = __wrap_iter<pointer>;
437#endif
438    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;
439
440    static constexpr size_type extent = dynamic_extent;
441
442// [span.cons], span constructors, copy, assignment, and destructor
443    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
444
445    constexpr span           (const span&) noexcept = default;
446    constexpr span& operator=(const span&) noexcept = default;
447
448    template <class _It,
449              enable_if_t<contiguous_iterator<_It> &&
450                              is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
451                          nullptr_t> = nullptr>
452    _LIBCPP_INLINE_VISIBILITY
453    constexpr span(_It __first, size_type __count)
454        : __data{_VSTD::to_address(__first)}, __size{__count} {}
455
456    template <
457        class _It, class _End,
458        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
459                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
460                    nullptr_t> = nullptr>
461    _LIBCPP_INLINE_VISIBILITY
462    constexpr span(_It __first, _End __last)
463        : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
464
465    template <size_t _Sz>
466    _LIBCPP_INLINE_VISIBILITY
467    constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
468
469    template <class _OtherElementType, size_t _Sz,
470              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
471    _LIBCPP_INLINE_VISIBILITY
472    constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
473
474    template <class _OtherElementType, size_t _Sz,
475              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
476    _LIBCPP_INLINE_VISIBILITY
477    constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
478
479#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
480    template <class _Container>
481        requires __span_compatible_range<_Container, element_type>
482    _LIBCPP_INLINE_VISIBILITY
483    constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
484    template <class _Container>
485        requires __span_compatible_range<const _Container, element_type>
486    _LIBCPP_INLINE_VISIBILITY
487    constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
488#else
489    template <__span_compatible_range<element_type> _Range>
490    _LIBCPP_INLINE_VISIBILITY
491    constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
492#endif
493
494    template <class _OtherElementType, size_t _OtherExtent>
495    _LIBCPP_INLINE_VISIBILITY
496        constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
497                       enable_if_t<
498                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
499                          nullptr_t> = nullptr) noexcept
500        : __data{__other.data()}, __size{__other.size()} {}
501
502//    ~span() noexcept = default;
503
504    template <size_t _Count>
505    _LIBCPP_INLINE_VISIBILITY
506    constexpr span<element_type, _Count> first() const noexcept
507    {
508        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
509        return span<element_type, _Count>{data(), _Count};
510    }
511
512    template <size_t _Count>
513    _LIBCPP_INLINE_VISIBILITY
514    constexpr span<element_type, _Count> last() const noexcept
515    {
516        _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
517        return span<element_type, _Count>{data() + size() - _Count, _Count};
518    }
519
520    _LIBCPP_INLINE_VISIBILITY
521    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
522    {
523        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
524        return {data(), __count};
525    }
526
527    _LIBCPP_INLINE_VISIBILITY
528    constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
529    {
530        _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
531        return {data() + size() - __count, __count};
532    }
533
534    template <size_t _Offset, size_t _Count = dynamic_extent>
535    _LIBCPP_INLINE_VISIBILITY
536    constexpr span<element_type, _Count> subspan() const noexcept
537    {
538        _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
539        _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
540        return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
541    }
542
543    constexpr span<element_type, dynamic_extent>
544    _LIBCPP_INLINE_VISIBILITY
545    subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
546    {
547        _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
548        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
549        if (__count == dynamic_extent)
550            return {data() + __offset, size() - __offset};
551        _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
552        return {data() + __offset, __count};
553    }
554
555    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return __size; }
556    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return __size * sizeof(element_type); }
557    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
558
559    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
560    {
561        _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
562        return __data[__idx];
563    }
564
565    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
566    {
567        _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
568        return __data[0];
569    }
570
571    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
572    {
573        _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
574        return __data[size()-1];
575    }
576
577
578    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }
579
580// [span.iter], span iterator support
581    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
582#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
583        return iterator(data());
584#else
585        return iterator(this, data());
586#endif
587    }
588    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
589#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR
590        return iterator(data() + size());
591#else
592        return iterator(this, data() + size());
593#endif
594    }
595    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
596    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }
597
598    _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
599    { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
600
601    _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
602    { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
603
604private:
605    pointer   __data;
606    size_type __size;
607};
608
609template <class _Tp, size_t _Extent>
610inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
611
612template <class _ElementType, size_t _Extent>
613inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
614
615//  as_bytes & as_writable_bytes
616template <class _Tp, size_t _Extent>
617_LIBCPP_INLINE_VISIBILITY
618auto as_bytes(span<_Tp, _Extent> __s) noexcept
619-> decltype(__s.__as_bytes())
620{ return    __s.__as_bytes(); }
621
622template <class _Tp, size_t _Extent>
623_LIBCPP_INLINE_VISIBILITY
624auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
625-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
626{ return __s.__as_writable_bytes(); }
627
628#if _LIBCPP_STD_VER > 17
629template<contiguous_iterator _It, class _EndOrSize>
630    span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
631#endif // _LIBCPP_STD_VER > 17
632
633template<class _Tp, size_t _Sz>
634    span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
635
636template<class _Tp, size_t _Sz>
637    span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
638
639template<class _Tp, size_t _Sz>
640    span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
641
642#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
643template<class _Container>
644    span(_Container&) -> span<typename _Container::value_type>;
645
646template<class _Container>
647    span(const _Container&) -> span<const typename _Container::value_type>;
648#else
649template<ranges::contiguous_range _Range>
650    span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
651#endif
652
653#endif // _LIBCPP_STD_VER > 17
654
655_LIBCPP_END_NAMESPACE_STD
656
657_LIBCPP_POP_MACROS
658
659#endif // _LIBCPP_SPAN
660