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