array 9.7 KB

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