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