You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.8KB

  1. // class template array -*- C++ -*-
  2. // Copyright (C) 2004-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 tr1/array
  21. * This is a TR1 C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_TR1_ARRAY
  24. #define _GLIBCXX_TR1_ARRAY 1
  25. #pragma GCC system_header
  26. #include <bits/stl_algobase.h>
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. namespace tr1
  31. {
  32. /**
  33. * @brief A standard container for storing a fixed size sequence of elements.
  34. *
  35. * @ingroup sequences
  36. *
  37. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  38. * <a href="tables.html#66">reversible container</a>, and a
  39. * <a href="tables.html#67">sequence</a>.
  40. *
  41. * Sets support random access iterators.
  42. *
  43. * @param Tp Type of element. Required to be a complete type.
  44. * @param N Number of elements.
  45. */
  46. template<typename _Tp, std::size_t _Nm>
  47. struct array
  48. {
  49. typedef _Tp value_type;
  50. typedef value_type& reference;
  51. typedef const value_type& const_reference;
  52. typedef value_type* iterator;
  53. typedef const value_type* const_iterator;
  54. typedef std::size_t size_type;
  55. typedef std::ptrdiff_t difference_type;
  56. typedef std::reverse_iterator<iterator> reverse_iterator;
  57. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  58. // Support for zero-sized arrays mandatory.
  59. value_type _M_instance[_Nm ? _Nm : 1];
  60. // No explicit construct/copy/destroy for aggregate type.
  61. void
  62. assign(const value_type& __u)
  63. { std::fill_n(begin(), size(), __u); }
  64. void
  65. swap(array& __other)
  66. { std::swap_ranges(begin(), end(), __other.begin()); }
  67. // Iterators.
  68. iterator
  69. begin()
  70. { return iterator(std::__addressof(_M_instance[0])); }
  71. const_iterator
  72. begin() const
  73. { return const_iterator(std::__addressof(_M_instance[0])); }
  74. iterator
  75. end()
  76. { return iterator(std::__addressof(_M_instance[_Nm])); }
  77. const_iterator
  78. end() const
  79. { return const_iterator(std::__addressof(_M_instance[_Nm])); }
  80. reverse_iterator
  81. rbegin()
  82. { return reverse_iterator(end()); }
  83. const_reverse_iterator
  84. rbegin() const
  85. { return const_reverse_iterator(end()); }
  86. reverse_iterator
  87. rend()
  88. { return reverse_iterator(begin()); }
  89. const_reverse_iterator
  90. rend() const
  91. { return const_reverse_iterator(begin()); }
  92. // Capacity.
  93. size_type
  94. size() const { return _Nm; }
  95. size_type
  96. max_size() const { return _Nm; }
  97. _GLIBCXX_NODISCARD bool
  98. empty() const { return size() == 0; }
  99. // Element access.
  100. reference
  101. operator[](size_type __n)
  102. { return _M_instance[__n]; }
  103. const_reference
  104. operator[](size_type __n) const
  105. { return _M_instance[__n]; }
  106. reference
  107. at(size_type __n)
  108. {
  109. if (__n >= _Nm)
  110. std::__throw_out_of_range(__N("array::at"));
  111. return _M_instance[__n];
  112. }
  113. const_reference
  114. at(size_type __n) const
  115. {
  116. if (__n >= _Nm)
  117. std::__throw_out_of_range(__N("array::at"));
  118. return _M_instance[__n];
  119. }
  120. reference
  121. front()
  122. { return *begin(); }
  123. const_reference
  124. front() const
  125. { return *begin(); }
  126. reference
  127. back()
  128. { return _Nm ? *(end() - 1) : *end(); }
  129. const_reference
  130. back() const
  131. { return _Nm ? *(end() - 1) : *end(); }
  132. _Tp*
  133. data()
  134. { return std::__addressof(_M_instance[0]); }
  135. const _Tp*
  136. data() const
  137. { return std::__addressof(_M_instance[0]); }
  138. };
  139. // Array comparisons.
  140. template<typename _Tp, std::size_t _Nm>
  141. inline bool
  142. operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  143. { return std::equal(__one.begin(), __one.end(), __two.begin()); }
  144. template<typename _Tp, std::size_t _Nm>
  145. inline bool
  146. operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  147. { return !(__one == __two); }
  148. template<typename _Tp, std::size_t _Nm>
  149. inline bool
  150. operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
  151. {
  152. return std::lexicographical_compare(__a.begin(), __a.end(),
  153. __b.begin(), __b.end());
  154. }
  155. template<typename _Tp, std::size_t _Nm>
  156. inline bool
  157. operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  158. { return __two < __one; }
  159. template<typename _Tp, std::size_t _Nm>
  160. inline bool
  161. operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  162. { return !(__one > __two); }
  163. template<typename _Tp, std::size_t _Nm>
  164. inline bool
  165. operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
  166. { return !(__one < __two); }
  167. // Specialized algorithms [6.2.2.2].
  168. template<typename _Tp, std::size_t _Nm>
  169. inline void
  170. swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
  171. { __one.swap(__two); }
  172. // Tuple interface to class template array [6.2.2.5].
  173. /// tuple_size
  174. template<typename _Tp>
  175. class tuple_size;
  176. /// tuple_element
  177. template<int _Int, typename _Tp>
  178. class tuple_element;
  179. template<typename _Tp, std::size_t _Nm>
  180. struct tuple_size<array<_Tp, _Nm> >
  181. { static const int value = _Nm; };
  182. template<typename _Tp, std::size_t _Nm>
  183. const int
  184. tuple_size<array<_Tp, _Nm> >::value;
  185. template<int _Int, typename _Tp, std::size_t _Nm>
  186. struct tuple_element<_Int, array<_Tp, _Nm> >
  187. { typedef _Tp type; };
  188. template<int _Int, typename _Tp, std::size_t _Nm>
  189. inline _Tp&
  190. get(array<_Tp, _Nm>& __arr)
  191. { return __arr[_Int]; }
  192. template<int _Int, typename _Tp, std::size_t _Nm>
  193. inline const _Tp&
  194. get(const array<_Tp, _Nm>& __arr)
  195. { return __arr[_Int]; }
  196. }
  197. _GLIBCXX_END_NAMESPACE_VERSION
  198. }
  199. #endif // _GLIBCXX_TR1_ARRAY