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