array 9.8 KB

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