1// -*- C++ -*- 2//===---------------------------- array -----------------------------------===// 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_ARRAY 11#define _LIBCPP_ARRAY 12 13/* 14 array synopsis 15 16namespace std 17{ 18template <class T, size_t N > 19struct array 20{ 21 // types: 22 typedef T & reference; 23 typedef const T & const_reference; 24 typedef implementation defined iterator; 25 typedef implementation defined const_iterator; 26 typedef size_t size_type; 27 typedef ptrdiff_t difference_type; 28 typedef T value_type; 29 typedef T* pointer; 30 typedef const T* const_pointer; 31 typedef std::reverse_iterator<iterator> reverse_iterator; 32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 33 34 // No explicit construct/copy/destroy for aggregate type 35 void fill(const T& u); 36 void swap(array& a) noexcept(is_nothrow_swappable_v<T>); 37 38 // iterators: 39 iterator begin() noexcept; 40 const_iterator begin() const noexcept; 41 iterator end() noexcept; 42 const_iterator end() const noexcept; 43 44 reverse_iterator rbegin() noexcept; 45 const_reverse_iterator rbegin() const noexcept; 46 reverse_iterator rend() noexcept; 47 const_reverse_iterator rend() const noexcept; 48 49 const_iterator cbegin() const noexcept; 50 const_iterator cend() const noexcept; 51 const_reverse_iterator crbegin() const noexcept; 52 const_reverse_iterator crend() const noexcept; 53 54 // capacity: 55 constexpr size_type size() const noexcept; 56 constexpr size_type max_size() const noexcept; 57 constexpr bool empty() const noexcept; 58 59 // element access: 60 reference operator[](size_type n); 61 const_reference operator[](size_type n) const; // constexpr in C++14 62 const_reference at(size_type n) const; // constexpr in C++14 63 reference at(size_type n); 64 65 reference front(); 66 const_reference front() const; // constexpr in C++14 67 reference back(); 68 const_reference back() const; // constexpr in C++14 69 70 T* data() noexcept; 71 const T* data() const noexcept; 72}; 73 74 template <class T, class... U> 75 array(T, U...) -> array<T, 1 + sizeof...(U)>; 76 77template <class T, size_t N> 78 bool operator==(const array<T,N>& x, const array<T,N>& y); 79template <class T, size_t N> 80 bool operator!=(const array<T,N>& x, const array<T,N>& y); 81template <class T, size_t N> 82 bool operator<(const array<T,N>& x, const array<T,N>& y); 83template <class T, size_t N> 84 bool operator>(const array<T,N>& x, const array<T,N>& y); 85template <class T, size_t N> 86 bool operator<=(const array<T,N>& x, const array<T,N>& y); 87template <class T, size_t N> 88 bool operator>=(const array<T,N>& x, const array<T,N>& y); 89 90template <class T, size_t N > 91 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17 92 93template <class T> struct tuple_size; 94template <size_t I, class T> struct tuple_element; 95template <class T, size_t N> struct tuple_size<array<T, N>>; 96template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>; 97template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14 98template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14 99template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14 100template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14 101 102} // std 103 104*/ 105 106#include <__config> 107#include <__tuple> 108#include <type_traits> 109#include <utility> 110#include <iterator> 111#include <algorithm> 112#include <stdexcept> 113#include <cstdlib> // for _LIBCPP_UNREACHABLE 114#include <version> 115#include <__debug> 116 117#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 118#pragma GCC system_header 119#endif 120 121 122 123_LIBCPP_BEGIN_NAMESPACE_STD 124 125 126template <class _Tp, size_t _Size> 127struct _LIBCPP_TEMPLATE_VIS array 128{ 129 // types: 130 typedef array __self; 131 typedef _Tp value_type; 132 typedef value_type& reference; 133 typedef const value_type& const_reference; 134 typedef value_type* iterator; 135 typedef const value_type* const_iterator; 136 typedef value_type* pointer; 137 typedef const value_type* const_pointer; 138 typedef size_t size_type; 139 typedef ptrdiff_t difference_type; 140 typedef std::reverse_iterator<iterator> reverse_iterator; 141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 142 143 _Tp __elems_[_Size]; 144 145 // No explicit construct/copy/destroy for aggregate type 146 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) { 147 _VSTD::fill_n(__elems_, _Size, __u); 148 } 149 150 _LIBCPP_INLINE_VISIBILITY 151 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) { 152 std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_); 153 } 154 155 // iterators: 156 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 157 iterator begin() _NOEXCEPT {return iterator(data());} 158 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 159 const_iterator begin() const _NOEXCEPT {return const_iterator(data());} 160 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 161 iterator end() _NOEXCEPT {return iterator(data() + _Size);} 162 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 163 const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);} 164 165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 166 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} 167 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 168 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} 169 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 170 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} 171 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 172 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} 173 174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 175 const_iterator cbegin() const _NOEXCEPT {return begin();} 176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 177 const_iterator cend() const _NOEXCEPT {return end();} 178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 179 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} 180 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 181 const_reverse_iterator crend() const _NOEXCEPT {return rend();} 182 183 // capacity: 184 _LIBCPP_INLINE_VISIBILITY 185 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;} 186 _LIBCPP_INLINE_VISIBILITY 187 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;} 188 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 189 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; } 190 191 // element access: 192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 193 reference operator[](size_type __n) _NOEXCEPT {return __elems_[__n];} 194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 195 const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];} 196 197 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n) 198 { 199 if (__n >= _Size) 200 __throw_out_of_range("array::at"); 201 return __elems_[__n]; 202 } 203 204 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const 205 { 206 if (__n >= _Size) 207 __throw_out_of_range("array::at"); 208 return __elems_[__n]; 209 } 210 211 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return __elems_[0];} 212 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];} 213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return __elems_[_Size - 1];} 214 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return __elems_[_Size - 1];} 215 216 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 217 value_type* data() _NOEXCEPT {return __elems_;} 218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 219 const value_type* data() const _NOEXCEPT {return __elems_;} 220}; 221 222template <class _Tp> 223struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0> 224{ 225 // types: 226 typedef array __self; 227 typedef _Tp value_type; 228 typedef value_type& reference; 229 typedef const value_type& const_reference; 230 typedef value_type* iterator; 231 typedef const value_type* const_iterator; 232 typedef value_type* pointer; 233 typedef const value_type* const_pointer; 234 typedef size_t size_type; 235 typedef ptrdiff_t difference_type; 236 typedef std::reverse_iterator<iterator> reverse_iterator; 237 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 238 239 typedef typename conditional<is_const<_Tp>::value, const char, 240 char>::type _CharType; 241 242 struct _ArrayInStructT { _Tp __data_[1]; }; 243 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)]; 244 245 // No explicit construct/copy/destroy for aggregate type 246 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) { 247 static_assert(!is_const<_Tp>::value, 248 "cannot fill zero-sized array of type 'const T'"); 249 } 250 251 _LIBCPP_INLINE_VISIBILITY 252 void swap(array&) _NOEXCEPT { 253 static_assert(!is_const<_Tp>::value, 254 "cannot swap zero-sized array of type 'const T'"); 255 } 256 257 // iterators: 258 _LIBCPP_INLINE_VISIBILITY 259 iterator begin() _NOEXCEPT {return iterator(data());} 260 _LIBCPP_INLINE_VISIBILITY 261 const_iterator begin() const _NOEXCEPT {return const_iterator(data());} 262 _LIBCPP_INLINE_VISIBILITY 263 iterator end() _NOEXCEPT {return iterator(data());} 264 _LIBCPP_INLINE_VISIBILITY 265 const_iterator end() const _NOEXCEPT {return const_iterator(data());} 266 267 _LIBCPP_INLINE_VISIBILITY 268 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} 269 _LIBCPP_INLINE_VISIBILITY 270 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} 271 _LIBCPP_INLINE_VISIBILITY 272 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} 273 _LIBCPP_INLINE_VISIBILITY 274 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} 275 276 _LIBCPP_INLINE_VISIBILITY 277 const_iterator cbegin() const _NOEXCEPT {return begin();} 278 _LIBCPP_INLINE_VISIBILITY 279 const_iterator cend() const _NOEXCEPT {return end();} 280 _LIBCPP_INLINE_VISIBILITY 281 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} 282 _LIBCPP_INLINE_VISIBILITY 283 const_reverse_iterator crend() const _NOEXCEPT {return rend();} 284 285 // capacity: 286 _LIBCPP_INLINE_VISIBILITY 287 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; } 288 _LIBCPP_INLINE_VISIBILITY 289 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;} 290 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 291 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;} 292 293 // element access: 294 _LIBCPP_INLINE_VISIBILITY 295 reference operator[](size_type) _NOEXCEPT { 296 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 297 _LIBCPP_UNREACHABLE(); 298 } 299 300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 301 const_reference operator[](size_type) const _NOEXCEPT { 302 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 303 _LIBCPP_UNREACHABLE(); 304 } 305 306 _LIBCPP_INLINE_VISIBILITY 307 reference at(size_type) { 308 __throw_out_of_range("array<T, 0>::at"); 309 _LIBCPP_UNREACHABLE(); 310 } 311 312 _LIBCPP_INLINE_VISIBILITY 313 const_reference at(size_type) const { 314 __throw_out_of_range("array<T, 0>::at"); 315 _LIBCPP_UNREACHABLE(); 316 } 317 318 _LIBCPP_INLINE_VISIBILITY 319 reference front() _NOEXCEPT { 320 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 321 _LIBCPP_UNREACHABLE(); 322 } 323 324 _LIBCPP_INLINE_VISIBILITY 325 const_reference front() const _NOEXCEPT { 326 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 327 _LIBCPP_UNREACHABLE(); 328 } 329 330 _LIBCPP_INLINE_VISIBILITY 331 reference back() _NOEXCEPT { 332 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 333 _LIBCPP_UNREACHABLE(); 334 } 335 336 _LIBCPP_INLINE_VISIBILITY 337 const_reference back() const _NOEXCEPT { 338 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 339 _LIBCPP_UNREACHABLE(); 340 } 341 342 _LIBCPP_INLINE_VISIBILITY 343 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);} 344 _LIBCPP_INLINE_VISIBILITY 345 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);} 346}; 347 348 349#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES 350template<class _Tp, class... _Args, 351 class = _EnableIf<__all<_IsSame<_Tp, _Args>::value...>::value> 352 > 353array(_Tp, _Args...) 354 -> array<_Tp, 1 + sizeof...(_Args)>; 355#endif 356 357template <class _Tp, size_t _Size> 358inline _LIBCPP_INLINE_VISIBILITY 359_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 360operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 361{ 362 return _VSTD::equal(__x.begin(), __x.end(), __y.begin()); 363} 364 365template <class _Tp, size_t _Size> 366inline _LIBCPP_INLINE_VISIBILITY 367_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 368operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 369{ 370 return !(__x == __y); 371} 372 373template <class _Tp, size_t _Size> 374inline _LIBCPP_INLINE_VISIBILITY 375_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 376operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 377{ 378 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), 379 __y.begin(), __y.end()); 380} 381 382template <class _Tp, size_t _Size> 383inline _LIBCPP_INLINE_VISIBILITY 384_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 385operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 386{ 387 return __y < __x; 388} 389 390template <class _Tp, size_t _Size> 391inline _LIBCPP_INLINE_VISIBILITY 392_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 393operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 394{ 395 return !(__y < __x); 396} 397 398template <class _Tp, size_t _Size> 399inline _LIBCPP_INLINE_VISIBILITY 400_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 401operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 402{ 403 return !(__x < __y); 404} 405 406template <class _Tp, size_t _Size> 407inline _LIBCPP_INLINE_VISIBILITY 408typename enable_if 409< 410 _Size == 0 || 411 __is_swappable<_Tp>::value, 412 void 413>::type 414swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) 415 _NOEXCEPT_(noexcept(__x.swap(__y))) 416{ 417 __x.swap(__y); 418} 419 420template <class _Tp, size_t _Size> 421struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > 422 : public integral_constant<size_t, _Size> {}; 423 424template <size_t _Ip, class _Tp, size_t _Size> 425struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > 426{ 427 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)"); 428 typedef _Tp type; 429}; 430 431template <size_t _Ip, class _Tp, size_t _Size> 432inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 433_Tp& 434get(array<_Tp, _Size>& __a) _NOEXCEPT 435{ 436 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); 437 return __a.__elems_[_Ip]; 438} 439 440template <size_t _Ip, class _Tp, size_t _Size> 441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 442const _Tp& 443get(const array<_Tp, _Size>& __a) _NOEXCEPT 444{ 445 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); 446 return __a.__elems_[_Ip]; 447} 448 449#ifndef _LIBCPP_CXX03_LANG 450 451template <size_t _Ip, class _Tp, size_t _Size> 452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 453_Tp&& 454get(array<_Tp, _Size>&& __a) _NOEXCEPT 455{ 456 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); 457 return _VSTD::move(__a.__elems_[_Ip]); 458} 459 460template <size_t _Ip, class _Tp, size_t _Size> 461inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 462const _Tp&& 463get(const array<_Tp, _Size>&& __a) _NOEXCEPT 464{ 465 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)"); 466 return _VSTD::move(__a.__elems_[_Ip]); 467} 468 469#endif // !_LIBCPP_CXX03_LANG 470 471#if _LIBCPP_STD_VER > 17 472 473template <typename _Tp, size_t _Size, size_t... _Index> 474_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> 475__to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) { 476 return {{__arr[_Index]...}}; 477} 478 479template <typename _Tp, size_t _Size, size_t... _Index> 480_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> 481__to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) { 482 return {{_VSTD::move(__arr[_Index])...}}; 483} 484 485template <typename _Tp, size_t _Size> 486_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> 487to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) { 488 static_assert( 489 !is_array_v<_Tp>, 490 "[array.creation]/1: to_array does not accept multidimensional arrays."); 491 static_assert( 492 is_constructible_v<_Tp, _Tp&>, 493 "[array.creation]/1: to_array requires copy constructible elements."); 494 return __to_array_lvalue_impl(__arr, make_index_sequence<_Size>()); 495} 496 497template <typename _Tp, size_t _Size> 498_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size> 499to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) { 500 static_assert( 501 !is_array_v<_Tp>, 502 "[array.creation]/4: to_array does not accept multidimensional arrays."); 503 static_assert( 504 is_move_constructible_v<_Tp>, 505 "[array.creation]/4: to_array requires move constructible elements."); 506 return __to_array_rvalue_impl(_VSTD::move(__arr), 507 make_index_sequence<_Size>()); 508} 509 510#endif // _LIBCPP_STD_VER > 17 511 512_LIBCPP_END_NAMESPACE_STD 513 514#endif // _LIBCPP_ARRAY 515