|
| 1 | +/* Copyright (C) 2023 Povilas Kanapickas < [email protected]> |
| 2 | + |
| 3 | + This file is part of cppreference-doc |
| 4 | + |
| 5 | + This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 |
| 6 | + Unported License. To view a copy of this license, visit |
| 7 | + http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative |
| 8 | + Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. |
| 9 | + |
| 10 | + Permission is granted to copy, distribute and/or modify this document |
| 11 | + under the terms of the GNU Free Documentation License, Version 1.3 or |
| 12 | + any later version published by the Free Software Foundation; with no |
| 13 | + Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. |
| 14 | +*/ |
| 15 | + |
| 16 | +#ifndef CPPREFERENCE_SPAN_H |
| 17 | +#define CPPREFERENCE_SPAN_H |
| 18 | + |
| 19 | +#if CPPREFERENCE_STDVER >= 2020 |
| 20 | + |
| 21 | +#include <cstddef> |
| 22 | +#include <limits> |
| 23 | +#include <type_traits> |
| 24 | + |
| 25 | +namespace std { |
| 26 | + |
| 27 | +inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); |
| 28 | + |
| 29 | +template<class T, size_t Extent = dynamic_extent> |
| 30 | +class span { |
| 31 | + using element_type = T; |
| 32 | + using value_type = remove_cv_t<T>; |
| 33 | + using size_type = size_t; |
| 34 | + using difference_type = ptrdiff_t; |
| 35 | + using pointer = element_type*; |
| 36 | + using const_pointer = const element_type*; |
| 37 | + using reference = element_type&; |
| 38 | + using const_reference = const element_type&; |
| 39 | + using iterator = T*; // actual type is unspecified |
| 40 | + using const_iterator = const T*; // actual type is const_iterator<iterator>; |
| 41 | + using reverse_iterator = T*; // actual type is reverse_iterator<iterator>; |
| 42 | + using const_reverse_iterator = const T*; // actual type is const_iterator<reverse_iterator>; |
| 43 | + static constexpr size_type extent = Extent; |
| 44 | + |
| 45 | + // constructor |
| 46 | + constexpr span() noexcept; |
| 47 | + |
| 48 | + template<class It> |
| 49 | + constexpr explicit(extent != dynamic_extent) span(It first, size_type count); |
| 50 | + |
| 51 | + template<class It, class End> |
| 52 | + constexpr explicit(extent != dynamic_extent) span(It first, End last); |
| 53 | + |
| 54 | + template<size_t N> |
| 55 | + constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept; |
| 56 | + |
| 57 | + template<class U, size_t N> |
| 58 | + constexpr span(array<U, N>& arr) noexcept; |
| 59 | + |
| 60 | + template<class U, size_t N> |
| 61 | + constexpr span(const array<U, N>& arr) noexcept; |
| 62 | + |
| 63 | + template<class R> |
| 64 | + constexpr explicit(extent != dynamic_extent) span(R&& r); |
| 65 | + |
| 66 | + constexpr span(const span& other) noexcept = default; |
| 67 | + |
| 68 | + template<class OtherT, size_t OtherExtent> |
| 69 | + constexpr explicit span(const span<OtherT, OtherExtent>& s) noexcept; |
| 70 | + |
| 71 | + ~span() noexcept = default; |
| 72 | + |
| 73 | + constexpr span& operator=(const span& other) noexcept = default; |
| 74 | + |
| 75 | + // subviews |
| 76 | + template<size_t Count> |
| 77 | + constexpr span<element_type, Count> first() const; |
| 78 | + |
| 79 | + template<size_t Count> |
| 80 | + constexpr span<element_type, Count> last() const; |
| 81 | + |
| 82 | + template<size_t Offset, size_t Count = dynamic_extent> |
| 83 | + constexpr span<element_type, /* see description */> 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, |
| 88 | + size_type count = dynamic_extent) const; |
| 89 | + // 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 | + // 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 | + // iterator support |
| 101 | + constexpr iterator begin() const noexcept; |
| 102 | + constexpr iterator end() const noexcept; |
| 103 | + constexpr const_iterator cbegin() const noexcept; |
| 104 | + constexpr const_iterator cend() const noexcept; |
| 105 | + constexpr reverse_iterator rbegin() const noexcept; |
| 106 | + constexpr reverse_iterator rend() const noexcept; |
| 107 | + constexpr const_reverse_iterator crbegin() const noexcept; |
| 108 | + constexpr const_reverse_iterator crend() const noexcept; |
| 109 | +}; |
| 110 | + |
| 111 | +template<class T, size_t N> |
| 112 | +span<const byte, N> as_bytes(span<T, N> s) noexcept; |
| 113 | + |
| 114 | +template<class T, size_t N> |
| 115 | +span<byte, N> as_writable_bytes(span<T, N> s) noexcept |
| 116 | + |
| 117 | +} // namespace std |
| 118 | +#endif |
| 119 | + |
| 120 | +#endif // CPPREFERENCE_OSTREAM_H |
0 commit comments