blob: 61e2158e1c952b449939432ca1050074688b94a9 [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>
26 inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
27
Marshall Clowd2ad87e2018-07-24 03:01:02 +000028// [span.objectrep], views of object representation
Marshall Clowcd6d8802019-02-27 00:32:16 +000029template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000030 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000031 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000032
Marshall Clowcd6d8802019-02-27 00:32:16 +000033template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000034 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000035 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000036
37
Marshall Clowcd6d8802019-02-27 00:32:16 +000038template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000039class span {
40public:
41 // constants and types
42 using element_type = ElementType;
43 using value_type = remove_cv_t<ElementType>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050044 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000045 using difference_type = ptrdiff_t;
46 using pointer = element_type*;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000047 using const_pointer = const element_type*;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000048 using reference = element_type&;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000049 using const_reference = const element_type&;
Marshall Clowcd6d8802019-02-27 00:32:16 +000050 using iterator = implementation-defined;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000051 using reverse_iterator = std::reverse_iterator<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;
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040056 constexpr explicit(Extent != dynamic_extent) span(pointer ptr, size_type count);
57 constexpr explicit(Extent != dynamic_extent) span(pointer firstElem, pointer lastElem);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000058 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>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040065 constexpr explicit(Extent != dynamic_extent) span(Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000066 template <class Container>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040067 constexpr explicit(Extent != dynamic_extent) span(const Container& cont);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000068 constexpr span(const span& other) noexcept = default;
Marshall Clowcd6d8802019-02-27 00:32:16 +000069 template <class OtherElementType, size_t OtherExtent>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040070 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000071 ~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;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000100 constexpr reverse_iterator rbegin() const noexcept;
101 constexpr reverse_iterator rend() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000102
103private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500104 pointer data_; // exposition only
105 size_type size_; // exposition only
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000106};
107
108template<class T, size_t N>
109 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000110
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000111template<class T, size_t N>
112 span(array<T, N>&) -> span<T, N>;
113
114template<class T, size_t N>
115 span(const array<T, N>&) -> span<const T, N>;
116
117template<class Container>
118 span(Container&) -> span<typename Container::value_type>;
119
120template<class Container>
121 span(const Container&) -> span<const typename Container::value_type>;
122
123} // namespace std
124
125*/
126
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000127#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400128#include <__debug>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100129#include <__ranges/enable_borrowed_range.h>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000130#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000131#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100132#include <iterator> // for iterators
133#include <type_traits> // for remove_cv, etc
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000134
135#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
136#pragma GCC system_header
137#endif
138
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500139_LIBCPP_PUSH_MACROS
140#include <__undef_macros>
141
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000142_LIBCPP_BEGIN_NAMESPACE_STD
143
144#if _LIBCPP_STD_VER > 17
145
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500146inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000147template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000148
149
150template <class _Tp>
151struct __is_span_impl : public false_type {};
152
Marshall Clowcd6d8802019-02-27 00:32:16 +0000153template <class _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000154struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
155
156template <class _Tp>
157struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
158
159template <class _Tp>
160struct __is_std_array_impl : public false_type {};
161
162template <class _Tp, size_t _Sz>
163struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
164
165template <class _Tp>
166struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
167
168template <class _Tp, class _ElementType, class = void>
169struct __is_span_compatible_container : public false_type {};
170
171template <class _Tp, class _ElementType>
172struct __is_span_compatible_container<_Tp, _ElementType,
173 void_t<
174 // is not a specialization of span
175 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
176 // is not a specialization of array
177 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
178 // is_array_v<Container> is false,
179 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
180 // data(cont) and size(cont) are well formed
181 decltype(data(declval<_Tp>())),
182 decltype(size(declval<_Tp>())),
183 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
184 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000185 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
186 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000187 nullptr_t>::type
188 >>
189 : public true_type {};
190
191
Marshall Clowcd6d8802019-02-27 00:32:16 +0000192template <typename _Tp, size_t _Extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000193class _LIBCPP_TEMPLATE_VIS span {
194public:
195// constants and types
196 using element_type = _Tp;
197 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500198 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000199 using difference_type = ptrdiff_t;
200 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000201 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000202 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000203 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400204#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
205 using iterator = pointer;
206#else
207 using iterator = __wrap_iter<pointer>;
208#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000209 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000210
Louis Dionne7b89ab02019-11-14 09:07:05 -0500211 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000212
Louis Dionne44bcff92018-08-03 22:36:53 +0000213// [span.cons], span constructors, copy, assignment, and destructor
Michael Schellenberger Costa11807352020-05-14 09:28:27 -0400214 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
215 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000216
217 constexpr span (const span&) noexcept = default;
218 constexpr span& operator=(const span&) noexcept = default;
219
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400220 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __ptr, size_type __count) : __data{__ptr}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000221 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400222 _LIBCPP_INLINE_VISIBILITY constexpr explicit span(pointer __f, pointer __l) : __data{__f}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000223 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
224
225 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400226
227 template <class _OtherElementType,
228 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
229 _LIBCPP_INLINE_VISIBILITY
230 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
231
232 template <class _OtherElementType,
233 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
234 _LIBCPP_INLINE_VISIBILITY
235 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000236
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400237 template <class _Container>
238 _LIBCPP_INLINE_VISIBILITY
239 constexpr explicit span( _Container& __c,
240 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
241 : __data{_VSTD::data(__c)} {
242 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
243 }
244
245 template <class _Container>
246 _LIBCPP_INLINE_VISIBILITY
247 constexpr explicit span(const _Container& __c,
248 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
249 : __data{_VSTD::data(__c)} {
250 _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (range)");
251 }
252
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000253 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000254 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000255 constexpr span(const span<_OtherElementType, _Extent>& __other,
256 enable_if_t<
257 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
258 nullptr_t> = nullptr)
259 : __data{__other.data()} {}
260
261 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000262 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400263 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000264 enable_if_t<
265 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
266 nullptr_t> = nullptr) noexcept
267 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
268
269
270// ~span() noexcept = default;
271
Marshall Clowcd6d8802019-02-27 00:32:16 +0000272 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000273 _LIBCPP_INLINE_VISIBILITY
274 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000275 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000276 static_assert(_Count <= _Extent, "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400277 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000278 }
279
Marshall Clowcd6d8802019-02-27 00:32:16 +0000280 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000281 _LIBCPP_INLINE_VISIBILITY
282 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000283 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000284 static_assert(_Count <= _Extent, "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400285 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000286 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000287
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000288 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500289 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000290 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000291 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000292 return {data(), __count};
293 }
294
295 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500296 constexpr span<element_type, dynamic_extent> last(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::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000299 return {data() + size() - __count, __count};
300 }
301
Marshall Clowcd6d8802019-02-27 00:32:16 +0000302 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000303 _LIBCPP_INLINE_VISIBILITY
304 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000305 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
306 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000307 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100308 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400309
310 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
311 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000312 }
313
314
Marshall Clowe246c9f2019-03-06 03:59:44 +0000315 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000316 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500317 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000318 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000319 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
320 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000321 if (__count == dynamic_extent)
322 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100323 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000324 return {data() + __offset, __count};
325 }
326
Louis Dionne7b89ab02019-11-14 09:07:05 -0500327 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
328 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
329 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000330
Louis Dionne7b89ab02019-11-14 09:07:05 -0500331 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000332 {
Louis Dionne956eca82020-02-11 11:38:00 +0100333 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000334 return __data[__idx];
335 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000336
Marshall Clow89408802019-02-25 17:54:08 +0000337 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000338 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100339 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000340 return __data[0];
341 }
342
343 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
344 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100345 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000346 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000347 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000348
349 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
350
351// [span.iter], span iterator support
352 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
353 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000354 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
355 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000356
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000357 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400358 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000359
Louis Dionneac9f0c62019-03-28 01:27:52 +0000360 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400361 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000362
363private:
364 pointer __data;
365
366};
367
368
369template <typename _Tp>
370class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
371private:
372
373public:
374// constants and types
375 using element_type = _Tp;
376 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500377 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000378 using difference_type = ptrdiff_t;
379 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000380 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000381 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000382 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400383#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
384 using iterator = pointer;
385#else
386 using iterator = __wrap_iter<pointer>;
387#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000388 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000389
Louis Dionne7b89ab02019-11-14 09:07:05 -0500390 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000391
Louis Dionne44bcff92018-08-03 22:36:53 +0000392// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000393 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
394
395 constexpr span (const span&) noexcept = default;
396 constexpr span& operator=(const span&) noexcept = default;
397
Louis Dionne7b89ab02019-11-14 09:07:05 -0500398 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, size_type __count) : __data{__ptr}, __size{__count} {}
Marshall Clowcd6d8802019-02-27 00:32:16 +0000399 _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 +0000400
401 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000402 _LIBCPP_INLINE_VISIBILITY
403 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000404
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400405 template <class _OtherElementType, size_t _Sz,
406 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000407 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400408 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000409
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400410 template <class _OtherElementType, size_t _Sz,
411 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000412 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400413 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000414
415 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000416 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000417 constexpr span( _Container& __c,
418 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500419 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000420
421 template <class _Container>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000422 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000423 constexpr span(const _Container& __c,
424 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
Louis Dionne7b89ab02019-11-14 09:07:05 -0500425 : __data{_VSTD::data(__c)}, __size{(size_type) _VSTD::size(__c)} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000426
427
Marshall Clowcd6d8802019-02-27 00:32:16 +0000428 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000429 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000430 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
431 enable_if_t<
432 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
433 nullptr_t> = nullptr) noexcept
434 : __data{__other.data()}, __size{__other.size()} {}
435
436// ~span() noexcept = default;
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> first() 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::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400443 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000444 }
445
Marshall Clowcd6d8802019-02-27 00:32:16 +0000446 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000447 _LIBCPP_INLINE_VISIBILITY
448 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000449 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000450 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400451 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000452 }
453
454 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500455 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000456 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000457 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000458 return {data(), __count};
459 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000460
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000461 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500462 constexpr span<element_type, dynamic_extent> last (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::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000465 return {data() + size() - __count, __count};
466 }
467
Marshall Clowcd6d8802019-02-27 00:32:16 +0000468 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000469 _LIBCPP_INLINE_VISIBILITY
Louis Dionne22c735e2020-02-11 11:57:35 +0100470 constexpr span<element_type, _Count> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000471 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000472 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100473 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400474 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000475 }
476
477 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000478 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500479 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000480 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000481 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
482 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000483 if (__count == dynamic_extent)
484 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100485 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000486 return {data() + __offset, __count};
487 }
488
Louis Dionne7b89ab02019-11-14 09:07:05 -0500489 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
490 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
491 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000492
Louis Dionne7b89ab02019-11-14 09:07:05 -0500493 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000494 {
Louis Dionne956eca82020-02-11 11:38:00 +0100495 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000496 return __data[__idx];
497 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000498
Marshall Clow89408802019-02-25 17:54:08 +0000499 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000500 {
Marshall Clow89408802019-02-25 17:54:08 +0000501 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
502 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000503 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000504
Marshall Clow89408802019-02-25 17:54:08 +0000505 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
506 {
507 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
508 return __data[size()-1];
509 }
510
511
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000512 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
513
514// [span.iter], span iterator support
515 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
516 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000517 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
518 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000519
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000520 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
521 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
522
Louis Dionneac9f0c62019-03-28 01:27:52 +0000523 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000524 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
525
526private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500527 pointer __data;
528 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000529};
530
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100531#if !defined(_LIBCPP_HAS_NO_RANGES)
532template <class _Tp, size_t _Extent>
533inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
534#endif // !defined(_LIBCPP_HAS_NO_RANGES)
535
Louis Dionneac9f0c62019-03-28 01:27:52 +0000536// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000537template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000538_LIBCPP_INLINE_VISIBILITY
539auto as_bytes(span<_Tp, _Extent> __s) noexcept
540-> decltype(__s.__as_bytes())
541{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000542
Marshall Clowcd6d8802019-02-27 00:32:16 +0000543template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000544_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000545auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
546-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
547{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000548
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000549// Deduction guides
550template<class _Tp, size_t _Sz>
551 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000552
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000553template<class _Tp, size_t _Sz>
554 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
555
556template<class _Tp, size_t _Sz>
557 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
558
559template<class _Container>
560 span(_Container&) -> span<typename _Container::value_type>;
561
562template<class _Container>
563 span(const _Container&) -> span<const typename _Container::value_type>;
564
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000565#endif // _LIBCPP_STD_VER > 17
566
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000567_LIBCPP_END_NAMESPACE_STD
568
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500569_LIBCPP_POP_MACROS
570
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000571#endif // _LIBCPP_SPAN