array 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Debugging array implementation -*- C++ -*-
  2. // Copyright (C) 2012-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 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 <debug/safe_sequence.h>
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. namespace __debug
  30. {
  31. template<typename _Tp, std::size_t _Nm>
  32. struct array
  33. {
  34. typedef _Tp value_type;
  35. typedef value_type* pointer;
  36. typedef const value_type* const_pointer;
  37. typedef value_type& reference;
  38. typedef const value_type& const_reference;
  39. typedef value_type* iterator;
  40. typedef const value_type* const_iterator;
  41. typedef std::size_t size_type;
  42. typedef std::ptrdiff_t difference_type;
  43. typedef std::reverse_iterator<iterator> reverse_iterator;
  44. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  45. // Support for zero-sized arrays mandatory.
  46. typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
  47. typename _AT_Type::_Type _M_elems;
  48. template<std::size_t _Size>
  49. struct _Array_check_subscript
  50. {
  51. std::size_t size() { return _Size; }
  52. _Array_check_subscript(std::size_t __index)
  53. { __glibcxx_check_subscript(__index); }
  54. };
  55. template<std::size_t _Size>
  56. struct _Array_check_nonempty
  57. {
  58. bool empty() { return _Size == 0; }
  59. _Array_check_nonempty()
  60. { __glibcxx_check_nonempty(); }
  61. };
  62. // No explicit construct/copy/destroy for aggregate type.
  63. // DR 776.
  64. void
  65. fill(const value_type& __u)
  66. { std::fill_n(begin(), size(), __u); }
  67. void
  68. swap(array& __other)
  69. noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
  70. { std::swap_ranges(begin(), end(), __other.begin()); }
  71. // Iterators.
  72. iterator
  73. begin() noexcept
  74. { return iterator(data()); }
  75. const_iterator
  76. begin() const noexcept
  77. { return const_iterator(data()); }
  78. iterator
  79. end() noexcept
  80. { return iterator(data() + _Nm); }
  81. const_iterator
  82. end() const noexcept
  83. { return const_iterator(data() + _Nm); }
  84. reverse_iterator
  85. rbegin() noexcept
  86. { return reverse_iterator(end()); }
  87. const_reverse_iterator
  88. rbegin() const noexcept
  89. { return const_reverse_iterator(end()); }
  90. reverse_iterator
  91. rend() noexcept
  92. { return reverse_iterator(begin()); }
  93. const_reverse_iterator
  94. rend() const noexcept
  95. { return const_reverse_iterator(begin()); }
  96. const_iterator
  97. cbegin() const noexcept
  98. { return const_iterator(data()); }
  99. const_iterator
  100. cend() const noexcept
  101. { return const_iterator(data() + _Nm); }
  102. const_reverse_iterator
  103. crbegin() const noexcept
  104. { return const_reverse_iterator(end()); }
  105. const_reverse_iterator
  106. crend() const noexcept
  107. { return const_reverse_iterator(begin()); }
  108. // Capacity.
  109. constexpr size_type
  110. size() const noexcept { return _Nm; }
  111. constexpr size_type
  112. max_size() const noexcept { return _Nm; }
  113. constexpr bool
  114. empty() const noexcept { return size() == 0; }
  115. // Element access.
  116. reference
  117. operator[](size_type __n) noexcept
  118. {
  119. __glibcxx_check_subscript(__n);
  120. return _AT_Type::_S_ref(_M_elems, __n);
  121. }
  122. constexpr const_reference
  123. operator[](size_type __n) const noexcept
  124. {
  125. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  126. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)),
  127. _AT_Type::_S_ref(_M_elems, 0));
  128. }
  129. reference
  130. at(size_type __n)
  131. {
  132. if (__n >= _Nm)
  133. std::__throw_out_of_range_fmt(__N("array::at: __n "
  134. "(which is %zu) >= _Nm "
  135. "(which is %zu)"),
  136. __n, _Nm);
  137. return _AT_Type::_S_ref(_M_elems, __n);
  138. }
  139. constexpr const_reference
  140. at(size_type __n) const
  141. {
  142. // Result of conditional expression must be an lvalue so use
  143. // boolean ? lvalue : (throw-expr, lvalue)
  144. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  145. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  146. ">= _Nm (which is %zu)"),
  147. __n, _Nm),
  148. _AT_Type::_S_ref(_M_elems, 0));
  149. }
  150. reference
  151. front() noexcept
  152. {
  153. __glibcxx_check_nonempty();
  154. return *begin();
  155. }
  156. constexpr const_reference
  157. front() const noexcept
  158. {
  159. return _Nm ? _AT_Type::_S_ref(_M_elems, 0)
  160. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
  161. _AT_Type::_S_ref(_M_elems, 0));
  162. }
  163. reference
  164. back() noexcept
  165. {
  166. __glibcxx_check_nonempty();
  167. return _Nm ? *(end() - 1) : *end();
  168. }
  169. constexpr const_reference
  170. back() const noexcept
  171. {
  172. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  173. : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
  174. _AT_Type::_S_ref(_M_elems, 0));
  175. }
  176. pointer
  177. data() noexcept
  178. { return _AT_Type::_S_ptr(_M_elems); }
  179. const_pointer
  180. data() const noexcept
  181. { return _AT_Type::_S_ptr(_M_elems); }
  182. };
  183. // Array comparisons.
  184. template<typename _Tp, std::size_t _Nm>
  185. inline bool
  186. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  187. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  188. template<typename _Tp, std::size_t _Nm>
  189. inline bool
  190. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  191. { return !(__one == __two); }
  192. template<typename _Tp, std::size_t _Nm>
  193. inline bool
  194. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  195. {
  196. return std::lexicographical_compare(__a.begin(), __a.end(),
  197. __b.begin(), __b.end());
  198. }
  199. template<typename _Tp, std::size_t _Nm>
  200. inline bool
  201. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  202. { return __two < __one; }
  203. template<typename _Tp, std::size_t _Nm>
  204. inline bool
  205. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  206. { return !(__one > __two); }
  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 !(__one < __two); }
  211. // Specialized algorithms.
  212. template<typename _Tp, std::size_t _Nm>
  213. inline void
  214. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  215. noexcept(noexcept(__one.swap(__two)))
  216. { __one.swap(__two); }
  217. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  218. constexpr _Tp&
  219. get(array<_Tp, _Nm>& __arr) noexcept
  220. {
  221. static_assert(_Int < _Nm, "index is out of bounds");
  222. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  223. _S_ref(__arr._M_elems, _Int);
  224. }
  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 std::move(__debug::get<_Int>(__arr));
  231. }
  232. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  233. constexpr const _Tp&
  234. get(const array<_Tp, _Nm>& __arr) noexcept
  235. {
  236. static_assert(_Int < _Nm, "index is out of bounds");
  237. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  238. _S_ref(__arr._M_elems, _Int);
  239. }
  240. } // namespace __debug
  241. // Tuple interface to class template array.
  242. /// tuple_size
  243. template<typename _Tp, std::size_t _Nm>
  244. struct tuple_size<__debug::array<_Tp, _Nm>>
  245. : public integral_constant<std::size_t, _Nm> { };
  246. /// tuple_element
  247. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  248. struct tuple_element<_Int, __debug::array<_Tp, _Nm>>
  249. {
  250. static_assert(_Int < _Nm, "index is out of bounds");
  251. typedef _Tp type;
  252. };
  253. } // namespace std
  254. #endif // _GLIBCXX_DEBUG_ARRAY