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 16namespace std { 17 18// constants 19inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); 20 21// [views.span], class template span 22template <class ElementType, size_t Extent = dynamic_extent> 23 class span; 24 25template<class ElementType, size_t Extent> 26 inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true; 27 28template<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 32template <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 36template <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 41template <class ElementType, size_t Extent = dynamic_extent> 42class span { 43public: 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 106private: 107 pointer data_; // exposition only 108 size_type size_; // exposition only 109}; 110 111template<class It, class EndOrSize> 112 span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; 113 114template<class T, size_t N> 115 span(T (&)[N]) -> span<T, N>; 116 117template<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 123template<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/concepts.h> 135#include <__iterator/data.h> 136#include <__iterator/iterator_traits.h> 137#include <__iterator/size.h> 138#include <__iterator/wrap_iter.h> 139#include <__memory/pointer_traits.h> 140#include <__ranges/concepts.h> 141#include <__ranges/data.h> 142#include <__ranges/enable_borrowed_range.h> 143#include <__ranges/enable_view.h> 144#include <__ranges/size.h> 145#include <__utility/forward.h> 146#include <array> // for array 147#include <cstddef> // for byte 148#include <limits> 149#include <type_traits> // for remove_cv, etc 150#include <version> 151 152#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 153# pragma GCC system_header 154#endif 155 156_LIBCPP_PUSH_MACROS 157#include <__undef_macros> 158 159_LIBCPP_BEGIN_NAMESPACE_STD 160 161#if _LIBCPP_STD_VER > 17 162 163template <class _Tp> 164struct __is_std_array : false_type {}; 165 166template <class _Tp, size_t _Sz> 167struct __is_std_array<array<_Tp, _Sz>> : true_type {}; 168 169template <class _Tp> 170struct __is_std_span : false_type {}; 171 172template <class _Tp, size_t _Sz> 173struct __is_std_span<span<_Tp, _Sz>> : true_type {}; 174 175#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 176// This is a temporary workaround until we ship <ranges> -- we've unfortunately been 177// shipping <span> before its API was finalized, and we used to provide a constructor 178// from container types that had the requirements below. To avoid breaking code that 179// has started relying on the range-based constructor until we ship all of <ranges>, 180// we emulate the constructor requirements like this. 181template <class _Range, class _ElementType> 182concept __span_compatible_range = 183 !__is_std_span<remove_cvref_t<_Range>>::value && 184 !__is_std_array<remove_cvref_t<_Range>>::value && 185 !is_array_v<remove_cvref_t<_Range>> && 186 requires (_Range&& __r) { 187 data(std::forward<_Range>(__r)); 188 size(std::forward<_Range>(__r)); 189 } && 190 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>; 191#else 192template <class _Range, class _ElementType> 193concept __span_compatible_range = 194 ranges::contiguous_range<_Range> && 195 ranges::sized_range<_Range> && 196 (ranges::borrowed_range<_Range> || is_const_v<_ElementType>) && 197 !__is_std_span<remove_cvref_t<_Range>>::value && 198 !__is_std_array<remove_cvref_t<_Range>>::value && 199 !is_array_v<remove_cvref_t<_Range>> && 200 is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>; 201#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 202 203template <class _From, class _To> 204concept __span_array_convertible = is_convertible_v<_From(*)[], _To(*)[]>; 205 206template <class _It, class _Tp> 207concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>; 208 209template <class _Sentinel, class _It> 210concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>; 211 212#if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) 213# define _LIBCPP_SPAN_USE_POINTER_ITERATOR 214#endif 215 216template <typename _Tp, size_t _Extent> 217class _LIBCPP_TEMPLATE_VIS span { 218public: 219// constants and types 220 using element_type = _Tp; 221 using value_type = remove_cv_t<_Tp>; 222 using size_type = size_t; 223 using difference_type = ptrdiff_t; 224 using pointer = _Tp *; 225 using const_pointer = const _Tp *; 226 using reference = _Tp &; 227 using const_reference = const _Tp &; 228#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 229 using iterator = pointer; 230#else 231 using iterator = __wrap_iter<pointer>; 232#endif 233 using reverse_iterator = _VSTD::reverse_iterator<iterator>; 234 235 static constexpr size_type extent = _Extent; 236 237// [span.cons], span constructors, copy, assignment, and destructor 238 template <size_t _Sz = _Extent> requires(_Sz == 0) 239 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {} 240 241 constexpr span (const span&) noexcept = default; 242 constexpr span& operator=(const span&) noexcept = default; 243 244 template <__span_compatible_iterator<element_type> _It> 245 _LIBCPP_INLINE_VISIBILITY 246 constexpr explicit span(_It __first, size_type __count) 247 : __data{_VSTD::to_address(__first)} { 248 (void)__count; 249 _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)"); 250 } 251 252 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> 253 _LIBCPP_INLINE_VISIBILITY 254 constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} { 255 (void)__last; 256 _LIBCPP_ASSERT((__last - __first >= 0), "invalid range in span's constructor (iterator, sentinel)"); 257 _LIBCPP_ASSERT(__last - __first == _Extent, 258 "invalid range in span's constructor (iterator, sentinel): last - first != extent"); 259 } 260 261 _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {} 262 263 template <__span_array_convertible<element_type> _OtherElementType> 264 _LIBCPP_INLINE_VISIBILITY 265 constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} 266 267 template <class _OtherElementType> 268 requires __span_array_convertible<const _OtherElementType, element_type> 269 _LIBCPP_INLINE_VISIBILITY 270 constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} 271 272#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 273 template <class _Container> 274 requires __span_compatible_range<_Container, element_type> 275 _LIBCPP_INLINE_VISIBILITY 276 constexpr explicit span(_Container& __c) : __data{std::data(__c)} { 277 _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)"); 278 } 279 template <class _Container> 280 requires __span_compatible_range<const _Container, element_type> 281 _LIBCPP_INLINE_VISIBILITY 282 constexpr explicit span(const _Container& __c) : __data{std::data(__c)} { 283 _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)"); 284 } 285#else 286 template <__span_compatible_range<element_type> _Range> 287 _LIBCPP_INLINE_VISIBILITY 288 constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} { 289 _LIBCPP_ASSERT(ranges::size(__r) == _Extent, "size mismatch in span's constructor (range)"); 290 } 291#endif 292 293 template <__span_array_convertible<element_type> _OtherElementType> 294 _LIBCPP_INLINE_VISIBILITY 295 constexpr span(const span<_OtherElementType, _Extent>& __other) 296 : __data{__other.data()} {} 297 298 template <__span_array_convertible<element_type> _OtherElementType> 299 _LIBCPP_INLINE_VISIBILITY 300 constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept 301 : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } 302 303 304// ~span() noexcept = default; 305 306 template <size_t _Count> 307 _LIBCPP_INLINE_VISIBILITY 308 constexpr span<element_type, _Count> first() const noexcept 309 { 310 static_assert(_Count <= _Extent, "Count out of range in span::first()"); 311 return span<element_type, _Count>{data(), _Count}; 312 } 313 314 template <size_t _Count> 315 _LIBCPP_INLINE_VISIBILITY 316 constexpr span<element_type, _Count> last() const noexcept 317 { 318 static_assert(_Count <= _Extent, "Count out of range in span::last()"); 319 return span<element_type, _Count>{data() + size() - _Count, _Count}; 320 } 321 322 _LIBCPP_INLINE_VISIBILITY 323 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept 324 { 325 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); 326 return {data(), __count}; 327 } 328 329 _LIBCPP_INLINE_VISIBILITY 330 constexpr span<element_type, dynamic_extent> last(size_type __count) const noexcept 331 { 332 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); 333 return {data() + size() - __count, __count}; 334 } 335 336 template <size_t _Offset, size_t _Count = dynamic_extent> 337 _LIBCPP_INLINE_VISIBILITY 338 constexpr auto subspan() const noexcept 339 -> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset> 340 { 341 static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); 342 static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()"); 343 344 using _ReturnType = span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>; 345 return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 346 } 347 348 349 _LIBCPP_INLINE_VISIBILITY 350 constexpr span<element_type, dynamic_extent> 351 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept 352 { 353 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); 354 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); 355 if (__count == dynamic_extent) 356 return {data() + __offset, size() - __offset}; 357 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); 358 return {data() + __offset, __count}; 359 } 360 361 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return _Extent; } 362 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } 363 [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } 364 365 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept 366 { 367 _LIBCPP_ASSERT(__idx < size(), "span<T,N>[] index out of bounds"); 368 return __data[__idx]; 369 } 370 371 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept 372 { 373 _LIBCPP_ASSERT(!empty(), "span<T, N>::front() on empty span"); 374 return __data[0]; 375 } 376 377 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept 378 { 379 _LIBCPP_ASSERT(!empty(), "span<T, N>::back() on empty span"); 380 return __data[size()-1]; 381 } 382 383 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } 384 385// [span.iter], span iterator support 386 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { 387#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 388 return iterator(data()); 389#else 390 return iterator(this, data()); 391#endif 392 } 393 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { 394#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 395 return iterator(data() + size()); 396#else 397 return iterator(this, data() + size()); 398#endif 399 } 400 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 401 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 402 403 _LIBCPP_INLINE_VISIBILITY span<const byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept 404 { return span<const byte, _Extent * sizeof(element_type)>{reinterpret_cast<const byte *>(data()), size_bytes()}; } 405 406 _LIBCPP_INLINE_VISIBILITY span<byte, _Extent * sizeof(element_type)> __as_writable_bytes() const noexcept 407 { return span<byte, _Extent * sizeof(element_type)>{reinterpret_cast<byte *>(data()), size_bytes()}; } 408 409private: 410 pointer __data; 411}; 412 413 414template <typename _Tp> 415class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { 416public: 417// constants and types 418 using element_type = _Tp; 419 using value_type = remove_cv_t<_Tp>; 420 using size_type = size_t; 421 using difference_type = ptrdiff_t; 422 using pointer = _Tp *; 423 using const_pointer = const _Tp *; 424 using reference = _Tp &; 425 using const_reference = const _Tp &; 426#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 427 using iterator = pointer; 428#else 429 using iterator = __wrap_iter<pointer>; 430#endif 431 using reverse_iterator = _VSTD::reverse_iterator<iterator>; 432 433 static constexpr size_type extent = dynamic_extent; 434 435// [span.cons], span constructors, copy, assignment, and destructor 436 _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} 437 438 constexpr span (const span&) noexcept = default; 439 constexpr span& operator=(const span&) noexcept = default; 440 441 template <__span_compatible_iterator<element_type> _It> 442 _LIBCPP_INLINE_VISIBILITY 443 constexpr span(_It __first, size_type __count) 444 : __data{_VSTD::to_address(__first)}, __size{__count} {} 445 446 template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End> 447 _LIBCPP_INLINE_VISIBILITY 448 constexpr span(_It __first, _End __last) 449 : __data(_VSTD::to_address(__first)), __size(__last - __first) {} 450 451 template <size_t _Sz> 452 _LIBCPP_INLINE_VISIBILITY 453 constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} 454 455 template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz> 456 _LIBCPP_INLINE_VISIBILITY 457 constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} 458 459 template <class _OtherElementType, size_t _Sz> 460 requires __span_array_convertible<const _OtherElementType, element_type> 461 _LIBCPP_INLINE_VISIBILITY 462 constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} 463 464#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 465 template <class _Container> 466 requires __span_compatible_range<_Container, element_type> 467 _LIBCPP_INLINE_VISIBILITY 468 constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {} 469 template <class _Container> 470 requires __span_compatible_range<const _Container, element_type> 471 _LIBCPP_INLINE_VISIBILITY 472 constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {} 473#else 474 template <__span_compatible_range<element_type> _Range> 475 _LIBCPP_INLINE_VISIBILITY 476 constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {} 477#endif 478 479 template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent> 480 _LIBCPP_INLINE_VISIBILITY 481 constexpr span(const span<_OtherElementType, _OtherExtent>& __other) noexcept 482 : __data{__other.data()}, __size{__other.size()} {} 483 484// ~span() noexcept = default; 485 486 template <size_t _Count> 487 _LIBCPP_INLINE_VISIBILITY 488 constexpr span<element_type, _Count> first() const noexcept 489 { 490 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); 491 return span<element_type, _Count>{data(), _Count}; 492 } 493 494 template <size_t _Count> 495 _LIBCPP_INLINE_VISIBILITY 496 constexpr span<element_type, _Count> last() const noexcept 497 { 498 _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); 499 return span<element_type, _Count>{data() + size() - _Count, _Count}; 500 } 501 502 _LIBCPP_INLINE_VISIBILITY 503 constexpr span<element_type, dynamic_extent> first(size_type __count) const noexcept 504 { 505 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); 506 return {data(), __count}; 507 } 508 509 _LIBCPP_INLINE_VISIBILITY 510 constexpr span<element_type, dynamic_extent> last (size_type __count) const noexcept 511 { 512 _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); 513 return {data() + size() - __count, __count}; 514 } 515 516 template <size_t _Offset, size_t _Count = dynamic_extent> 517 _LIBCPP_INLINE_VISIBILITY 518 constexpr span<element_type, _Count> subspan() const noexcept 519 { 520 _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); 521 _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()"); 522 return span<element_type, _Count>{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; 523 } 524 525 constexpr span<element_type, dynamic_extent> 526 _LIBCPP_INLINE_VISIBILITY 527 subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept 528 { 529 _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); 530 _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); 531 if (__count == dynamic_extent) 532 return {data() + __offset, size() - __offset}; 533 _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); 534 return {data() + __offset, __count}; 535 } 536 537 _LIBCPP_INLINE_VISIBILITY constexpr size_type size() const noexcept { return __size; } 538 _LIBCPP_INLINE_VISIBILITY constexpr size_type size_bytes() const noexcept { return __size * sizeof(element_type); } 539 [[nodiscard]] _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } 540 541 _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept 542 { 543 _LIBCPP_ASSERT(__idx < size(), "span<T>[] index out of bounds"); 544 return __data[__idx]; 545 } 546 547 _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept 548 { 549 _LIBCPP_ASSERT(!empty(), "span<T>[].front() on empty span"); 550 return __data[0]; 551 } 552 553 _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept 554 { 555 _LIBCPP_ASSERT(!empty(), "span<T>[].back() on empty span"); 556 return __data[size()-1]; 557 } 558 559 560 _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } 561 562// [span.iter], span iterator support 563 _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { 564#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 565 return iterator(data()); 566#else 567 return iterator(this, data()); 568#endif 569 } 570 _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { 571#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR 572 return iterator(data() + size()); 573#else 574 return iterator(this, data() + size()); 575#endif 576 } 577 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } 578 _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } 579 580 _LIBCPP_INLINE_VISIBILITY span<const byte, dynamic_extent> __as_bytes() const noexcept 581 { return {reinterpret_cast<const byte *>(data()), size_bytes()}; } 582 583 _LIBCPP_INLINE_VISIBILITY span<byte, dynamic_extent> __as_writable_bytes() const noexcept 584 { return {reinterpret_cast<byte *>(data()), size_bytes()}; } 585 586private: 587 pointer __data; 588 size_type __size; 589}; 590 591template <class _Tp, size_t _Extent> 592inline constexpr bool ranges::enable_borrowed_range<span<_Tp, _Extent> > = true; 593 594template <class _ElementType, size_t _Extent> 595inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true; 596 597// as_bytes & as_writable_bytes 598template <class _Tp, size_t _Extent> 599_LIBCPP_INLINE_VISIBILITY 600auto as_bytes(span<_Tp, _Extent> __s) noexcept 601{ return __s.__as_bytes(); } 602 603template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>) 604_LIBCPP_INLINE_VISIBILITY 605auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept 606{ return __s.__as_writable_bytes(); } 607 608#if _LIBCPP_STD_VER > 17 609template<contiguous_iterator _It, class _EndOrSize> 610 span(_It, _EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>; 611#endif // _LIBCPP_STD_VER > 17 612 613template<class _Tp, size_t _Sz> 614 span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; 615 616template<class _Tp, size_t _Sz> 617 span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; 618 619template<class _Tp, size_t _Sz> 620 span(const array<_Tp, _Sz>&) -> span<const _Tp, _Sz>; 621 622#if defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 623template<class _Container> 624 span(_Container&) -> span<typename _Container::value_type>; 625 626template<class _Container> 627 span(const _Container&) -> span<const typename _Container::value_type>; 628#else 629template<ranges::contiguous_range _Range> 630 span(_Range&&) -> span<remove_reference_t<ranges::range_reference_t<_Range>>>; 631#endif 632 633#endif // _LIBCPP_STD_VER > 17 634 635_LIBCPP_END_NAMESPACE_STD 636 637_LIBCPP_POP_MACROS 638 639#endif // _LIBCPP_SPAN 640