blob: 1d4335b3ad579759cbc8432c524a97be6cc8ad18 [file] [log] [blame]
Marshall Clowd2ad87e2018-07-24 03:01:02 +00001// -*- C++ -*-
2//===------------------------------ span ---------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Marshall Clowd2ad87e2018-07-24 03:01:02 +00007//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SPAN
11#define _LIBCPP_SPAN
12
13/*
14 span synopsis
15
16namespace std {
17
18// constants
Marshall Clowcd6d8802019-02-27 00:32:16 +000019inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowd2ad87e2018-07-24 03:01:02 +000020
21// [views.span], class template span
Marshall Clowcd6d8802019-02-27 00:32:16 +000022template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000023 class span;
24
Mark de Weveraf59b2d2020-11-24 18:08:02 +010025template<class ElementType, size_t Extent>
Christopher Di Bella92072c22021-05-14 06:20:07 +000026 inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
27
28template<class ElementType, size_t Extent>
Mark de Weveraf59b2d2020-11-24 18:08:02 +010029 inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
30
Marshall Clowd2ad87e2018-07-24 03:01:02 +000031// [span.objectrep], views of object representation
Marshall Clowcd6d8802019-02-27 00:32:16 +000032template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000033 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000034 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000035
Marshall Clowcd6d8802019-02-27 00:32:16 +000036template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000037 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000038 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000039
40
Marshall Clowcd6d8802019-02-27 00:32:16 +000041template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000042class span {
43public:
44 // constants and types
45 using element_type = ElementType;
46 using value_type = remove_cv_t<ElementType>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050047 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000048 using difference_type = ptrdiff_t;
49 using pointer = element_type*;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000050 using const_pointer = const element_type*;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000051 using reference = element_type&;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000052 using const_reference = const element_type&;
Marshall Clowcd6d8802019-02-27 00:32:16 +000053 using iterator = implementation-defined;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000054 using reverse_iterator = std::reverse_iterator<iterator>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050055 static constexpr size_type extent = Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000056
57 // [span.cons], span constructors, copy, assignment, and destructor
58 constexpr span() noexcept;
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040059 constexpr explicit(Extent != dynamic_extent) span(pointer ptr, size_type count);
60 constexpr explicit(Extent != dynamic_extent) span(pointer firstElem, pointer lastElem);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000061 template <size_t N>
62 constexpr span(element_type (&arr)[N]) noexcept;
63 template <size_t N>
64 constexpr span(array<value_type, N>& arr) noexcept;
65 template <size_t N>
66 constexpr span(const array<value_type, N>& arr) noexcept;
67 template <class Container>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040068 constexpr explicit(Extent != dynamic_extent) span(Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000069 template <class Container>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040070 constexpr explicit(Extent != dynamic_extent) span(const Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000071 constexpr span(const span& other) noexcept = default;
Marshall Clowcd6d8802019-02-27 00:32:16 +000072 template <class OtherElementType, size_t OtherExtent>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040073 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000074 ~span() noexcept = default;
75 constexpr span& operator=(const span& other) noexcept = default;
76
77 // [span.sub], span subviews
Marshall Clowcd6d8802019-02-27 00:32:16 +000078 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000079 constexpr span<element_type, Count> first() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000080 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000081 constexpr span<element_type, Count> last() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000082 template <size_t Offset, size_t Count = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000083 constexpr span<element_type, see below> subspan() const;
84
Louis Dionne7b89ab02019-11-14 09:07:05 -050085 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;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000088
89 // [span.obs], span observers
Louis Dionne7b89ab02019-11-14 09:07:05 -050090 constexpr size_type size() const noexcept;
91 constexpr size_type size_bytes() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000092 constexpr bool empty() const noexcept;
93
94 // [span.elem], span element access
Louis Dionne7b89ab02019-11-14 09:07:05 -050095 constexpr reference operator[](size_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000096 constexpr reference front() const;
97 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000098 constexpr pointer data() const noexcept;
99
100 // [span.iterators], span iterator support
101 constexpr iterator begin() const noexcept;
102 constexpr iterator end() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000103 constexpr reverse_iterator rbegin() const noexcept;
104 constexpr reverse_iterator rend() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000105
106private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500107 pointer data_; // exposition only
108 size_type size_; // exposition only
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000109};
110
111template<class T, size_t N>
112 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000113
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000114template<class T, size_t N>
115 span(array<T, N>&) -> span<T, N>;
116
117template<class T, size_t N>
118 span(const array<T, N>&) -> span<const T, N>;
119
120template<class Container>
121 span(Container&) -> span<typename Container::value_type>;
122
123template<class Container>
124 span(const Container&) -> span<const typename Container::value_type>;
125
126} // namespace std
127
128*/
129
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000130#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400131#include <__debug>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100132#include <__ranges/enable_borrowed_range.h>
Christopher Di Bella92072c22021-05-14 06:20:07 +0000133#include <__ranges/enable_view.h>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000134#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000135#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100136#include <iterator> // for iterators
137#include <type_traits> // for remove_cv, etc
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000138
139#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
140#pragma GCC system_header
141#endif
142
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500143_LIBCPP_PUSH_MACROS
144#include <__undef_macros>
145
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000146_LIBCPP_BEGIN_NAMESPACE_STD
147
148#if _LIBCPP_STD_VER > 17
149
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500150inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000151template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000152
153
154template <class _Tp>
155struct __is_span_impl : public false_type {};
156
Marshall Clowcd6d8802019-02-27 00:32:16 +0000157template <class _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000158struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
159
160template <class _Tp>
161struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
162
163template <class _Tp>
164struct __is_std_array_impl : public false_type {};
165
166template <class _Tp, size_t _Sz>
167struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
168
169template <class _Tp>
170struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
171
172template <class _Tp, class _ElementType, class = void>
173struct __is_span_compatible_container : public false_type {};
174
175template <class _Tp, class _ElementType>
176struct __is_span_compatible_container<_Tp, _ElementType,
177 void_t<
178 // is not a specialization of span
179 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
180 // is not a specialization of array
181 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
182 // is_array_v<Container> is false,
183 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
184 // data(cont) and size(cont) are well formed
185 decltype(data(declval<_Tp>())),
186 decltype(size(declval<_Tp>())),
187 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
188 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000189 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
190 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000191 nullptr_t>::type
192 >>
193 : public true_type {};
194
195
Marshall Clowcd6d8802019-02-27 00:32:16 +0000196template <typename _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000197class _LIBCPP_TEMPLATE_VIS span {
198public:
199// constants and types
200 using element_type = _Tp;
201 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500202 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000203 using difference_type = ptrdiff_t;
204 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000205 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000206 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000207 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400208#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
209 using iterator = pointer;
210#else
211 using iterator = __wrap_iter<pointer>;
212#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000213 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000214
Louis Dionne7b89ab02019-11-14 09:07:05 -0500215 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000216
Louis Dionne44bcff92018-08-03 22:36:53 +0000217// [span.cons], span constructors, copy, assignment, and destructor
Michael Schellenberger Costa11807352020-05-14 09:28:27 -0400218 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
219 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000220
221 constexpr span (const span&) noexcept = default;
222 constexpr span& operator=(const span&) noexcept = default;
223
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400224 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000225 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400226 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000227 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
228
229 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400230
231 template <class _OtherElementType,
232 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
233 _LIBCPP_INLINE_VISIBILITY
234 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
235
236 template <class _OtherElementType,
237 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
238 _LIBCPP_INLINE_VISIBILITY
239 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000240
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400241 template <class _Container>
242 _LIBCPP_INLINE_VISIBILITY
243 constexpr explicit span( _Container& __c,
244 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
245 : __data{_VSTD::data(__c)} {
246 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
247 }
248
249 template <class _Container>
250 _LIBCPP_INLINE_VISIBILITY
251 constexpr explicit span(const _Container& __c,
252 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
253 : __data{_VSTD::data(__c)} {
254 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
255 }
256
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000257 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000258 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000259 constexpr span(const span<_OtherElementType, _Extent>& __other,
260 enable_if_t<
261 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
262 nullptr_t> = nullptr)
263 : __data{__other.data()} {}
264
265 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000266 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400267 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000268 enable_if_t<
269 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
270 nullptr_t> = nullptr) noexcept
271 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
272
273
274// ~span() noexcept = default;
275
Marshall Clowcd6d8802019-02-27 00:32:16 +0000276 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000277 _LIBCPP_INLINE_VISIBILITY
278 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000279 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000280 static_assert(_Count <= _Extent, "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400281 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000282 }
283
Marshall Clowcd6d8802019-02-27 00:32:16 +0000284 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000285 _LIBCPP_INLINE_VISIBILITY
286 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000287 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000288 static_assert(_Count <= _Extent, "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400289 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000290 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000291
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000292 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500293 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000294 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000295 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000296 return {data(), __count};
297 }
298
299 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500300 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000301 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000302 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000303 return {data() + size() - __count, __count};
304 }
305
Marshall Clowcd6d8802019-02-27 00:32:16 +0000306 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000307 _LIBCPP_INLINE_VISIBILITY
308 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000309 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
310 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000311 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100312 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400313
314 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
315 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000316 }
317
318
Marshall Clowe246c9f2019-03-06 03:59:44 +0000319 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000320 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500321 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000322 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000323 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
324 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000325 if (__count == dynamic_extent)
326 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100327 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000328 return {data() + __offset, __count};
329 }
330
Louis Dionne7b89ab02019-11-14 09:07:05 -0500331 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
332 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
333 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000334
Louis Dionne7b89ab02019-11-14 09:07:05 -0500335 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000336 {
Louis Dionne956eca82020-02-11 11:38:00 +0100337 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000338 return __data[__idx];
339 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000340
Marshall Clow89408802019-02-25 17:54:08 +0000341 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000342 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100343 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000344 return __data[0];
345 }
346
347 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
348 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100349 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000350 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000351 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000352
353 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
354
355// [span.iter], span iterator support
356 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
357 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000358 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
359 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000360
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000361 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400362 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000363
Louis Dionneac9f0c62019-03-28 01:27:52 +0000364 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400365 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000366
367private:
368 pointer __data;
369
370};
371
372
373template <typename _Tp>
374class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
375private:
376
377public:
378// constants and types
379 using element_type = _Tp;
380 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500381 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000382 using difference_type = ptrdiff_t;
383 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000384 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000385 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000386 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400387#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
388 using iterator = pointer;
389#else
390 using iterator = __wrap_iter<pointer>;
391#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000392 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000393
Louis Dionne7b89ab02019-11-14 09:07:05 -0500394 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000395
Louis Dionne44bcff92018-08-03 22:36:53 +0000396// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000397 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
398
399 constexpr span (const span&) noexcept = default;
400 constexpr span& operator=(const span&) noexcept = default;
401
Louis Dionne7b89ab02019-11-14 09:07:05 -0500402 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
Marshall Clowcd6d8802019-02-27 00:32:16 +0000403 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{static_cast<size_t>(distance(__f, __l))} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000404
405 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000406 _LIBCPP_INLINE_VISIBILITY
407 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000408
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400409 template <class _OtherElementType, size_t _Sz,
410 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000411 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400412 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000413
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400414 template <class _OtherElementType, size_t _Sz,
415 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000416 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400417 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000418
419 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000420 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000421 constexpr span( _Container& __c,
422 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500423 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000424
425 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000426 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000427 constexpr span(const _Container& __c,
428 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500429 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000430
431
Marshall Clowcd6d8802019-02-27 00:32:16 +0000432 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000433 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000434 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
435 enable_if_t<
436 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
437 nullptr_t> = nullptr) noexcept
438 : __data{__other.data()}, __size{__other.size()} {}
439
440// ~span() noexcept = default;
441
Marshall Clowcd6d8802019-02-27 00:32:16 +0000442 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000443 _LIBCPP_INLINE_VISIBILITY
444 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000445 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000446 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400447 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000448 }
449
Marshall Clowcd6d8802019-02-27 00:32:16 +0000450 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000451 _LIBCPP_INLINE_VISIBILITY
452 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000453 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000454 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400455 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000456 }
457
458 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500459 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000460 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000461 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000462 return {data(), __count};
463 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000464
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000465 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500466 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000467 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000468 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000469 return {data() + size() - __count, __count};
470 }
471
Marshall Clowcd6d8802019-02-27 00:32:16 +0000472 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000473 _LIBCPP_INLINE_VISIBILITY
Louis Dionne22c735e2020-02-11 11:57:35 +0100474 constexpr span<element_type, _Count> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000475 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000476 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100477 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400478 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000479 }
480
481 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000482 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500483 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000484 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000485 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
486 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000487 if (__count == dynamic_extent)
488 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100489 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000490 return {data() + __offset, __count};
491 }
492
Louis Dionne7b89ab02019-11-14 09:07:05 -0500493 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
494 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
495 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000496
Louis Dionne7b89ab02019-11-14 09:07:05 -0500497 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000498 {
Louis Dionne956eca82020-02-11 11:38:00 +0100499 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000500 return __data[__idx];
501 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000502
Marshall Clow89408802019-02-25 17:54:08 +0000503 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000504 {
Marshall Clow89408802019-02-25 17:54:08 +0000505 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
506 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000507 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000508
Marshall Clow89408802019-02-25 17:54:08 +0000509 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
510 {
511 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
512 return __data[size()-1];
513 }
514
515
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000516 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
517
518// [span.iter], span iterator support
519 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
520 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000521 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
522 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000523
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000524 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
525 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
526
Louis Dionneac9f0c62019-03-28 01:27:52 +0000527 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000528 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
529
530private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500531 pointer __data;
532 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000533};
534
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100535#if !defined(_LIBCPP_HAS_NO_RANGES)
536template <class _Tp, size_t _Extent>
537inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
Christopher Di Bella92072c22021-05-14 06:20:07 +0000538
539template <class _ElementType, size_t _Extent>
540inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100541#endif // !defined(_LIBCPP_HAS_NO_RANGES)
542
Louis Dionneac9f0c62019-03-28 01:27:52 +0000543// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000544template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000545_LIBCPP_INLINE_VISIBILITY
546auto as_bytes(span<_Tp, _Extent> __s) noexcept
547-> decltype(__s.__as_bytes())
548{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000549
Marshall Clowcd6d8802019-02-27 00:32:16 +0000550template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000551_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000552auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
553-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
554{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000555
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000556// Deduction guides
557template<class _Tp, size_t _Sz>
558 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000559
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000560template<class _Tp, size_t _Sz>
561 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
562
563template<class _Tp, size_t _Sz>
564 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
565
566template<class _Container>
567 span(_Container&) -> span<typename _Container::value_type>;
568
569template<class _Container>
570 span(const _Container&) -> span<const typename _Container::value_type>;
571
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000572#endif // _LIBCPP_STD_VER > 17
573
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000574_LIBCPP_END_NAMESPACE_STD
575
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500576_LIBCPP_POP_MACROS
577
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000578#endif // _LIBCPP_SPAN