blob: f18d00c4392846c388f54e1969d61b2e30778818 [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 const_iterator = implementation-defined;
50 using reverse_iterator = std::reverse_iterator<iterator>;
51 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050052 static constexpr size_type extent = Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000053
54 // [span.cons], span constructors, copy, assignment, and destructor
55 constexpr span() noexcept;
Louis Dionne7b89ab02019-11-14 09:07:05 -050056 constexpr span(pointer ptr, size_type count);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000057 constexpr span(pointer firstElem, pointer lastElem);
58 template <size_t N>
59 constexpr span(element_type (&arr)[N]) noexcept;
60 template <size_t N>
61 constexpr span(array<value_type, N>& arr) noexcept;
62 template <size_t N>
63 constexpr span(const array<value_type, N>& arr) noexcept;
64 template <class Container>
65 constexpr span(Container& cont);
66 template <class Container>
67 constexpr span(const Container& cont);
68 constexpr span(const span& other) noexcept = default;
Marshall Clowcd6d8802019-02-27 00:32:16 +000069 template <class OtherElementType, size_t OtherExtent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000070 constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
71 ~span() noexcept = default;
72 constexpr span& operator=(const span& other) noexcept = default;
73
74 // [span.sub], span subviews
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> first() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000077 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000078 constexpr span<element_type, Count> last() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000079 template <size_t Offset, size_t Count = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000080 constexpr span<element_type, see below> subspan() const;
81
Louis Dionne7b89ab02019-11-14 09:07:05 -050082 constexpr span<element_type, dynamic_extent> first(size_type count) const;
83 constexpr span<element_type, dynamic_extent> last(size_type count) const;
84 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000085
86 // [span.obs], span observers
Louis Dionne7b89ab02019-11-14 09:07:05 -050087 constexpr size_type size() const noexcept;
88 constexpr size_type size_bytes() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000089 constexpr bool empty() const noexcept;
90
91 // [span.elem], span element access
Louis Dionne7b89ab02019-11-14 09:07:05 -050092 constexpr reference operator[](size_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000093 constexpr reference front() const;
94 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000095 constexpr pointer data() const noexcept;
96
97 // [span.iterators], span iterator support
98 constexpr iterator begin() const noexcept;
99 constexpr iterator end() const noexcept;
100 constexpr const_iterator cbegin() const noexcept;
101 constexpr const_iterator cend() const noexcept;
102 constexpr reverse_iterator rbegin() const noexcept;
103 constexpr reverse_iterator rend() const noexcept;
104 constexpr const_reverse_iterator crbegin() const noexcept;
105 constexpr const_reverse_iterator crend() const noexcept;
106
107private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500108 pointer data_; // exposition only
109 size_type size_; // exposition only
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000110};
111
112template<class T, size_t N>
113 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000114
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000115template<class T, size_t N>
116 span(array<T, N>&) -> span<T, N>;
117
118template<class T, size_t N>
119 span(const array<T, N>&) -> span<const T, N>;
120
121template<class Container>
122 span(Container&) -> span<typename Container::value_type>;
123
124template<class Container>
125 span(const Container&) -> span<const typename Container::value_type>;
126
127} // namespace std
128
129*/
130
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000131#include <__config>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000132#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000133#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100134#include <iterator> // for iterators
135#include <type_traits> // for remove_cv, etc
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000136
137#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
138#pragma GCC system_header
139#endif
140
141_LIBCPP_BEGIN_NAMESPACE_STD
142
143#if _LIBCPP_STD_VER > 17
144
Louis Dionnecba47f72020-02-10 13:38:04 +0100145inline constexpr size_t dynamic_extent = (numeric_limits<size_t>::max)();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000146template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000147
148
149template <class _Tp>
150struct __is_span_impl : public false_type {};
151
Marshall Clowcd6d8802019-02-27 00:32:16 +0000152template <class _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000153struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
154
155template <class _Tp>
156struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
157
158template <class _Tp>
159struct __is_std_array_impl : public false_type {};
160
161template <class _Tp, size_t _Sz>
162struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
163
164template <class _Tp>
165struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
166
167template <class _Tp, class _ElementType, class = void>
168struct __is_span_compatible_container : public false_type {};
169
170template <class _Tp, class _ElementType>
171struct __is_span_compatible_container<_Tp, _ElementType,
172 void_t<
173 // is not a specialization of span
174 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
175 // is not a specialization of array
176 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
177 // is_array_v<Container> is false,
178 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
179 // data(cont) and size(cont) are well formed
180 decltype(data(declval<_Tp>())),
181 decltype(size(declval<_Tp>())),
182 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
183 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000184 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
185 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000186 nullptr_t>::type
187 >>
188 : public true_type {};
189
190
Marshall Clowcd6d8802019-02-27 00:32:16 +0000191template <typename _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000192class _LIBCPP_TEMPLATE_VIS span {
193public:
194// constants and types
195 using element_type = _Tp;
196 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500197 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000198 using difference_type = ptrdiff_t;
199 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000200 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000201 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000202 using const_reference = const _Tp &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000203 using iterator = __wrap_iter<pointer>;
204 using const_iterator = __wrap_iter<const_pointer>;
205 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
206 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
207
Louis Dionne7b89ab02019-11-14 09:07:05 -0500208 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000209
Louis Dionne44bcff92018-08-03 22:36:53 +0000210// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000211 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}
212 { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); }
213
214 constexpr span (const span&) noexcept = default;
215 constexpr span& operator=(const span&) noexcept = default;
216
Louis Dionne7b89ab02019-11-14 09:07:05 -0500217 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000218 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
219 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}
220 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
221
222 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
223 _LIBCPP_INLINE_VISIBILITY constexpr span( array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {}
224 _LIBCPP_INLINE_VISIBILITY constexpr span(const array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {}
225
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000226 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000227 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000228 constexpr span(const span<_OtherElementType, _Extent>& __other,
229 enable_if_t<
230 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
231 nullptr_t> = nullptr)
232 : __data{__other.data()} {}
233
234 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000235 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000236 constexpr span(const span<_OtherElementType, dynamic_extent>& __other,
237 enable_if_t<
238 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
239 nullptr_t> = nullptr) noexcept
240 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
241
242
243// ~span() noexcept = default;
244
Marshall Clowcd6d8802019-02-27 00:32:16 +0000245 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000246 _LIBCPP_INLINE_VISIBILITY
247 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000248 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000249 static_assert(_Count <= _Extent, "Count out of range in span::first()");
250 return {data(), _Count};
251 }
252
Marshall Clowcd6d8802019-02-27 00:32:16 +0000253 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000254 _LIBCPP_INLINE_VISIBILITY
255 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000256 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000257 static_assert(_Count <= _Extent, "Count out of range in span::last()");
258 return {data() + size() - _Count, _Count};
259 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000260
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000261 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500262 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000263 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000264 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000265 return {data(), __count};
266 }
267
268 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500269 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000270 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000271 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000272 return {data() + size() - __count, __count};
273 }
274
Marshall Clowcd6d8802019-02-27 00:32:16 +0000275 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000276 _LIBCPP_INLINE_VISIBILITY
277 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000278 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
279 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000280 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000281 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
282 }
283
284
Marshall Clowe246c9f2019-03-06 03:59:44 +0000285 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000286 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500287 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000288 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000289 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
290 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000291 if (__count == dynamic_extent)
292 return {data() + __offset, size() - __offset};
Marshall Clowe246c9f2019-03-06 03:59:44 +0000293 _LIBCPP_ASSERT(__offset <= size() - __count, "count + offset out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000294 return {data() + __offset, __count};
295 }
296
Louis Dionne7b89ab02019-11-14 09:07:05 -0500297 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
298 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
299 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000300
Louis Dionne7b89ab02019-11-14 09:07:05 -0500301 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000302 {
Louis Dionne956eca82020-02-11 11:38:00 +0100303 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000304 return __data[__idx];
305 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000306
Marshall Clow89408802019-02-25 17:54:08 +0000307 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000308 {
Marshall Clow89408802019-02-25 17:54:08 +0000309 static_assert(_Extent > 0, "span<T,N>[].front() on empty span");
310 return __data[0];
311 }
312
313 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
314 {
315 static_assert(_Extent > 0, "span<T,N>[].back() on empty span");
316 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000317 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000318
319 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
320
321// [span.iter], span iterator support
322 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
323 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
324 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
325 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
326 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
327 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
328 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
329 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
330
331 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
332 {
333 pointer __p = __data;
334 __data = __other.__data;
335 __other.__data = __p;
336 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000337
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000338 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
339 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
340
Louis Dionneac9f0c62019-03-28 01:27:52 +0000341 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000342 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
343
344private:
345 pointer __data;
346
347};
348
349
350template <typename _Tp>
351class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
352private:
353
354public:
355// constants and types
356 using element_type = _Tp;
357 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500358 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000359 using difference_type = ptrdiff_t;
360 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000361 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000362 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000363 using const_reference = const _Tp &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000364 using iterator = __wrap_iter<pointer>;
365 using const_iterator = __wrap_iter<const_pointer>;
366 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
367 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
368
Louis Dionne7b89ab02019-11-14 09:07:05 -0500369 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000370
Louis Dionne44bcff92018-08-03 22:36:53 +0000371// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000372 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
373
374 constexpr span (const span&) noexcept = default;
375 constexpr span& operator=(const span&) noexcept = default;
376
Louis Dionne7b89ab02019-11-14 09:07:05 -0500377 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
Marshall Clowcd6d8802019-02-27 00:32:16 +0000378 _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 +0000379
380 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000381 _LIBCPP_INLINE_VISIBILITY
382 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000383
384 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000385 _LIBCPP_INLINE_VISIBILITY
386 constexpr span(array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000387
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000388 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000389 _LIBCPP_INLINE_VISIBILITY
390 constexpr span(const array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000391
392 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000393 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000394 constexpr span( _Container& __c,
395 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500396 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000397
398 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000399 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000400 constexpr span(const _Container& __c,
401 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500402 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000403
404
Marshall Clowcd6d8802019-02-27 00:32:16 +0000405 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000406 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000407 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
408 enable_if_t<
409 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
410 nullptr_t> = nullptr) noexcept
411 : __data{__other.data()}, __size{__other.size()} {}
412
413// ~span() noexcept = default;
414
Marshall Clowcd6d8802019-02-27 00:32:16 +0000415 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000416 _LIBCPP_INLINE_VISIBILITY
417 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000418 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000419 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
420 return {data(), _Count};
421 }
422
Marshall Clowcd6d8802019-02-27 00:32:16 +0000423 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000424 _LIBCPP_INLINE_VISIBILITY
425 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000426 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000427 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
428 return {data() + size() - _Count, _Count};
429 }
430
431 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500432 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000433 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000434 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000435 return {data(), __count};
436 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000437
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000438 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500439 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000440 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000441 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000442 return {data() + size() - __count, __count};
443 }
444
Marshall Clowcd6d8802019-02-27 00:32:16 +0000445 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000446 _LIBCPP_INLINE_VISIBILITY
447 constexpr span<_Tp, dynamic_extent> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000448 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000449 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000450 _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()");
451 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
452 }
453
454 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000455 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500456 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000457 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000458 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
459 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000460 if (__count == dynamic_extent)
461 return {data() + __offset, size() - __offset};
Marshall Clowe246c9f2019-03-06 03:59:44 +0000462 _LIBCPP_ASSERT(__offset <= size() - __count, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000463 return {data() + __offset, __count};
464 }
465
Louis Dionne7b89ab02019-11-14 09:07:05 -0500466 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
467 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
468 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000469
Louis Dionne7b89ab02019-11-14 09:07:05 -0500470 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000471 {
Louis Dionne956eca82020-02-11 11:38:00 +0100472 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000473 return __data[__idx];
474 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000475
Marshall Clow89408802019-02-25 17:54:08 +0000476 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000477 {
Marshall Clow89408802019-02-25 17:54:08 +0000478 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
479 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000480 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000481
Marshall Clow89408802019-02-25 17:54:08 +0000482 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
483 {
484 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
485 return __data[size()-1];
486 }
487
488
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000489 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
490
491// [span.iter], span iterator support
492 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
493 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
494 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
495 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
496 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
497 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
498 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
499 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
500
501 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
502 {
503 pointer __p = __data;
504 __data = __other.__data;
505 __other.__data = __p;
506
Louis Dionne7b89ab02019-11-14 09:07:05 -0500507 size_type __sz = __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000508 __size = __other.__size;
509 __other.__size = __sz;
510 }
511
512 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
513 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
514
Louis Dionneac9f0c62019-03-28 01:27:52 +0000515 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000516 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
517
518private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500519 pointer __data;
520 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000521};
522
Marshall Clowe4859a02019-02-27 15:41:37 +0000523// tuple interface
524template <class _Tp, size_t _Size>
525struct _LIBCPP_TEMPLATE_VIS tuple_size<span<_Tp, _Size>>
526 : public integral_constant<size_t, _Size> {};
527
528template <class _Tp>
529struct _LIBCPP_TEMPLATE_VIS tuple_size<span<_Tp, dynamic_extent>>; // declared but not defined
530
531
532template <size_t _Ip, class _Tp, size_t _Size>
Louis Dionnee684aa62019-04-01 16:39:34 +0000533struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, span<_Tp, _Size>>
Marshall Clowe4859a02019-02-27 15:41:37 +0000534{
535 static_assert( dynamic_extent != _Size, "std::tuple_element<> not supported for std::span<T, dynamic_extent>");
536 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::span)");
Marshall Clowe4859a02019-02-27 15:41:37 +0000537 typedef _Tp type;
538};
539
540template <size_t _Ip, class _Tp, size_t _Size>
541_LIBCPP_INLINE_VISIBILITY constexpr
542_Tp&
543get(span<_Tp, _Size> __s) noexcept
544{
545 static_assert( dynamic_extent != _Size, "std::get<> not supported for std::span<T, dynamic_extent>");
546 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::span)");
547 return __s[_Ip];
548}
549
550
Louis Dionneac9f0c62019-03-28 01:27:52 +0000551// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000552template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000553_LIBCPP_INLINE_VISIBILITY
554auto as_bytes(span<_Tp, _Extent> __s) noexcept
555-> decltype(__s.__as_bytes())
556{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000557
Marshall Clowcd6d8802019-02-27 00:32:16 +0000558template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000559_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000560auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
561-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
562{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000563
Marshall Clowcd6d8802019-02-27 00:32:16 +0000564template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000565_LIBCPP_INLINE_VISIBILITY
566constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
567{ __lhs.swap(__rhs); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000568
569
570// Deduction guides
571template<class _Tp, size_t _Sz>
572 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000573
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000574template<class _Tp, size_t _Sz>
575 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
576
577template<class _Tp, size_t _Sz>
578 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
579
580template<class _Container>
581 span(_Container&) -> span<typename _Container::value_type>;
582
583template<class _Container>
584 span(const _Container&) -> span<const typename _Container::value_type>;
585
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000586#endif // _LIBCPP_STD_VER > 17
587
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000588_LIBCPP_END_NAMESPACE_STD
589
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000590#endif // _LIBCPP_SPAN