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