array 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // -*- C++ -*-
  2. //===---------------------------- array -----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_ARRAY
  11. #define _LIBCPP_ARRAY
  12. /*
  13. array synopsis
  14. namespace std
  15. {
  16. template <class T, size_t N >
  17. struct array
  18. {
  19. // types:
  20. typedef T & reference;
  21. typedef const T & const_reference;
  22. typedef implementation defined iterator;
  23. typedef implementation defined const_iterator;
  24. typedef size_t size_type;
  25. typedef ptrdiff_t difference_type;
  26. typedef T value_type;
  27. typedef T* pointer;
  28. typedef const T* const_pointer;
  29. typedef std::reverse_iterator<iterator> reverse_iterator;
  30. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  31. // No explicit construct/copy/destroy for aggregate type
  32. void fill(const T& u);
  33. void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
  34. // iterators:
  35. iterator begin() noexcept;
  36. const_iterator begin() const noexcept;
  37. iterator end() noexcept;
  38. const_iterator end() const noexcept;
  39. reverse_iterator rbegin() noexcept;
  40. const_reverse_iterator rbegin() const noexcept;
  41. reverse_iterator rend() noexcept;
  42. const_reverse_iterator rend() const noexcept;
  43. const_iterator cbegin() const noexcept;
  44. const_iterator cend() const noexcept;
  45. const_reverse_iterator crbegin() const noexcept;
  46. const_reverse_iterator crend() const noexcept;
  47. // capacity:
  48. constexpr size_type size() const noexcept;
  49. constexpr size_type max_size() const noexcept;
  50. constexpr bool empty() const noexcept;
  51. // element access:
  52. reference operator[](size_type n);
  53. const_reference operator[](size_type n) const; // constexpr in C++14
  54. const_reference at(size_type n) const; // constexpr in C++14
  55. reference at(size_type n);
  56. reference front();
  57. const_reference front() const; // constexpr in C++14
  58. reference back();
  59. const_reference back() const; // constexpr in C++14
  60. T* data() noexcept;
  61. const T* data() const noexcept;
  62. };
  63. template <class T, size_t N>
  64. bool operator==(const array<T,N>& x, const array<T,N>& y);
  65. template <class T, size_t N>
  66. bool operator!=(const array<T,N>& x, const array<T,N>& y);
  67. template <class T, size_t N>
  68. bool operator<(const array<T,N>& x, const array<T,N>& y);
  69. template <class T, size_t N>
  70. bool operator>(const array<T,N>& x, const array<T,N>& y);
  71. template <class T, size_t N>
  72. bool operator<=(const array<T,N>& x, const array<T,N>& y);
  73. template <class T, size_t N>
  74. bool operator>=(const array<T,N>& x, const array<T,N>& y);
  75. template <class T, size_t N >
  76. void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y)));
  77. template <class T> class tuple_size;
  78. template <size_t I, class T> class tuple_element;
  79. template <class T, size_t N> struct tuple_size<array<T, N>>;
  80. template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
  81. template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
  82. template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
  83. template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
  84. template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
  85. } // std
  86. */
  87. #include <__config>
  88. #include <__tuple>
  89. #include <type_traits>
  90. #include <utility>
  91. #include <iterator>
  92. #include <algorithm>
  93. #include <stdexcept>
  94. #if defined(_LIBCPP_NO_EXCEPTIONS)
  95. #include <cassert>
  96. #endif
  97. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  98. #pragma GCC system_header
  99. #endif
  100. _LIBCPP_BEGIN_NAMESPACE_STD
  101. template <class _Tp, size_t _Size>
  102. struct _LIBCPP_TYPE_VIS_ONLY array
  103. {
  104. // types:
  105. typedef array __self;
  106. typedef _Tp value_type;
  107. typedef value_type& reference;
  108. typedef const value_type& const_reference;
  109. typedef value_type* iterator;
  110. typedef const value_type* const_iterator;
  111. typedef value_type* pointer;
  112. typedef const value_type* const_pointer;
  113. typedef size_t size_type;
  114. typedef ptrdiff_t difference_type;
  115. typedef std::reverse_iterator<iterator> reverse_iterator;
  116. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  117. value_type __elems_[_Size > 0 ? _Size : 1];
  118. // No explicit construct/copy/destroy for aggregate type
  119. _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
  120. {_VSTD::fill_n(__elems_, _Size, __u);}
  121. _LIBCPP_INLINE_VISIBILITY
  122. void swap(array& __a) _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value)
  123. { __swap_dispatch((std::integral_constant<bool, _Size == 0>()), __a); }
  124. _LIBCPP_INLINE_VISIBILITY
  125. void __swap_dispatch(std::true_type, array&) {}
  126. _LIBCPP_INLINE_VISIBILITY
  127. void __swap_dispatch(std::false_type, array& __a)
  128. { _VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
  129. // iterators:
  130. _LIBCPP_INLINE_VISIBILITY
  131. iterator begin() _NOEXCEPT {return iterator(__elems_);}
  132. _LIBCPP_INLINE_VISIBILITY
  133. const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);}
  134. _LIBCPP_INLINE_VISIBILITY
  135. iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);}
  136. _LIBCPP_INLINE_VISIBILITY
  137. const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);}
  138. _LIBCPP_INLINE_VISIBILITY
  139. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  140. _LIBCPP_INLINE_VISIBILITY
  141. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  142. _LIBCPP_INLINE_VISIBILITY
  143. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  144. _LIBCPP_INLINE_VISIBILITY
  145. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  146. _LIBCPP_INLINE_VISIBILITY
  147. const_iterator cbegin() const _NOEXCEPT {return begin();}
  148. _LIBCPP_INLINE_VISIBILITY
  149. const_iterator cend() const _NOEXCEPT {return end();}
  150. _LIBCPP_INLINE_VISIBILITY
  151. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  152. _LIBCPP_INLINE_VISIBILITY
  153. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  154. // capacity:
  155. _LIBCPP_INLINE_VISIBILITY
  156. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
  157. _LIBCPP_INLINE_VISIBILITY
  158. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
  159. _LIBCPP_INLINE_VISIBILITY
  160. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
  161. // element access:
  162. _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];}
  163. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference operator[](size_type __n) const {return __elems_[__n];}
  164. reference at(size_type __n);
  165. _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
  166. _LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];}
  167. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
  168. _LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
  169. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
  170. _LIBCPP_INLINE_VISIBILITY
  171. value_type* data() _NOEXCEPT {return __elems_;}
  172. _LIBCPP_INLINE_VISIBILITY
  173. const value_type* data() const _NOEXCEPT {return __elems_;}
  174. };
  175. template <class _Tp, size_t _Size>
  176. typename array<_Tp, _Size>::reference
  177. array<_Tp, _Size>::at(size_type __n)
  178. {
  179. if (__n >= _Size)
  180. #ifndef _LIBCPP_NO_EXCEPTIONS
  181. throw out_of_range("array::at");
  182. #else
  183. assert(!"array::at out_of_range");
  184. #endif
  185. return __elems_[__n];
  186. }
  187. template <class _Tp, size_t _Size>
  188. _LIBCPP_CONSTEXPR_AFTER_CXX11
  189. typename array<_Tp, _Size>::const_reference
  190. array<_Tp, _Size>::at(size_type __n) const
  191. {
  192. if (__n >= _Size)
  193. #ifndef _LIBCPP_NO_EXCEPTIONS
  194. throw out_of_range("array::at");
  195. #else
  196. assert(!"array::at out_of_range");
  197. #endif
  198. return __elems_[__n];
  199. }
  200. template <class _Tp, size_t _Size>
  201. inline _LIBCPP_INLINE_VISIBILITY
  202. bool
  203. operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  204. {
  205. return _VSTD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
  206. }
  207. template <class _Tp, size_t _Size>
  208. inline _LIBCPP_INLINE_VISIBILITY
  209. bool
  210. operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  211. {
  212. return !(__x == __y);
  213. }
  214. template <class _Tp, size_t _Size>
  215. inline _LIBCPP_INLINE_VISIBILITY
  216. bool
  217. operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  218. {
  219. return _VSTD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
  220. }
  221. template <class _Tp, size_t _Size>
  222. inline _LIBCPP_INLINE_VISIBILITY
  223. bool
  224. operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  225. {
  226. return __y < __x;
  227. }
  228. template <class _Tp, size_t _Size>
  229. inline _LIBCPP_INLINE_VISIBILITY
  230. bool
  231. operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  232. {
  233. return !(__y < __x);
  234. }
  235. template <class _Tp, size_t _Size>
  236. inline _LIBCPP_INLINE_VISIBILITY
  237. bool
  238. operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  239. {
  240. return !(__x < __y);
  241. }
  242. template <class _Tp, size_t _Size>
  243. inline _LIBCPP_INLINE_VISIBILITY
  244. typename enable_if
  245. <
  246. _Size == 0 ||
  247. __is_swappable<_Tp>::value,
  248. void
  249. >::type
  250. swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
  251. _NOEXCEPT_(noexcept(__x.swap(__y)))
  252. {
  253. __x.swap(__y);
  254. }
  255. template <class _Tp, size_t _Size>
  256. class _LIBCPP_TYPE_VIS_ONLY tuple_size<array<_Tp, _Size> >
  257. : public integral_constant<size_t, _Size> {};
  258. template <size_t _Ip, class _Tp, size_t _Size>
  259. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, array<_Tp, _Size> >
  260. {
  261. public:
  262. typedef _Tp type;
  263. };
  264. template <size_t _Ip, class _Tp, size_t _Size>
  265. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  266. _Tp&
  267. get(array<_Tp, _Size>& __a) _NOEXCEPT
  268. {
  269. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
  270. return __a.__elems_[_Ip];
  271. }
  272. template <size_t _Ip, class _Tp, size_t _Size>
  273. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  274. const _Tp&
  275. get(const array<_Tp, _Size>& __a) _NOEXCEPT
  276. {
  277. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
  278. return __a.__elems_[_Ip];
  279. }
  280. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  281. template <size_t _Ip, class _Tp, size_t _Size>
  282. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  283. _Tp&&
  284. get(array<_Tp, _Size>&& __a) _NOEXCEPT
  285. {
  286. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
  287. return _VSTD::move(__a.__elems_[_Ip]);
  288. }
  289. template <size_t _Ip, class _Tp, size_t _Size>
  290. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  291. const _Tp&&
  292. get(const array<_Tp, _Size>&& __a) _NOEXCEPT
  293. {
  294. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
  295. return _VSTD::move(__a.__elems_[_Ip]);
  296. }
  297. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  298. _LIBCPP_END_NAMESPACE_STD
  299. #endif // _LIBCPP_ARRAY