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> class 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 <__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) {return __elems_[__n];} 194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 195 const_reference operator[](size_type __n) const {return __elems_[__n];} 196 197 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n); 198 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const; 199 200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() {return __elems_[0];} 201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];} 202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() {return __elems_[_Size - 1];} 203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size - 1];} 204 205 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 206 value_type* data() _NOEXCEPT {return __elems_;} 207 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 208 const value_type* data() const _NOEXCEPT {return __elems_;} 209}; 210 211 212template <class _Tp, size_t _Size> 213_LIBCPP_CONSTEXPR_AFTER_CXX14 214typename array<_Tp, _Size>::reference 215array<_Tp, _Size>::at(size_type __n) 216{ 217 if (__n >= _Size) 218 __throw_out_of_range("array::at"); 219 220 return __elems_[__n]; 221} 222 223template <class _Tp, size_t _Size> 224_LIBCPP_CONSTEXPR_AFTER_CXX11 225typename array<_Tp, _Size>::const_reference 226array<_Tp, _Size>::at(size_type __n) const 227{ 228 if (__n >= _Size) 229 __throw_out_of_range("array::at"); 230 return __elems_[__n]; 231} 232 233template <class _Tp> 234struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0> 235{ 236 // types: 237 typedef array __self; 238 typedef _Tp value_type; 239 typedef value_type& reference; 240 typedef const value_type& const_reference; 241 typedef value_type* iterator; 242 typedef const value_type* const_iterator; 243 typedef value_type* pointer; 244 typedef const value_type* const_pointer; 245 typedef size_t size_type; 246 typedef ptrdiff_t difference_type; 247 typedef std::reverse_iterator<iterator> reverse_iterator; 248 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 249 250 typedef typename conditional<is_const<_Tp>::value, const char, 251 char>::type _CharType; 252 253 struct _ArrayInStructT { _Tp __data_[1]; }; 254 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)]; 255 256 // No explicit construct/copy/destroy for aggregate type 257 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) { 258 static_assert(!is_const<_Tp>::value, 259 "cannot fill zero-sized array of type 'const T'"); 260 } 261 262 _LIBCPP_INLINE_VISIBILITY 263 void swap(array&) _NOEXCEPT { 264 static_assert(!is_const<_Tp>::value, 265 "cannot swap zero-sized array of type 'const T'"); 266 } 267 268 // iterators: 269 _LIBCPP_INLINE_VISIBILITY 270 iterator begin() _NOEXCEPT {return iterator(data());} 271 _LIBCPP_INLINE_VISIBILITY 272 const_iterator begin() const _NOEXCEPT {return const_iterator(data());} 273 _LIBCPP_INLINE_VISIBILITY 274 iterator end() _NOEXCEPT {return iterator(data());} 275 _LIBCPP_INLINE_VISIBILITY 276 const_iterator end() const _NOEXCEPT {return const_iterator(data());} 277 278 _LIBCPP_INLINE_VISIBILITY 279 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} 280 _LIBCPP_INLINE_VISIBILITY 281 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} 282 _LIBCPP_INLINE_VISIBILITY 283 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} 284 _LIBCPP_INLINE_VISIBILITY 285 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} 286 287 _LIBCPP_INLINE_VISIBILITY 288 const_iterator cbegin() const _NOEXCEPT {return begin();} 289 _LIBCPP_INLINE_VISIBILITY 290 const_iterator cend() const _NOEXCEPT {return end();} 291 _LIBCPP_INLINE_VISIBILITY 292 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} 293 _LIBCPP_INLINE_VISIBILITY 294 const_reverse_iterator crend() const _NOEXCEPT {return rend();} 295 296 // capacity: 297 _LIBCPP_INLINE_VISIBILITY 298 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; } 299 _LIBCPP_INLINE_VISIBILITY 300 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;} 301 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 302 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;} 303 304 // element access: 305 _LIBCPP_INLINE_VISIBILITY 306 reference operator[](size_type) { 307 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 308 _LIBCPP_UNREACHABLE(); 309 } 310 311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 312 const_reference operator[](size_type) const { 313 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 314 _LIBCPP_UNREACHABLE(); 315 } 316 317 _LIBCPP_INLINE_VISIBILITY 318 reference at(size_type) { 319 __throw_out_of_range("array<T, 0>::at"); 320 _LIBCPP_UNREACHABLE(); 321 } 322 323 _LIBCPP_INLINE_VISIBILITY 324 const_reference at(size_type) const { 325 __throw_out_of_range("array<T, 0>::at"); 326 _LIBCPP_UNREACHABLE(); 327 } 328 329 _LIBCPP_INLINE_VISIBILITY 330 reference front() { 331 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 332 _LIBCPP_UNREACHABLE(); 333 } 334 335 _LIBCPP_INLINE_VISIBILITY 336 const_reference front() const { 337 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 338 _LIBCPP_UNREACHABLE(); 339 } 340 341 _LIBCPP_INLINE_VISIBILITY 342 reference back() { 343 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 344 _LIBCPP_UNREACHABLE(); 345 } 346 347 _LIBCPP_INLINE_VISIBILITY 348 const_reference back() const { 349 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 350 _LIBCPP_UNREACHABLE(); 351 } 352 353 _LIBCPP_INLINE_VISIBILITY 354 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);} 355 _LIBCPP_INLINE_VISIBILITY 356 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);} 357}; 358 359 360#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES 361template<class _Tp, class... _Args, 362 class = typename enable_if<(is_same_v<_Tp, _Args> && ...), void>::type 363 > 364array(_Tp, _Args...) 365 -> array<_Tp, 1 + sizeof...(_Args)>; 366#endif 367 368template <class _Tp, size_t _Size> 369inline _LIBCPP_INLINE_VISIBILITY 370bool 371operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 372{ 373 return _VSTD::equal(__x.begin(), __x.end(), __y.begin()); 374} 375 376template <class _Tp, size_t _Size> 377inline _LIBCPP_INLINE_VISIBILITY 378bool 379operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 380{ 381 return !(__x == __y); 382} 383 384template <class _Tp, size_t _Size> 385inline _LIBCPP_INLINE_VISIBILITY 386bool 387operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 388{ 389 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), 390 __y.begin(), __y.end()); 391} 392 393template <class _Tp, size_t _Size> 394inline _LIBCPP_INLINE_VISIBILITY 395bool 396operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 397{ 398 return __y < __x; 399} 400 401template <class _Tp, size_t _Size> 402inline _LIBCPP_INLINE_VISIBILITY 403bool 404operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 405{ 406 return !(__y < __x); 407} 408 409template <class _Tp, size_t _Size> 410inline _LIBCPP_INLINE_VISIBILITY 411bool 412operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 413{ 414 return !(__x < __y); 415} 416 417template <class _Tp, size_t _Size> 418inline _LIBCPP_INLINE_VISIBILITY 419typename enable_if 420< 421 _Size == 0 || 422 __is_swappable<_Tp>::value, 423 void 424>::type 425swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) 426 _NOEXCEPT_(noexcept(__x.swap(__y))) 427{ 428 __x.swap(__y); 429} 430 431template <class _Tp, size_t _Size> 432class _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > 433 : public integral_constant<size_t, _Size> {}; 434 435template <size_t _Ip, class _Tp, size_t _Size> 436class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > 437{ 438 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)"); 439public: 440 typedef _Tp type; 441}; 442 443template <size_t _Ip, class _Tp, size_t _Size> 444inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 445_Tp& 446get(array<_Tp, _Size>& __a) _NOEXCEPT 447{ 448 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); 449 return __a.__elems_[_Ip]; 450} 451 452template <size_t _Ip, class _Tp, size_t _Size> 453inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 454const _Tp& 455get(const array<_Tp, _Size>& __a) _NOEXCEPT 456{ 457 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); 458 return __a.__elems_[_Ip]; 459} 460 461#ifndef _LIBCPP_CXX03_LANG 462 463template <size_t _Ip, class _Tp, size_t _Size> 464inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 465_Tp&& 466get(array<_Tp, _Size>&& __a) _NOEXCEPT 467{ 468 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); 469 return _VSTD::move(__a.__elems_[_Ip]); 470} 471 472template <size_t _Ip, class _Tp, size_t _Size> 473inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 474const _Tp&& 475get(const array<_Tp, _Size>&& __a) _NOEXCEPT 476{ 477 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)"); 478 return _VSTD::move(__a.__elems_[_Ip]); 479} 480 481#endif // !_LIBCPP_CXX03_LANG 482 483_LIBCPP_END_NAMESPACE_STD 484 485#endif // _LIBCPP_ARRAY 486