blob: 260a74404fe58a9ec8e28a422148066d86e82422 [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>
Louis Dionne77249522021-06-11 09:55:11 -0400132#include <__iterator/wrap_iter.h>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100133#include <__ranges/enable_borrowed_range.h>
Christopher Di Bella92072c22021-05-14 06:20:07 +0000134#include <__ranges/enable_view.h>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000135#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000136#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100137#include <iterator> // for iterators
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000138#include <limits>
Louis Dionne533a14e2020-02-11 11:16:40 +0100139#include <type_traits> // for remove_cv, etc
Christopher Di Bella60fc6602021-07-13 19:44:07 +0000140#include <version>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000141
142#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
143#pragma GCC system_header
144#endif
145
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500146_LIBCPP_PUSH_MACROS
147#include <__undef_macros>
148
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000149_LIBCPP_BEGIN_NAMESPACE_STD
150
151#if _LIBCPP_STD_VER > 17
152
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500153inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000154template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000155
156
157template <class _Tp>
158struct __is_span_impl : public false_type {};
159
Marshall Clowcd6d8802019-02-27 00:32:16 +0000160template <class _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000161struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
162
163template <class _Tp>
164struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
165
166template <class _Tp>
167struct __is_std_array_impl : public false_type {};
168
169template <class _Tp, size_t _Sz>
170struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
171
172template <class _Tp>
173struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
174
175template <class _Tp, class _ElementType, class = void>
176struct __is_span_compatible_container : public false_type {};
177
178template <class _Tp, class _ElementType>
179struct __is_span_compatible_container<_Tp, _ElementType,
180 void_t<
181 // is not a specialization of span
182 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
183 // is not a specialization of array
184 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
185 // is_array_v<Container> is false,
186 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
187 // data(cont) and size(cont) are well formed
188 decltype(data(declval<_Tp>())),
189 decltype(size(declval<_Tp>())),
190 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
191 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000192 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
193 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000194 nullptr_t>::type
195 >>
196 : public true_type {};
197
198
Marshall Clowcd6d8802019-02-27 00:32:16 +0000199template <typename _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000200class _LIBCPP_TEMPLATE_VIS span {
201public:
202// constants and types
203 using element_type = _Tp;
204 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500205 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000206 using difference_type = ptrdiff_t;
207 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000208 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000209 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000210 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400211#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
212 using iterator = pointer;
213#else
214 using iterator = __wrap_iter<pointer>;
215#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000216 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000217
Louis Dionne7b89ab02019-11-14 09:07:05 -0500218 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000219
Louis Dionne44bcff92018-08-03 22:36:53 +0000220// [span.cons], span constructors, copy, assignment, and destructor
Michael Schellenberger Costa11807352020-05-14 09:28:27 -0400221 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
222 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000223
224 constexpr span (const span&) noexcept = default;
225 constexpr span& operator=(const span&) noexcept = default;
226
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400227 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000228 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400229 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000230 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
231
232 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400233
234 template <class _OtherElementType,
235 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
236 _LIBCPP_INLINE_VISIBILITY
237 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
238
239 template <class _OtherElementType,
240 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
241 _LIBCPP_INLINE_VISIBILITY
242 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000243
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400244 template <class _Container>
245 _LIBCPP_INLINE_VISIBILITY
246 constexpr explicit span( _Container& __c,
247 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
248 : __data{_VSTD::data(__c)} {
249 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
250 }
251
252 template <class _Container>
253 _LIBCPP_INLINE_VISIBILITY
254 constexpr explicit span(const _Container& __c,
255 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
256 : __data{_VSTD::data(__c)} {
257 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
258 }
259
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000260 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000261 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000262 constexpr span(const span<_OtherElementType, _Extent>& __other,
263 enable_if_t<
264 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
265 nullptr_t> = nullptr)
266 : __data{__other.data()} {}
267
268 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000269 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400270 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000271 enable_if_t<
272 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
273 nullptr_t> = nullptr) noexcept
274 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
275
276
277// ~span() noexcept = default;
278
Marshall Clowcd6d8802019-02-27 00:32:16 +0000279 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000280 _LIBCPP_INLINE_VISIBILITY
281 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000282 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000283 static_assert(_Count <= _Extent, "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400284 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000285 }
286
Marshall Clowcd6d8802019-02-27 00:32:16 +0000287 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000288 _LIBCPP_INLINE_VISIBILITY
289 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000290 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000291 static_assert(_Count <= _Extent, "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400292 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000293 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000294
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000295 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500296 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000297 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000298 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000299 return {data(), __count};
300 }
301
302 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500303 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000304 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000305 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000306 return {data() + size() - __count, __count};
307 }
308
Marshall Clowcd6d8802019-02-27 00:32:16 +0000309 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000310 _LIBCPP_INLINE_VISIBILITY
311 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000312 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
313 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000314 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100315 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400316
317 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
318 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000319 }
320
321
Marshall Clowe246c9f2019-03-06 03:59:44 +0000322 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000323 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500324 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000325 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000326 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
327 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000328 if (__count == dynamic_extent)
329 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100330 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000331 return {data() + __offset, __count};
332 }
333
Louis Dionne7b89ab02019-11-14 09:07:05 -0500334 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
335 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
336 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000337
Louis Dionne7b89ab02019-11-14 09:07:05 -0500338 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000339 {
Louis Dionne956eca82020-02-11 11:38:00 +0100340 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000341 return __data[__idx];
342 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000343
Marshall Clow89408802019-02-25 17:54:08 +0000344 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000345 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100346 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000347 return __data[0];
348 }
349
350 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
351 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100352 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000353 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000354 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000355
356 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
357
358// [span.iter], span iterator support
359 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
360 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000361 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
362 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000363
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000364 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400365 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000366
Louis Dionneac9f0c62019-03-28 01:27:52 +0000367 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400368 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000369
370private:
371 pointer __data;
372
373};
374
375
376template <typename _Tp>
377class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
378private:
379
380public:
381// constants and types
382 using element_type = _Tp;
383 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500384 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000385 using difference_type = ptrdiff_t;
386 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000387 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000388 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000389 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400390#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
391 using iterator = pointer;
392#else
393 using iterator = __wrap_iter<pointer>;
394#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000395 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000396
Louis Dionne7b89ab02019-11-14 09:07:05 -0500397 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000398
Louis Dionne44bcff92018-08-03 22:36:53 +0000399// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000400 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
401
402 constexpr span (const span&) noexcept = default;
403 constexpr span& operator=(const span&) noexcept = default;
404
Louis Dionne7b89ab02019-11-14 09:07:05 -0500405 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
Marshall Clowcd6d8802019-02-27 00:32:16 +0000406 _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 +0000407
408 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000409 _LIBCPP_INLINE_VISIBILITY
410 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000411
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400412 template <class _OtherElementType, size_t _Sz,
413 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000414 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400415 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000416
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400417 template <class _OtherElementType, size_t _Sz,
418 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000419 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400420 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000421
422 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000423 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000424 constexpr span( _Container& __c,
425 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500426 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000427
428 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000429 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000430 constexpr span(const _Container& __c,
431 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500432 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000433
434
Marshall Clowcd6d8802019-02-27 00:32:16 +0000435 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000436 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000437 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
438 enable_if_t<
439 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
440 nullptr_t> = nullptr) noexcept
441 : __data{__other.data()}, __size{__other.size()} {}
442
443// ~span() noexcept = default;
444
Marshall Clowcd6d8802019-02-27 00:32:16 +0000445 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000446 _LIBCPP_INLINE_VISIBILITY
447 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000448 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000449 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400450 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000451 }
452
Marshall Clowcd6d8802019-02-27 00:32:16 +0000453 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000454 _LIBCPP_INLINE_VISIBILITY
455 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000456 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000457 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400458 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000459 }
460
461 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500462 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000463 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000464 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000465 return {data(), __count};
466 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000467
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000468 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500469 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000470 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000471 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000472 return {data() + size() - __count, __count};
473 }
474
Marshall Clowcd6d8802019-02-27 00:32:16 +0000475 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000476 _LIBCPP_INLINE_VISIBILITY
Louis Dionne22c735e2020-02-11 11:57:35 +0100477 constexpr span<element_type, _Count> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000478 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000479 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100480 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400481 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000482 }
483
484 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000485 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500486 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000487 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000488 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
489 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000490 if (__count == dynamic_extent)
491 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100492 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000493 return {data() + __offset, __count};
494 }
495
Louis Dionne7b89ab02019-11-14 09:07:05 -0500496 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
497 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
498 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000499
Louis Dionne7b89ab02019-11-14 09:07:05 -0500500 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000501 {
Louis Dionne956eca82020-02-11 11:38:00 +0100502 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000503 return __data[__idx];
504 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000505
Marshall Clow89408802019-02-25 17:54:08 +0000506 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000507 {
Marshall Clow89408802019-02-25 17:54:08 +0000508 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
509 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000510 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000511
Marshall Clow89408802019-02-25 17:54:08 +0000512 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
513 {
514 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
515 return __data[size()-1];
516 }
517
518
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000519 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
520
521// [span.iter], span iterator support
522 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
523 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000524 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
525 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000526
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000527 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
528 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
529
Louis Dionneac9f0c62019-03-28 01:27:52 +0000530 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000531 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
532
533private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500534 pointer __data;
535 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000536};
537
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100538#if !defined(_LIBCPP_HAS_NO_RANGES)
539template <class _Tp, size_t _Extent>
540inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
Christopher Di Bella92072c22021-05-14 06:20:07 +0000541
542template <class _ElementType, size_t _Extent>
543inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100544#endif // !defined(_LIBCPP_HAS_NO_RANGES)
545
Louis Dionneac9f0c62019-03-28 01:27:52 +0000546// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000547template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000548_LIBCPP_INLINE_VISIBILITY
549auto as_bytes(span<_Tp, _Extent> __s) noexcept
550-> decltype(__s.__as_bytes())
551{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000552
Marshall Clowcd6d8802019-02-27 00:32:16 +0000553template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000554_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000555auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
556-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
557{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000558
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000559// Deduction guides
560template<class _Tp, size_t _Sz>
561 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000562
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000563template<class _Tp, size_t _Sz>
564 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
565
566template<class _Tp, size_t _Sz>
567 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
568
569template<class _Container>
570 span(_Container&) -> span<typename _Container::value_type>;
571
572template<class _Container>
573 span(const _Container&) -> span<const typename _Container::value_type>;
574
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000575#endif // _LIBCPP_STD_VER > 17
576
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000577_LIBCPP_END_NAMESPACE_STD
578
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500579_LIBCPP_POP_MACROS
580
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000581#endif // _LIBCPP_SPAN