array 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. #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);
  32. void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
  33. // iterators:
  34. iterator begin() noexcept;
  35. const_iterator begin() const noexcept;
  36. iterator end() noexcept;
  37. const_iterator end() const noexcept;
  38. reverse_iterator rbegin() noexcept;
  39. const_reverse_iterator rbegin() const noexcept;
  40. reverse_iterator rend() noexcept;
  41. const_reverse_iterator rend() const noexcept;
  42. const_iterator cbegin() const noexcept;
  43. const_iterator cend() const noexcept;
  44. const_reverse_iterator crbegin() const noexcept;
  45. const_reverse_iterator crend() const noexcept;
  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);
  52. const_reference operator[](size_type n) const; // constexpr in C++14
  53. const_reference at(size_type n) const; // constexpr in C++14
  54. reference at(size_type n);
  55. reference front();
  56. const_reference front() const; // constexpr in C++14
  57. reference back();
  58. const_reference back() const; // constexpr in C++14
  59. T* data() noexcept;
  60. const T* data() const noexcept;
  61. };
  62. template <class T, class... U>
  63. array(T, U...) -> array<T, 1 + sizeof...(U)>;
  64. template <class T, size_t N>
  65. bool operator==(const array<T,N>& x, const array<T,N>& y);
  66. template <class T, size_t N>
  67. bool operator!=(const array<T,N>& x, const array<T,N>& y);
  68. template <class T, size_t N>
  69. bool operator<(const array<T,N>& x, const array<T,N>& y);
  70. template <class T, size_t N>
  71. bool operator>(const array<T,N>& x, const array<T,N>& y);
  72. template <class T, size_t N>
  73. bool operator<=(const array<T,N>& x, const array<T,N>& y);
  74. template <class T, size_t N>
  75. bool operator>=(const array<T,N>& x, const array<T,N>& y);
  76. template <class T, size_t N >
  77. void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17
  78. template <class T> struct tuple_size;
  79. template <size_t I, class T> class tuple_element;
  80. template <class T, size_t N> struct tuple_size<array<T, N>>;
  81. template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
  82. template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
  83. template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
  84. template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
  85. template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
  86. } // std
  87. */
  88. #include <__config>
  89. #include <__tuple>
  90. #include <type_traits>
  91. #include <utility>
  92. #include <iterator>
  93. #include <algorithm>
  94. #include <stdexcept>
  95. #include <cstdlib> // for _LIBCPP_UNREACHABLE
  96. #include <version>
  97. #include <__debug>
  98. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  99. #pragma GCC system_header
  100. #endif
  101. _LIBCPP_BEGIN_NAMESPACE_STD
  102. template <class _Tp, size_t _Size>
  103. struct _LIBCPP_TEMPLATE_VIS array
  104. {
  105. // types:
  106. typedef array __self;
  107. typedef _Tp value_type;
  108. typedef value_type& reference;
  109. typedef const value_type& const_reference;
  110. typedef value_type* iterator;
  111. typedef const value_type* const_iterator;
  112. typedef value_type* pointer;
  113. typedef const value_type* const_pointer;
  114. typedef size_t size_type;
  115. typedef ptrdiff_t difference_type;
  116. typedef std::reverse_iterator<iterator> reverse_iterator;
  117. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  118. _Tp __elems_[_Size];
  119. // No explicit construct/copy/destroy for aggregate type
  120. _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
  121. _VSTD::fill_n(__elems_, _Size, __u);
  122. }
  123. _LIBCPP_INLINE_VISIBILITY
  124. void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
  125. std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
  126. }
  127. // iterators:
  128. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  129. iterator begin() _NOEXCEPT {return iterator(data());}
  130. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  131. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  132. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  133. iterator end() _NOEXCEPT {return iterator(data() + _Size);}
  134. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  135. const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
  136. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  137. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  138. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  139. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  140. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  141. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  142. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  143. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  144. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  145. const_iterator cbegin() const _NOEXCEPT {return begin();}
  146. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  147. const_iterator cend() const _NOEXCEPT {return end();}
  148. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  149. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  150. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  151. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  152. // capacity:
  153. _LIBCPP_INLINE_VISIBILITY
  154. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
  155. _LIBCPP_INLINE_VISIBILITY
  156. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
  157. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  158. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; }
  159. // element access:
  160. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  161. reference operator[](size_type __n) {return __elems_[__n];}
  162. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  163. const_reference operator[](size_type __n) const {return __elems_[__n];}
  164. _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
  165. _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
  166. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() {return __elems_[0];}
  167. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
  168. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() {return __elems_[_Size - 1];}
  169. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size - 1];}
  170. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  171. value_type* data() _NOEXCEPT {return __elems_;}
  172. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  173. const value_type* data() const _NOEXCEPT {return __elems_;}
  174. };
  175. template <class _Tp, size_t _Size>
  176. _LIBCPP_CONSTEXPR_AFTER_CXX14
  177. typename array<_Tp, _Size>::reference
  178. array<_Tp, _Size>::at(size_type __n)
  179. {
  180. if (__n >= _Size)
  181. __throw_out_of_range("array::at");
  182. return __elems_[__n];
  183. }
  184. template <class _Tp, size_t _Size>
  185. _LIBCPP_CONSTEXPR_AFTER_CXX11
  186. typename array<_Tp, _Size>::const_reference
  187. array<_Tp, _Size>::at(size_type __n) const
  188. {
  189. if (__n >= _Size)
  190. __throw_out_of_range("array::at");
  191. return __elems_[__n];
  192. }
  193. template <class _Tp>
  194. struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
  195. {
  196. // types:
  197. typedef array __self;
  198. typedef _Tp value_type;
  199. typedef value_type& reference;
  200. typedef const value_type& const_reference;
  201. typedef value_type* iterator;
  202. typedef const value_type* const_iterator;
  203. typedef value_type* pointer;
  204. typedef const value_type* const_pointer;
  205. typedef size_t size_type;
  206. typedef ptrdiff_t difference_type;
  207. typedef std::reverse_iterator<iterator> reverse_iterator;
  208. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  209. typedef typename conditional<is_const<_Tp>::value, const char,
  210. char>::type _CharType;
  211. struct _ArrayInStructT { _Tp __data_[1]; };
  212. _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
  213. // No explicit construct/copy/destroy for aggregate type
  214. _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
  215. static_assert(!is_const<_Tp>::value,
  216. "cannot fill zero-sized array of type 'const T'");
  217. }
  218. _LIBCPP_INLINE_VISIBILITY
  219. void swap(array&) _NOEXCEPT {
  220. static_assert(!is_const<_Tp>::value,
  221. "cannot swap zero-sized array of type 'const T'");
  222. }
  223. // iterators:
  224. _LIBCPP_INLINE_VISIBILITY
  225. iterator begin() _NOEXCEPT {return iterator(data());}
  226. _LIBCPP_INLINE_VISIBILITY
  227. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  228. _LIBCPP_INLINE_VISIBILITY
  229. iterator end() _NOEXCEPT {return iterator(data());}
  230. _LIBCPP_INLINE_VISIBILITY
  231. const_iterator end() const _NOEXCEPT {return const_iterator(data());}
  232. _LIBCPP_INLINE_VISIBILITY
  233. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  234. _LIBCPP_INLINE_VISIBILITY
  235. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  236. _LIBCPP_INLINE_VISIBILITY
  237. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  238. _LIBCPP_INLINE_VISIBILITY
  239. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  240. _LIBCPP_INLINE_VISIBILITY
  241. const_iterator cbegin() const _NOEXCEPT {return begin();}
  242. _LIBCPP_INLINE_VISIBILITY
  243. const_iterator cend() const _NOEXCEPT {return end();}
  244. _LIBCPP_INLINE_VISIBILITY
  245. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  246. _LIBCPP_INLINE_VISIBILITY
  247. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  248. // capacity:
  249. _LIBCPP_INLINE_VISIBILITY
  250. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
  251. _LIBCPP_INLINE_VISIBILITY
  252. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
  253. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  254. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
  255. // element access:
  256. _LIBCPP_INLINE_VISIBILITY
  257. reference operator[](size_type) {
  258. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  259. _LIBCPP_UNREACHABLE();
  260. }
  261. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  262. const_reference operator[](size_type) const {
  263. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  264. _LIBCPP_UNREACHABLE();
  265. }
  266. _LIBCPP_INLINE_VISIBILITY
  267. reference at(size_type) {
  268. __throw_out_of_range("array<T, 0>::at");
  269. _LIBCPP_UNREACHABLE();
  270. }
  271. _LIBCPP_INLINE_VISIBILITY
  272. const_reference at(size_type) const {
  273. __throw_out_of_range("array<T, 0>::at");
  274. _LIBCPP_UNREACHABLE();
  275. }
  276. _LIBCPP_INLINE_VISIBILITY
  277. reference front() {
  278. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  279. _LIBCPP_UNREACHABLE();
  280. }
  281. _LIBCPP_INLINE_VISIBILITY
  282. const_reference front() const {
  283. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  284. _LIBCPP_UNREACHABLE();
  285. }
  286. _LIBCPP_INLINE_VISIBILITY
  287. reference back() {
  288. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  289. _LIBCPP_UNREACHABLE();
  290. }
  291. _LIBCPP_INLINE_VISIBILITY
  292. const_reference back() const {
  293. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  294. _LIBCPP_UNREACHABLE();
  295. }
  296. _LIBCPP_INLINE_VISIBILITY
  297. value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
  298. _LIBCPP_INLINE_VISIBILITY
  299. const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
  300. };
  301. #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
  302. template<class _Tp, class... _Args,
  303. class = typename enable_if<(is_same_v<_Tp, _Args> && ...), void>::type
  304. >
  305. array(_Tp, _Args...)
  306. -> array<_Tp, 1 + sizeof...(_Args)>;
  307. #endif
  308. template <class _Tp, size_t _Size>
  309. inline _LIBCPP_INLINE_VISIBILITY
  310. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  311. operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  312. {
  313. return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  314. }
  315. template <class _Tp, size_t _Size>
  316. inline _LIBCPP_INLINE_VISIBILITY
  317. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  318. operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  319. {
  320. return !(__x == __y);
  321. }
  322. template <class _Tp, size_t _Size>
  323. inline _LIBCPP_INLINE_VISIBILITY
  324. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  325. operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  326. {
  327. return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
  328. __y.begin(), __y.end());
  329. }
  330. template <class _Tp, size_t _Size>
  331. inline _LIBCPP_INLINE_VISIBILITY
  332. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  333. operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  334. {
  335. return __y < __x;
  336. }
  337. template <class _Tp, size_t _Size>
  338. inline _LIBCPP_INLINE_VISIBILITY
  339. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  340. operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  341. {
  342. return !(__y < __x);
  343. }
  344. template <class _Tp, size_t _Size>
  345. inline _LIBCPP_INLINE_VISIBILITY
  346. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  347. operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  348. {
  349. return !(__x < __y);
  350. }
  351. template <class _Tp, size_t _Size>
  352. inline _LIBCPP_INLINE_VISIBILITY
  353. typename enable_if
  354. <
  355. _Size == 0 ||
  356. __is_swappable<_Tp>::value,
  357. void
  358. >::type
  359. swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
  360. _NOEXCEPT_(noexcept(__x.swap(__y)))
  361. {
  362. __x.swap(__y);
  363. }
  364. template <class _Tp, size_t _Size>
  365. struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
  366. : public integral_constant<size_t, _Size> {};
  367. template <size_t _Ip, class _Tp, size_t _Size>
  368. class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
  369. {
  370. static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
  371. public:
  372. typedef _Tp type;
  373. };
  374. template <size_t _Ip, class _Tp, size_t _Size>
  375. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  376. _Tp&
  377. get(array<_Tp, _Size>& __a) _NOEXCEPT
  378. {
  379. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
  380. return __a.__elems_[_Ip];
  381. }
  382. template <size_t _Ip, class _Tp, size_t _Size>
  383. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  384. const _Tp&
  385. get(const array<_Tp, _Size>& __a) _NOEXCEPT
  386. {
  387. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
  388. return __a.__elems_[_Ip];
  389. }
  390. #ifndef _LIBCPP_CXX03_LANG
  391. template <size_t _Ip, class _Tp, size_t _Size>
  392. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  393. _Tp&&
  394. get(array<_Tp, _Size>&& __a) _NOEXCEPT
  395. {
  396. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
  397. return _VSTD::move(__a.__elems_[_Ip]);
  398. }
  399. template <size_t _Ip, class _Tp, size_t _Size>
  400. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  401. const _Tp&&
  402. get(const array<_Tp, _Size>&& __a) _NOEXCEPT
  403. {
  404. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
  405. return _VSTD::move(__a.__elems_[_Ip]);
  406. }
  407. #endif // !_LIBCPP_CXX03_LANG
  408. _LIBCPP_END_NAMESPACE_STD
  409. #endif // _LIBCPP_ARRAY