blob: b212f8722bed55d3b94851bc646c04743f2a58f7 [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
19inline constexpr ptrdiff_t dynamic_extent = -1;
20
21// [views.span], class template span
22template <class ElementType, ptrdiff_t Extent = dynamic_extent>
23 class span;
24
Marshall Clowd2ad87e2018-07-24 03:01:02 +000025// [span.objectrep], views of object representation
26template <class ElementType, ptrdiff_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000027 span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowd2ad87e2018-07-24 03:01:02 +000028 (static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
29
30template <class ElementType, ptrdiff_t Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +000031 span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
Marshall Clowd2ad87e2018-07-24 03:01:02 +000032 (static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
33
34
35namespace std {
36template <class ElementType, ptrdiff_t Extent = dynamic_extent>
37class span {
38public:
39 // constants and types
40 using element_type = ElementType;
41 using value_type = remove_cv_t<ElementType>;
42 using index_type = ptrdiff_t;
43 using difference_type = ptrdiff_t;
44 using pointer = element_type*;
45 using reference = element_type&;
46 using iterator = implementation-defined;
47 using const_iterator = implementation-defined;
48 using reverse_iterator = std::reverse_iterator<iterator>;
49 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
50 static constexpr index_type extent = Extent;
51
52 // [span.cons], span constructors, copy, assignment, and destructor
53 constexpr span() noexcept;
54 constexpr span(pointer ptr, index_type count);
55 constexpr span(pointer firstElem, pointer lastElem);
56 template <size_t N>
57 constexpr span(element_type (&arr)[N]) noexcept;
58 template <size_t N>
59 constexpr span(array<value_type, N>& arr) noexcept;
60 template <size_t N>
61 constexpr span(const array<value_type, N>& arr) noexcept;
62 template <class Container>
63 constexpr span(Container& cont);
64 template <class Container>
65 constexpr span(const Container& cont);
66 constexpr span(const span& other) noexcept = default;
67 template <class OtherElementType, ptrdiff_t OtherExtent>
68 constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
69 ~span() noexcept = default;
70 constexpr span& operator=(const span& other) noexcept = default;
71
72 // [span.sub], span subviews
73 template <ptrdiff_t Count>
74 constexpr span<element_type, Count> first() const;
75 template <ptrdiff_t Count>
76 constexpr span<element_type, Count> last() const;
77 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
78 constexpr span<element_type, see below> subspan() const;
79
80 constexpr span<element_type, dynamic_extent> first(index_type count) const;
81 constexpr span<element_type, dynamic_extent> last(index_type count) const;
82 constexpr span<element_type, dynamic_extent> subspan(index_type offset, index_type count = dynamic_extent) const;
83
84 // [span.obs], span observers
85 constexpr index_type size() const noexcept;
86 constexpr index_type size_bytes() const noexcept;
87 constexpr bool empty() const noexcept;
88
89 // [span.elem], span element access
90 constexpr reference operator[](index_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000091 constexpr reference front() const;
92 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000093 constexpr pointer data() const noexcept;
94
95 // [span.iterators], span iterator support
96 constexpr iterator begin() const noexcept;
97 constexpr iterator end() const noexcept;
98 constexpr const_iterator cbegin() const noexcept;
99 constexpr const_iterator cend() const noexcept;
100 constexpr reverse_iterator rbegin() const noexcept;
101 constexpr reverse_iterator rend() const noexcept;
102 constexpr const_reverse_iterator crbegin() const noexcept;
103 constexpr const_reverse_iterator crend() const noexcept;
104
105private:
106 pointer data_; // exposition only
107 index_type size_; // exposition only
108};
109
110template<class T, size_t N>
111 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000112
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000113template<class T, size_t N>
114 span(array<T, N>&) -> span<T, N>;
115
116template<class T, size_t N>
117 span(const array<T, N>&) -> span<const T, N>;
118
119template<class Container>
120 span(Container&) -> span<typename Container::value_type>;
121
122template<class Container>
123 span(const Container&) -> span<const typename Container::value_type>;
124
125} // namespace std
126
127*/
128
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000129#include <__config>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000130#include <cstddef> // for ptrdiff_t
131#include <iterator> // for iterators
132#include <array> // for array
133#include <type_traits> // for remove_cv, etc
134#include <cstddef> // for byte
135
136#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
137#pragma GCC system_header
138#endif
139
140_LIBCPP_BEGIN_NAMESPACE_STD
141
142#if _LIBCPP_STD_VER > 17
143
144inline constexpr ptrdiff_t dynamic_extent = -1;
145template <typename _Tp, ptrdiff_t _Extent = dynamic_extent> class span;
146
147
148template <class _Tp>
149struct __is_span_impl : public false_type {};
150
151template <class _Tp, ptrdiff_t _Extent>
152struct __is_span_impl<span<_Tp, _Extent>> : public true_type {};
153
154template <class _Tp>
155struct __is_span : public __is_span_impl<remove_cv_t<_Tp>> {};
156
157template <class _Tp>
158struct __is_std_array_impl : public false_type {};
159
160template <class _Tp, size_t _Sz>
161struct __is_std_array_impl<array<_Tp, _Sz>> : public true_type {};
162
163template <class _Tp>
164struct __is_std_array : public __is_std_array_impl<remove_cv_t<_Tp>> {};
165
166template <class _Tp, class _ElementType, class = void>
167struct __is_span_compatible_container : public false_type {};
168
169template <class _Tp, class _ElementType>
170struct __is_span_compatible_container<_Tp, _ElementType,
171 void_t<
172 // is not a specialization of span
173 typename enable_if<!__is_span<_Tp>::value, nullptr_t>::type,
174 // is not a specialization of array
175 typename enable_if<!__is_std_array<_Tp>::value, nullptr_t>::type,
176 // is_array_v<Container> is false,
177 typename enable_if<!is_array_v<_Tp>, nullptr_t>::type,
178 // data(cont) and size(cont) are well formed
179 decltype(data(declval<_Tp>())),
180 decltype(size(declval<_Tp>())),
181 // remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
182 typename enable_if<
Louis Dionne44bcff92018-08-03 22:36:53 +0000183 is_convertible_v<remove_pointer_t<decltype(data(declval<_Tp &>()))>(*)[],
184 _ElementType(*)[]>,
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000185 nullptr_t>::type
186 >>
187 : public true_type {};
188
189
190template <typename _Tp, ptrdiff_t _Extent>
191class _LIBCPP_TEMPLATE_VIS span {
192public:
193// constants and types
194 using element_type = _Tp;
195 using value_type = remove_cv_t<_Tp>;
196 using index_type = ptrdiff_t;
197 using difference_type = ptrdiff_t;
198 using pointer = _Tp *;
199 using const_pointer = const _Tp *; // not in standard
200 using reference = _Tp &;
201 using const_reference = const _Tp &; // not in standard
202 using iterator = __wrap_iter<pointer>;
203 using const_iterator = __wrap_iter<const_pointer>;
204 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
205 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
206
207 static constexpr index_type extent = _Extent;
208 static_assert (_Extent >= 0, "Can't have a span with an extent < 0");
209
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
217 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}
218 { (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
226 template <class _Container>
227 inline _LIBCPP_INLINE_VISIBILITY
228 constexpr span( _Container& __c,
229 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
230 : __data{_VSTD::data(__c)}
Louis Dionne0ba926d2018-10-03 21:36:16 +0000231 { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (container)"); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000232
233 template <class _Container>
234 inline _LIBCPP_INLINE_VISIBILITY
235 constexpr span(const _Container& __c,
236 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
237 : __data{_VSTD::data(__c)}
238 { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (const container)"); }
239
240 template <class _OtherElementType>
241 inline _LIBCPP_INLINE_VISIBILITY
242 constexpr span(const span<_OtherElementType, _Extent>& __other,
243 enable_if_t<
244 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
245 nullptr_t> = nullptr)
246 : __data{__other.data()} {}
247
248 template <class _OtherElementType>
249 inline _LIBCPP_INLINE_VISIBILITY
250 constexpr span(const span<_OtherElementType, dynamic_extent>& __other,
251 enable_if_t<
252 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
253 nullptr_t> = nullptr) noexcept
254 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
255
256
257// ~span() noexcept = default;
258
259 template <ptrdiff_t _Count>
260 inline _LIBCPP_INLINE_VISIBILITY
261 constexpr span<element_type, _Count> first() const noexcept
262 {
263 static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
264 static_assert(_Count <= _Extent, "Count out of range in span::first()");
265 return {data(), _Count};
266 }
267
268 template <ptrdiff_t _Count>
269 inline _LIBCPP_INLINE_VISIBILITY
270 constexpr span<element_type, _Count> last() const noexcept
271 {
272 static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
273 static_assert(_Count <= _Extent, "Count out of range in span::last()");
274 return {data() + size() - _Count, _Count};
275 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000276
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000277 _LIBCPP_INLINE_VISIBILITY
278 constexpr span<element_type, dynamic_extent> first(index_type __count) const noexcept
279 {
280 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)");
281 return {data(), __count};
282 }
283
284 _LIBCPP_INLINE_VISIBILITY
285 constexpr span<element_type, dynamic_extent> last(index_type __count) const noexcept
286 {
287 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)");
288 return {data() + size() - __count, __count};
289 }
290
291 template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
292 inline _LIBCPP_INLINE_VISIBILITY
293 constexpr auto subspan() const noexcept
294 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
295 {
296 _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()");
297 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
298 }
299
300
301 inline _LIBCPP_INLINE_VISIBILITY
302 constexpr span<element_type, dynamic_extent>
303 subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000304 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000305 _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)");
306 _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
307 if (__count == dynamic_extent)
308 return {data() + __offset, size() - __offset};
309 _LIBCPP_ASSERT(__offset + __count <= size(), "count + offset out of range in span::subspan(offset, count)");
310 return {data() + __offset, __count};
311 }
312
313 _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return _Extent; }
314 _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
315 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
316
317 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept
318 {
319 _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000320 return __data[__idx];
321 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000322
Marshall Clow89408802019-02-25 17:54:08 +0000323 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000324 {
Marshall Clow89408802019-02-25 17:54:08 +0000325 static_assert(_Extent > 0, "span<T,N>[].front() on empty span");
326 return __data[0];
327 }
328
329 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
330 {
331 static_assert(_Extent > 0, "span<T,N>[].back() on empty span");
332 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000333 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000334
335 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
336
337// [span.iter], span iterator support
338 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
339 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
340 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
341 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
342 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
343 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
344 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
345 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
346
347 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
348 {
349 pointer __p = __data;
350 __data = __other.__data;
351 __other.__data = __p;
352 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000353
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000354 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
355 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
356
357 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writeable_bytes() const noexcept
358 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
359
360private:
361 pointer __data;
362
363};
364
365
366template <typename _Tp>
367class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
368private:
369
370public:
371// constants and types
372 using element_type = _Tp;
373 using value_type = remove_cv_t<_Tp>;
374 using index_type = ptrdiff_t;
375 using difference_type = ptrdiff_t;
376 using pointer = _Tp *;
377 using const_pointer = const _Tp *; // not in standard
378 using reference = _Tp &;
379 using const_reference = const _Tp &; // not in standard
380 using iterator = __wrap_iter<pointer>;
381 using const_iterator = __wrap_iter<const_pointer>;
382 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
383 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
384
385 static constexpr index_type extent = dynamic_extent;
386
Louis Dionne44bcff92018-08-03 22:36:53 +0000387// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000388 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
389
390 constexpr span (const span&) noexcept = default;
391 constexpr span& operator=(const span&) noexcept = default;
392
393 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {}
394 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{distance(__f, __l)} {}
395
396 template <size_t _Sz>
397 inline _LIBCPP_INLINE_VISIBILITY
398 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
399
400 template <size_t _Sz>
401 inline _LIBCPP_INLINE_VISIBILITY
402 constexpr span(array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000403
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000404 template <size_t _Sz>
405 inline _LIBCPP_INLINE_VISIBILITY
406 constexpr span(const array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
407
408 template <class _Container>
409 inline _LIBCPP_INLINE_VISIBILITY
410 constexpr span( _Container& __c,
411 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
412 : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {}
413
414 template <class _Container>
415 inline _LIBCPP_INLINE_VISIBILITY
416 constexpr span(const _Container& __c,
417 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
418 : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {}
419
420
421 template <class _OtherElementType, ptrdiff_t _OtherExtent>
422 inline _LIBCPP_INLINE_VISIBILITY
423 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
424 enable_if_t<
425 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
426 nullptr_t> = nullptr) noexcept
427 : __data{__other.data()}, __size{__other.size()} {}
428
429// ~span() noexcept = default;
430
431 template <ptrdiff_t _Count>
432 inline _LIBCPP_INLINE_VISIBILITY
433 constexpr span<element_type, _Count> first() const noexcept
434 {
Louis Dionne0ba926d2018-10-03 21:36:16 +0000435 static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000436 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
437 return {data(), _Count};
438 }
439
440 template <ptrdiff_t _Count>
441 inline _LIBCPP_INLINE_VISIBILITY
442 constexpr span<element_type, _Count> last() const noexcept
443 {
Louis Dionne0ba926d2018-10-03 21:36:16 +0000444 static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000445 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
446 return {data() + size() - _Count, _Count};
447 }
448
449 _LIBCPP_INLINE_VISIBILITY
450 constexpr span<element_type, dynamic_extent> first(index_type __count) const noexcept
451 {
452 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)");
453 return {data(), __count};
454 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000455
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000456 _LIBCPP_INLINE_VISIBILITY
457 constexpr span<element_type, dynamic_extent> last (index_type __count) const noexcept
458 {
459 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)");
460 return {data() + size() - __count, __count};
461 }
462
463 template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
464 inline _LIBCPP_INLINE_VISIBILITY
465 constexpr span<_Tp, dynamic_extent> subspan() const noexcept
466 {
467 _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()");
468 _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()");
469 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
470 }
471
472 constexpr span<element_type, dynamic_extent>
473 inline _LIBCPP_INLINE_VISIBILITY
474 subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000475 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000476 _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)");
477 _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
478 if (__count == dynamic_extent)
479 return {data() + __offset, size() - __offset};
480 _LIBCPP_ASSERT(__offset + __count <= size(), "Offset + count out of range in span::subspan(offset, count)");
481 return {data() + __offset, __count};
482 }
483
484 _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return __size; }
485 _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return __size * sizeof(element_type); }
486 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
487
488 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept
489 {
490 _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000491 return __data[__idx];
492 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000493
Marshall Clow89408802019-02-25 17:54:08 +0000494 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000495 {
Marshall Clow89408802019-02-25 17:54:08 +0000496 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
497 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000498 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000499
Marshall Clow89408802019-02-25 17:54:08 +0000500 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
501 {
502 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
503 return __data[size()-1];
504 }
505
506
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000507 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
508
509// [span.iter], span iterator support
510 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
511 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
512 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
513 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
514 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
515 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
516 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
517 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
518
519 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
520 {
521 pointer __p = __data;
522 __data = __other.__data;
523 __other.__data = __p;
524
525 index_type __sz = __size;
526 __size = __other.__size;
527 __other.__size = __sz;
528 }
529
530 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
531 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
532
533 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writeable_bytes() const noexcept
534 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
535
536private:
537 pointer __data;
538 index_type __size;
539};
540
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000541// as_bytes & as_writeable_bytes
542template <class _Tp, ptrdiff_t _Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +0000543 auto as_bytes(span<_Tp, _Extent> __s) noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000544 -> decltype(__s.__as_bytes())
545 { return __s.__as_bytes(); }
546
547template <class _Tp, ptrdiff_t _Extent>
548 auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept
549 -> typename enable_if<!is_const_v<_Tp>, decltype(__s.__as_writeable_bytes())>::type
550 { return __s.__as_writeable_bytes(); }
551
552template <class _Tp, ptrdiff_t _Extent>
553 constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
554 { __lhs.swap(__rhs); }
555
556
557// Deduction guides
558template<class _Tp, size_t _Sz>
559 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000560
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000561template<class _Tp, size_t _Sz>
562 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
563
564template<class _Tp, size_t _Sz>
565 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
566
567template<class _Container>
568 span(_Container&) -> span<typename _Container::value_type>;
569
570template<class _Container>
571 span(const _Container&) -> span<const typename _Container::value_type>;
572
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000573#endif // _LIBCPP_STD_VER > 17
574
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000575_LIBCPP_END_NAMESPACE_STD
576
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000577#endif // _LIBCPP_SPAN