array 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // <array> -*- C++ -*-
  2. // Copyright (C) 2007-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/array
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_ARRAY
  24. #define _GLIBCXX_ARRAY 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <compare>
  30. #include <initializer_list>
  31. #include <type_traits>
  32. #include <bits/functexcept.h>
  33. #include <bits/stl_algobase.h>
  34. #include <bits/range_access.h> // std::begin, std::end etc.
  35. #include <bits/utility.h> // std::index_sequence, std::tuple_size
  36. #include <debug/assertions.h>
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. template<typename _Tp, size_t _Nm>
  41. struct __array_traits
  42. {
  43. using _Type = _Tp[_Nm];
  44. using _Is_swappable = __is_swappable<_Tp>;
  45. using _Is_nothrow_swappable = __is_nothrow_swappable<_Tp>;
  46. };
  47. template<typename _Tp>
  48. struct __array_traits<_Tp, 0>
  49. {
  50. // Empty type used instead of _Tp[0] for std::array<_Tp, 0>.
  51. struct _Type
  52. {
  53. // Indexing is undefined.
  54. __attribute__((__always_inline__,__noreturn__))
  55. _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
  56. // Conversion to a pointer produces a null pointer.
  57. __attribute__((__always_inline__))
  58. constexpr explicit operator _Tp*() const noexcept { return nullptr; }
  59. };
  60. using _Is_swappable = true_type;
  61. using _Is_nothrow_swappable = true_type;
  62. };
  63. /**
  64. * @brief A standard container for storing a fixed size sequence of elements.
  65. *
  66. * @ingroup sequences
  67. *
  68. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  69. * <a href="tables.html#66">reversible container</a>, and a
  70. * <a href="tables.html#67">sequence</a>.
  71. *
  72. * Sets support random access iterators.
  73. *
  74. * @tparam Tp Type of element. Required to be a complete type.
  75. * @tparam Nm Number of elements.
  76. */
  77. template<typename _Tp, std::size_t _Nm>
  78. struct array
  79. {
  80. typedef _Tp value_type;
  81. typedef value_type* pointer;
  82. typedef const value_type* const_pointer;
  83. typedef value_type& reference;
  84. typedef const value_type& const_reference;
  85. typedef value_type* iterator;
  86. typedef const value_type* const_iterator;
  87. typedef std::size_t size_type;
  88. typedef std::ptrdiff_t difference_type;
  89. typedef std::reverse_iterator<iterator> reverse_iterator;
  90. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  91. // Support for zero-sized arrays mandatory.
  92. typename __array_traits<_Tp, _Nm>::_Type _M_elems;
  93. // No explicit construct/copy/destroy for aggregate type.
  94. // DR 776.
  95. _GLIBCXX20_CONSTEXPR void
  96. fill(const value_type& __u)
  97. { std::fill_n(begin(), size(), __u); }
  98. _GLIBCXX20_CONSTEXPR void
  99. swap(array& __other)
  100. noexcept(__array_traits<_Tp, _Nm>::_Is_nothrow_swappable::value)
  101. { std::swap_ranges(begin(), end(), __other.begin()); }
  102. // Iterators.
  103. [[__gnu__::__const__, __nodiscard__]]
  104. _GLIBCXX17_CONSTEXPR iterator
  105. begin() noexcept
  106. { return iterator(data()); }
  107. [[__nodiscard__]]
  108. _GLIBCXX17_CONSTEXPR const_iterator
  109. begin() const noexcept
  110. { return const_iterator(data()); }
  111. [[__gnu__::__const__, __nodiscard__]]
  112. _GLIBCXX17_CONSTEXPR iterator
  113. end() noexcept
  114. { return iterator(data() + _Nm); }
  115. [[__nodiscard__]]
  116. _GLIBCXX17_CONSTEXPR const_iterator
  117. end() const noexcept
  118. { return const_iterator(data() + _Nm); }
  119. [[__gnu__::__const__, __nodiscard__]]
  120. _GLIBCXX17_CONSTEXPR reverse_iterator
  121. rbegin() noexcept
  122. { return reverse_iterator(end()); }
  123. [[__nodiscard__]]
  124. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  125. rbegin() const noexcept
  126. { return const_reverse_iterator(end()); }
  127. [[__gnu__::__const__, __nodiscard__]]
  128. _GLIBCXX17_CONSTEXPR reverse_iterator
  129. rend() noexcept
  130. { return reverse_iterator(begin()); }
  131. [[__nodiscard__]]
  132. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  133. rend() const noexcept
  134. { return const_reverse_iterator(begin()); }
  135. [[__nodiscard__]]
  136. _GLIBCXX17_CONSTEXPR const_iterator
  137. cbegin() const noexcept
  138. { return const_iterator(data()); }
  139. [[__nodiscard__]]
  140. _GLIBCXX17_CONSTEXPR const_iterator
  141. cend() const noexcept
  142. { return const_iterator(data() + _Nm); }
  143. [[__nodiscard__]]
  144. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  145. crbegin() const noexcept
  146. { return const_reverse_iterator(end()); }
  147. [[__nodiscard__]]
  148. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  149. crend() const noexcept
  150. { return const_reverse_iterator(begin()); }
  151. // Capacity.
  152. [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
  153. constexpr size_type
  154. size() const noexcept { return _Nm; }
  155. [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
  156. constexpr size_type
  157. max_size() const noexcept { return _Nm; }
  158. [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
  159. constexpr bool
  160. empty() const noexcept { return size() == 0; }
  161. // Element access.
  162. [[__nodiscard__]]
  163. _GLIBCXX17_CONSTEXPR reference
  164. operator[](size_type __n) noexcept
  165. {
  166. __glibcxx_requires_subscript(__n);
  167. return _M_elems[__n];
  168. }
  169. [[__nodiscard__]]
  170. constexpr const_reference
  171. operator[](size_type __n) const noexcept
  172. {
  173. #if __cplusplus >= 201402L
  174. __glibcxx_requires_subscript(__n);
  175. #endif
  176. return _M_elems[__n];
  177. }
  178. _GLIBCXX17_CONSTEXPR reference
  179. at(size_type __n)
  180. {
  181. if (__n >= _Nm)
  182. std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  183. ">= _Nm (which is %zu)"),
  184. __n, _Nm);
  185. return _M_elems[__n];
  186. }
  187. constexpr const_reference
  188. at(size_type __n) const
  189. {
  190. // Result of conditional expression must be an lvalue so use
  191. // boolean ? lvalue : (throw-expr, lvalue)
  192. return __n < _Nm ? _M_elems[__n]
  193. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  194. ">= _Nm (which is %zu)"),
  195. __n, _Nm),
  196. _M_elems[__n]);
  197. }
  198. [[__nodiscard__]]
  199. _GLIBCXX17_CONSTEXPR reference
  200. front() noexcept
  201. {
  202. __glibcxx_requires_nonempty();
  203. return _M_elems[(size_type)0];
  204. }
  205. [[__nodiscard__]]
  206. constexpr const_reference
  207. front() const noexcept
  208. {
  209. #if __cplusplus >= 201402L
  210. __glibcxx_requires_nonempty();
  211. #endif
  212. return _M_elems[(size_type)0];
  213. }
  214. [[__nodiscard__]]
  215. _GLIBCXX17_CONSTEXPR reference
  216. back() noexcept
  217. {
  218. __glibcxx_requires_nonempty();
  219. return _M_elems[_Nm - 1];
  220. }
  221. [[__nodiscard__]]
  222. constexpr const_reference
  223. back() const noexcept
  224. {
  225. #if __cplusplus >= 201402L
  226. __glibcxx_requires_nonempty();
  227. #endif
  228. return _M_elems[_Nm - 1];
  229. }
  230. [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
  231. _GLIBCXX17_CONSTEXPR pointer
  232. data() noexcept
  233. { return static_cast<pointer>(_M_elems); }
  234. [[__nodiscard__]]
  235. _GLIBCXX17_CONSTEXPR const_pointer
  236. data() const noexcept
  237. { return static_cast<const_pointer>(_M_elems); }
  238. };
  239. #if __cpp_deduction_guides >= 201606
  240. template<typename _Tp, typename... _Up>
  241. array(_Tp, _Up...)
  242. -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
  243. 1 + sizeof...(_Up)>;
  244. #endif
  245. // Array comparisons.
  246. template<typename _Tp, std::size_t _Nm>
  247. [[__nodiscard__]]
  248. _GLIBCXX20_CONSTEXPR
  249. inline bool
  250. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  251. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  252. #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
  253. template<typename _Tp, size_t _Nm>
  254. [[nodiscard]]
  255. constexpr __detail::__synth3way_t<_Tp>
  256. operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  257. {
  258. if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
  259. if (!std::__is_constant_evaluated())
  260. {
  261. constexpr size_t __n = _Nm * sizeof(_Tp);
  262. return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
  263. }
  264. for (size_t __i = 0; __i < _Nm; ++__i)
  265. {
  266. auto __c = __detail::__synth3way(__a[__i], __b[__i]);
  267. if (__c != 0)
  268. return __c;
  269. }
  270. return strong_ordering::equal;
  271. }
  272. #else
  273. template<typename _Tp, std::size_t _Nm>
  274. [[__nodiscard__]]
  275. _GLIBCXX20_CONSTEXPR
  276. inline bool
  277. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  278. { return !(__one == __two); }
  279. template<typename _Tp, std::size_t _Nm>
  280. [[__nodiscard__]]
  281. _GLIBCXX20_CONSTEXPR
  282. inline bool
  283. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  284. {
  285. return std::lexicographical_compare(__a.begin(), __a.end(),
  286. __b.begin(), __b.end());
  287. }
  288. template<typename _Tp, std::size_t _Nm>
  289. [[__nodiscard__]]
  290. _GLIBCXX20_CONSTEXPR
  291. inline bool
  292. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  293. { return __two < __one; }
  294. template<typename _Tp, std::size_t _Nm>
  295. [[__nodiscard__]]
  296. _GLIBCXX20_CONSTEXPR
  297. inline bool
  298. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  299. { return !(__one > __two); }
  300. template<typename _Tp, std::size_t _Nm>
  301. [[__nodiscard__]]
  302. _GLIBCXX20_CONSTEXPR
  303. inline bool
  304. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  305. { return !(__one < __two); }
  306. #endif // three_way_comparison && concepts
  307. // Specialized algorithms.
  308. template<typename _Tp, std::size_t _Nm>
  309. _GLIBCXX20_CONSTEXPR
  310. inline
  311. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  312. // Constrained free swap overload, see p0185r1
  313. __enable_if_t<__array_traits<_Tp, _Nm>::_Is_swappable::value>
  314. #else
  315. void
  316. #endif
  317. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  318. noexcept(noexcept(__one.swap(__two)))
  319. { __one.swap(__two); }
  320. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  321. template<typename _Tp, std::size_t _Nm>
  322. __enable_if_t<!__array_traits<_Tp, _Nm>::_Is_swappable::value>
  323. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  324. #endif
  325. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  326. [[__nodiscard__]]
  327. constexpr _Tp&
  328. get(array<_Tp, _Nm>& __arr) noexcept
  329. {
  330. static_assert(_Int < _Nm, "array index is within bounds");
  331. return __arr._M_elems[_Int];
  332. }
  333. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  334. [[__nodiscard__]]
  335. constexpr _Tp&&
  336. get(array<_Tp, _Nm>&& __arr) noexcept
  337. {
  338. static_assert(_Int < _Nm, "array index is within bounds");
  339. return std::move(std::get<_Int>(__arr));
  340. }
  341. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  342. [[__nodiscard__]]
  343. constexpr const _Tp&
  344. get(const array<_Tp, _Nm>& __arr) noexcept
  345. {
  346. static_assert(_Int < _Nm, "array index is within bounds");
  347. return __arr._M_elems[_Int];
  348. }
  349. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  350. [[__nodiscard__]]
  351. constexpr const _Tp&&
  352. get(const array<_Tp, _Nm>&& __arr) noexcept
  353. {
  354. static_assert(_Int < _Nm, "array index is within bounds");
  355. return std::move(std::get<_Int>(__arr));
  356. }
  357. #if __cplusplus >= 202002L && __cpp_generic_lambdas >= 201707L
  358. #define __cpp_lib_to_array 201907L
  359. template<typename _Tp, size_t _Nm>
  360. [[nodiscard]]
  361. constexpr array<remove_cv_t<_Tp>, _Nm>
  362. to_array(_Tp (&__a)[_Nm])
  363. noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
  364. {
  365. static_assert(!is_array_v<_Tp>);
  366. static_assert(is_constructible_v<_Tp, _Tp&>);
  367. if constexpr (is_constructible_v<_Tp, _Tp&>)
  368. {
  369. if constexpr (is_trivial_v<_Tp>)
  370. {
  371. array<remove_cv_t<_Tp>, _Nm> __arr;
  372. if (!__is_constant_evaluated() && _Nm != 0)
  373. __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
  374. else
  375. for (size_t __i = 0; __i < _Nm; ++__i)
  376. __arr._M_elems[__i] = __a[__i];
  377. return __arr;
  378. }
  379. else
  380. return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) {
  381. return array<remove_cv_t<_Tp>, _Nm>{{ __a[_Idx]... }};
  382. }(make_index_sequence<_Nm>{});
  383. }
  384. else
  385. __builtin_unreachable(); // FIXME: see PR c++/91388
  386. }
  387. template<typename _Tp, size_t _Nm>
  388. [[nodiscard]]
  389. constexpr array<remove_cv_t<_Tp>, _Nm>
  390. to_array(_Tp (&&__a)[_Nm])
  391. noexcept(is_nothrow_move_constructible_v<_Tp>)
  392. {
  393. static_assert(!is_array_v<_Tp>);
  394. static_assert(is_move_constructible_v<_Tp>);
  395. if constexpr (is_move_constructible_v<_Tp>)
  396. {
  397. if constexpr (is_trivial_v<_Tp>)
  398. {
  399. array<remove_cv_t<_Tp>, _Nm> __arr;
  400. if (!__is_constant_evaluated() && _Nm != 0)
  401. __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
  402. else
  403. for (size_t __i = 0; __i < _Nm; ++__i)
  404. __arr._M_elems[__i] = __a[__i];
  405. return __arr;
  406. }
  407. else
  408. return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) {
  409. return array<remove_cv_t<_Tp>, _Nm>{{ std::move(__a[_Idx])... }};
  410. }(make_index_sequence<_Nm>{});
  411. }
  412. else
  413. __builtin_unreachable(); // FIXME: see PR c++/91388
  414. }
  415. #endif // C++20
  416. // Tuple interface to class template array.
  417. /// Partial specialization for std::array
  418. template<typename _Tp, size_t _Nm>
  419. struct tuple_size<array<_Tp, _Nm>>
  420. : public integral_constant<size_t, _Nm> { };
  421. /// Partial specialization for std::array
  422. template<size_t _Ind, typename _Tp, size_t _Nm>
  423. struct tuple_element<_Ind, array<_Tp, _Nm>>
  424. {
  425. static_assert(_Ind < _Nm, "array index is in range");
  426. using type = _Tp;
  427. };
  428. #if __cplusplus >= 201703L
  429. template<typename _Tp, size_t _Nm>
  430. inline constexpr size_t tuple_size_v<array<_Tp, _Nm>> = _Nm;
  431. template<typename _Tp, size_t _Nm>
  432. inline constexpr size_t tuple_size_v<const array<_Tp, _Nm>> = _Nm;
  433. #endif
  434. template<typename _Tp, size_t _Nm>
  435. struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
  436. { };
  437. _GLIBCXX_END_NAMESPACE_VERSION
  438. } // namespace std
  439. #endif // C++11
  440. #endif // _GLIBCXX_ARRAY