array 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // <array> -*- C++ -*-
  2. // Copyright (C) 2007-2021 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 <utility>
  30. #include <bits/functexcept.h>
  31. #include <bits/stl_algobase.h>
  32. #include <bits/range_access.h>
  33. #include <debug/assertions.h>
  34. namespace std _GLIBCXX_VISIBILITY(default)
  35. {
  36. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  37. template<typename _Tp, std::size_t _Nm>
  38. struct __array_traits
  39. {
  40. typedef _Tp _Type[_Nm];
  41. typedef __is_swappable<_Tp> _Is_swappable;
  42. typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
  43. static constexpr _Tp&
  44. _S_ref(const _Type& __t, std::size_t __n) noexcept
  45. { return const_cast<_Tp&>(__t[__n]); }
  46. static constexpr _Tp*
  47. _S_ptr(const _Type& __t) noexcept
  48. { return const_cast<_Tp*>(__t); }
  49. };
  50. template<typename _Tp>
  51. struct __array_traits<_Tp, 0>
  52. {
  53. struct _Type { };
  54. typedef true_type _Is_swappable;
  55. typedef true_type _Is_nothrow_swappable;
  56. static constexpr _Tp&
  57. _S_ref(const _Type&, std::size_t) noexcept
  58. { return *static_cast<_Tp*>(nullptr); }
  59. static constexpr _Tp*
  60. _S_ptr(const _Type&) noexcept
  61. { return nullptr; }
  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. typedef __array_traits<_Tp, _Nm> _AT_Type;
  93. typename _AT_Type::_Type _M_elems;
  94. // No explicit construct/copy/destroy for aggregate type.
  95. // DR 776.
  96. _GLIBCXX20_CONSTEXPR void
  97. fill(const value_type& __u)
  98. { std::fill_n(begin(), size(), __u); }
  99. _GLIBCXX20_CONSTEXPR void
  100. swap(array& __other)
  101. noexcept(_AT_Type::_Is_nothrow_swappable::value)
  102. { std::swap_ranges(begin(), end(), __other.begin()); }
  103. // Iterators.
  104. _GLIBCXX17_CONSTEXPR iterator
  105. begin() noexcept
  106. { return iterator(data()); }
  107. _GLIBCXX17_CONSTEXPR const_iterator
  108. begin() const noexcept
  109. { return const_iterator(data()); }
  110. _GLIBCXX17_CONSTEXPR iterator
  111. end() noexcept
  112. { return iterator(data() + _Nm); }
  113. _GLIBCXX17_CONSTEXPR const_iterator
  114. end() const noexcept
  115. { return const_iterator(data() + _Nm); }
  116. _GLIBCXX17_CONSTEXPR reverse_iterator
  117. rbegin() noexcept
  118. { return reverse_iterator(end()); }
  119. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  120. rbegin() const noexcept
  121. { return const_reverse_iterator(end()); }
  122. _GLIBCXX17_CONSTEXPR reverse_iterator
  123. rend() noexcept
  124. { return reverse_iterator(begin()); }
  125. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  126. rend() const noexcept
  127. { return const_reverse_iterator(begin()); }
  128. _GLIBCXX17_CONSTEXPR const_iterator
  129. cbegin() const noexcept
  130. { return const_iterator(data()); }
  131. _GLIBCXX17_CONSTEXPR const_iterator
  132. cend() const noexcept
  133. { return const_iterator(data() + _Nm); }
  134. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  135. crbegin() const noexcept
  136. { return const_reverse_iterator(end()); }
  137. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  138. crend() const noexcept
  139. { return const_reverse_iterator(begin()); }
  140. // Capacity.
  141. constexpr size_type
  142. size() const noexcept { return _Nm; }
  143. constexpr size_type
  144. max_size() const noexcept { return _Nm; }
  145. _GLIBCXX_NODISCARD constexpr bool
  146. empty() const noexcept { return size() == 0; }
  147. // Element access.
  148. _GLIBCXX17_CONSTEXPR reference
  149. operator[](size_type __n) noexcept
  150. {
  151. __glibcxx_requires_subscript(__n);
  152. return _AT_Type::_S_ref(_M_elems, __n);
  153. }
  154. constexpr const_reference
  155. operator[](size_type __n) const noexcept
  156. {
  157. #if __cplusplus >= 201402L
  158. __glibcxx_requires_subscript(__n);
  159. #endif
  160. return _AT_Type::_S_ref(_M_elems, __n);
  161. }
  162. _GLIBCXX17_CONSTEXPR reference
  163. at(size_type __n)
  164. {
  165. if (__n >= _Nm)
  166. std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  167. ">= _Nm (which is %zu)"),
  168. __n, _Nm);
  169. return _AT_Type::_S_ref(_M_elems, __n);
  170. }
  171. constexpr const_reference
  172. at(size_type __n) const
  173. {
  174. // Result of conditional expression must be an lvalue so use
  175. // boolean ? lvalue : (throw-expr, lvalue)
  176. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  177. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  178. ">= _Nm (which is %zu)"),
  179. __n, _Nm),
  180. _AT_Type::_S_ref(_M_elems, 0));
  181. }
  182. _GLIBCXX17_CONSTEXPR reference
  183. front() noexcept
  184. {
  185. __glibcxx_requires_nonempty();
  186. return *begin();
  187. }
  188. constexpr const_reference
  189. front() const noexcept
  190. {
  191. #if __cplusplus >= 201402L
  192. __glibcxx_requires_nonempty();
  193. #endif
  194. return _AT_Type::_S_ref(_M_elems, 0);
  195. }
  196. _GLIBCXX17_CONSTEXPR reference
  197. back() noexcept
  198. {
  199. __glibcxx_requires_nonempty();
  200. return _Nm ? *(end() - 1) : *end();
  201. }
  202. constexpr const_reference
  203. back() const noexcept
  204. {
  205. #if __cplusplus >= 201402L
  206. __glibcxx_requires_nonempty();
  207. #endif
  208. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  209. : _AT_Type::_S_ref(_M_elems, 0);
  210. }
  211. _GLIBCXX17_CONSTEXPR pointer
  212. data() noexcept
  213. { return _AT_Type::_S_ptr(_M_elems); }
  214. _GLIBCXX17_CONSTEXPR const_pointer
  215. data() const noexcept
  216. { return _AT_Type::_S_ptr(_M_elems); }
  217. };
  218. #if __cpp_deduction_guides >= 201606
  219. template<typename _Tp, typename... _Up>
  220. array(_Tp, _Up...)
  221. -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
  222. 1 + sizeof...(_Up)>;
  223. #endif
  224. // Array comparisons.
  225. template<typename _Tp, std::size_t _Nm>
  226. _GLIBCXX20_CONSTEXPR
  227. inline bool
  228. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  229. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  230. #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
  231. template<typename _Tp, size_t _Nm>
  232. constexpr __detail::__synth3way_t<_Tp>
  233. operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  234. {
  235. #ifdef __cpp_lib_is_constant_evaluated
  236. if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
  237. if (!std::is_constant_evaluated())
  238. {
  239. constexpr size_t __n = _Nm * sizeof(_Tp);
  240. return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
  241. }
  242. #endif
  243. for (size_t __i = 0; __i < _Nm; ++__i)
  244. {
  245. auto __c = __detail::__synth3way(__a[__i], __b[__i]);
  246. if (__c != 0)
  247. return __c;
  248. }
  249. return strong_ordering::equal;
  250. }
  251. #else
  252. template<typename _Tp, std::size_t _Nm>
  253. _GLIBCXX20_CONSTEXPR
  254. inline bool
  255. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  256. { return !(__one == __two); }
  257. template<typename _Tp, std::size_t _Nm>
  258. _GLIBCXX20_CONSTEXPR
  259. inline bool
  260. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  261. {
  262. return std::lexicographical_compare(__a.begin(), __a.end(),
  263. __b.begin(), __b.end());
  264. }
  265. template<typename _Tp, std::size_t _Nm>
  266. _GLIBCXX20_CONSTEXPR
  267. inline bool
  268. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  269. { return __two < __one; }
  270. template<typename _Tp, std::size_t _Nm>
  271. _GLIBCXX20_CONSTEXPR
  272. inline bool
  273. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  274. { return !(__one > __two); }
  275. template<typename _Tp, std::size_t _Nm>
  276. _GLIBCXX20_CONSTEXPR
  277. inline bool
  278. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  279. { return !(__one < __two); }
  280. #endif // three_way_comparison && concepts
  281. // Specialized algorithms.
  282. template<typename _Tp, std::size_t _Nm>
  283. _GLIBCXX20_CONSTEXPR
  284. inline
  285. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  286. // Constrained free swap overload, see p0185r1
  287. typename enable_if<
  288. __array_traits<_Tp, _Nm>::_Is_swappable::value
  289. >::type
  290. #else
  291. void
  292. #endif
  293. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  294. noexcept(noexcept(__one.swap(__two)))
  295. { __one.swap(__two); }
  296. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  297. template<typename _Tp, std::size_t _Nm>
  298. typename enable_if<
  299. !__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
  300. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  301. #endif
  302. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  303. constexpr _Tp&
  304. get(array<_Tp, _Nm>& __arr) noexcept
  305. {
  306. static_assert(_Int < _Nm, "array index is within bounds");
  307. return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  308. }
  309. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  310. constexpr _Tp&&
  311. get(array<_Tp, _Nm>&& __arr) noexcept
  312. {
  313. static_assert(_Int < _Nm, "array index is within bounds");
  314. return std::move(std::get<_Int>(__arr));
  315. }
  316. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  317. constexpr const _Tp&
  318. get(const array<_Tp, _Nm>& __arr) noexcept
  319. {
  320. static_assert(_Int < _Nm, "array index is within bounds");
  321. return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  322. }
  323. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  324. constexpr const _Tp&&
  325. get(const array<_Tp, _Nm>&& __arr) noexcept
  326. {
  327. static_assert(_Int < _Nm, "array index is within bounds");
  328. return std::move(std::get<_Int>(__arr));
  329. }
  330. #if __cplusplus > 201703L
  331. #define __cpp_lib_to_array 201907L
  332. template<bool _Move = false, typename _Tp, size_t... _Idx>
  333. constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
  334. __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
  335. {
  336. if constexpr (_Move)
  337. return {{std::move(__a[_Idx])...}};
  338. else
  339. return {{__a[_Idx]...}};
  340. }
  341. template<typename _Tp, size_t _Nm>
  342. constexpr array<remove_cv_t<_Tp>, _Nm>
  343. to_array(_Tp (&__a)[_Nm])
  344. noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
  345. {
  346. static_assert(!is_array_v<_Tp>);
  347. static_assert(is_constructible_v<_Tp, _Tp&>);
  348. if constexpr (is_constructible_v<_Tp, _Tp&>)
  349. return __to_array(__a, make_index_sequence<_Nm>{});
  350. __builtin_unreachable(); // FIXME: see PR c++/91388
  351. }
  352. template<typename _Tp, size_t _Nm>
  353. constexpr array<remove_cv_t<_Tp>, _Nm>
  354. to_array(_Tp (&&__a)[_Nm])
  355. noexcept(is_nothrow_move_constructible_v<_Tp>)
  356. {
  357. static_assert(!is_array_v<_Tp>);
  358. static_assert(is_move_constructible_v<_Tp>);
  359. if constexpr (is_move_constructible_v<_Tp>)
  360. return __to_array<1>(__a, make_index_sequence<_Nm>{});
  361. __builtin_unreachable(); // FIXME: see PR c++/91388
  362. }
  363. #endif // C++20
  364. // Tuple interface to class template array.
  365. /// tuple_size
  366. template<typename _Tp>
  367. struct tuple_size;
  368. /// Partial specialization for std::array
  369. template<typename _Tp, std::size_t _Nm>
  370. struct tuple_size<array<_Tp, _Nm>>
  371. : public integral_constant<std::size_t, _Nm> { };
  372. /// tuple_element
  373. template<std::size_t _Int, typename _Tp>
  374. struct tuple_element;
  375. /// Partial specialization for std::array
  376. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  377. struct tuple_element<_Int, array<_Tp, _Nm>>
  378. {
  379. static_assert(_Int < _Nm, "index is out of bounds");
  380. typedef _Tp type;
  381. };
  382. template<typename _Tp, std::size_t _Nm>
  383. struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
  384. { };
  385. _GLIBCXX_END_NAMESPACE_VERSION
  386. } // namespace std
  387. #endif // C++11
  388. #endif // _GLIBCXX_ARRAY