array 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #ifndef _LIBCPP_ARRAY
  10. #define _LIBCPP_ARRAY
  11. /*
  12. array synopsis
  13. namespace std
  14. {
  15. template <class T, size_t N >
  16. struct array
  17. {
  18. // types:
  19. typedef T & reference;
  20. typedef const T & const_reference;
  21. typedef implementation defined iterator;
  22. typedef implementation defined const_iterator;
  23. typedef size_t size_type;
  24. typedef ptrdiff_t difference_type;
  25. typedef T value_type;
  26. typedef T* pointer;
  27. typedef const T* const_pointer;
  28. typedef std::reverse_iterator<iterator> reverse_iterator;
  29. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  30. // No explicit construct/copy/destroy for aggregate type
  31. void fill(const T& u); // constexpr in C++20
  32. void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20
  33. // iterators:
  34. iterator begin() noexcept; // constexpr in C++17
  35. const_iterator begin() const noexcept; // constexpr in C++17
  36. iterator end() noexcept; // constexpr in C++17
  37. const_iterator end() const noexcept; // constexpr in C++17
  38. reverse_iterator rbegin() noexcept; // constexpr in C++17
  39. const_reverse_iterator rbegin() const noexcept; // constexpr in C++17
  40. reverse_iterator rend() noexcept; // constexpr in C++17
  41. const_reverse_iterator rend() const noexcept; // constexpr in C++17
  42. const_iterator cbegin() const noexcept; // constexpr in C++17
  43. const_iterator cend() const noexcept; // constexpr in C++17
  44. const_reverse_iterator crbegin() const noexcept; // constexpr in C++17
  45. const_reverse_iterator crend() const noexcept; // constexpr in C++17
  46. // capacity:
  47. constexpr size_type size() const noexcept;
  48. constexpr size_type max_size() const noexcept;
  49. constexpr bool empty() const noexcept;
  50. // element access:
  51. reference operator[](size_type n); // constexpr in C++17
  52. const_reference operator[](size_type n) const; // constexpr in C++14
  53. reference at(size_type n); // constexpr in C++17
  54. const_reference at(size_type n) const; // constexpr in C++14
  55. reference front(); // constexpr in C++17
  56. const_reference front() const; // constexpr in C++14
  57. reference back(); // constexpr in C++17
  58. const_reference back() const; // constexpr in C++14
  59. T* data() noexcept; // constexpr in C++17
  60. const T* data() const noexcept; // constexpr in C++17
  61. };
  62. template <class T, class... U>
  63. array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17
  64. template <class T, size_t N>
  65. bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  66. template <class T, size_t N>
  67. bool operator!=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
  68. template <class T, size_t N>
  69. bool operator<(const array<T,N>& x, const array<T,N>& y); // removed in C++20
  70. template <class T, size_t N>
  71. bool operator>(const array<T,N>& x, const array<T,N>& y); // removed in C++20
  72. template <class T, size_t N>
  73. bool operator<=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
  74. template <class T, size_t N>
  75. bool operator>=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
  76. template<class T, size_t N>
  77. constexpr synth-three-way-result<T>
  78. operator<=>(const array<T, N>& x, const array<T, N>& y); // since C++20
  79. template <class T, size_t N >
  80. void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
  81. template <class T, size_t N>
  82. constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20
  83. template <class T, size_t N>
  84. constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20
  85. template <class T> struct tuple_size;
  86. template <size_t I, class T> struct tuple_element;
  87. template <class T, size_t N> struct tuple_size<array<T, N>>;
  88. template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
  89. template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
  90. template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
  91. template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
  92. template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
  93. } // std
  94. */
  95. #include <__algorithm/equal.h>
  96. #include <__algorithm/fill_n.h>
  97. #include <__algorithm/lexicographical_compare.h>
  98. #include <__algorithm/lexicographical_compare_three_way.h>
  99. #include <__algorithm/swap_ranges.h>
  100. #include <__assert>
  101. #include <__config>
  102. #include <__fwd/array.h>
  103. #include <__iterator/reverse_iterator.h>
  104. #include <__iterator/wrap_iter.h>
  105. #include <__tuple/sfinae_helpers.h>
  106. #include <__type_traits/conditional.h>
  107. #include <__type_traits/is_array.h>
  108. #include <__type_traits/is_const.h>
  109. #include <__type_traits/is_constructible.h>
  110. #include <__type_traits/is_move_constructible.h>
  111. #include <__type_traits/is_nothrow_constructible.h>
  112. #include <__type_traits/is_nothrow_move_constructible.h>
  113. #include <__type_traits/is_same.h>
  114. #include <__type_traits/is_swappable.h>
  115. #include <__type_traits/remove_cv.h>
  116. #include <__utility/empty.h>
  117. #include <__utility/integer_sequence.h>
  118. #include <__utility/move.h>
  119. #include <__utility/unreachable.h>
  120. #include <stdexcept>
  121. #include <version>
  122. // standard-mandated includes
  123. // [iterator.range]
  124. #include <__iterator/access.h>
  125. #include <__iterator/data.h>
  126. #include <__iterator/empty.h>
  127. #include <__iterator/reverse_access.h>
  128. #include <__iterator/size.h>
  129. // [array.syn]
  130. #include <compare>
  131. #include <initializer_list>
  132. // [tuple.helper]
  133. #include <__tuple/tuple_element.h>
  134. #include <__tuple/tuple_size.h>
  135. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  136. # pragma GCC system_header
  137. #endif
  138. _LIBCPP_PUSH_MACROS
  139. #include <__undef_macros>
  140. _LIBCPP_BEGIN_NAMESPACE_STD
  141. template <class _Tp, size_t _Size>
  142. struct _LIBCPP_TEMPLATE_VIS array {
  143. // types:
  144. using __self = array;
  145. using value_type = _Tp;
  146. using reference = value_type&;
  147. using const_reference = const value_type&;
  148. using pointer = value_type*;
  149. using const_pointer = const value_type*;
  150. #if defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY)
  151. using iterator = __wrap_iter<pointer>;
  152. using const_iterator = __wrap_iter<const_pointer>;
  153. #else
  154. using iterator = pointer;
  155. using const_iterator = const_pointer;
  156. #endif
  157. using size_type = size_t;
  158. using difference_type = ptrdiff_t;
  159. using reverse_iterator = std::reverse_iterator<iterator>;
  160. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  161. _Tp __elems_[_Size];
  162. // No explicit construct/copy/destroy for aggregate type
  163. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void fill(const value_type& __u) {
  164. std::fill_n(data(), _Size, __u);
  165. }
  166. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(array& __a)
  167. _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
  168. std::swap_ranges(data(), data() + _Size, __a.data());
  169. }
  170. // iterators:
  171. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 iterator begin() _NOEXCEPT { return iterator(data()); }
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator begin() const _NOEXCEPT {
  173. return const_iterator(data());
  174. }
  175. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 iterator end() _NOEXCEPT { return iterator(data() + _Size); }
  176. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator end() const _NOEXCEPT {
  177. return const_iterator(data() + _Size);
  178. }
  179. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator rbegin() _NOEXCEPT {
  180. return reverse_iterator(end());
  181. }
  182. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator rbegin() const _NOEXCEPT {
  183. return const_reverse_iterator(end());
  184. }
  185. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator rend() _NOEXCEPT {
  186. return reverse_iterator(begin());
  187. }
  188. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator rend() const _NOEXCEPT {
  189. return const_reverse_iterator(begin());
  190. }
  191. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator cbegin() const _NOEXCEPT { return begin(); }
  192. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator cend() const _NOEXCEPT { return end(); }
  193. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator crbegin() const _NOEXCEPT {
  194. return rbegin();
  195. }
  196. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
  197. // capacity:
  198. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT { return _Size; }
  199. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT { return _Size; }
  200. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {
  201. return _Size == 0;
  202. }
  203. // element access:
  204. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference operator[](size_type __n) _NOEXCEPT {
  205. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < _Size, "out-of-bounds access in std::array<T, N>");
  206. return __elems_[__n];
  207. }
  208. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference operator[](size_type __n) const _NOEXCEPT {
  209. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < _Size, "out-of-bounds access in std::array<T, N>");
  210. return __elems_[__n];
  211. }
  212. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference at(size_type __n) {
  213. if (__n >= _Size)
  214. __throw_out_of_range("array::at");
  215. return __elems_[__n];
  216. }
  217. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference at(size_type __n) const {
  218. if (__n >= _Size)
  219. __throw_out_of_range("array::at");
  220. return __elems_[__n];
  221. }
  222. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference front() _NOEXCEPT { return (*this)[0]; }
  223. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference front() const _NOEXCEPT { return (*this)[0]; }
  224. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference back() _NOEXCEPT { return (*this)[_Size - 1]; }
  225. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference back() const _NOEXCEPT {
  226. return (*this)[_Size - 1];
  227. }
  228. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 value_type* data() _NOEXCEPT { return __elems_; }
  229. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const value_type* data() const _NOEXCEPT { return __elems_; }
  230. };
  231. template <class _Tp>
  232. struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0> {
  233. // types:
  234. typedef array __self;
  235. typedef _Tp value_type;
  236. typedef value_type& reference;
  237. typedef const value_type& const_reference;
  238. typedef value_type* iterator;
  239. typedef const value_type* const_iterator;
  240. typedef value_type* pointer;
  241. typedef const value_type* const_pointer;
  242. typedef size_t size_type;
  243. typedef ptrdiff_t difference_type;
  244. typedef std::reverse_iterator<iterator> reverse_iterator;
  245. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  246. typedef __conditional_t<is_const<_Tp>::value, const __empty, __empty> _EmptyType;
  247. struct _ArrayInStructT {
  248. _Tp __data_[1];
  249. };
  250. _ALIGNAS_TYPE(_ArrayInStructT) _EmptyType __elems_[sizeof(_ArrayInStructT)];
  251. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 value_type* data() _NOEXCEPT { return nullptr; }
  252. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const value_type* data() const _NOEXCEPT { return nullptr; }
  253. // No explicit construct/copy/destroy for aggregate type
  254. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void fill(const value_type&) {
  255. static_assert(!is_const<_Tp>::value, "cannot fill zero-sized array of type 'const T'");
  256. }
  257. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(array&) _NOEXCEPT {
  258. static_assert(!is_const<_Tp>::value, "cannot swap zero-sized array of type 'const T'");
  259. }
  260. // iterators:
  261. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 iterator begin() _NOEXCEPT { return iterator(data()); }
  262. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator begin() const _NOEXCEPT {
  263. return const_iterator(data());
  264. }
  265. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 iterator end() _NOEXCEPT { return iterator(data()); }
  266. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator end() const _NOEXCEPT {
  267. return const_iterator(data());
  268. }
  269. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator rbegin() _NOEXCEPT {
  270. return reverse_iterator(end());
  271. }
  272. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator rbegin() const _NOEXCEPT {
  273. return const_reverse_iterator(end());
  274. }
  275. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator rend() _NOEXCEPT {
  276. return reverse_iterator(begin());
  277. }
  278. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator rend() const _NOEXCEPT {
  279. return const_reverse_iterator(begin());
  280. }
  281. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator cbegin() const _NOEXCEPT { return begin(); }
  282. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_iterator cend() const _NOEXCEPT { return end(); }
  283. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator crbegin() const _NOEXCEPT {
  284. return rbegin();
  285. }
  286. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
  287. // capacity:
  288. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT { return 0; }
  289. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT { return 0; }
  290. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT { return true; }
  291. // element access:
  292. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference operator[](size_type) _NOEXCEPT {
  293. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  294. __libcpp_unreachable();
  295. }
  296. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference operator[](size_type) const _NOEXCEPT {
  297. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  298. __libcpp_unreachable();
  299. }
  300. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference at(size_type) {
  301. __throw_out_of_range("array<T, 0>::at");
  302. __libcpp_unreachable();
  303. }
  304. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference at(size_type) const {
  305. __throw_out_of_range("array<T, 0>::at");
  306. __libcpp_unreachable();
  307. }
  308. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference front() _NOEXCEPT {
  309. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::front() on a zero-sized array");
  310. __libcpp_unreachable();
  311. }
  312. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference front() const _NOEXCEPT {
  313. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::front() on a zero-sized array");
  314. __libcpp_unreachable();
  315. }
  316. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reference back() _NOEXCEPT {
  317. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::back() on a zero-sized array");
  318. __libcpp_unreachable();
  319. }
  320. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const_reference back() const _NOEXCEPT {
  321. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::back() on a zero-sized array");
  322. __libcpp_unreachable();
  323. }
  324. };
  325. #if _LIBCPP_STD_VER >= 17
  326. template <class _Tp, class... _Args, class = enable_if_t<__all<_IsSame<_Tp, _Args>::value...>::value> >
  327. array(_Tp, _Args...) -> array<_Tp, 1 + sizeof...(_Args)>;
  328. #endif
  329. template <class _Tp, size_t _Size>
  330. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  331. operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  332. return std::equal(__x.begin(), __x.end(), __y.begin());
  333. }
  334. #if _LIBCPP_STD_VER <= 17
  335. template <class _Tp, size_t _Size>
  336. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  337. return !(__x == __y);
  338. }
  339. template <class _Tp, size_t _Size>
  340. inline _LIBCPP_HIDE_FROM_ABI bool operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  341. return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
  342. }
  343. template <class _Tp, size_t _Size>
  344. inline _LIBCPP_HIDE_FROM_ABI bool operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  345. return __y < __x;
  346. }
  347. template <class _Tp, size_t _Size>
  348. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  349. return !(__y < __x);
  350. }
  351. template <class _Tp, size_t _Size>
  352. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  353. return !(__x < __y);
  354. }
  355. #else // _LIBCPP_STD_VER <= 17
  356. template <class _Tp, size_t _Size>
  357. _LIBCPP_HIDE_FROM_ABI constexpr __synth_three_way_result<_Tp>
  358. operator<=>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
  359. return std::lexicographical_compare_three_way(
  360. __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>);
  361. }
  362. #endif // _LIBCPP_STD_VER <= 17
  363. template <class _Tp, size_t _Size, __enable_if_t<_Size == 0 || __is_swappable<_Tp>::value, int> = 0>
  364. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
  365. _NOEXCEPT_(noexcept(__x.swap(__y))) {
  366. __x.swap(__y);
  367. }
  368. template <class _Tp, size_t _Size>
  369. struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > : public integral_constant<size_t, _Size> {};
  370. template <size_t _Ip, class _Tp, size_t _Size>
  371. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > {
  372. static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
  373. typedef _Tp type;
  374. };
  375. template <size_t _Ip, class _Tp, size_t _Size>
  376. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp& get(array<_Tp, _Size>& __a) _NOEXCEPT {
  377. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
  378. return __a.__elems_[_Ip];
  379. }
  380. template <size_t _Ip, class _Tp, size_t _Size>
  381. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Tp& get(const array<_Tp, _Size>& __a) _NOEXCEPT {
  382. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
  383. return __a.__elems_[_Ip];
  384. }
  385. template <size_t _Ip, class _Tp, size_t _Size>
  386. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp&& get(array<_Tp, _Size>&& __a) _NOEXCEPT {
  387. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
  388. return std::move(__a.__elems_[_Ip]);
  389. }
  390. template <size_t _Ip, class _Tp, size_t _Size>
  391. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 const _Tp&& get(const array<_Tp, _Size>&& __a) _NOEXCEPT {
  392. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
  393. return std::move(__a.__elems_[_Ip]);
  394. }
  395. #if _LIBCPP_STD_VER >= 20
  396. template <typename _Tp, size_t _Size, size_t... _Index>
  397. _LIBCPP_HIDE_FROM_ABI constexpr array<remove_cv_t<_Tp>, _Size>
  398. __to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
  399. return {{__arr[_Index]...}};
  400. }
  401. template <typename _Tp, size_t _Size, size_t... _Index>
  402. _LIBCPP_HIDE_FROM_ABI constexpr array<remove_cv_t<_Tp>, _Size>
  403. __to_array_rvalue_impl(_Tp (&&__arr)[_Size], index_sequence<_Index...>) {
  404. return {{std::move(__arr[_Index])...}};
  405. }
  406. template <typename _Tp, size_t _Size>
  407. _LIBCPP_HIDE_FROM_ABI constexpr array<remove_cv_t<_Tp>, _Size>
  408. to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
  409. static_assert(!is_array_v<_Tp>, "[array.creation]/1: to_array does not accept multidimensional arrays.");
  410. static_assert(is_constructible_v<_Tp, _Tp&>, "[array.creation]/1: to_array requires copy constructible elements.");
  411. return std::__to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
  412. }
  413. template <typename _Tp, size_t _Size>
  414. _LIBCPP_HIDE_FROM_ABI constexpr array<remove_cv_t<_Tp>, _Size>
  415. to_array(_Tp (&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
  416. static_assert(!is_array_v<_Tp>, "[array.creation]/4: to_array does not accept multidimensional arrays.");
  417. static_assert(is_move_constructible_v<_Tp>, "[array.creation]/4: to_array requires move constructible elements.");
  418. return std::__to_array_rvalue_impl(std::move(__arr), make_index_sequence<_Size>());
  419. }
  420. #endif // _LIBCPP_STD_VER >= 20
  421. _LIBCPP_END_NAMESPACE_STD
  422. _LIBCPP_POP_MACROS
  423. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  424. # include <algorithm>
  425. # include <concepts>
  426. # include <cstdlib>
  427. # include <iterator>
  428. # include <type_traits>
  429. # include <utility>
  430. #endif
  431. #endif // _LIBCPP_ARRAY