Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

409 lines
12KB

  1. // Debugging array implementation -*- C++ -*-
  2. // Copyright (C) 2012-2020 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. _GLIBCXX_NODISCARD 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. _GLIBCXX20_CONSTEXPR void
  67. fill(const value_type& __u)
  68. { std::fill_n(begin(), size(), __u); }
  69. _GLIBCXX20_CONSTEXPR 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. _GLIBCXX_NODISCARD 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. _GLIBCXX20_CONSTEXPR
  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. #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
  197. template<typename _Tp, size_t _Nm>
  198. constexpr __detail::__synth3way_t<_Tp>
  199. operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  200. {
  201. if constexpr (_Nm && __is_byte<_Tp>::__value)
  202. return __builtin_memcmp(__a.data(), __b.data(), _Nm) <=> 0;
  203. else
  204. {
  205. for (size_t __i = 0; __i < _Nm; ++__i)
  206. {
  207. auto __c = __detail::__synth3way(__a[__i], __b[__i]);
  208. if (__c != 0)
  209. return __c;
  210. }
  211. }
  212. return strong_ordering::equal;
  213. }
  214. #else
  215. template<typename _Tp, std::size_t _Nm>
  216. _GLIBCXX20_CONSTEXPR
  217. inline bool
  218. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  219. { return !(__one == __two); }
  220. template<typename _Tp, std::size_t _Nm>
  221. _GLIBCXX20_CONSTEXPR
  222. inline bool
  223. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  224. {
  225. return std::lexicographical_compare(__a.begin(), __a.end(),
  226. __b.begin(), __b.end());
  227. }
  228. template<typename _Tp, std::size_t _Nm>
  229. _GLIBCXX20_CONSTEXPR
  230. inline bool
  231. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  232. { return __two < __one; }
  233. template<typename _Tp, std::size_t _Nm>
  234. _GLIBCXX20_CONSTEXPR
  235. inline bool
  236. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  237. { return !(__one > __two); }
  238. template<typename _Tp, std::size_t _Nm>
  239. _GLIBCXX20_CONSTEXPR
  240. inline bool
  241. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  242. { return !(__one < __two); }
  243. #endif // three_way_comparison && concepts
  244. // Specialized algorithms.
  245. #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
  246. template<typename _Tp, size_t _Nm>
  247. typename enable_if<
  248. !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
  249. swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
  250. #endif
  251. template<typename _Tp, std::size_t _Nm>
  252. _GLIBCXX20_CONSTEXPR
  253. inline void
  254. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  255. noexcept(noexcept(__one.swap(__two)))
  256. { __one.swap(__two); }
  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, "index is out of bounds");
  262. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  263. _S_ref(__arr._M_elems, _Int);
  264. }
  265. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  266. constexpr _Tp&&
  267. get(array<_Tp, _Nm>&& __arr) noexcept
  268. {
  269. static_assert(_Int < _Nm, "index is out of bounds");
  270. return std::move(__debug::get<_Int>(__arr));
  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, "index is out of bounds");
  277. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  278. _S_ref(__arr._M_elems, _Int);
  279. }
  280. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  281. constexpr const _Tp&&
  282. get(const array<_Tp, _Nm>&& __arr) noexcept
  283. {
  284. static_assert(_Int < _Nm, "index is out of bounds");
  285. return std::move(__debug::get<_Int>(__arr));
  286. }
  287. #if __cplusplus > 201703L
  288. #define __cpp_lib_to_array 201907L
  289. template<bool _Move = false, typename _Tp, size_t... _Idx>
  290. constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
  291. __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
  292. {
  293. if constexpr (_Move)
  294. return {{std::move(__a[_Idx])...}};
  295. else
  296. return {{__a[_Idx]...}};
  297. }
  298. template<typename _Tp, size_t _Nm>
  299. constexpr array<remove_cv_t<_Tp>, _Nm>
  300. to_array(_Tp (&__a)[_Nm])
  301. noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
  302. {
  303. static_assert(!is_array_v<_Tp>);
  304. static_assert(is_constructible_v<_Tp, _Tp&>);
  305. if constexpr (is_constructible_v<_Tp, _Tp&>)
  306. return __debug::__to_array(__a, make_index_sequence<_Nm>{});
  307. __builtin_unreachable(); // FIXME: see PR c++/91388
  308. }
  309. template<typename _Tp, size_t _Nm>
  310. constexpr array<remove_cv_t<_Tp>, _Nm>
  311. to_array(_Tp (&&__a)[_Nm])
  312. noexcept(is_nothrow_move_constructible_v<_Tp>)
  313. {
  314. static_assert(!is_array_v<_Tp>);
  315. static_assert(is_move_constructible_v<_Tp>);
  316. if constexpr (is_move_constructible_v<_Tp>)
  317. return __debug::__to_array<1>(__a, make_index_sequence<_Nm>{});
  318. __builtin_unreachable(); // FIXME: see PR c++/91388
  319. }
  320. #endif // C++20
  321. } // namespace __debug
  322. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  323. // Tuple interface to class template array.
  324. /// tuple_size
  325. template<typename _Tp, std::size_t _Nm>
  326. struct tuple_size<std::__debug::array<_Tp, _Nm>>
  327. : public integral_constant<std::size_t, _Nm> { };
  328. /// tuple_element
  329. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  330. struct tuple_element<_Int, std::__debug::array<_Tp, _Nm>>
  331. {
  332. static_assert(_Int < _Nm, "index is out of bounds");
  333. typedef _Tp type;
  334. };
  335. template<typename _Tp, std::size_t _Nm>
  336. struct __is_tuple_like_impl<std::__debug::array<_Tp, _Nm>> : true_type
  337. { };
  338. _GLIBCXX_END_NAMESPACE_VERSION
  339. } // namespace std
  340. #endif // _GLIBCXX_DEBUG_ARRAY