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