| 1 |
// -*- C++ -*- |
| 2 |
//===----------------------------------------------------------------------===// |
| 3 |
// |
| 4 |
// 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 |
| 7 |
// |
| 8 |
//===---------------------------------------------------------------------===// |
| 9 |
|
| 10 |
#ifndef _LIBCPP_SPAN |
| 11 |
#define _LIBCPP_SPAN |
| 12 |
|
| 13 |
/* |
| 14 |
span synopsis |
| 15 |
|
| 16 |
namespace std { |
| 17 |
|
| 18 |
// constants |
| 19 |
inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); |
| 20 |
|
| 21 |
// [views.span], class template span |
| 22 |
template <class ElementType, size_t Extent = dynamic_extent> |
| 23 |
class span; |
| 24 |
|
| 25 |
template<class ElementType, size_t Extent> |
| 26 |
inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true; |
| 27 |
|
| 28 |
template<class ElementType, size_t Extent> |
| 29 |
inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true; |
| 30 |
|
| 31 |
// [span.objectrep], views of object representation |
| 32 |
template <class ElementType, size_t Extent> |
| 33 |
span<const byte, ((Extent == dynamic_extent) ? dynamic_extent : |
| 34 |
(sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept; |
| 35 |
|
| 36 |
template <class ElementType, size_t Extent> |
| 37 |
span< byte, ((Extent == dynamic_extent) ? dynamic_extent : |
| 38 |
(sizeof(ElementType) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept; |
| 39 |
|
| 40 |
|
| 41 |
template <class ElementType, size_t Extent = dynamic_extent> |
| 42 |
class span { |
| 43 |
public: |
| 44 |
// constants and types |
| 45 |
using element_type = ElementType; |
| 46 |
using value_type = remove_cv_t<ElementType>; |
| 47 |
using size_type = size_t; |
| 48 |
using difference_type = ptrdiff_t; |
| 49 |
using pointer = element_type*; |
| 50 |
using const_pointer = const element_type*; |
| 51 |
using reference = element_type&; |
| 52 |
using const_reference = const element_type&; |
| 53 |
using iterator = implementation-defined; |
| 54 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 55 |
static constexpr size_type extent = Extent; |
| 56 |
|
| 57 |
// [span.cons], span constructors, copy, assignment, and destructor |
| 58 |
constexpr span() noexcept; |
| 59 |
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); |
| 63 |
template <size_t N> |
| 64 |
constexpr span(type_identity_t<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; |
| 69 |
template<class R> |
| 70 |
constexpr explicit(Extent != dynamic_extent) span(R&& r); |
| 71 |
constexpr span(const span& other) noexcept = default; |
| 72 |
template <class OtherElementType, size_t OtherExtent> |
| 73 |
constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept; |
| 74 |
~span() noexcept = default; |
| 75 |
constexpr span& operator=(const span& other) noexcept = default; |
| 76 |
|
| 77 |
// [span.sub], span subviews |
| 78 |
template <size_t Count> |
| 79 |
constexpr span<element_type, Count> first() const; |
| 80 |
template <size_t Count> |
| 81 |
constexpr span<element_type, Count> last() const; |
| 82 |
template <size_t Offset, size_t Count = dynamic_extent> |
| 83 |
constexpr span<element_type, see below> subspan() const; |
| 84 |
|
| 85 |
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; |
| 88 |
|
| 89 |
// [span.obs], span observers |
| 90 |
constexpr size_type size() const noexcept; |
| 91 |
constexpr size_type size_bytes() const noexcept; |
| 92 |
[[nodiscard]] constexpr bool empty() const noexcept; |
| 93 |
|
| 94 |
// [span.elem], span element access |
| 95 |
constexpr reference operator[](size_type idx) const; |
| 96 |
constexpr reference front() const; |
| 97 |
constexpr reference back() const; |
| 98 |
constexpr pointer data() const noexcept; |
| 99 |
|
| 100 |
// [span.iterators], span iterator support |
| 101 |
constexpr iterator begin() const noexcept; |
| 102 |
constexpr iterator end() const noexcept; |
| 103 |
constexpr reverse_iterator rbegin() const noexcept; |
| 104 |
constexpr reverse_iterator rend() const noexcept; |
| 105 |
|
| 106 |
private: |
| 107 |
pointer data_; // exposition only |
| 108 |
size_type size_; // exposition only |
| 109 |
}; |
| 110 |
|
| 111 |
template<class It, class EndOrSize> |
| 112 |
span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; |
| 113 |
|
| 114 |
template<class T, size_t N> |
| 115 |
span(T (&)[N]) -> span<T, N>; |
| 116 |
|
| 117 |
template<class T, size_t N> |
| 118 |
span(array<T, N>&) -> span<T, N>; |
| 119 |
|
| 120 |
template<class T, size_t N> |
| 121 |
span(const array<T, N>&) -> span<const T, N>; |
| 122 |
|
| 123 |
template<class R> |
| 124 |
span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>; |
| 125 |
|
| 126 |
} // namespace std |
| 127 |
|
| 128 |
*/ |
| 129 |
|
| 130 |
#include <__assert> // all public C++ headers provide the assertion handler |
| 131 |
#include <__config> |
| 132 |
#include <__debug> |
| 133 |
#include <__fwd/span.h> |
| 134 |
#include <__iterator/bounded_iter.h> |
| 135 |
#include <__iterator/concepts.h> |
| 136 |
#include <__iterator/iterator_traits.h> |
| 137 |
#include <__iterator/wrap_iter.h> |
| 138 |
#include <__memory/pointer_traits.h> |
| 139 |
#include <__ranges/concepts.h> |
| 140 |
#include <__ranges/data.h> |
| 141 |
#include <__ranges/enable_borrowed_range.h> |
| 142 |
#include <__ranges/enable_view.h> |
| 143 |
#include <__ranges/size.h> |
| 144 |
#include <__utility/forward.h> |
| 145 |
#include <array> // for array |
| 146 |
#include <cstddef> // for byte |
| 147 |
#include <limits> |
| 148 |
#include <type_traits> // for remove_cv, etc |
| 149 |
#include <version> |
| 150 |
|
| 151 |
// standard-mandated includes |
| 152 |
|
| 153 |
// [iterator.range] |
| 154 |
#include <__iterator/access.h> |
| 155 |
#include <__iterator/data.h> |
| 156 |
#include <__iterator/empty.h> |
| 157 |
#include <__iterator/reverse_access.h> |
| 158 |
#include <__iterator/size.h> |
| 159 |
|
| 160 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 161 |
# pragma GCC system_header |
| 162 |
#endif |
| 163 |
|
| 164 |
_LIBCPP_PUSH_MACROS |
| 165 |
#include <__undef_macros> |
| 166 |
|
| 167 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 168 |
|
| 169 |
#if _LIBCPP_STD_VER > 17 |
| 170 |
|
| 171 |
template <class _Tp> |
| 172 |
struct __is_std_array : false_type {}; |
| 173 |
|
| 174 |
template <class _Tp, size_t _Sz> |
| 175 |
struct __is_std_array<array<_Tp, _Sz>> : true_type {}; |
| 176 |
|
| 177 |
template <class _Tp> |
| 178 |
struct __is_std_span : false_type {}; |
| 179 |
|
| 180 |
template <class _Tp, size_t _Sz> |
| 181 |
struct __is_std_span<span<_Tp, _Sz>> : true_type {}; |
| 182 |
|
| 183 |
template <class _Range, class _ElementType> |
| 184 |
concept __span_compatible_range = |
| 185 |
ranges::contiguous_range<_Range> && |
| 186 |
ranges::sized_range<_Range> && |
| 187 |
(ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && |
| 188 |
!__is_std_span<remove_cvref_t<_Range>>::value && |
| 189 |
!__is_std_array<remove_cvref_t<_Range>>::value && |
| 190 |
!is_array_v<remove_cvref_t<_Range>> && |
| 191 |
is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>; |
| 192 |
|
| 193 |
template <class _From, class _To> |
| 194 |
concept __span_array_convertible = is_convertible_v<_From(*)[], _To(*)[]>; |
| 195 |
|
| 196 |
template <class _It, class _Tp> |
| 197 |
concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>; |
| 198 |
|
| 199 |
template <class _Sentinel, class _It> |
| 200 |
concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>; |
| 201 |
|
| 202 |
template <typename _Tp, size_t _Extent> |
| 203 |
class _LIBCPP_TEMPLATE_VIS span { |
| 204 |
public: |
| 205 |
// constants and types |
| 206 |
using element_type = _Tp; |
| 207 |
using value_type = remove_cv_t<_Tp>; |
| 208 |
using size_type = size_t; |
| 209 |
using difference_type = ptrdiff_t; |
| 210 |
using pointer = _Tp *; |
| 211 |
using const_pointer = const _Tp *; |
| 212 |
using reference = _Tp &; |
| 213 |
using const_reference = const _Tp &; |
| 214 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 215 |
using iterator = __bounded_iter<pointer>; |
| 216 |
#else |
| 217 |
using iterator = __wrap_iter<pointer>; |
| 218 |
#endif |
| 219 |
using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
| 220 |
|
| 221 |
static constexpr size_type extent = _Extent; |
| 222 |
|
| 223 |
// [span.cons], span constructors, copy, assignment, and destructor |
| 224 |
template <size_t _Sz = _Extent> requires(_Sz == 0) |
| 225 |
_LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data_{nullptr} {} |
| 226 |
|
| 227 |
constexpr span (const span&) noexcept = default; |
| 228 |
constexpr span& operator=(const span&) noexcept = default; |
| 229 |
|
| 230 |
template <__span_compatible_iterator<element_type> _It> |
| 231 |
_LIBCPP_INLINE_VISIBILITY |
| 232 |
constexpr explicit span(_It __first, size_type __count) |
| 233 |
: __data_{_VSTD::to_address(__first)} { |
| 234 |
(void)__count; |
| 235 |
_LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); |
| 236 |
} |
| 237 |
|
| 238 |
template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> |
| 239 |
_LIBCPP_INLINE_VISIBILITY |
| 240 |
constexpr explicit span(_It __first, _End __last) : __data_{_VSTD::to_address(__first)} { |
| 241 |
(void)__last; |
| 242 |
_LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)"); |
| 243 |
_LIBCPP_ASSERT(__last - __first == _Extent, |
| 244 |
"invalid range in span's constructor (iterator, sentinel): last - first != extent"); |
| 245 |
} |
| 246 |
|
| 247 |
_LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data_{__arr} {} |
| 248 |
|
| 249 |
template <__span_array_convertible<element_type> _OtherElementType> |
| 250 |
_LIBCPP_INLINE_VISIBILITY |
| 251 |
constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {} |
| 252 |
|
| 253 |
template <class _OtherElementType> |
| 254 |
requires __span_array_convertible<const _OtherElementType, element_type> |
| 255 |
_LIBCPP_INLINE_VISIBILITY |
| 256 |
constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data_{__arr.data()} {} |
| 257 |
|
| 258 |
template <__span_compatible_range<element_type> _Range> |
| 259 |
_LIBCPP_INLINE_VISIBILITY |
| 260 |
constexpr explicit span(_Range&& __r) : __data_{ranges::data(__r)} { |
| 261 |
_LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); |
| 262 |
} |
| 263 |
|
| 264 |
template <__span_array_convertible<element_type> _OtherElementType> |
| 265 |
_LIBCPP_INLINE_VISIBILITY |
| 266 |
constexpr span(const span<_OtherElementType, _Extent>& __other) |
| 267 |
: __data_{__other.data()} {} |
| 268 |
|
| 269 |
template <__span_array_convertible<element_type> _OtherElementType> |
| 270 |
_LIBCPP_INLINE_VISIBILITY |
| 271 |
constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept |
| 272 |
: __data_{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } |
| 273 |
|
| 274 |
|
| 275 |
// ~span() noexcept = default; |
| 276 |
|
| 277 |
template <size_t _Count> |
| 278 |
_LIBCPP_INLINE_VISIBILITY |
| 279 |
constexpr span<element_type, _Count> first() const noexcept |
| 280 |
{ |
| 281 |
static_assert(_Count <= _Extent, "span<T, N>::first<Count>(): Count out of range"); |
| 282 |
return span<element_type, _Count>{data(), _Count}; |
| 283 |
} |
| 284 |
|
| 285 |
template <size_t _Count> |
| 286 |
_LIBCPP_INLINE_VISIBILITY |
| 287 |
constexpr span<element_type, _Count> last() const noexcept |
| 288 |
{ |
| 289 |
static_assert(_Count <= _Extent, "span<T, N>::last<Count>(): Count out of range"); |
| 290 |
return span<element_type, _Count>{data() + size() - _Count, _Count}; |
| 291 |
} |
| 292 |
|
| 293 |
_LIBCPP_INLINE_VISIBILITY |
| 294 |
constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
| 295 |
{ |
| 296 |
_LIBCPP_ASSERT(__count <= size(), "span<T, N>::first(count): count out of range"); |
| 297 |
return {data(), __count}; |
| 298 |
} |
| 299 |
|
| 300 |
_LIBCPP_INLINE_VISIBILITY |
| 301 |
constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept |
| 302 |
{ |
| 303 |
_LIBCPP_ASSERT(__count <= size(), "span<T, N>::last(count): count out of range"); |
| 304 |
return {data() + size() - __count, __count}; |
| 305 |
} |
| 306 |
|
| 307 |
template <size_t _Offset, size_t _Count = dynamic_extent> |
| 308 |
_LIBCPP_INLINE_VISIBILITY |
| 309 |
constexpr auto subspan() const noexcept |
| 310 |
-> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> |
| 311 |
{ |
| 312 |
static_assert(_Offset <= _Extent, "span<T, N>::subspan<Offset, Count>(): Offset out of range"); |
| 313 |
static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span<T, N>::subspan<Offset, Count>(): Offset + Count out of range"); |
| 314 |
|
| 315 |
using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; |
| 316 |
return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
_LIBCPP_INLINE_VISIBILITY |
| 321 |
constexpr span<element_type, dynamic_extent> |
| 322 |
subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
| 323 |
{ |
| 324 |
_LIBCPP_ASSERT(__offset <= size(), "span<T, N>::subspan(offset, count): offset out of range"); |
| 325 |
_LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "span<T, N>::subspan(offset, count): count out of range"); |
| 326 |
if (__count == dynamic_extent) |
| 327 |
return {data() + __offset, size() - __offset}; |
| 328 |
_LIBCPP_ASSERT(__count <= size() - __offset, "span<T, N>::subspan(offset, count): offset + count out of range"); |
| 329 |
return {data() + __offset, __count}; |
| 330 |
} |
| 331 |
|
| 332 |
_LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; } |
| 333 |
_LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } |
| 334 |
[[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } |
| 335 |
|
| 336 |
_LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
| 337 |
{ |
| 338 |
_LIBCPP_ASSERT(__idx < size(), "span<T, N>::operator[](index): index out of range"); |
| 339 |
return __data_[__idx]; |
| 340 |
} |
| 341 |
|
| 342 |
_LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
| 343 |
{ |
| 344 |
_LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span"); |
| 345 |
return __data_[0]; |
| 346 |
} |
| 347 |
|
| 348 |
_LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 349 |
{ |
| 350 |
_LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span"); |
| 351 |
return __data_[size()-1]; |
| 352 |
} |
| 353 |
|
| 354 |
_LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data_; } |
| 355 |
|
| 356 |
// [span.iter], span iterator support |
| 357 |
_LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { |
| 358 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 359 |
return std::__make_bounded_iter(data(), data(), data() + size()); |
| 360 |
#else |
| 361 |
return iterator(this, data()); |
| 362 |
#endif |
| 363 |
} |
| 364 |
_LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { |
| 365 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 366 |
return std::__make_bounded_iter(data() + size(), data(), data() + size()); |
| 367 |
#else |
| 368 |
return iterator(this, data() + size()); |
| 369 |
#endif |
| 370 |
} |
| 371 |
_LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 372 |
_LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
| 373 |
|
| 374 |
_LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept |
| 375 |
{ return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; } |
| 376 |
|
| 377 |
_LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept |
| 378 |
{ return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; } |
| 379 |
|
| 380 |
private: |
| 381 |
pointer __data_; |
| 382 |
}; |
| 383 |
|
| 384 |
|
| 385 |
template <typename _Tp> |
| 386 |
class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { |
| 387 |
public: |
| 388 |
// constants and types |
| 389 |
using element_type = _Tp; |
| 390 |
using value_type = remove_cv_t<_Tp>; |
| 391 |
using size_type = size_t; |
| 392 |
using difference_type = ptrdiff_t; |
| 393 |
using pointer = _Tp *; |
| 394 |
using const_pointer = const _Tp *; |
| 395 |
using reference = _Tp &; |
| 396 |
using const_reference = const _Tp &; |
| 397 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 398 |
using iterator = __bounded_iter<pointer>; |
| 399 |
#else |
| 400 |
using iterator = __wrap_iter<pointer>; |
| 401 |
#endif |
| 402 |
using reverse_iterator = _VSTD::reverse_iterator<iterator>; |
| 403 |
|
| 404 |
static constexpr size_type extent = dynamic_extent; |
| 405 |
|
| 406 |
// [span.cons], span constructors, copy, assignment, and destructor |
| 407 |
_LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data_{nullptr}, __size_{0} {} |
| 408 |
|
| 409 |
constexpr span (const span&) noexcept = default; |
| 410 |
constexpr span& operator=(const span&) noexcept = default; |
| 411 |
|
| 412 |
template <__span_compatible_iterator<element_type> _It> |
| 413 |
_LIBCPP_INLINE_VISIBILITY |
| 414 |
constexpr span(_It __first, size_type __count) |
| 415 |
: __data_{_VSTD::to_address(__first)}, __size_{__count} {} |
| 416 |
|
| 417 |
template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> |
| 418 |
_LIBCPP_INLINE_VISIBILITY constexpr span(_It __first, _End __last) |
| 419 |
: __data_(_VSTD::to_address(__first)), __size_(__last - __first) { |
| 420 |
_LIBCPP_ASSERT(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)"); |
| 421 |
} |
| 422 |
|
| 423 |
template <size_t _Sz> |
| 424 |
_LIBCPP_INLINE_VISIBILITY |
| 425 |
constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data_{__arr}, __size_{_Sz} {} |
| 426 |
|
| 427 |
template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz> |
| 428 |
_LIBCPP_INLINE_VISIBILITY |
| 429 |
constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {} |
| 430 |
|
| 431 |
template <class _OtherElementType, size_t _Sz> |
| 432 |
requires __span_array_convertible<const _OtherElementType, element_type> |
| 433 |
_LIBCPP_INLINE_VISIBILITY |
| 434 |
constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data_{__arr.data()}, __size_{_Sz} {} |
| 435 |
|
| 436 |
template <__span_compatible_range<element_type> _Range> |
| 437 |
_LIBCPP_INLINE_VISIBILITY |
| 438 |
constexpr span(_Range&& __r) : __data_(ranges::data(__r)), __size_{ranges::size(__r)} {} |
| 439 |
|
| 440 |
template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent> |
| 441 |
_LIBCPP_INLINE_VISIBILITY |
| 442 |
constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept |
| 443 |
: __data_{__other.data()}, __size_{__other.size()} {} |
| 444 |
|
| 445 |
// ~span() noexcept = default; |
| 446 |
|
| 447 |
template <size_t _Count> |
| 448 |
_LIBCPP_INLINE_VISIBILITY |
| 449 |
constexpr span<element_type, _Count> first() const noexcept |
| 450 |
{ |
| 451 |
_LIBCPP_ASSERT(_Count <= size(), "span<T>::first<Count>(): Count out of range"); |
| 452 |
return span<element_type, _Count>{data(), _Count}; |
| 453 |
} |
| 454 |
|
| 455 |
template <size_t _Count> |
| 456 |
_LIBCPP_INLINE_VISIBILITY |
| 457 |
constexpr span<element_type, _Count> last() const noexcept |
| 458 |
{ |
| 459 |
_LIBCPP_ASSERT(_Count <= size(), "span<T>::last<Count>(): Count out of range"); |
| 460 |
return span<element_type, _Count>{data() + size() - _Count, _Count}; |
| 461 |
} |
| 462 |
|
| 463 |
_LIBCPP_INLINE_VISIBILITY |
| 464 |
constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept |
| 465 |
{ |
| 466 |
_LIBCPP_ASSERT(__count <= size(), "span<T>::first(count): count out of range"); |
| 467 |
return {data(), __count}; |
| 468 |
} |
| 469 |
|
| 470 |
_LIBCPP_INLINE_VISIBILITY |
| 471 |
constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept |
| 472 |
{ |
| 473 |
_LIBCPP_ASSERT(__count <= size(), "span<T>::last(count): count out of range"); |
| 474 |
return {data() + size() - __count, __count}; |
| 475 |
} |
| 476 |
|
| 477 |
template <size_t _Offset, size_t _Count = dynamic_extent> |
| 478 |
_LIBCPP_INLINE_VISIBILITY |
| 479 |
constexpr span<element_type, _Count> subspan() const noexcept |
| 480 |
{ |
| 481 |
_LIBCPP_ASSERT(_Offset <= size(), "span<T>::subspan<Offset, Count>(): Offset out of range"); |
| 482 |
_LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "span<T>::subspan<Offset, Count>(): Offset + Count out of range"); |
| 483 |
return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; |
| 484 |
} |
| 485 |
|
| 486 |
constexpr span<element_type, dynamic_extent> |
| 487 |
_LIBCPP_INLINE_VISIBILITY |
| 488 |
subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept |
| 489 |
{ |
| 490 |
_LIBCPP_ASSERT(__offset <= size(), "span<T>::subspan(offset, count): offset out of range"); |
| 491 |
_LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "span<T>::subspan(offset, count): count out of range"); |
| 492 |
if (__count == dynamic_extent) |
| 493 |
return {data() + __offset, size() - __offset}; |
| 494 |
_LIBCPP_ASSERT(__count <= size() - __offset, "span<T>::subspan(offset, count): offset + count out of range"); |
| 495 |
return {data() + __offset, __count}; |
| 496 |
} |
| 497 |
|
| 498 |
_LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size_; } |
| 499 |
_LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size_ * sizeof(element_type); } |
| 500 |
[[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size_ == 0; } |
| 501 |
|
| 502 |
_LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept |
| 503 |
{ |
| 504 |
_LIBCPP_ASSERT(__idx < size(), "span<T>::operator[](index): index out of range"); |
| 505 |
return __data_[__idx]; |
| 506 |
} |
| 507 |
|
| 508 |
_LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept |
| 509 |
{ |
| 510 |
_LIBCPP_ASSERT(!empty(), "span<T>::front() on empty span"); |
| 511 |
return __data_[0]; |
| 512 |
} |
| 513 |
|
| 514 |
_LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept |
| 515 |
{ |
| 516 |
_LIBCPP_ASSERT(!empty(), "span<T>::back() on empty span"); |
| 517 |
return __data_[size()-1]; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
_LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data_; } |
| 522 |
|
| 523 |
// [span.iter], span iterator support |
| 524 |
_LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { |
| 525 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 526 |
return std::__make_bounded_iter(data(), data(), data() + size()); |
| 527 |
#else |
| 528 |
return iterator(this, data()); |
| 529 |
#endif |
| 530 |
} |
| 531 |
_LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { |
| 532 |
#ifdef _LIBCPP_DEBUG_ITERATOR_BOUNDS_CHECKING |
| 533 |
return std::__make_bounded_iter(data() + size(), data(), data() + size()); |
| 534 |
#else |
| 535 |
return iterator(this, data() + size()); |
| 536 |
#endif |
| 537 |
} |
| 538 |
_LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } |
| 539 |
_LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } |
| 540 |
|
| 541 |
_LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept |
| 542 |
{ return {reinterpret_cast<const byte *>(data()), size_bytes()}; } |
| 543 |
|
| 544 |
_LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept |
| 545 |
{ return {reinterpret_cast<byte *>(data()), size_bytes()}; } |
| 546 |
|
| 547 |
private: |
| 548 |
pointer __data_; |
| 549 |
size_type __size_; |
| 550 |
}; |
| 551 |
|
| 552 |
template <class _Tp, size_t _Extent> |
| 553 |
inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true; |
| 554 |
|
| 555 |
template <class _ElementType, size_t _Extent> |
| 556 |
inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true; |
| 557 |
|
| 558 |
// as_bytes & as_writable_bytes |
| 559 |
template <class _Tp, size_t _Extent> |
| 560 |
_LIBCPP_INLINE_VISIBILITY |
| 561 |
auto as_bytes(span<_Tp, _Extent> __s) noexcept |
| 562 |
{ return __s.__as_bytes(); } |
| 563 |
|
| 564 |
template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>) |
| 565 |
_LIBCPP_INLINE_VISIBILITY |
| 566 |
auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept |
| 567 |
{ return __s.__as_writable_bytes(); } |
| 568 |
|
| 569 |
#if _LIBCPP_STD_VER > 17 |
| 570 |
template<contiguous_iterator _It, class _EndOrSize> |
| 571 |
span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; |
| 572 |
#endif // _LIBCPP_STD_VER > 17 |
| 573 |
|
| 574 |
template<class _Tp, size_t _Sz> |
| 575 |
span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; |
| 576 |
|
| 577 |
template<class _Tp, size_t _Sz> |
| 578 |
span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; |
| 579 |
|
| 580 |
template<class _Tp, size_t _Sz> |
| 581 |
span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; |
| 582 |
|
| 583 |
template<ranges::contiguous_range _Range> |
| 584 |
span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>; |
| 585 |
|
| 586 |
#endif // _LIBCPP_STD_VER > 17 |
| 587 |
|
| 588 |
_LIBCPP_END_NAMESPACE_STD |
| 589 |
|
| 590 |
_LIBCPP_POP_MACROS |
| 591 |
|
| 592 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
| 593 |
# include <concepts> |
| 594 |
# include <functional> |
| 595 |
# include <iterator> |
| 596 |
#endif |
| 597 |
|
| 598 |
#endif // _LIBCPP_SPAN |