aboutsummaryrefslogtreecommitdiff
path: root/libcxx/include/span
blob: 67d2ac241ff2b97406415b26bd998c333fcccf44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP_SPAN
#define _LIBCPP_SPAN

/*
    span synopsis

namespace std {

// constants
inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();

// [views.span], class template span
template <class ElementType, size_t Extent = dynamic_extent>
    class span;

template<class ElementType, size_t Extent>
  inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;

template<class ElementType, size_t Extent>
    inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;

// [span.objectrep], views of object representation
template <class ElementType, size_t Extent>
    span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
        (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;

template <class ElementType, size_t Extent>
    span<      byte, ((Extent == dynamic_extent) ? dynamic_extent :
        (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;


template <class ElementType, size_t Extent = dynamic_extent>
class span {
public:
    // constants and types
    using element_type = ElementType;
    using value_type = remove_cv_t<ElementType>;
    using size_type = size_t;
    using difference_type = ptrdiff_t;
    using pointer = element_type*;
    using const_pointer = const element_type*;
    using reference = element_type&;
    using const_reference = const element_type&;
    using iterator = implementation-defined;
    using reverse_iterator = std::reverse_iterator<iterator>;
    static constexpr size_type extent = Extent;

    // [span.cons], span constructors, copy, assignment, and destructor
    constexpr span() noexcept;
    template <class It>
    constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
    template <class It, class End>
    constexpr explicit(Extent != dynamic_extent) span(It first, End last);
    template <size_t N>
        constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept;
    template <size_t N>
        constexpr span(array<value_type, N>& arr) noexcept;
    template <size_t N>
        constexpr span(const array<value_type, N>& arr) noexcept;
    template<class R>
      constexpr explicit(Extent != dynamic_extent) span(R&& r);
    constexpr span(const span& other) noexcept = default;
    template <class OtherElementType, size_t OtherExtent>
        constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
    ~span() noexcept = default;
    constexpr span& operator=(const span& other) noexcept = default;

    // [span.sub], span subviews
    template <size_t Count>
        constexpr span<element_type, Count> first() const;
    template <size_t Count>
        constexpr span<element_type, Count> last() const;
    template <size_t Offset, size_t Count = dynamic_extent>
        constexpr span<element_type, see below> subspan() const;

    constexpr span<element_type, dynamic_extent> first(size_type count) const;
    constexpr span<element_type, dynamic_extent> last(size_type count) const;
    constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;

    // [span.obs], span observers
    constexpr size_type size() const noexcept;
    constexpr size_type size_bytes() const noexcept;
    [[nodiscard]] constexpr bool empty() const noexcept;

    // [span.elem], span element access
    constexpr reference operator[](size_type idx) const;
    constexpr reference front() const;
    constexpr reference back() const;
    constexpr pointer data() const noexcept;

    // [span.iterators], span iterator support
    constexpr iterator begin() const noexcept;
    constexpr iterator end() const noexcept;
    constexpr reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() const noexcept;

private:
    pointer data_;    // exposition only
    size_type size_;  // exposition only
};

template<class It, class EndOrSize>
    span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;

template<class T, size_t N>
    span(T (&)[N]) -> span<T, N>;

template<class T, size_t N>
    span(array<T, N>&) -> span<T, N>;

template<class T, size_t N>
    span(const array<T, N>&) -> span<const T, N>;

template<class R>
    span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;

} // namespace std

*/

#include <__assert> // all public C++ headers provide the assertion handler
#include <__config>
#include <__debug>
#include <__fwd/span.h>
#include <__iterator/bounded_iter.h>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/wrap_iter.h>
#include <__memory/pointer_traits.h>
#include <__ranges/concepts.h>
#include <__ranges/data.h>
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/enable_view.h>
#include <__ranges/size.h>
#include <__utility/forward.h>
#include <array>        // for array
#include <cstddef>      // for byte
#include <limits>
#include <type_traits>  // for remove_cv, etc
#include <version>

#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
#  include <functional>
#  include <iterator>
#endif

// standard-mandated includes

// [iterator.range]
#include <__iterator/access.h>
#include <__iterator/data.h>
#include <__iterator/empty.h>
#include <__iterator/reverse_access.h>
#include <__iterator/size.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER > 17

template <class _Tp>
struct __is_std_array : false_type {};

template <class _Tp, size_t _Sz>
struct __is_std_array<array<_Tp, _Sz>> : true_type {};

template <class _Tp>
struct __is_std_span : false_type {};

template <class _Tp, size_t _Sz>
struct __is_std_span<span<_Tp, _Sz>> : true_type {};

#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
// This is a temporary workaround until we ship <ranges> -- we've unfortunately been
// shipping <span> before its API was finalized, and we used to provide a constructor
// from container types that had the requirements below. To avoid breaking code that
// has started relying on the range-based constructor until we ship all of <ranges>,
// we emulate the constructor requirements like this.
template <class _Range, class _ElementType>
concept __span_compatible_range =
  !__is_std_span<remove_cvref_t<_Range>>::value  &&
  !__is_std_array<remove_cvref_t<_Range>>::value &&
  !is_array_v<remove_cvref_t<_Range>> &&
  requires (_Range&& __r) {
      data(std::forward<_Range>(__r));
      size(std::forward<_Range>(__r));
  } &&
  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
#else
template <class _Range, class _ElementType>
concept __span_compatible_range =
  ranges::contiguous_range<_Range> &&
  ranges::sized_range<_Range> &&
  (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
  !__is_std_span<remove_cvref_t<_Range>>::value  &&
  !__is_std_array<remove_cvref_t<_Range>>::value &&
  !is_array_v<remove_cvref_t<_Range>> &&
  is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)

template <class _From, class _To>
concept __span_array_convertible = is_convertible_v<_From(*)[], _To(*)[]>;

template <class _It, class _Tp>
concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;

template <class _Sentinel, class _It>
concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;

template <typename _Tp, size_t _Extent>
class _LIBCPP_TEMPLATE_VIS span {
public:
//  constants and types
    using element_type           = _Tp;
    using value_type             = remove_cv_t<_Tp>;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using pointer                = _Tp *;
    using const_pointer          = const _Tp *;
    using reference              = _Tp &;
    using const_reference        = const _Tp &;
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
    using iterator               = __bounded_iter<pointer>;
#else
    using iterator               = __wrap_iter<pointer>;
#endif
    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;

    static constexpr size_type extent = _Extent;

// [span.cons], span constructors, copy, assignment, and destructor
    template <size_t _Sz = _Extent> requires(_Sz == 0)
    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}

    constexpr span           (const span&) noexcept = default;
    constexpr span& operator=(const span&) noexcept = default;

    template <__span_compatible_iterator<element_type> _It>
    _LIBCPP_INLINE_VISIBILITY
    constexpr explicit span(_It __first, size_type __count)
        : __data{_VSTD::to_address(__first)} {
      (void)__count;
      _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
    }

    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
    _LIBCPP_INLINE_VISIBILITY
    constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
      (void)__last;
      _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
      _LIBCPP_ASSERT(__last - __first == _Extent,
                     "invalid range in span's constructor (iterator, sentinel): last - first != extent");
    }

    _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}

    template <__span_array_convertible<element_type> _OtherElementType>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}

    template <class _OtherElementType>
        requires __span_array_convertible<const _OtherElementType, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}

#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
    template <class _Container>
        requires __span_compatible_range<_Container, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr explicit span(_Container& __c) : __data{std::data(__c)} {
      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
    }
    template <class _Container>
        requires __span_compatible_range<const _Container, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr explicit span(const _Container& __c) : __data{std::data(__c)} {
      _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)");
    }
#else
    template <__span_compatible_range<element_type> _Range>
    _LIBCPP_INLINE_VISIBILITY
    constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
      _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
    }
#endif

    template <__span_array_convertible<element_type> _OtherElementType>
    _LIBCPP_INLINE_VISIBILITY
        constexpr span(const span<_OtherElementType, _Extent>& __other)
        : __data{__other.data()} {}

    template <__span_array_convertible<element_type> _OtherElementType>
    _LIBCPP_INLINE_VISIBILITY
        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
        : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }


//  ~span() noexcept = default;

    template <size_t _Count>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, _Count> first() const noexcept
    {
        static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range");
        return span<element_type, _Count>{data(), _Count};
    }

    template <size_t _Count>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, _Count> last() const noexcept
    {
        static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range");
        return span<element_type, _Count>{data() + size() - _Count, _Count};
    }

    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
    {
        _LIBCPP_ASSERT(__count <= size(), "span<T, N>::first(count): count out of range");
        return {data(), __count};
    }

    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
    {
        _LIBCPP_ASSERT(__count <= size(), "span<T, N>::last(count): count out of range");
        return {data() + size() - __count, __count};
    }

    template <size_t _Offset, size_t _Count = dynamic_extent>
    _LIBCPP_INLINE_VISIBILITY
    constexpr auto subspan() const noexcept
        -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
    {
        static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range");
        static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range");

        using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
        return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
    }


    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, dynamic_extent>
       subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
    {
        _LIBCPP_ASSERT(__offset <= size(), "span<T, N>::subspan(offset, count): offset out of range");
        _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "span<T, N>::subspan(offset, count): count out of range");
        if (__count == dynamic_extent)
            return {data() + __offset, size() - __offset};
        _LIBCPP_ASSERT(__count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range");
        return {data() + __offset, __count};
    }

    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return _Extent; }
    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return _Extent * sizeof(element_type); }
    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }

    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
    {
        _LIBCPP_ASSERT(__idx < size(), "span<T, N>::operator[](index): index out of range");
        return __data[__idx];
    }

    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
    {
        _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
        return __data[0];
    }

    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
    {
        _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
        return __data[size()-1];
    }

    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }

// [span.iter], span iterator support
    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
        return std::__make_bounded_iter(data(), data(), data() + size());
#else
        return iterator(this, data());
#endif
    }
    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
        return std::__make_bounded_iter(data() + size(), data(), data() + size());
#else
        return iterator(this, data() + size());
#endif
    }
    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }

    _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
    { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }

    _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
    { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }

private:
    pointer    __data;
};


template <typename _Tp>
class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
public:
//  constants and types
    using element_type           = _Tp;
    using value_type             = remove_cv_t<_Tp>;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using pointer                = _Tp *;
    using const_pointer          = const _Tp *;
    using reference              = _Tp &;
    using const_reference        = const _Tp &;
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
    using iterator               = __bounded_iter<pointer>;
#else
    using iterator               = __wrap_iter<pointer>;
#endif
    using reverse_iterator       = _VSTD::reverse_iterator<iterator>;

    static constexpr size_type extent = dynamic_extent;

// [span.cons], span constructors, copy, assignment, and destructor
    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}

    constexpr span           (const span&) noexcept = default;
    constexpr span& operator=(const span&) noexcept = default;

    template <__span_compatible_iterator<element_type> _It>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(_It __first, size_type __count)
        : __data{_VSTD::to_address(__first)}, __size{__count} {}

    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
    _LIBCPP_INLINE_VISIBILITY constexpr span(_It __first, _End __last)
        : __data(_VSTD::to_address(__first)), __size(__last - __first) {
      _LIBCPP_ASSERT(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
    }

    template <size_t _Sz>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}

    template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}

    template <class _OtherElementType, size_t _Sz>
        requires __span_array_convertible<const _OtherElementType, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}

#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
    template <class _Container>
        requires __span_compatible_range<_Container, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
    template <class _Container>
        requires __span_compatible_range<const _Container, element_type>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {}
#else
    template <__span_compatible_range<element_type> _Range>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
#endif

    template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
    _LIBCPP_INLINE_VISIBILITY
        constexpr span(const span<_OtherElementType, _OtherExtent>& __other)  noexcept
        : __data{__other.data()}, __size{__other.size()} {}

//    ~span() noexcept = default;

    template <size_t _Count>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, _Count> first() const noexcept
    {
        _LIBCPP_ASSERT(_Count <= size(), "span<T>::first<Count>(): Count out of range");
        return span<element_type, _Count>{data(), _Count};
    }

    template <size_t _Count>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, _Count> last() const noexcept
    {
        _LIBCPP_ASSERT(_Count <= size(), "span<T>::last<Count>(): Count out of range");
        return span<element_type, _Count>{data() + size() - _Count, _Count};
    }

    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
    {
        _LIBCPP_ASSERT(__count <= size(), "span<T>::first(count): count out of range");
        return {data(), __count};
    }

    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
    {
        _LIBCPP_ASSERT(__count <= size(), "span<T>::last(count): count out of range");
        return {data() + size() - __count, __count};
    }

    template <size_t _Offset, size_t _Count = dynamic_extent>
    _LIBCPP_INLINE_VISIBILITY
    constexpr span<element_type, _Count> subspan() const noexcept
    {
        _LIBCPP_ASSERT(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range");
        _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "span<T>::subspan<Offset, Count>(): Offset + Count out of range");
        return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
    }

    constexpr span<element_type, dynamic_extent>
    _LIBCPP_INLINE_VISIBILITY
    subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
    {
        _LIBCPP_ASSERT(__offset <= size(), "span<T>::subspan(offset, count): offset out of range");
        _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "span<T>::subspan(offset, count): count out of range");
        if (__count == dynamic_extent)
            return {data() + __offset, size() - __offset};
        _LIBCPP_ASSERT(__count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range");
        return {data() + __offset, __count};
    }

    _LIBCPP_INLINE_VISIBILITY constexpr size_type size()           const noexcept { return __size; }
    _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes()     const noexcept { return __size * sizeof(element_type); }
    [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }

    _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
    {
        _LIBCPP_ASSERT(__idx < size(), "span<T>::operator[](index): index out of range");
        return __data[__idx];
    }

    _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
    {
        _LIBCPP_ASSERT(!empty(), "span<T>::front() on empty span");
        return __data[0];
    }

    _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
    {
        _LIBCPP_ASSERT(!empty(), "span<T>::back() on empty span");
        return __data[size()-1];
    }


    _LIBCPP_INLINE_VISIBILITY constexpr pointer data()                         const noexcept { return __data; }

// [span.iter], span iterator support
    _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept {
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
        return std::__make_bounded_iter(data(), data(), data() + size());
#else
        return iterator(this, data());
#endif
    }
    _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept {
#ifdef _LIBCPP_ENABLE_DEBUG_MODE
        return std::__make_bounded_iter(data() + size(), data(), data() + size());
#else
        return iterator(this, data() + size());
#endif
    }
    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator        rbegin() const noexcept { return reverse_iterator(end()); }
    _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator          rend() const noexcept { return reverse_iterator(begin()); }

    _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
    { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }

    _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
    { return {reinterpret_cast<byte *>(data()), size_bytes()}; }

private:
    pointer   __data;
    size_type __size;
};

template <class _Tp, size_t _Extent>
inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;

template <class _ElementType, size_t _Extent>
inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;

//  as_bytes & as_writable_bytes
template <class _Tp, size_t _Extent>
_LIBCPP_INLINE_VISIBILITY
auto as_bytes(span<_Tp, _Extent> __s) noexcept
{ return __s.__as_bytes(); }

template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>)
_LIBCPP_INLINE_VISIBILITY
auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
{ return __s.__as_writable_bytes(); }

#if _LIBCPP_STD_VER > 17
template<contiguous_iterator _It, class _EndOrSize>
    span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
#endif // _LIBCPP_STD_VER > 17

template<class _Tp, size_t _Sz>
    span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;

template<class _Tp, size_t _Sz>
    span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;

template<class _Tp, size_t _Sz>
    span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;

#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
template<class _Container>
    span(_Container&) -> span<typename _Container::value_type>;

template<class _Container>
    span(const _Container&) -> span<const typename _Container::value_type>;
#else
template<ranges::contiguous_range _Range>
    span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
#endif

#endif // _LIBCPP_STD_VER > 17

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS

#endif // _LIBCPP_SPAN