array 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Profile 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 profile/array
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_ARRAY
  24. #define _GLIBCXX_PROFILE_ARRAY 1
  25. #pragma GCC system_header
  26. #include <array>
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. namespace __profile
  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. // No explicit construct/copy/destroy for aggregate type.
  49. // DR 776.
  50. void
  51. fill(const value_type& __u)
  52. { std::fill_n(begin(), size(), __u); }
  53. void
  54. swap(array& __other)
  55. noexcept(_AT_Type::_Is_nothrow_swappable::value)
  56. { std::swap_ranges(begin(), end(), __other.begin()); }
  57. // Iterators.
  58. _GLIBCXX17_CONSTEXPR iterator
  59. begin() noexcept
  60. { return iterator(data()); }
  61. _GLIBCXX17_CONSTEXPR const_iterator
  62. begin() const noexcept
  63. { return const_iterator(data()); }
  64. _GLIBCXX17_CONSTEXPR iterator
  65. end() noexcept
  66. { return iterator(data() + _Nm); }
  67. _GLIBCXX17_CONSTEXPR const_iterator
  68. end() const noexcept
  69. { return const_iterator(data() + _Nm); }
  70. _GLIBCXX17_CONSTEXPR reverse_iterator
  71. rbegin() noexcept
  72. { return reverse_iterator(end()); }
  73. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  74. rbegin() const noexcept
  75. { return const_reverse_iterator(end()); }
  76. _GLIBCXX17_CONSTEXPR reverse_iterator
  77. rend() noexcept
  78. { return reverse_iterator(begin()); }
  79. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  80. rend() const noexcept
  81. { return const_reverse_iterator(begin()); }
  82. _GLIBCXX17_CONSTEXPR const_iterator
  83. cbegin() const noexcept
  84. { return const_iterator(data()); }
  85. _GLIBCXX17_CONSTEXPR const_iterator
  86. cend() const noexcept
  87. { return const_iterator(data() + _Nm); }
  88. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  89. crbegin() const noexcept
  90. { return const_reverse_iterator(end()); }
  91. _GLIBCXX17_CONSTEXPR const_reverse_iterator
  92. crend() const noexcept
  93. { return const_reverse_iterator(begin()); }
  94. // Capacity.
  95. constexpr size_type
  96. size() const noexcept { return _Nm; }
  97. constexpr size_type
  98. max_size() const noexcept { return _Nm; }
  99. constexpr bool
  100. empty() const noexcept { return size() == 0; }
  101. // Element access.
  102. reference
  103. operator[](size_type __n) noexcept
  104. { return _AT_Type::_S_ref(_M_elems, __n); }
  105. constexpr const_reference
  106. operator[](size_type __n) const noexcept
  107. { return _AT_Type::_S_ref(_M_elems, __n); }
  108. _GLIBCXX17_CONSTEXPR reference
  109. at(size_type __n)
  110. {
  111. if (__n >= _Nm)
  112. std::__throw_out_of_range_fmt(__N("array::at: __n "
  113. "(which is %zu) >= _Nm "
  114. "(which is %zu)"),
  115. __n, _Nm);
  116. return _AT_Type::_S_ref(_M_elems, __n);
  117. }
  118. constexpr const_reference
  119. at(size_type __n) const
  120. {
  121. // Result of conditional expression must be an lvalue so use
  122. // boolean ? lvalue : (throw-expr, lvalue)
  123. return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
  124. : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
  125. ">= _Nm (which is %zu)"),
  126. __n, _Nm),
  127. _AT_Type::_S_ref(_M_elems, 0));
  128. }
  129. _GLIBCXX17_CONSTEXPR reference
  130. front() noexcept
  131. { return *begin(); }
  132. constexpr const_reference
  133. front() const noexcept
  134. { return _AT_Type::_S_ref(_M_elems, 0); }
  135. _GLIBCXX17_CONSTEXPR reference
  136. back() noexcept
  137. { return _Nm ? *(end() - 1) : *end(); }
  138. constexpr const_reference
  139. back() const noexcept
  140. {
  141. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  142. : _AT_Type::_S_ref(_M_elems, 0);
  143. }
  144. _GLIBCXX17_CONSTEXPR pointer
  145. data() noexcept
  146. { return _AT_Type::_S_ptr(_M_elems); }
  147. _GLIBCXX17_CONSTEXPR const_pointer
  148. data() const noexcept
  149. { return _AT_Type::_S_ptr(_M_elems); }
  150. };
  151. // Array comparisons.
  152. template<typename _Tp, std::size_t _Nm>
  153. inline bool
  154. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  155. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  156. template<typename _Tp, std::size_t _Nm>
  157. inline bool
  158. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  159. { return !(__one == __two); }
  160. template<typename _Tp, std::size_t _Nm>
  161. inline bool
  162. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  163. {
  164. return std::lexicographical_compare(__a.begin(), __a.end(),
  165. __b.begin(), __b.end());
  166. }
  167. template<typename _Tp, std::size_t _Nm>
  168. inline bool
  169. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  170. { return __two < __one; }
  171. template<typename _Tp, std::size_t _Nm>
  172. inline bool
  173. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  174. { return !(__one > __two); }
  175. template<typename _Tp, std::size_t _Nm>
  176. inline bool
  177. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  178. { return !(__one < __two); }
  179. // Specialized algorithms.
  180. template<typename _Tp, std::size_t _Nm>
  181. inline void
  182. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  183. noexcept(noexcept(__one.swap(__two)))
  184. { __one.swap(__two); }
  185. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  186. constexpr _Tp&
  187. get(array<_Tp, _Nm>& __arr) noexcept
  188. {
  189. static_assert(_Int < _Nm, "index is out of bounds");
  190. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  191. _S_ref(__arr._M_elems, _Int);
  192. }
  193. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  194. constexpr _Tp&&
  195. get(array<_Tp, _Nm>&& __arr) noexcept
  196. {
  197. static_assert(_Int < _Nm, "index is out of bounds");
  198. return std::move(__profile::get<_Int>(__arr));
  199. }
  200. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  201. constexpr const _Tp&
  202. get(const array<_Tp, _Nm>& __arr) noexcept
  203. {
  204. static_assert(_Int < _Nm, "index is out of bounds");
  205. return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
  206. _S_ref(__arr._M_elems, _Int);
  207. }
  208. } // namespace __profile
  209. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  210. // Tuple interface to class template array.
  211. /// tuple_size
  212. template<typename _Tp, std::size_t _Nm>
  213. struct tuple_size<std::__profile::array<_Tp, _Nm>>
  214. : public integral_constant<std::size_t, _Nm> { };
  215. /// tuple_element
  216. template<std::size_t _Int, typename _Tp, std::size_t _Nm>
  217. struct tuple_element<_Int, std::__profile::array<_Tp, _Nm>>
  218. {
  219. static_assert(_Int < _Nm, "index is out of bounds");
  220. typedef _Tp type;
  221. };
  222. template<typename _Tp, std::size_t _Nm>
  223. struct __is_tuple_like_impl<std::__profile::array<_Tp, _Nm>> : true_type
  224. { };
  225. _GLIBCXX_END_NAMESPACE_VERSION
  226. } // namespace std
  227. #endif // _GLIBCXX_PROFILE_ARRAY