array 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // <array> -*- C++ -*-
  2. // Copyright (C) 2007-2022 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, std::size_t _Nm>
  41. struct __array_traits
  42. {
  43. typedef _Tp _Type[_Nm];
  44. typedef __is_swappable<_Tp> _Is_swappable;
  45. typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
  46. static constexpr _Tp&
  47. _S_ref(const _Type& __t, std::size_t __n) noexcept
  48. { return const_cast<_Tp&>(__t[__n]); }
  49. static constexpr _Tp*
  50. _S_ptr(const _Type& __t) noexcept
  51. { return const_cast<_Tp*>(__t); }
  52. };
  53. template<typename _Tp>
  54. struct __array_traits<_Tp, 0>
  55. {
  56. struct _Type { };
  57. typedef true_type _Is_swappable;
  58. typedef true_type _Is_nothrow_swappable;
  59. static constexpr _Tp&
  60. _S_ref(const _Type&, std::size_t) noexcept
  61. { return *static_cast<_Tp*>(nullptr); }
  62. static constexpr _Tp*
  63. _S_ptr(const _Type&) noexcept
  64. { return nullptr; }
  65. };
  66. /**
  67. * @brief A standard container for storing a fixed size sequence of elements.
  68. *
  69. * @ingroup sequences
  70. *
  71. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  72. * <a href="tables.html#66">reversible container</a>, and a
  73. * <a href="tables.html#67">sequence</a>.
  74. *
  75. * Sets support random access iterators.
  76. *
  77. * @tparam Tp Type of element. Required to be a complete type.
  78. * @tparam Nm Number of elements.
  79. */
  80. template<typename _Tp, std::size_t _Nm>
  81. struct array
  82. {
  83. typedef _Tp value_type;
  84. typedef value_type* pointer;
  85. typedef const value_type* const_pointer;
  86. typedef value_type& reference;
  87. typedef const value_type& const_reference;
  88. typedef value_type* iterator;
  89. typedef const value_type* const_iterator;
  90. typedef std::size_t size_type;
  91. typedef std::ptrdiff_t difference_type;
  92. typedef std::reverse_iterator<iterator> reverse_iterator;
  93. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  94. // Support for zero-sized arrays mandatory.
  95. typedef __array_traits<_Tp, _Nm> _AT_Type;
  96. typename _AT_Type::_Type _M_elems;
  97. // No explicit construct/copy/destroy for aggregate type.
  98. // DR 776.
  99. _GLIBCXX20_CONSTEXPR void
  100. fill(const value_type& __u)
  101. { std::fill_n(begin(), size(), __u); }
  102. _GLIBCXX20_CONSTEXPR void
  103. swap(array& __other)
  104. noexcept(_AT_Type::_Is_nothrow_swappable::value)
  105. { std::swap_ranges(begin(), end(), __other.begin()); }
  106. // Iterators.
  107. [[__gnu__::__const__, __nodiscard__]]
  108. _GLIBCXX17_CONSTEXPR iterator
  109. begin() noexcept
  110. { return iterator(data()); }
  111. [[__nodiscard__]]
  112. _GLIBCXX17_CONSTEXPR const_iterator
  113. begin() const noexcept
  114. { return const_iterator(data()); }
  115. [[__gnu__::__const__, __nodiscard__]]
  116. _GLIBCXX17_CONSTEXPR iterator
  117. end() noexcept
  118. { return iterator(data() + _Nm); }
  119. [[__nodiscard__]]
  120. _GLIBCXX17_CONSTEXPR const_iterator
  121. end() const noexcept
  122. { return const_iterator(data() + _Nm); }
  123. [[__gnu__::__const__, __nodiscard__]]
  124. _GLIBCXX17_CONSTEXPR reverse_iterator
  125. rbegin() noexcept
  126. { return reverse_iterator(end()); }
  127. [[__nodiscard__]]
  128. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  129. rbegin() const noexcept
  130. { return const_reverse_iterator(end()); }
  131. [[__gnu__::__const__, __nodiscard__]]
  132. _GLIBCXX17_CONSTEXPR reverse_iterator
  133. rend() noexcept
  134. { return reverse_iterator(begin()); }
  135. [[__nodiscard__]]
  136. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  137. rend() const noexcept
  138. { return const_reverse_iterator(begin()); }
  139. [[__nodiscard__]]
  140. _GLIBCXX17_CONSTEXPR const_iterator
  141. cbegin() const noexcept
  142. { return const_iterator(data()); }
  143. [[__nodiscard__]]
  144. _GLIBCXX17_CONSTEXPR const_iterator
  145. cend() const noexcept
  146. { return const_iterator(data() + _Nm); }
  147. [[__nodiscard__]]
  148. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  149. crbegin() const noexcept
  150. { return const_reverse_iterator(end()); }
  151. [[__nodiscard__]]
  152. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  153. crend() const noexcept
  154. { return const_reverse_iterator(begin()); }
  155. // Capacity.
  156. [[__gnu__::__const__, __nodiscard__]]
  157. constexpr size_type
  158. size() const noexcept { return _Nm; }
  159. [[__gnu__::__const__, __nodiscard__]]
  160. constexpr size_type
  161. max_size() const noexcept { return _Nm; }
  162. [[__gnu__::__const__, __nodiscard__]]
  163. constexpr bool
  164. empty() const noexcept { return size() == 0; }
  165. // Element access.
  166. [[__nodiscard__]]
  167. _GLIBCXX17_CONSTEXPR reference
  168. operator[](size_type __n) noexcept
  169. {
  170. __glibcxx_requires_subscript(__n);
  171. return _AT_Type::_S_ref(_M_elems, __n);
  172. }
  173. [[__nodiscard__]]
  174. constexpr const_reference
  175. operator[](size_type __n) const noexcept
  176. {
  177. #if __cplusplus >= 201402L
  178. __glibcxx_requires_subscript(__n);
  179. #endif
  180. return _AT_Type::_S_ref(_M_elems, __n);
  181. }
  182. _GLIBCXX17_CONSTEXPR reference
  183. at(size_type __n)
  184. {
  185. if (__n >= _Nm)
  186. std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  187. ">= _Nm (which is %zu)"),
  188. __n, _Nm);
  189. return _AT_Type::_S_ref(_M_elems, __n);
  190. }
  191. constexpr const_reference
  192. at(size_type __n) const
  193. {
  194. // Result of conditional expression must be an lvalue so use
  195. // boolean ? lvalue : (throw-expr, lvalue)
  196. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  197. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  198. ">= _Nm (which is %zu)"),
  199. __n, _Nm),
  200. _AT_Type::_S_ref(_M_elems, 0));
  201. }
  202. [[__nodiscard__]]
  203. _GLIBCXX17_CONSTEXPR reference
  204. front() noexcept
  205. {
  206. __glibcxx_requires_nonempty();
  207. return *begin();
  208. }
  209. [[__nodiscard__]]
  210. constexpr const_reference
  211. front() const noexcept
  212. {
  213. #if __cplusplus >= 201402L
  214. __glibcxx_requires_nonempty();
  215. #endif
  216. return _AT_Type::_S_ref(_M_elems, 0);
  217. }
  218. [[__nodiscard__]]
  219. _GLIBCXX17_CONSTEXPR reference
  220. back() noexcept
  221. {
  222. __glibcxx_requires_nonempty();
  223. return _Nm ? *(end() - 1) : *end();
  224. }
  225. [[__nodiscard__]]
  226. constexpr const_reference
  227. back() const noexcept
  228. {
  229. #if __cplusplus >= 201402L
  230. __glibcxx_requires_nonempty();
  231. #endif
  232. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  233. : _AT_Type::_S_ref(_M_elems, 0);
  234. }
  235. [[__gnu__::__const__, __nodiscard__]]
  236. _GLIBCXX17_CONSTEXPR pointer
  237. data() noexcept
  238. { return _AT_Type::_S_ptr(_M_elems); }
  239. [[__nodiscard__]]
  240. _GLIBCXX17_CONSTEXPR const_pointer
  241. data() const noexcept
  242. { return _AT_Type::_S_ptr(_M_elems); }
  243. };
  244. #if __cpp_deduction_guides >= 201606
  245. template<typename _Tp, typename... _Up>
  246. array(_Tp, _Up...)
  247. -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
  248. 1 + sizeof...(_Up)>;
  249. #endif
  250. // Array comparisons.
  251. template<typename _Tp, std::size_t _Nm>
  252. [[__nodiscard__]]
  253. _GLIBCXX20_CONSTEXPR
  254. inline bool
  255. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  256. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  257. #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
  258. template<typename _Tp, size_t _Nm>
  259. [[nodiscard]]
  260. constexpr __detail::__synth3way_t<_Tp>
  261. operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  262. {
  263. if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
  264. if (!std::__is_constant_evaluated())
  265. {
  266. constexpr size_t __n = _Nm * sizeof(_Tp);
  267. return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
  268. }
  269. for (size_t __i = 0; __i < _Nm; ++__i)
  270. {
  271. auto __c = __detail::__synth3way(__a[__i], __b[__i]);
  272. if (__c != 0)
  273. return __c;
  274. }
  275. return strong_ordering::equal;
  276. }
  277. #else
  278. template<typename _Tp, std::size_t _Nm>
  279. [[__nodiscard__]]
  280. _GLIBCXX20_CONSTEXPR
  281. inline bool
  282. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  283. { return !(__one == __two); }
  284. template<typename _Tp, std::size_t _Nm>
  285. [[__nodiscard__]]
  286. _GLIBCXX20_CONSTEXPR
  287. inline bool
  288. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  289. {
  290. return std::lexicographical_compare(__a.begin(), __a.end(),
  291. __b.begin(), __b.end());
  292. }
  293. template<typename _Tp, std::size_t _Nm>
  294. [[__nodiscard__]]
  295. _GLIBCXX20_CONSTEXPR
  296. inline bool
  297. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  298. { return __two < __one; }
  299. template<typename _Tp, std::size_t _Nm>
  300. [[__nodiscard__]]
  301. _GLIBCXX20_CONSTEXPR
  302. inline bool
  303. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  304. { return !(__one > __two); }
  305. template<typename _Tp, std::size_t _Nm>
  306. [[__nodiscard__]]
  307. _GLIBCXX20_CONSTEXPR
  308. inline bool
  309. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  310. { return !(__one < __two); }
  311. #endif // three_way_comparison && concepts
  312. // Specialized algorithms.
  313. template<typename _Tp, std::size_t _Nm>
  314. _GLIBCXX20_CONSTEXPR
  315. inline
  316. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  317. // Constrained free swap overload, see p0185r1
  318. typename enable_if<
  319. __array_traits<_Tp, _Nm>::_Is_swappable::value
  320. >::type
  321. #else
  322. void
  323. #endif
  324. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  325. noexcept(noexcept(__one.swap(__two)))
  326. { __one.swap(__two); }
  327. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  328. template<typename _Tp, std::size_t _Nm>
  329. typename enable_if<
  330. !__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
  331. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  332. #endif
  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 __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  340. }
  341. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  342. [[__nodiscard__]]
  343. constexpr _Tp&&
  344. get(array<_Tp, _Nm>&& __arr) noexcept
  345. {
  346. static_assert(_Int < _Nm, "array index is within bounds");
  347. return std::move(std::get<_Int>(__arr));
  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 __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  356. }
  357. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  358. [[__nodiscard__]]
  359. constexpr const _Tp&&
  360. get(const array<_Tp, _Nm>&& __arr) noexcept
  361. {
  362. static_assert(_Int < _Nm, "array index is within bounds");
  363. return std::move(std::get<_Int>(__arr));
  364. }
  365. #if __cplusplus > 201703L
  366. #define __cpp_lib_to_array 201907L
  367. template<bool _Move = false, typename _Tp, size_t... _Idx>
  368. constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
  369. __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
  370. {
  371. if constexpr (_Move)
  372. return {{std::move(__a[_Idx])...}};
  373. else
  374. return {{__a[_Idx]...}};
  375. }
  376. template<typename _Tp, size_t _Nm>
  377. [[nodiscard]]
  378. constexpr array<remove_cv_t<_Tp>, _Nm>
  379. to_array(_Tp (&__a)[_Nm])
  380. noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
  381. {
  382. static_assert(!is_array_v<_Tp>);
  383. static_assert(is_constructible_v<_Tp, _Tp&>);
  384. if constexpr (is_constructible_v<_Tp, _Tp&>)
  385. return __to_array(__a, make_index_sequence<_Nm>{});
  386. __builtin_unreachable(); // FIXME: see PR c++/91388
  387. }
  388. template<typename _Tp, size_t _Nm>
  389. [[nodiscard]]
  390. constexpr array<remove_cv_t<_Tp>, _Nm>
  391. to_array(_Tp (&&__a)[_Nm])
  392. noexcept(is_nothrow_move_constructible_v<_Tp>)
  393. {
  394. static_assert(!is_array_v<_Tp>);
  395. static_assert(is_move_constructible_v<_Tp>);
  396. if constexpr (is_move_constructible_v<_Tp>)
  397. return __to_array<1>(__a, make_index_sequence<_Nm>{});
  398. __builtin_unreachable(); // FIXME: see PR c++/91388
  399. }
  400. #endif // C++20
  401. // Tuple interface to class template array.
  402. /// Partial specialization for std::array
  403. template<typename _Tp, size_t _Nm>
  404. struct tuple_size<array<_Tp, _Nm>>
  405. : public integral_constant<size_t, _Nm> { };
  406. /// Partial specialization for std::array
  407. template<size_t _Ind, typename _Tp, size_t _Nm>
  408. struct tuple_element<_Ind, array<_Tp, _Nm>>
  409. {
  410. static_assert(_Ind < _Nm, "array index is in range");
  411. using type = _Tp;
  412. };
  413. #if __cplusplus >= 201703L
  414. template<typename _Tp, size_t _Nm>
  415. inline constexpr size_t tuple_size_v<array<_Tp, _Nm>> = _Nm;
  416. template<typename _Tp, size_t _Nm>
  417. inline constexpr size_t tuple_size_v<const array<_Tp, _Nm>> = _Nm;
  418. #endif
  419. template<typename _Tp, size_t _Nm>
  420. struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
  421. { };
  422. _GLIBCXX_END_NAMESPACE_VERSION
  423. } // namespace std
  424. #endif // C++11
  425. #endif // _GLIBCXX_ARRAY