blob: 29f833f6022d7d22e4dde8b9da8723513a0a44a2 [file] [log] [blame]
Marshall Clowd2ad87e2018-07-24 03:01:02 +00001// -*- C++ -*-
2//===------------------------------ span ---------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clowd2ad87e2018-07-24 03:01:02 +00007//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SPAN
11#define _LIBCPP_SPAN
12
13/*
14 span synopsis
15
16namespace std {
17
18// constants
Marshall Clowcd6d8802019-02-27 00:32:16 +000019inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowd2ad87e2018-07-24 03:01:02 +000020
21// [views.span], class template span
Marshall Clowcd6d8802019-02-27 00:32:16 +000022template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000023 class span;
24
Mark de Weveraf59b2d2020-11-24 18:08:02 +010025template<class ElementType, size_t Extent>
Christopher Di Bella92072c22021-05-14 06:20:07 +000026 inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true;
27
28template<class ElementType, size_t Extent>
Mark de Weveraf59b2d2020-11-24 18:08:02 +010029 inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true;
30
Marshall Clowd2ad87e2018-07-24 03:01:02 +000031// [span.objectrep], views of object representation
Marshall Clowcd6d8802019-02-27 00:32:16 +000032template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000033 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000034 (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000035
Marshall Clowcd6d8802019-02-27 00:32:16 +000036template <class ElementType, size_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000037 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowe246c9f2019-03-06 03:59:44 +000038 (sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000039
40
Marshall Clowcd6d8802019-02-27 00:32:16 +000041template <class ElementType, size_t Extent = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000042class span {
43public:
44 // constants and types
45 using element_type = ElementType;
46 using value_type = remove_cv_t<ElementType>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050047 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000048 using difference_type = ptrdiff_t;
49 using pointer = element_type*;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000050 using const_pointer = const element_type*;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000051 using reference = element_type&;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000052 using const_reference = const element_type&;
Marshall Clowcd6d8802019-02-27 00:32:16 +000053 using iterator = implementation-defined;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000054 using reverse_iterator = std::reverse_iterator<iterator>;
Louis Dionne7b89ab02019-11-14 09:07:05 -050055 static constexpr size_type extent = Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000056
57 // [span.cons], span constructors, copy, assignment, and destructor
58 constexpr span() noexcept;
Joe Loserdaf1bdf2021-10-08 16:57:44 -040059 template <class It>
60 constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
61 template <class It, class End>
62 constexpr explicit(Extent != dynamic_extent) span(It first, End last);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000063 template <size_t N>
64 constexpr span(element_type (&arr)[N]) noexcept;
65 template <size_t N>
66 constexpr span(array<value_type, N>& arr) noexcept;
67 template <size_t N>
68 constexpr span(const array<value_type, N>& arr) noexcept;
Joe Loserdaf1bdf2021-10-08 16:57:44 -040069 template<class R>
70 constexpr explicit(Extent != dynamic_extent) span(R&& r);
Marshall Clowd2ad87e2018-07-24 03:01:02 +000071 constexpr span(const span& other) noexcept = default;
Marshall Clowcd6d8802019-02-27 00:32:16 +000072 template <class OtherElementType, size_t OtherExtent>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -040073 constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000074 ~span() noexcept = default;
75 constexpr span& operator=(const span& other) noexcept = default;
76
77 // [span.sub], span subviews
Marshall Clowcd6d8802019-02-27 00:32:16 +000078 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000079 constexpr span<element_type, Count> first() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000080 template <size_t Count>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000081 constexpr span<element_type, Count> last() const;
Marshall Clowcd6d8802019-02-27 00:32:16 +000082 template <size_t Offset, size_t Count = dynamic_extent>
Marshall Clowd2ad87e2018-07-24 03:01:02 +000083 constexpr span<element_type, see below> subspan() const;
84
Louis Dionne7b89ab02019-11-14 09:07:05 -050085 constexpr span<element_type, dynamic_extent> first(size_type count) const;
86 constexpr span<element_type, dynamic_extent> last(size_type count) const;
87 constexpr span<element_type, dynamic_extent> subspan(size_type offset, size_type count = dynamic_extent) const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000088
89 // [span.obs], span observers
Louis Dionne7b89ab02019-11-14 09:07:05 -050090 constexpr size_type size() const noexcept;
91 constexpr size_type size_bytes() const noexcept;
Joe Loserf4a2a1e2021-10-12 22:30:58 -040092 [[nodiscard]] constexpr bool empty() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000093
94 // [span.elem], span element access
Louis Dionne7b89ab02019-11-14 09:07:05 -050095 constexpr reference operator[](size_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000096 constexpr reference front() const;
97 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000098 constexpr pointer data() const noexcept;
99
100 // [span.iterators], span iterator support
101 constexpr iterator begin() const noexcept;
102 constexpr iterator end() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000103 constexpr reverse_iterator rbegin() const noexcept;
104 constexpr reverse_iterator rend() const noexcept;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000105
106private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500107 pointer data_; // exposition only
108 size_type size_; // exposition only
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000109};
110
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400111template<class It, class EndOrSize>
112 span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
113
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000114template<class T, size_t N>
115 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000116
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000117template<class T, size_t N>
118 span(array<T, N>&) -> span<T, N>;
119
120template<class T, size_t N>
121 span(const array<T, N>&) -> span<const T, N>;
122
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400123template<class R>
124 span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000125
126} // namespace std
127
128*/
129
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000130#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -0400131#include <__debug>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400132#include <__iterator/concepts.h>
Louis Dionne77249522021-06-11 09:55:11 -0400133#include <__iterator/wrap_iter.h>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400134#include <__ranges/concepts.h>
135#include <__ranges/data.h>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100136#include <__ranges/enable_borrowed_range.h>
Christopher Di Bella92072c22021-05-14 06:20:07 +0000137#include <__ranges/enable_view.h>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400138#include <__ranges/size.h>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000139#include <array> // for array
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000140#include <cstddef> // for byte
Louis Dionne533a14e2020-02-11 11:16:40 +0100141#include <iterator> // for iterators
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000142#include <limits>
Louis Dionne533a14e2020-02-11 11:16:40 +0100143#include <type_traits> // for remove_cv, etc
Christopher Di Bella60fc6602021-07-13 19:44:07 +0000144#include <version>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000145
146#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
147#pragma GCC system_header
148#endif
149
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500150_LIBCPP_PUSH_MACROS
151#include <__undef_macros>
152
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000153_LIBCPP_BEGIN_NAMESPACE_STD
154
155#if _LIBCPP_STD_VER > 17
156
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500157inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max();
Marshall Clowcd6d8802019-02-27 00:32:16 +0000158template <typename _Tp, size_t _Extent = dynamic_extent> class span;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000159
160
161template <class _Tp>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400162struct __is_std_array : false_type {};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000163
164template <class _Tp, size_t _Sz>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400165struct __is_std_array<array<_Tp, _Sz>> : true_type {};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000166
167template <class _Tp>
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400168struct __is_std_span : false_type {};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000169
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400170template <class _Tp, size_t _Sz>
171struct __is_std_span<span<_Tp, _Sz>> : true_type {};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000172
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400173#if !defined(_LIBCPP_HAS_NO_RANGES)
174template <class _Range, class _ElementType>
175concept __span_compatible_range =
176 ranges::contiguous_range<_Range> &&
177 ranges::sized_range<_Range> &&
178 (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) &&
179 !__is_std_span<remove_cvref_t<_Range>>::value &&
180 !__is_std_array<remove_cvref_t<_Range>>::value &&
181 !is_array_v<remove_cvref_t<_Range>> &&
182 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
183#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000184
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 &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400197#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
198 using iterator = pointer;
199#else
200 using iterator = __wrap_iter<pointer>;
201#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000202 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000203
Louis Dionne7b89ab02019-11-14 09:07:05 -0500204 static constexpr size_type extent = _Extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000205
Louis Dionne44bcff92018-08-03 22:36:53 +0000206// [span.cons], span constructors, copy, assignment, and destructor
Michael Schellenberger Costa11807352020-05-14 09:28:27 -0400207 template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
208 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000209
210 constexpr span (const span&) noexcept = default;
211 constexpr span& operator=(const span&) noexcept = default;
212
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400213#if !defined(_LIBCPP_HAS_NO_RANGES)
214 template <class _It,
215 enable_if_t<contiguous_iterator<_It> &&
216 is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
217 nullptr_t> = nullptr>
218 _LIBCPP_INLINE_VISIBILITY
219 constexpr explicit span(_It __first, size_type __count)
220 : __data{_VSTD::to_address(__first)} {
221 (void)__count;
222 _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
223 }
224
225 template <
226 class _It, class _End,
227 enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
228 contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
229 nullptr_t> = nullptr>
230 _LIBCPP_INLINE_VISIBILITY
231 constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
232 (void)__last;
233 _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)");
234 _LIBCPP_ASSERT(__last - __first == _Extent,
235 "invalid range in span's constructor (iterator, sentinel): last - first != extent");
236 }
237#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000238
239 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400240
241 template <class _OtherElementType,
242 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
243 _LIBCPP_INLINE_VISIBILITY
244 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
245
246 template <class _OtherElementType,
247 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
248 _LIBCPP_INLINE_VISIBILITY
249 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000250
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400251#if !defined(_LIBCPP_HAS_NO_RANGES)
252 template <__span_compatible_range<element_type> _Range>
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400253 _LIBCPP_INLINE_VISIBILITY
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400254 constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} {
255 _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)");
256 }
257#endif
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400258
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000259 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000260 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000261 constexpr span(const span<_OtherElementType, _Extent>& __other,
262 enable_if_t<
263 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
264 nullptr_t> = nullptr)
265 : __data{__other.data()} {}
266
267 template <class _OtherElementType>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000268 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400269 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000270 enable_if_t<
271 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
272 nullptr_t> = nullptr) noexcept
273 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
274
275
276// ~span() noexcept = default;
277
Marshall Clowcd6d8802019-02-27 00:32:16 +0000278 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000279 _LIBCPP_INLINE_VISIBILITY
280 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000281 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000282 static_assert(_Count <= _Extent, "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400283 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000284 }
285
Marshall Clowcd6d8802019-02-27 00:32:16 +0000286 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000287 _LIBCPP_INLINE_VISIBILITY
288 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000289 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000290 static_assert(_Count <= _Extent, "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400291 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000292 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000293
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000294 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500295 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000296 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000297 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000298 return {data(), __count};
299 }
300
301 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500302 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000303 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000304 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000305 return {data() + size() - __count, __count};
306 }
307
Marshall Clowcd6d8802019-02-27 00:32:16 +0000308 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000309 _LIBCPP_INLINE_VISIBILITY
310 constexpr auto subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000311 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
312 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000313 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100314 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400315
316 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>;
317 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000318 }
319
320
Marshall Clowe246c9f2019-03-06 03:59:44 +0000321 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000322 constexpr span<element_type, dynamic_extent>
Louis Dionne7b89ab02019-11-14 09:07:05 -0500323 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000324 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000325 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
326 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000327 if (__count == dynamic_extent)
328 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100329 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000330 return {data() + __offset, __count};
331 }
332
Joe Loserf4a2a1e2021-10-12 22:30:58 -0400333 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; }
334 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
335 [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000336
Louis Dionne7b89ab02019-11-14 09:07:05 -0500337 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000338 {
Louis Dionne956eca82020-02-11 11:38:00 +0100339 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000340 return __data[__idx];
341 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000342
Marshall Clow89408802019-02-25 17:54:08 +0000343 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000344 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100345 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000346 return __data[0];
347 }
348
349 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
350 {
Louis Dionne0c12d932020-02-14 14:31:15 +0100351 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span");
Marshall Clow89408802019-02-25 17:54:08 +0000352 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000353 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000354
355 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
356
357// [span.iter], span iterator support
358 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
359 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000360 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
361 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000362
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000363 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400364 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000365
Louis Dionneac9f0c62019-03-28 01:27:52 +0000366 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400367 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000368
369private:
370 pointer __data;
371
372};
373
374
375template <typename _Tp>
376class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
377private:
378
379public:
380// constants and types
381 using element_type = _Tp;
382 using value_type = remove_cv_t<_Tp>;
Louis Dionne7b89ab02019-11-14 09:07:05 -0500383 using size_type = size_t;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000384 using difference_type = ptrdiff_t;
385 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000386 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000387 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000388 using const_reference = const _Tp &;
Arthur O'Dwyer0a5557f2021-04-20 22:08:00 -0400389#if (_LIBCPP_DEBUG_LEVEL == 2) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
390 using iterator = pointer;
391#else
392 using iterator = __wrap_iter<pointer>;
393#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000394 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000395
Louis Dionne7b89ab02019-11-14 09:07:05 -0500396 static constexpr size_type extent = dynamic_extent;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000397
Louis Dionne44bcff92018-08-03 22:36:53 +0000398// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000399 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
400
401 constexpr span (const span&) noexcept = default;
402 constexpr span& operator=(const span&) noexcept = default;
403
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400404#if !defined(_LIBCPP_HAS_NO_RANGES)
405 template <class _It,
406 enable_if_t<contiguous_iterator<_It> &&
407 is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
408 nullptr_t> = nullptr>
409 _LIBCPP_INLINE_VISIBILITY
410 constexpr span(_It __first, size_type __count)
411 : __data{_VSTD::to_address(__first)}, __size{__count} {}
412
413 template <
414 class _It, class _End,
415 enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
416 contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
417 nullptr_t> = nullptr>
418 _LIBCPP_INLINE_VISIBILITY
419 constexpr span(_It __first, _End __last)
420 : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
421#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000422
423 template <size_t _Sz>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000424 _LIBCPP_INLINE_VISIBILITY
425 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000426
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400427 template <class _OtherElementType, size_t _Sz,
428 enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000429 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400430 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000431
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400432 template <class _OtherElementType, size_t _Sz,
433 enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000434 _LIBCPP_INLINE_VISIBILITY
Michael Schellenberger Costab0444f32020-05-14 10:50:03 -0400435 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000436
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400437#if !defined(_LIBCPP_HAS_NO_RANGES)
438 template <__span_compatible_range<element_type> _Range>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000439 _LIBCPP_INLINE_VISIBILITY
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400440 constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
441# endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000442
Marshall Clowcd6d8802019-02-27 00:32:16 +0000443 template <class _OtherElementType, size_t _OtherExtent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000444 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000445 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
446 enable_if_t<
447 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
448 nullptr_t> = nullptr) noexcept
449 : __data{__other.data()}, __size{__other.size()} {}
450
451// ~span() noexcept = default;
452
Marshall Clowcd6d8802019-02-27 00:32:16 +0000453 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000454 _LIBCPP_INLINE_VISIBILITY
455 constexpr span<element_type, _Count> first() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000456 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000457 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400458 return span<element_type, _Count>{data(), _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000459 }
460
Marshall Clowcd6d8802019-02-27 00:32:16 +0000461 template <size_t _Count>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000462 _LIBCPP_INLINE_VISIBILITY
463 constexpr span<element_type, _Count> last() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000464 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000465 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400466 return span<element_type, _Count>{data() + size() - _Count, _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000467 }
468
469 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500470 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000471 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000472 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000473 return {data(), __count};
474 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000475
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000476 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500477 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000478 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000479 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000480 return {data() + size() - __count, __count};
481 }
482
Marshall Clowcd6d8802019-02-27 00:32:16 +0000483 template <size_t _Offset, size_t _Count = dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000484 _LIBCPP_INLINE_VISIBILITY
Louis Dionne22c735e2020-02-11 11:57:35 +0100485 constexpr span<element_type, _Count> subspan() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000486 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000487 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()");
Louis Dionne45931e92020-02-12 16:20:09 +0100488 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()");
Michael Schellenberger Costaea234282020-05-13 09:50:06 -0400489 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000490 }
491
492 constexpr span<element_type, dynamic_extent>
Marshall Clowe246c9f2019-03-06 03:59:44 +0000493 _LIBCPP_INLINE_VISIBILITY
Louis Dionne7b89ab02019-11-14 09:07:05 -0500494 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000495 {
Marshall Clowe246c9f2019-03-06 03:59:44 +0000496 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
497 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000498 if (__count == dynamic_extent)
499 return {data() + __offset, size() - __offset};
Louis Dionne45931e92020-02-12 16:20:09 +0100500 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000501 return {data() + __offset, __count};
502 }
503
Joe Loserf4a2a1e2021-10-12 22:30:58 -0400504 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; }
505 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); }
506 [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000507
Louis Dionne7b89ab02019-11-14 09:07:05 -0500508 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000509 {
Louis Dionne956eca82020-02-11 11:38:00 +0100510 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000511 return __data[__idx];
512 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000513
Marshall Clow89408802019-02-25 17:54:08 +0000514 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000515 {
Marshall Clow89408802019-02-25 17:54:08 +0000516 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
517 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000518 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000519
Marshall Clow89408802019-02-25 17:54:08 +0000520 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
521 {
522 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
523 return __data[size()-1];
524 }
525
526
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000527 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
528
529// [span.iter], span iterator support
530 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
531 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000532 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
533 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000534
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000535 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
536 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
537
Louis Dionneac9f0c62019-03-28 01:27:52 +0000538 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000539 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
540
541private:
Louis Dionne7b89ab02019-11-14 09:07:05 -0500542 pointer __data;
543 size_type __size;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000544};
545
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100546#if !defined(_LIBCPP_HAS_NO_RANGES)
547template <class _Tp, size_t _Extent>
548inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true;
Christopher Di Bella92072c22021-05-14 06:20:07 +0000549
550template <class _ElementType, size_t _Extent>
551inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100552#endif // !defined(_LIBCPP_HAS_NO_RANGES)
553
Louis Dionneac9f0c62019-03-28 01:27:52 +0000554// as_bytes & as_writable_bytes
Marshall Clowcd6d8802019-02-27 00:32:16 +0000555template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000556_LIBCPP_INLINE_VISIBILITY
557auto as_bytes(span<_Tp, _Extent> __s) noexcept
558-> decltype(__s.__as_bytes())
559{ return __s.__as_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000560
Marshall Clowcd6d8802019-02-27 00:32:16 +0000561template <class _Tp, size_t _Extent>
Marshall Clowe4859a02019-02-27 15:41:37 +0000562_LIBCPP_INLINE_VISIBILITY
Louis Dionneac9f0c62019-03-28 01:27:52 +0000563auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
564-> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
565{ return __s.__as_writable_bytes(); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000566
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400567#if !defined(_LIBCPP_HAS_NO_RANGES)
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000568// Deduction guides
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400569template<contiguous_iterator _It, class _EndOrSize>
570 span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
571#endif
572
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000573template<class _Tp, size_t _Sz>
574 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000575
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000576template<class _Tp, size_t _Sz>
577 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
578
579template<class _Tp, size_t _Sz>
580 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
581
Joe Loserdaf1bdf2021-10-08 16:57:44 -0400582#if !defined(_LIBCPP_HAS_NO_RANGES)
583template<ranges::contiguous_range _Range>
584 span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>;
585#endif
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000586
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000587#endif // _LIBCPP_STD_VER > 17
588
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000589_LIBCPP_END_NAMESPACE_STD
590
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500591_LIBCPP_POP_MACROS
592
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000593#endif // _LIBCPP_SPAN