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