blob: 78340003445adfaf564ec9503bb8275d12106b70 [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*;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000045 using const_pointer = const element_type*;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000046 using reference = element_type&;
47 using iterator = implementation-defined;
Marshall Clow4b7f2c32019-02-25 17:58:03 +000048 using const_reference = const element_type&;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000049 using const_iterator = implementation-defined;
50 using reverse_iterator = std::reverse_iterator<iterator>;
51 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
52 static constexpr index_type extent = Extent;
53
54 // [span.cons], span constructors, copy, assignment, and destructor
55 constexpr span() noexcept;
56 constexpr span(pointer ptr, index_type count);
57 constexpr span(pointer firstElem, pointer lastElem);
58 template <size_t N>
59 constexpr span(element_type (&arr)[N]) noexcept;
60 template <size_t N>
61 constexpr span(array<value_type, N>& arr) noexcept;
62 template <size_t N>
63 constexpr span(const array<value_type, N>& arr) noexcept;
64 template <class Container>
65 constexpr span(Container& cont);
66 template <class Container>
67 constexpr span(const Container& cont);
68 constexpr span(const span& other) noexcept = default;
69 template <class OtherElementType, ptrdiff_t OtherExtent>
70 constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
71 ~span() noexcept = default;
72 constexpr span& operator=(const span& other) noexcept = default;
73
74 // [span.sub], span subviews
75 template <ptrdiff_t Count>
76 constexpr span<element_type, Count> first() const;
77 template <ptrdiff_t Count>
78 constexpr span<element_type, Count> last() const;
79 template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
80 constexpr span<element_type, see below> subspan() const;
81
82 constexpr span<element_type, dynamic_extent> first(index_type count) const;
83 constexpr span<element_type, dynamic_extent> last(index_type count) const;
84 constexpr span<element_type, dynamic_extent> subspan(index_type offset, index_type count = dynamic_extent) const;
85
86 // [span.obs], span observers
87 constexpr index_type size() const noexcept;
88 constexpr index_type size_bytes() const noexcept;
89 constexpr bool empty() const noexcept;
90
91 // [span.elem], span element access
92 constexpr reference operator[](index_type idx) const;
Marshall Clow89408802019-02-25 17:54:08 +000093 constexpr reference front() const;
94 constexpr reference back() const;
Marshall Clowd2ad87e2018-07-24 03:01:02 +000095 constexpr pointer data() const noexcept;
96
97 // [span.iterators], span iterator support
98 constexpr iterator begin() const noexcept;
99 constexpr iterator end() const noexcept;
100 constexpr const_iterator cbegin() const noexcept;
101 constexpr const_iterator cend() const noexcept;
102 constexpr reverse_iterator rbegin() const noexcept;
103 constexpr reverse_iterator rend() const noexcept;
104 constexpr const_reverse_iterator crbegin() const noexcept;
105 constexpr const_reverse_iterator crend() const noexcept;
106
107private:
108 pointer data_; // exposition only
109 index_type size_; // exposition only
110};
111
112template<class T, size_t N>
113 span(T (&)[N]) -> span<T, N>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000114
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000115template<class T, size_t N>
116 span(array<T, N>&) -> span<T, N>;
117
118template<class T, size_t N>
119 span(const array<T, N>&) -> span<const T, N>;
120
121template<class Container>
122 span(Container&) -> span<typename Container::value_type>;
123
124template<class Container>
125 span(const Container&) -> span<const typename Container::value_type>;
126
127} // namespace std
128
129*/
130
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000131#include <__config>
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000132#include <cstddef> // for ptrdiff_t
133#include <iterator> // for iterators
134#include <array> // for array
135#include <type_traits> // for remove_cv, etc
136#include <cstddef> // for byte
137
138#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
139#pragma GCC system_header
140#endif
141
142_LIBCPP_BEGIN_NAMESPACE_STD
143
144#if _LIBCPP_STD_VER > 17
145
146inline constexpr ptrdiff_t dynamic_extent = -1;
147template <typename _Tp, ptrdiff_t _Extent = dynamic_extent> class span;
148
149
150template <class _Tp>
151struct __is_span_impl : public false_type {};
152
153template <class _Tp, ptrdiff_t _Extent>
154struct __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
192template <typename _Tp, ptrdiff_t _Extent>
193class _LIBCPP_TEMPLATE_VIS span {
194public:
195// constants and types
196 using element_type = _Tp;
197 using value_type = remove_cv_t<_Tp>;
198 using index_type = ptrdiff_t;
199 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 &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000204 using iterator = __wrap_iter<pointer>;
205 using const_iterator = __wrap_iter<const_pointer>;
206 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
207 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
208
209 static constexpr index_type extent = _Extent;
210 static_assert (_Extent >= 0, "Can't have a span with an extent < 0");
211
Louis Dionne44bcff92018-08-03 22:36:53 +0000212// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000213 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}
214 { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); }
215
216 constexpr span (const span&) noexcept = default;
217 constexpr span& operator=(const span&) noexcept = default;
218
219 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}
220 { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); }
221 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}
222 { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); }
223
224 _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {}
225 _LIBCPP_INLINE_VISIBILITY constexpr span( array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {}
226 _LIBCPP_INLINE_VISIBILITY constexpr span(const array<value_type, _Extent>& __arr) noexcept : __data{__arr.data()} {}
227
228 template <class _Container>
229 inline _LIBCPP_INLINE_VISIBILITY
230 constexpr span( _Container& __c,
231 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
232 : __data{_VSTD::data(__c)}
Louis Dionne0ba926d2018-10-03 21:36:16 +0000233 { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (container)"); }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000234
235 template <class _Container>
236 inline _LIBCPP_INLINE_VISIBILITY
237 constexpr span(const _Container& __c,
238 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
239 : __data{_VSTD::data(__c)}
240 { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (const container)"); }
241
242 template <class _OtherElementType>
243 inline _LIBCPP_INLINE_VISIBILITY
244 constexpr span(const span<_OtherElementType, _Extent>& __other,
245 enable_if_t<
246 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
247 nullptr_t> = nullptr)
248 : __data{__other.data()} {}
249
250 template <class _OtherElementType>
251 inline _LIBCPP_INLINE_VISIBILITY
252 constexpr span(const span<_OtherElementType, dynamic_extent>& __other,
253 enable_if_t<
254 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
255 nullptr_t> = nullptr) noexcept
256 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
257
258
259// ~span() noexcept = default;
260
261 template <ptrdiff_t _Count>
262 inline _LIBCPP_INLINE_VISIBILITY
263 constexpr span<element_type, _Count> first() const noexcept
264 {
265 static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
266 static_assert(_Count <= _Extent, "Count out of range in span::first()");
267 return {data(), _Count};
268 }
269
270 template <ptrdiff_t _Count>
271 inline _LIBCPP_INLINE_VISIBILITY
272 constexpr span<element_type, _Count> last() const noexcept
273 {
274 static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
275 static_assert(_Count <= _Extent, "Count out of range in span::last()");
276 return {data() + size() - _Count, _Count};
277 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000278
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000279 _LIBCPP_INLINE_VISIBILITY
280 constexpr span<element_type, dynamic_extent> first(index_type __count) const noexcept
281 {
282 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)");
283 return {data(), __count};
284 }
285
286 _LIBCPP_INLINE_VISIBILITY
287 constexpr span<element_type, dynamic_extent> last(index_type __count) const noexcept
288 {
289 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)");
290 return {data() + size() - __count, __count};
291 }
292
293 template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
294 inline _LIBCPP_INLINE_VISIBILITY
295 constexpr auto subspan() const noexcept
296 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
297 {
298 _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()");
299 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
300 }
301
302
303 inline _LIBCPP_INLINE_VISIBILITY
304 constexpr span<element_type, dynamic_extent>
305 subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000306 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000307 _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)");
308 _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)");
309 if (__count == dynamic_extent)
310 return {data() + __offset, size() - __offset};
311 _LIBCPP_ASSERT(__offset + __count <= size(), "count + offset out of range in span::subspan(offset, count)");
312 return {data() + __offset, __count};
313 }
314
315 _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return _Extent; }
316 _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
317 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; }
318
319 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept
320 {
321 _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T,N>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000322 return __data[__idx];
323 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000324
Marshall Clow89408802019-02-25 17:54:08 +0000325 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000326 {
Marshall Clow89408802019-02-25 17:54:08 +0000327 static_assert(_Extent > 0, "span<T,N>[].front() on empty span");
328 return __data[0];
329 }
330
331 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
332 {
333 static_assert(_Extent > 0, "span<T,N>[].back() on empty span");
334 return __data[size()-1];
Louis Dionne44bcff92018-08-03 22:36:53 +0000335 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000336
337 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
338
339// [span.iter], span iterator support
340 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
341 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
342 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
343 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
344 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
345 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
346 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
347 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
348
349 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
350 {
351 pointer __p = __data;
352 __data = __other.__data;
353 __other.__data = __p;
354 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000355
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000356 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
357 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
358
359 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writeable_bytes() const noexcept
360 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
361
362private:
363 pointer __data;
364
365};
366
367
368template <typename _Tp>
369class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
370private:
371
372public:
373// constants and types
374 using element_type = _Tp;
375 using value_type = remove_cv_t<_Tp>;
376 using index_type = ptrdiff_t;
377 using difference_type = ptrdiff_t;
378 using pointer = _Tp *;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000379 using const_pointer = const _Tp *;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000380 using reference = _Tp &;
Marshall Clow4b7f2c32019-02-25 17:58:03 +0000381 using const_reference = const _Tp &;
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000382 using iterator = __wrap_iter<pointer>;
383 using const_iterator = __wrap_iter<const_pointer>;
384 using reverse_iterator = _VSTD::reverse_iterator<iterator>;
385 using const_reverse_iterator = _VSTD::reverse_iterator<const_iterator>;
386
387 static constexpr index_type extent = dynamic_extent;
388
Louis Dionne44bcff92018-08-03 22:36:53 +0000389// [span.cons], span constructors, copy, assignment, and destructor
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000390 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {}
391
392 constexpr span (const span&) noexcept = default;
393 constexpr span& operator=(const span&) noexcept = default;
394
395 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {}
396 _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{distance(__f, __l)} {}
397
398 template <size_t _Sz>
399 inline _LIBCPP_INLINE_VISIBILITY
400 constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
401
402 template <size_t _Sz>
403 inline _LIBCPP_INLINE_VISIBILITY
404 constexpr span(array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
Louis Dionne44bcff92018-08-03 22:36:53 +0000405
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000406 template <size_t _Sz>
407 inline _LIBCPP_INLINE_VISIBILITY
408 constexpr span(const array<value_type, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
409
410 template <class _Container>
411 inline _LIBCPP_INLINE_VISIBILITY
412 constexpr span( _Container& __c,
413 enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr)
414 : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {}
415
416 template <class _Container>
417 inline _LIBCPP_INLINE_VISIBILITY
418 constexpr span(const _Container& __c,
419 enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, nullptr_t> = nullptr)
420 : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {}
421
422
423 template <class _OtherElementType, ptrdiff_t _OtherExtent>
424 inline _LIBCPP_INLINE_VISIBILITY
425 constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
426 enable_if_t<
427 is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
428 nullptr_t> = nullptr) noexcept
429 : __data{__other.data()}, __size{__other.size()} {}
430
431// ~span() noexcept = default;
432
433 template <ptrdiff_t _Count>
434 inline _LIBCPP_INLINE_VISIBILITY
435 constexpr span<element_type, _Count> first() const noexcept
436 {
Louis Dionne0ba926d2018-10-03 21:36:16 +0000437 static_assert(_Count >= 0, "Count must be >= 0 in span::first()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000438 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()");
439 return {data(), _Count};
440 }
441
442 template <ptrdiff_t _Count>
443 inline _LIBCPP_INLINE_VISIBILITY
444 constexpr span<element_type, _Count> last() const noexcept
445 {
Louis Dionne0ba926d2018-10-03 21:36:16 +0000446 static_assert(_Count >= 0, "Count must be >= 0 in span::last()");
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000447 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()");
448 return {data() + size() - _Count, _Count};
449 }
450
451 _LIBCPP_INLINE_VISIBILITY
452 constexpr span<element_type, dynamic_extent> first(index_type __count) const noexcept
453 {
454 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)");
455 return {data(), __count};
456 }
Louis Dionne44bcff92018-08-03 22:36:53 +0000457
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000458 _LIBCPP_INLINE_VISIBILITY
459 constexpr span<element_type, dynamic_extent> last (index_type __count) const noexcept
460 {
461 _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)");
462 return {data() + size() - __count, __count};
463 }
464
465 template <ptrdiff_t _Offset, ptrdiff_t _Count = dynamic_extent>
466 inline _LIBCPP_INLINE_VISIBILITY
467 constexpr span<_Tp, dynamic_extent> subspan() const noexcept
468 {
469 _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()");
470 _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()");
471 return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
472 }
473
474 constexpr span<element_type, dynamic_extent>
475 inline _LIBCPP_INLINE_VISIBILITY
476 subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
Louis Dionne44bcff92018-08-03 22:36:53 +0000477 {
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000478 _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)");
479 _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
480 if (__count == dynamic_extent)
481 return {data() + __offset, size() - __offset};
482 _LIBCPP_ASSERT(__offset + __count <= size(), "Offset + count out of range in span::subspan(offset, count)");
483 return {data() + __offset, __count};
484 }
485
486 _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return __size; }
487 _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return __size * sizeof(element_type); }
488 _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; }
489
490 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept
491 {
492 _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span<T>[] index out of bounds");
Louis Dionne44bcff92018-08-03 22:36:53 +0000493 return __data[__idx];
494 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000495
Marshall Clow89408802019-02-25 17:54:08 +0000496 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000497 {
Marshall Clow89408802019-02-25 17:54:08 +0000498 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span");
499 return __data[0];
Louis Dionne44bcff92018-08-03 22:36:53 +0000500 }
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000501
Marshall Clow89408802019-02-25 17:54:08 +0000502 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept
503 {
504 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span");
505 return __data[size()-1];
506 }
507
508
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000509 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; }
510
511// [span.iter], span iterator support
512 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); }
513 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); }
514 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); }
515 _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); }
516 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
517 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
518 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
519 _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
520
521 _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept
522 {
523 pointer __p = __data;
524 __data = __other.__data;
525 __other.__data = __p;
526
527 index_type __sz = __size;
528 __size = __other.__size;
529 __other.__size = __sz;
530 }
531
532 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept
533 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; }
534
535 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writeable_bytes() const noexcept
536 { return {reinterpret_cast<byte *>(data()), size_bytes()}; }
537
538private:
539 pointer __data;
540 index_type __size;
541};
542
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000543// as_bytes & as_writeable_bytes
544template <class _Tp, ptrdiff_t _Extent>
Louis Dionne44bcff92018-08-03 22:36:53 +0000545 auto as_bytes(span<_Tp, _Extent> __s) noexcept
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000546 -> decltype(__s.__as_bytes())
547 { return __s.__as_bytes(); }
548
549template <class _Tp, ptrdiff_t _Extent>
550 auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept
551 -> typename enable_if<!is_const_v<_Tp>, decltype(__s.__as_writeable_bytes())>::type
552 { return __s.__as_writeable_bytes(); }
553
554template <class _Tp, ptrdiff_t _Extent>
555 constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
556 { __lhs.swap(__rhs); }
557
558
559// Deduction guides
560template<class _Tp, size_t _Sz>
561 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>;
Louis Dionne44bcff92018-08-03 22:36:53 +0000562
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000563template<class _Tp, size_t _Sz>
564 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>;
565
566template<class _Tp, size_t _Sz>
567 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>;
568
569template<class _Container>
570 span(_Container&) -> span<typename _Container::value_type>;
571
572template<class _Container>
573 span(const _Container&) -> span<const typename _Container::value_type>;
574
Marshall Clow8dc3ea12018-07-24 03:56:38 +0000575#endif // _LIBCPP_STD_VER > 17
576
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000577_LIBCPP_END_NAMESPACE_STD
578
Marshall Clowd2ad87e2018-07-24 03:01:02 +0000579#endif // _LIBCPP_SPAN