array 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // <array> -*- C++ -*-
  2. // Copyright (C) 2007-2019 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 <stdexcept>
  31. #include <bits/stl_algobase.h>
  32. #include <bits/range_access.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  36. template<typename _Tp, std::size_t _Nm>
  37. struct __array_traits
  38. {
  39. typedef _Tp _Type[_Nm];
  40. typedef __is_swappable<_Tp> _Is_swappable;
  41. typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
  42. static constexpr _Tp&
  43. _S_ref(const _Type& __t, std::size_t __n) noexcept
  44. { return const_cast<_Tp&>(__t[__n]); }
  45. static constexpr _Tp*
  46. _S_ptr(const _Type& __t) noexcept
  47. { return const_cast<_Tp*>(__t); }
  48. };
  49. template<typename _Tp>
  50. struct __array_traits<_Tp, 0>
  51. {
  52. struct _Type { };
  53. typedef true_type _Is_swappable;
  54. typedef true_type _Is_nothrow_swappable;
  55. static constexpr _Tp&
  56. _S_ref(const _Type&, std::size_t) noexcept
  57. { return *static_cast<_Tp*>(nullptr); }
  58. static constexpr _Tp*
  59. _S_ptr(const _Type&) noexcept
  60. { return nullptr; }
  61. };
  62. /**
  63. * @brief A standard container for storing a fixed size sequence of elements.
  64. *
  65. * @ingroup sequences
  66. *
  67. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  68. * <a href="tables.html#66">reversible container</a>, and a
  69. * <a href="tables.html#67">sequence</a>.
  70. *
  71. * Sets support random access iterators.
  72. *
  73. * @tparam Tp Type of element. Required to be a complete type.
  74. * @tparam N Number of elements.
  75. */
  76. template<typename _Tp, std::size_t _Nm>
  77. struct array
  78. {
  79. typedef _Tp value_type;
  80. typedef value_type* pointer;
  81. typedef const value_type* const_pointer;
  82. typedef value_type& reference;
  83. typedef const value_type& const_reference;
  84. typedef value_type* iterator;
  85. typedef const value_type* const_iterator;
  86. typedef std::size_t size_type;
  87. typedef std::ptrdiff_t difference_type;
  88. typedef std::reverse_iterator<iterator> reverse_iterator;
  89. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  90. // Support for zero-sized arrays mandatory.
  91. typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
  92. typename _AT_Type::_Type _M_elems;
  93. // No explicit construct/copy/destroy for aggregate type.
  94. // DR 776.
  95. void
  96. fill(const value_type& __u)
  97. { std::fill_n(begin(), size(), __u); }
  98. void
  99. swap(array& __other)
  100. noexcept(_AT_Type::_Is_nothrow_swappable::value)
  101. { std::swap_ranges(begin(), end(), __other.begin()); }
  102. // Iterators.
  103. _GLIBCXX17_CONSTEXPR iterator
  104. begin() noexcept
  105. { return iterator(data()); }
  106. _GLIBCXX17_CONSTEXPR const_iterator
  107. begin() const noexcept
  108. { return const_iterator(data()); }
  109. _GLIBCXX17_CONSTEXPR iterator
  110. end() noexcept
  111. { return iterator(data() + _Nm); }
  112. _GLIBCXX17_CONSTEXPR const_iterator
  113. end() const noexcept
  114. { return const_iterator(data() + _Nm); }
  115. _GLIBCXX17_CONSTEXPR reverse_iterator
  116. rbegin() noexcept
  117. { return reverse_iterator(end()); }
  118. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  119. rbegin() const noexcept
  120. { return const_reverse_iterator(end()); }
  121. _GLIBCXX17_CONSTEXPR reverse_iterator
  122. rend() noexcept
  123. { return reverse_iterator(begin()); }
  124. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  125. rend() const noexcept
  126. { return const_reverse_iterator(begin()); }
  127. _GLIBCXX17_CONSTEXPR const_iterator
  128. cbegin() const noexcept
  129. { return const_iterator(data()); }
  130. _GLIBCXX17_CONSTEXPR const_iterator
  131. cend() const noexcept
  132. { return const_iterator(data() + _Nm); }
  133. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  134. crbegin() const noexcept
  135. { return const_reverse_iterator(end()); }
  136. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  137. crend() const noexcept
  138. { return const_reverse_iterator(begin()); }
  139. // Capacity.
  140. constexpr size_type
  141. size() const noexcept { return _Nm; }
  142. constexpr size_type
  143. max_size() const noexcept { return _Nm; }
  144. _GLIBCXX_NODISCARD constexpr bool
  145. empty() const noexcept { return size() == 0; }
  146. // Element access.
  147. _GLIBCXX17_CONSTEXPR reference
  148. operator[](size_type __n) noexcept
  149. { return _AT_Type::_S_ref(_M_elems, __n); }
  150. constexpr const_reference
  151. operator[](size_type __n) const noexcept
  152. { return _AT_Type::_S_ref(_M_elems, __n); }
  153. _GLIBCXX17_CONSTEXPR reference
  154. at(size_type __n)
  155. {
  156. if (__n >= _Nm)
  157. std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  158. ">= _Nm (which is %zu)"),
  159. __n, _Nm);
  160. return _AT_Type::_S_ref(_M_elems, __n);
  161. }
  162. constexpr const_reference
  163. at(size_type __n) const
  164. {
  165. // Result of conditional expression must be an lvalue so use
  166. // boolean ? lvalue : (throw-expr, lvalue)
  167. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  168. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  169. ">= _Nm (which is %zu)"),
  170. __n, _Nm),
  171. _AT_Type::_S_ref(_M_elems, 0));
  172. }
  173. _GLIBCXX17_CONSTEXPR reference
  174. front() noexcept
  175. { return *begin(); }
  176. constexpr const_reference
  177. front() const noexcept
  178. { return _AT_Type::_S_ref(_M_elems, 0); }
  179. _GLIBCXX17_CONSTEXPR reference
  180. back() noexcept
  181. { return _Nm ? *(end() - 1) : *end(); }
  182. constexpr const_reference
  183. back() const noexcept
  184. {
  185. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  186. : _AT_Type::_S_ref(_M_elems, 0);
  187. }
  188. _GLIBCXX17_CONSTEXPR pointer
  189. data() noexcept
  190. { return _AT_Type::_S_ptr(_M_elems); }
  191. _GLIBCXX17_CONSTEXPR const_pointer
  192. data() const noexcept
  193. { return _AT_Type::_S_ptr(_M_elems); }
  194. };
  195. #if __cpp_deduction_guides >= 201606
  196. template<typename _Tp, typename... _Up>
  197. array(_Tp, _Up...)
  198. -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
  199. 1 + sizeof...(_Up)>;
  200. #endif
  201. // Array comparisons.
  202. template<typename _Tp, std::size_t _Nm>
  203. inline bool
  204. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  205. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  206. template<typename _Tp, std::size_t _Nm>
  207. inline bool
  208. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  209. { return !(__one == __two); }
  210. template<typename _Tp, std::size_t _Nm>
  211. inline bool
  212. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  213. {
  214. return std::lexicographical_compare(__a.begin(), __a.end(),
  215. __b.begin(), __b.end());
  216. }
  217. template<typename _Tp, std::size_t _Nm>
  218. inline bool
  219. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  220. { return __two < __one; }
  221. template<typename _Tp, std::size_t _Nm>
  222. inline bool
  223. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  224. { return !(__one > __two); }
  225. template<typename _Tp, std::size_t _Nm>
  226. inline bool
  227. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  228. { return !(__one < __two); }
  229. // Specialized algorithms.
  230. template<typename _Tp, std::size_t _Nm>
  231. inline
  232. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  233. // Constrained free swap overload, see p0185r1
  234. typename enable_if<
  235. _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value
  236. >::type
  237. #else
  238. void
  239. #endif
  240. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  241. noexcept(noexcept(__one.swap(__two)))
  242. { __one.swap(__two); }
  243. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  244. template<typename _Tp, std::size_t _Nm>
  245. typename enable_if<
  246. !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
  247. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  248. #endif
  249. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  250. constexpr _Tp&
  251. get(array<_Tp, _Nm>& __arr) noexcept
  252. {
  253. static_assert(_Int < _Nm, "array index is within bounds");
  254. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  255. _S_ref(__arr._M_elems, _Int);
  256. }
  257. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  258. constexpr _Tp&&
  259. get(array<_Tp, _Nm>&& __arr) noexcept
  260. {
  261. static_assert(_Int < _Nm, "array index is within bounds");
  262. return std::move(_GLIBCXX_STD_C::get<_Int>(__arr));
  263. }
  264. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  265. constexpr const _Tp&
  266. get(const array<_Tp, _Nm>& __arr) noexcept
  267. {
  268. static_assert(_Int < _Nm, "array index is within bounds");
  269. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  270. _S_ref(__arr._M_elems, _Int);
  271. }
  272. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  273. constexpr const _Tp&&
  274. get(const array<_Tp, _Nm>&& __arr) noexcept
  275. {
  276. static_assert(_Int < _Nm, "array index is within bounds");
  277. return std::move(_GLIBCXX_STD_C::get<_Int>(__arr));
  278. }
  279. _GLIBCXX_END_NAMESPACE_CONTAINER
  280. } // namespace std
  281. namespace std _GLIBCXX_VISIBILITY(default)
  282. {
  283. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  284. // Tuple interface to class template array.
  285. /// tuple_size
  286. template<typename _Tp>
  287. struct tuple_size;
  288. /// Partial specialization for std::array
  289. template<typename _Tp, std::size_t _Nm>
  290. struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
  291. : public integral_constant<std::size_t, _Nm> { };
  292. /// tuple_element
  293. template<std::size_t _Int, typename _Tp>
  294. struct tuple_element;
  295. /// Partial specialization for std::array
  296. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  297. struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
  298. {
  299. static_assert(_Int < _Nm, "index is out of bounds");
  300. typedef _Tp type;
  301. };
  302. template<typename _Tp, std::size_t _Nm>
  303. struct __is_tuple_like_impl<_GLIBCXX_STD_C::array<_Tp, _Nm>> : true_type
  304. { };
  305. _GLIBCXX_END_NAMESPACE_VERSION
  306. } // namespace std
  307. #ifdef _GLIBCXX_DEBUG
  308. # include <debug/array>
  309. #endif
  310. #ifdef _GLIBCXX_PROFILE
  311. # include <profile/array>
  312. #endif
  313. #endif // C++11
  314. #endif // _GLIBCXX_ARRAY