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 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const; 199 200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return __elems_[0];} 201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];} 202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return __elems_[_Size - 1];} 203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {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) _NOEXCEPT { 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 _NOEXCEPT { 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() _NOEXCEPT { 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 _NOEXCEPT { 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() _NOEXCEPT { 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 _NOEXCEPT { 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 370_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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 378_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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 386_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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 395_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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 403_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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 411_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 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> 432struct _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> 436struct _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)"); 439 typedef _Tp type; 440}; 441 442template <size_t _Ip, class _Tp, size_t _Size> 443inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 444_Tp& 445get(array<_Tp, _Size>& __a) _NOEXCEPT 446{ 447 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); 448 return __a.__elems_[_Ip]; 449} 450 451template <size_t _Ip, class _Tp, size_t _Size> 452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 453const _Tp& 454get(const array<_Tp, _Size>& __a) _NOEXCEPT 455{ 456 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); 457 return __a.__elems_[_Ip]; 458} 459 460#ifndef _LIBCPP_CXX03_LANG 461 462template <size_t _Ip, class _Tp, size_t _Size> 463inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 464_Tp&& 465get(array<_Tp, _Size>&& __a) _NOEXCEPT 466{ 467 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); 468 return _VSTD::move(__a.__elems_[_Ip]); 469} 470 471template <size_t _Ip, class _Tp, size_t _Size> 472inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 473const _Tp&& 474get(const array<_Tp, _Size>&& __a) _NOEXCEPT 475{ 476 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)"); 477 return _VSTD::move(__a.__elems_[_Ip]); 478} 479 480#endif // !_LIBCPP_CXX03_LANG 481 482_LIBCPP_END_NAMESPACE_STD 483 484#endif // _LIBCPP_ARRAY 485