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