array 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Debugging array implementation -*- C++ -*-
  2. // Copyright (C) 2012-2018 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 debug/array
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_ARRAY
  24. #define _GLIBCXX_DEBUG_ARRAY 1
  25. #pragma GCC system_header
  26. #include <array>
  27. #include <debug/formatter.h>
  28. #include <debug/macros.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. namespace __debug
  32. {
  33. template<typename _Tp, std::size_t _Nm>
  34. struct array
  35. {
  36. typedef _Tp value_type;
  37. typedef value_type* pointer;
  38. typedef const value_type* const_pointer;
  39. typedef value_type& reference;
  40. typedef const value_type& const_reference;
  41. typedef value_type* iterator;
  42. typedef const value_type* const_iterator;
  43. typedef std::size_t size_type;
  44. typedef std::ptrdiff_t difference_type;
  45. typedef std::reverse_iterator<iterator> reverse_iterator;
  46. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  47. // Support for zero-sized arrays mandatory.
  48. typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
  49. typename _AT_Type::_Type _M_elems;
  50. template<std::size_t _Size>
  51. struct _Array_check_subscript
  52. {
  53. std::size_t size() { return _Size; }
  54. _Array_check_subscript(std::size_t __index)
  55. { __glibcxx_check_subscript(__index); }
  56. };
  57. template<std::size_t _Size>
  58. struct _Array_check_nonempty
  59. {
  60. bool empty() { return _Size == 0; }
  61. _Array_check_nonempty()
  62. { __glibcxx_check_nonempty(); }
  63. };
  64. // No explicit construct/copy/destroy for aggregate type.
  65. // DR 776.
  66. void
  67. fill(const value_type& __u)
  68. { std::fill_n(begin(), size(), __u); }
  69. void
  70. swap(array& __other)
  71. noexcept(_AT_Type::_Is_nothrow_swappable::value)
  72. { std::swap_ranges(begin(), end(), __other.begin()); }
  73. // Iterators.
  74. _GLIBCXX17_CONSTEXPR iterator
  75. begin() noexcept
  76. { return iterator(data()); }
  77. _GLIBCXX17_CONSTEXPR const_iterator
  78. begin() const noexcept
  79. { return const_iterator(data()); }
  80. _GLIBCXX17_CONSTEXPR iterator
  81. end() noexcept
  82. { return iterator(data() + _Nm); }
  83. _GLIBCXX17_CONSTEXPR const_iterator
  84. end() const noexcept
  85. { return const_iterator(data() + _Nm); }
  86. _GLIBCXX17_CONSTEXPR reverse_iterator
  87. rbegin() noexcept
  88. { return reverse_iterator(end()); }
  89. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  90. rbegin() const noexcept
  91. { return const_reverse_iterator(end()); }
  92. _GLIBCXX17_CONSTEXPR reverse_iterator
  93. rend() noexcept
  94. { return reverse_iterator(begin()); }
  95. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  96. rend() const noexcept
  97. { return const_reverse_iterator(begin()); }
  98. _GLIBCXX17_CONSTEXPR const_iterator
  99. cbegin() const noexcept
  100. { return const_iterator(data()); }
  101. _GLIBCXX17_CONSTEXPR const_iterator
  102. cend() const noexcept
  103. { return const_iterator(data() + _Nm); }
  104. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  105. crbegin() const noexcept
  106. { return const_reverse_iterator(end()); }
  107. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  108. crend() const noexcept
  109. { return const_reverse_iterator(begin()); }
  110. // Capacity.
  111. constexpr size_type
  112. size() const noexcept { return _Nm; }
  113. constexpr size_type
  114. max_size() const noexcept { return _Nm; }
  115. constexpr bool
  116. empty() const noexcept { return size() == 0; }
  117. // Element access.
  118. _GLIBCXX17_CONSTEXPR reference
  119. operator[](size_type __n) noexcept
  120. {
  121. __glibcxx_check_subscript(__n);
  122. return _AT_Type::_S_ref(_M_elems, __n);
  123. }
  124. constexpr const_reference
  125. operator[](size_type __n) const noexcept
  126. {
  127. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  128. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)),
  129. _AT_Type::_S_ref(_M_elems, 0));
  130. }
  131. _GLIBCXX17_CONSTEXPR reference
  132. at(size_type __n)
  133. {
  134. if (__n >= _Nm)
  135. std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  136. ">= _Nm (which is %zu)"),
  137. __n, _Nm);
  138. return _AT_Type::_S_ref(_M_elems, __n);
  139. }
  140. constexpr const_reference
  141. at(size_type __n) const
  142. {
  143. // Result of conditional expression must be an lvalue so use
  144. // boolean ? lvalue : (throw-expr, lvalue)
  145. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  146. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  147. ">= _Nm (which is %zu)"),
  148. __n, _Nm),
  149. _AT_Type::_S_ref(_M_elems, 0));
  150. }
  151. _GLIBCXX17_CONSTEXPR reference
  152. front() noexcept
  153. {
  154. __glibcxx_check_nonempty();
  155. return *begin();
  156. }
  157. constexpr const_reference
  158. front() const noexcept
  159. {
  160. return _Nm ? _AT_Type::_S_ref(_M_elems, 0)
  161. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
  162. _AT_Type::_S_ref(_M_elems, 0));
  163. }
  164. _GLIBCXX17_CONSTEXPR reference
  165. back() noexcept
  166. {
  167. __glibcxx_check_nonempty();
  168. return _Nm ? *(end() - 1) : *end();
  169. }
  170. constexpr const_reference
  171. back() const noexcept
  172. {
  173. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  174. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
  175. _AT_Type::_S_ref(_M_elems, 0));
  176. }
  177. _GLIBCXX17_CONSTEXPR pointer
  178. data() noexcept
  179. { return _AT_Type::_S_ptr(_M_elems); }
  180. _GLIBCXX17_CONSTEXPR const_pointer
  181. data() const noexcept
  182. { return _AT_Type::_S_ptr(_M_elems); }
  183. };
  184. #if __cpp_deduction_guides >= 201606
  185. template<typename _Tp, typename... _Up>
  186. array(_Tp, _Up...)
  187. -> array<std::enable_if_t<(std::is_same_v<_Tp, _Up> && ...), _Tp>,
  188. 1 + sizeof...(_Up)>;
  189. #endif
  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. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  220. template<typename _Tp, size_t _Nm>
  221. typename enable_if<
  222. !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
  223. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  224. #endif
  225. template<typename _Tp, std::size_t _Nm>
  226. inline void
  227. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  228. noexcept(noexcept(__one.swap(__two)))
  229. { __one.swap(__two); }
  230. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  231. constexpr _Tp&
  232. get(array<_Tp, _Nm>& __arr) noexcept
  233. {
  234. static_assert(_Int < _Nm, "index is out of bounds");
  235. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  236. _S_ref(__arr._M_elems, _Int);
  237. }
  238. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  239. constexpr _Tp&&
  240. get(array<_Tp, _Nm>&& __arr) noexcept
  241. {
  242. static_assert(_Int < _Nm, "index is out of bounds");
  243. return std::move(__debug::get<_Int>(__arr));
  244. }
  245. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  246. constexpr const _Tp&
  247. get(const array<_Tp, _Nm>& __arr) noexcept
  248. {
  249. static_assert(_Int < _Nm, "index is out of bounds");
  250. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  251. _S_ref(__arr._M_elems, _Int);
  252. }
  253. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  254. constexpr const _Tp&&
  255. get(const array<_Tp, _Nm>&& __arr) noexcept
  256. {
  257. static_assert(_Int < _Nm, "index is out of bounds");
  258. return std::move(__debug::get<_Int>(__arr));
  259. }
  260. } // namespace __debug
  261. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  262. // Tuple interface to class template array.
  263. /// tuple_size
  264. template<typename _Tp, std::size_t _Nm>
  265. struct tuple_size<std::__debug::array<_Tp, _Nm>>
  266. : public integral_constant<std::size_t, _Nm> { };
  267. /// tuple_element
  268. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  269. struct tuple_element<_Int, std::__debug::array<_Tp, _Nm>>
  270. {
  271. static_assert(_Int < _Nm, "index is out of bounds");
  272. typedef _Tp type;
  273. };
  274. template<typename _Tp, std::size_t _Nm>
  275. struct __is_tuple_like_impl<std::__debug::array<_Tp, _Nm>> : true_type
  276. { };
  277. _GLIBCXX_END_NAMESPACE_VERSION
  278. } // namespace std
  279. #endif // _GLIBCXX_DEBUG_ARRAY