blob: 7ae0bdbd06b3ec491b58a99eefd9e42ff0732179 [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
Marshall Clowd2ad87e2018-07-24 03:01:02 +000025// [span.objectrep], views of object representation
Marshall Clowcd6d8802019-02-27 00:32:16 +000026template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000027 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000028 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000029
Marshall Clowcd6d8802019-02-27 00:32:16 +000030template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000031 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000032 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000033
34
35namespace std {
Marshall Clowcd6d8802019-02-27 00:32:16 +000036template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000037class span {
38public:
39 // constants and types
40 using element_type = ElementType;
41 using value_type = remove_cv_t<ElementType>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050042 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000043 using difference_type = ptrdiff_t;
44 using pointer = element_type*;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000045 using const_pointer = const element_type*;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000046 using reference = element_type&;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000047 using const_reference = const element_type&;
Marshall Clowcd6d8802019-02-27 00:32:16 +000048 using iterator = implementation-defined;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000049 using reverse_iterator = std::reverse_iterator<iterator>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050050 static constexpr size_type extent = Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000051
52 // [span.cons], span constructors, copy, assignment, and destructor
53 constexpr span() noexcept;
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040054 constexpr explicit(Extent != dynamic_extent) span(pointer ptr, size_type count);
55 constexpr explicit(Extent != dynamic_extent) span(pointer firstElem, pointer lastElem);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000056 template <size_t N>
57 constexpr span(element_type (&arr)[N]) noexcept;
58 template <size_t N>
59 constexpr span(array<value_type, N>& arr) noexcept;
60 template <size_t N>
61 constexpr span(const array<value_type, N>& arr) noexcept;
62 template <class Container>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040063 constexpr explicit(Extent != dynamic_extent) span(Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000064 template <class Container>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040065 constexpr explicit(Extent != dynamic_extent) span(const Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000066 constexpr span(const span& other) noexcept = default;
Marshall Clowcd6d8802019-02-27 00:32:16 +000067 template <class OtherElementType, size_t OtherExtent>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040068 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000069 ~span() noexcept = default;
70 constexpr span& operator=(const span& other) noexcept = default;
71
72 // [span.sub], span subviews
Marshall Clowcd6d8802019-02-27 00:32:16 +000073 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000074 constexpr span<element_type, Count> first() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000075 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000076 constexpr span<element_type, Count> last() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000077 template <size_t Offset, size_t Count = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000078 constexpr span<element_type, see below> subspan() const;
79
Louis Dionne7b89ab02019-11-14 09:07:05 -050080 constexpr span<element_type, dynamic_extent> first(size_type count) const;
81 constexpr span<element_type, dynamic_extent> last(size_type count) const;
82 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000083
84 // [span.obs], span observers
Louis Dionne7b89ab02019-11-14 09:07:05 -050085 constexpr size_type size() const noexcept;
86 constexpr size_type size_bytes() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000087 constexpr bool empty() const noexcept;
88
89 // [span.elem], span element access
Louis Dionne7b89ab02019-11-14 09:07:05 -050090 constexpr reference operator[](size_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000091 constexpr reference front() const;
92 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000093 constexpr pointer data() const noexcept;
94
95 // [span.iterators], span iterator support
96 constexpr iterator begin() const noexcept;
97 constexpr iterator end() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000098 constexpr reverse_iterator rbegin() const noexcept;
99 constexpr reverse_iterator rend() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000100
101private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500102 pointer data_; // exposition only
103 size_type size_; // exposition only
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000104};
105
106template<class T, size_t N>
107 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000108
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000109template<class T, size_t N>
110 span(array<T, N>&) -> span<T, N>;
111
112template<class T, size_t N>
113 span(const array<T, N>&) -> span<const T, N>;
114
115template<class Container>
116 span(Container&) -> span<typename Container::value_type>;
117
118template<class Container>
119 span(const Container&) -> span<const typename Container::value_type>;
120
121} // namespace std
122
123*/
124
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000125#include <__config>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000126#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000127#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100128#include <iterator> // for iterators
129#include <type_traits> // for remove_cv, etc
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000130
131#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
132#pragma GCC system_header
133#endif
134
135_LIBCPP_BEGIN_NAMESPACE_STD
136
137#if _LIBCPP_STD_VER > 17
138
Louis Dionnecba47f72020-02-10 13:38:04 +0100139inline constexpr size_t dynamic_extent = (numeric_limits<size_t>::max)();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000140template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000141
142
143template <class _Tp>
144struct __is_span_impl : public false_type {};
145
Marshall Clowcd6d8802019-02-27 00:32:16 +0000146template <class _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000147struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
148
149template <class _Tp>
150struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
151
152template <class _Tp>
153struct __is_std_array_impl : public false_type {};
154
155template <class _Tp, size_t _Sz>
156struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
157
158template <class _Tp>
159struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
160
161template <class _Tp, class _ElementType, class = void>
162struct __is_span_compatible_container : public false_type {};
163
164template <class _Tp, class _ElementType>
165struct __is_span_compatible_container<_Tp, _ElementType,
166 void_t<
167 // is not a specialization of span
168 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
169 // is not a specialization of array
170 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
171 // is_array_v<Container> is false,
172 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
173 // data(cont) and size(cont) are well formed
174 decltype(data(declval<_Tp>())),
175 decltype(size(declval<_Tp>())),
176 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
177 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000178 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
179 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000180 nullptr_t>::type
181 >>
182 : public true_type {};
183
184
Marshall Clowcd6d8802019-02-27 00:32:16 +0000185template <typename _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000186class _LIBCPP_TEMPLATE_VIS span {
187public:
188// constants and types
189 using element_type = _Tp;
190 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500191 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000192 using difference_type = ptrdiff_t;
193 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000194 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000195 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000196 using const_reference = const _Tp &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000197 using iterator = __wrap_iter<pointer>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000198 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000199
Louis Dionne7b89ab02019-11-14 09:07:05 -0500200 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000201
Louis Dionne44bcff92018-08-03 22:36:53 +0000202// [span.cons], span constructors, copy, assignment, and destructor
Michael Schellenberger Costa11807352020-05-14 09:28:27 -0400203 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
204 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000205
206 constexpr span (const span&) noexcept = default;
207 constexpr span& operator=(const span&) noexcept = default;
208
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400209 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000210 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400211 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000212 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
213
214 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400215
216 template <class _OtherElementType,
217 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
218 _LIBCPP_INLINE_VISIBILITY
219 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
220
221 template <class _OtherElementType,
222 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
223 _LIBCPP_INLINE_VISIBILITY
224 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000225
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400226 template <class _Container>
227 _LIBCPP_INLINE_VISIBILITY
228 constexpr explicit span( _Container& __c,
229 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
230 : __data{_VSTD::data(__c)} {
231 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
232 }
233
234 template <class _Container>
235 _LIBCPP_INLINE_VISIBILITY
236 constexpr explicit span(const _Container& __c,
237 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
238 : __data{_VSTD::data(__c)} {
239 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
240 }
241
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000242 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000243 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000244 constexpr span(const span<_OtherElementType, _Extent>& __other,
245 enable_if_t<
246 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
247 nullptr_t> = nullptr)
248 : __data{__other.data()} {}
249
250 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000251 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400252 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000253 enable_if_t<
254 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
255 nullptr_t> = nullptr) noexcept
256 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
257
258
259// ~span() noexcept = default;
260
Marshall Clowcd6d8802019-02-27 00:32:16 +0000261 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000262 _LIBCPP_INLINE_VISIBILITY
263 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000264 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000265 static_assert(_Count <= _Extent, "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400266 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000267 }
268
Marshall Clowcd6d8802019-02-27 00:32:16 +0000269 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000270 _LIBCPP_INLINE_VISIBILITY
271 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000272 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000273 static_assert(_Count <= _Extent, "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400274 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000275 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000276
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000277 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500278 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000279 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000280 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000281 return {data(), __count};
282 }
283
284 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500285 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000286 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000287 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000288 return {data() + size() - __count, __count};
289 }
290
Marshall Clowcd6d8802019-02-27 00:32:16 +0000291 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000292 _LIBCPP_INLINE_VISIBILITY
293 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000294 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
295 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000296 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100297 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400298
299 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
300 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000301 }
302
303
Marshall Clowe246c9f2019-03-06 03:59:44 +0000304 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000305 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500306 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000307 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000308 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
309 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000310 if (__count == dynamic_extent)
311 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100312 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000313 return {data() + __offset, __count};
314 }
315
Louis Dionne7b89ab02019-11-14 09:07:05 -0500316 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
317 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
318 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000319
Louis Dionne7b89ab02019-11-14 09:07:05 -0500320 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000321 {
Louis Dionne956eca82020-02-11 11:38:00 +0100322 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000323 return __data[__idx];
324 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000325
Marshall Clow89408802019-02-25 17:54:08 +0000326 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000327 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100328 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000329 return __data[0];
330 }
331
332 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
333 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100334 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000335 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000336 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000337
338 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
339
340// [span.iter], span iterator support
341 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
342 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000343 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
344 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000345
346 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
347 {
348 pointer __p = __data;
349 __data = __other.__data;
350 __other.__data = __p;
351 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000352
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000353 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400354 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000355
Louis Dionneac9f0c62019-03-28 01:27:52 +0000356 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400357 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000358
359private:
360 pointer __data;
361
362};
363
364
365template <typename _Tp>
366class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
367private:
368
369public:
370// constants and types
371 using element_type = _Tp;
372 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500373 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000374 using difference_type = ptrdiff_t;
375 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000376 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000377 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000378 using const_reference = const _Tp &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000379 using iterator = __wrap_iter<pointer>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000380 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000381
Louis Dionne7b89ab02019-11-14 09:07:05 -0500382 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000383
Louis Dionne44bcff92018-08-03 22:36:53 +0000384// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000385 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
386
387 constexpr span (const span&) noexcept = default;
388 constexpr span& operator=(const span&) noexcept = default;
389
Louis Dionne7b89ab02019-11-14 09:07:05 -0500390 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
Marshall Clowcd6d8802019-02-27 00:32:16 +0000391 _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 +0000392
393 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000394 _LIBCPP_INLINE_VISIBILITY
395 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000396
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400397 template <class _OtherElementType, size_t _Sz,
398 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000399 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400400 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000401
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400402 template <class _OtherElementType, size_t _Sz,
403 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000404 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400405 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000406
407 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000408 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000409 constexpr span( _Container& __c,
410 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500411 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000412
413 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000414 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000415 constexpr span(const _Container& __c,
416 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500417 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000418
419
Marshall Clowcd6d8802019-02-27 00:32:16 +0000420 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000421 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000422 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
423 enable_if_t<
424 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
425 nullptr_t> = nullptr) noexcept
426 : __data{__other.data()}, __size{__other.size()} {}
427
428// ~span() noexcept = default;
429
Marshall Clowcd6d8802019-02-27 00:32:16 +0000430 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000431 _LIBCPP_INLINE_VISIBILITY
432 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000433 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000434 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400435 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000436 }
437
Marshall Clowcd6d8802019-02-27 00:32:16 +0000438 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000439 _LIBCPP_INLINE_VISIBILITY
440 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000441 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000442 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400443 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000444 }
445
446 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500447 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000448 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000449 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000450 return {data(), __count};
451 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000452
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000453 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500454 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000455 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000456 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000457 return {data() + size() - __count, __count};
458 }
459
Marshall Clowcd6d8802019-02-27 00:32:16 +0000460 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000461 _LIBCPP_INLINE_VISIBILITY
Louis Dionne22c735e2020-02-11 11:57:35 +0100462 constexpr span<element_type, _Count> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000463 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000464 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100465 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400466 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000467 }
468
469 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000470 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500471 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000472 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000473 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
474 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000475 if (__count == dynamic_extent)
476 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100477 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000478 return {data() + __offset, __count};
479 }
480
Louis Dionne7b89ab02019-11-14 09:07:05 -0500481 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
482 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
483 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000484
Louis Dionne7b89ab02019-11-14 09:07:05 -0500485 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000486 {
Louis Dionne956eca82020-02-11 11:38:00 +0100487 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000488 return __data[__idx];
489 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000490
Marshall Clow89408802019-02-25 17:54:08 +0000491 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000492 {
Marshall Clow89408802019-02-25 17:54:08 +0000493 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
494 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000495 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000496
Marshall Clow89408802019-02-25 17:54:08 +0000497 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
498 {
499 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
500 return __data[size()-1];
501 }
502
503
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000504 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
505
506// [span.iter], span iterator support
507 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
508 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000509 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
510 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000511
512 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
513 {
514 pointer __p = __data;
515 __data = __other.__data;
516 __other.__data = __p;
517
Louis Dionne7b89ab02019-11-14 09:07:05 -0500518 size_type __sz = __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000519 __size = __other.__size;
520 __other.__size = __sz;
521 }
522
523 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
524 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
525
Louis Dionneac9f0c62019-03-28 01:27:52 +0000526 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000527 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
528
529private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500530 pointer __data;
531 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000532};
533
Louis Dionneac9f0c62019-03-28 01:27:52 +0000534// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000535template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000536_LIBCPP_INLINE_VISIBILITY
537auto as_bytes(span<_Tp, _Extent> __s) noexcept
538-> decltype(__s.__as_bytes())
539{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000540
Marshall Clowcd6d8802019-02-27 00:32:16 +0000541template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000542_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000543auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
544-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
545{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000546
Marshall Clowcd6d8802019-02-27 00:32:16 +0000547template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000548_LIBCPP_INLINE_VISIBILITY
549constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
550{ __lhs.swap(__rhs); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000551
552
553// Deduction guides
554template<class _Tp, size_t _Sz>
555 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000556
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000557template<class _Tp, size_t _Sz>
558 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
559
560template<class _Tp, size_t _Sz>
561 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
562
563template<class _Container>
564 span(_Container&) -> span<typename _Container::value_type>;
565
566template<class _Container>
567 span(const _Container&) -> span<const typename _Container::value_type>;
568
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000569#endif // _LIBCPP_STD_VER > 17
570
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000571_LIBCPP_END_NAMESPACE_STD
572
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000573#endif // _LIBCPP_SPAN