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