Array.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * This file is based on the std::array implementation of libstdc++ at
  3. * https://gcc.gnu.org/onlinedocs/gcc-7.1.0/libstdc++/api/a01056_source.html
  4. *
  5. * Changes:
  6. * - isolate, i.e. remove dependencies on internal libstdc++ stuff
  7. * - use c++17 behavior even in c++11 or c++14
  8. * - remove std::swappable special case because that doesn't work with MSVC
  9. * - constexpr more things
  10. * - add some features like prepend/tail
  11. *
  12. * If using std::array at runtime, feel free to either keep using std::array or
  13. * use this one - it doesn't really matter. For compile time computations, this
  14. * one here is preferred because std::array in C++11 misses some constexpr
  15. * specifiers, forcing these methods to be called at runtime instead of compile
  16. * time.
  17. */
  18. // Copyright (C) 2007-2017 Free Software Foundation, Inc.
  19. //
  20. // This file is part of the GNU ISO C++ Library. This library is free
  21. // software; you can redistribute it and/or modify it under the
  22. // terms of the GNU General Public License as published by the
  23. // Free Software Foundation; either version 3, or (at your option)
  24. // any later version.
  25. // This library is distributed in the hope that it will be useful,
  26. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. // GNU General Public License for more details.
  29. // Under Section 7 of GPL version 3, you are granted additional
  30. // permissions described in the GCC Runtime Library Exception, version
  31. // 3.1, as published by the Free Software Foundation.
  32. // You should have received a copy of the GNU General Public License and
  33. // a copy of the GCC Runtime Library Exception along with this program;
  34. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  35. // <http://www.gnu.org/licenses/>.
  36. #pragma once
  37. #include <c10/util/C++17.h>
  38. #include <algorithm>
  39. #include <stdexcept>
  40. #include <string>
  41. #include <utility>
  42. namespace c10 {
  43. namespace guts {
  44. namespace detail {
  45. template <typename _Tp, std::size_t _Nm>
  46. struct __array_traits final {
  47. using _Type = _Tp[_Nm];
  48. static constexpr _Tp& _S_ref(const _Type& __t, std::size_t __n) noexcept {
  49. return const_cast<_Tp&>(__t[__n]);
  50. }
  51. static constexpr _Tp* _S_ptr(const _Type& __t) noexcept {
  52. return const_cast<_Tp*>(__t);
  53. }
  54. };
  55. template <typename _Tp>
  56. struct __array_traits<_Tp, 0> final {
  57. struct _Type final {};
  58. static constexpr _Tp& _S_ref(const _Type& __t, std::size_t) noexcept {
  59. return *_S_ptr(__t);
  60. }
  61. static constexpr _Tp* _S_ptr(const _Type&) noexcept {
  62. return nullptr;
  63. }
  64. };
  65. [[noreturn]] inline void __throw_out_of_range(const std::string& msg) {
  66. throw std::out_of_range(msg);
  67. }
  68. } // namespace detail
  69. template <typename _Tp, std::size_t _Nm>
  70. class array final {
  71. public:
  72. using value_type = _Tp;
  73. using pointer = value_type*;
  74. using const_pointer = const value_type*;
  75. using reference = value_type&;
  76. using const_reference = const value_type&;
  77. using iterator = value_type*;
  78. using const_iterator = const value_type*;
  79. using size_type = std::size_t;
  80. using difference_type = std::ptrdiff_t;
  81. using reverse_iterator = std::reverse_iterator<iterator>;
  82. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  83. private:
  84. using _AT_Type = detail::__array_traits<_Tp, _Nm>;
  85. public: // needs to be public member for aggregate initialization
  86. typename _AT_Type::_Type _M_elems;
  87. public:
  88. // No explicit construct/copy/destroy for aggregate type.
  89. // DR 776.
  90. constexpr void fill(const value_type& __u) {
  91. std::fill_n(begin(), size(), __u);
  92. }
  93. constexpr void swap(array& __other) {
  94. std::swap_ranges(begin(), end(), __other.begin());
  95. }
  96. // Iterators.
  97. constexpr iterator begin() noexcept {
  98. return iterator(data());
  99. }
  100. constexpr const_iterator begin() const noexcept {
  101. return const_iterator(data());
  102. }
  103. constexpr iterator end() noexcept {
  104. return iterator(data() + _Nm);
  105. }
  106. constexpr const_iterator end() const noexcept {
  107. return const_iterator(data() + _Nm);
  108. }
  109. constexpr reverse_iterator rbegin() noexcept {
  110. return reverse_iterator(end());
  111. }
  112. constexpr const_reverse_iterator rbegin() const noexcept {
  113. return const_reverse_iterator(end());
  114. }
  115. constexpr reverse_iterator rend() noexcept {
  116. return reverse_iterator(begin());
  117. }
  118. constexpr const_reverse_iterator rend() const noexcept {
  119. return const_reverse_iterator(begin());
  120. }
  121. constexpr const_iterator cbegin() const noexcept {
  122. return const_iterator(data());
  123. }
  124. constexpr const_iterator cend() const noexcept {
  125. return const_iterator(data() + _Nm);
  126. }
  127. constexpr const_reverse_iterator crbegin() const noexcept {
  128. return const_reverse_iterator(end());
  129. }
  130. constexpr const_reverse_iterator crend() const noexcept {
  131. return const_reverse_iterator(begin());
  132. }
  133. // Capacity.
  134. constexpr size_type size() const noexcept {
  135. return _Nm;
  136. }
  137. constexpr size_type max_size() const noexcept {
  138. return _Nm;
  139. }
  140. constexpr bool empty() const noexcept {
  141. return size() == 0;
  142. }
  143. // Element access.
  144. constexpr reference operator[](size_type __n) noexcept {
  145. return _AT_Type::_S_ref(_M_elems, __n);
  146. }
  147. constexpr const_reference operator[](size_type __n) const noexcept {
  148. return _AT_Type::_S_ref(_M_elems, __n);
  149. }
  150. constexpr reference at(size_type __n) {
  151. if (__n >= _Nm) {
  152. detail::__throw_out_of_range(
  153. std::string() + "array::at: __n (which is " + to_string(__n) + ") " +
  154. ">= _Nm (which is " + to_string(_Nm) + ")");
  155. }
  156. return _AT_Type::_S_ref(_M_elems, __n);
  157. }
  158. constexpr const_reference at(size_type __n) const {
  159. // Result of conditional expression must be an lvalue so use
  160. // boolean ? lvalue : (throw-expr, lvalue)
  161. return __n < _Nm
  162. ? _AT_Type::_S_ref(_M_elems, __n)
  163. : (detail::__throw_out_of_range(
  164. std::string() + "array::at: __n (which is " + to_string(__n) +
  165. ") " + ">= _Nm (which is " + to_string(_Nm) + ")"),
  166. _AT_Type::_S_ref(_M_elems, 0));
  167. }
  168. constexpr reference front() noexcept {
  169. return *begin();
  170. }
  171. constexpr const_reference front() const noexcept {
  172. return _AT_Type::_S_ref(_M_elems, 0);
  173. }
  174. constexpr reference back() noexcept {
  175. return _Nm ? *(end() - 1) : *end();
  176. }
  177. constexpr const_reference back() const noexcept {
  178. return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
  179. : _AT_Type::_S_ref(_M_elems, 0);
  180. }
  181. constexpr pointer data() noexcept {
  182. return _AT_Type::_S_ptr(_M_elems);
  183. }
  184. constexpr const_pointer data() const noexcept {
  185. return _AT_Type::_S_ptr(_M_elems);
  186. }
  187. };
  188. #if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
  189. template <typename _Tp, typename... _Up>
  190. array(_Tp, _Up...) -> array<
  191. std::enable_if_t<(std::is_same<_Tp, _Up>::value && ...), _Tp>,
  192. 1 + sizeof...(_Up)>;
  193. #endif
  194. // Array comparisons.
  195. namespace detail {
  196. template <class T, size_t N>
  197. constexpr inline bool array_equals_(
  198. const array<T, N>& lhs,
  199. const array<T, N>& rhs,
  200. size_t current_index) {
  201. return (current_index == N)
  202. ? true
  203. : (lhs.at(current_index) == rhs.at(current_index) &&
  204. array_equals_(lhs, rhs, current_index + 1));
  205. }
  206. template <class T, size_t N>
  207. constexpr inline bool array_less_(
  208. const array<T, N>& lhs,
  209. const array<T, N>& rhs,
  210. size_t current_index) {
  211. return (current_index == N)
  212. ? false
  213. : (lhs.at(current_index) < rhs.at(current_index) ||
  214. array_less_(lhs, rhs, current_index + 1));
  215. }
  216. } // namespace detail
  217. template <typename _Tp, std::size_t _Nm>
  218. constexpr inline bool operator==(
  219. const array<_Tp, _Nm>& __one,
  220. const array<_Tp, _Nm>& __two) {
  221. return detail::array_equals_(__one, __two, 0);
  222. }
  223. template <typename _Tp, std::size_t _Nm>
  224. constexpr inline bool operator!=(
  225. const array<_Tp, _Nm>& __one,
  226. const array<_Tp, _Nm>& __two) {
  227. return !(__one == __two);
  228. }
  229. template <typename _Tp, std::size_t _Nm>
  230. constexpr inline bool operator<(
  231. const array<_Tp, _Nm>& __a,
  232. const array<_Tp, _Nm>& __b) {
  233. return detail::array_less_(__a, __b, 0);
  234. }
  235. template <typename _Tp, std::size_t _Nm>
  236. constexpr inline bool operator>(
  237. const array<_Tp, _Nm>& __one,
  238. const array<_Tp, _Nm>& __two) {
  239. return __two < __one;
  240. }
  241. template <typename _Tp, std::size_t _Nm>
  242. constexpr inline bool operator<=(
  243. const array<_Tp, _Nm>& __one,
  244. const array<_Tp, _Nm>& __two) {
  245. return !(__one > __two);
  246. }
  247. template <typename _Tp, std::size_t _Nm>
  248. constexpr inline bool operator>=(
  249. const array<_Tp, _Nm>& __one,
  250. const array<_Tp, _Nm>& __two) {
  251. return !(__one < __two);
  252. }
  253. // Specialized algorithms.
  254. template <typename _Tp, std::size_t _Nm>
  255. inline void swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) noexcept(
  256. noexcept(__one.swap(__two))) {
  257. __one.swap(__two);
  258. }
  259. template <std::size_t _Int, typename _Tp, std::size_t _Nm>
  260. constexpr _Tp& get(array<_Tp, _Nm>& __arr) noexcept {
  261. static_assert(_Int < _Nm, "array index is within bounds");
  262. return detail::__array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  263. }
  264. template <std::size_t _Int, typename _Tp, std::size_t _Nm>
  265. constexpr _Tp&& get(array<_Tp, _Nm>&& __arr) noexcept {
  266. static_assert(_Int < _Nm, "array index is within bounds");
  267. return std::move(get<_Int>(__arr));
  268. }
  269. template <std::size_t _Int, typename _Tp, std::size_t _Nm>
  270. constexpr const _Tp& get(const array<_Tp, _Nm>& __arr) noexcept {
  271. static_assert(_Int < _Nm, "array index is within bounds");
  272. return detail::__array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
  273. }
  274. /**
  275. * Some added features not available in std::array.
  276. * Only call these at compile time, they're slow if called at runtime.
  277. * Examples:
  278. * tail({2, 3, 4}) == {3, 4}
  279. * prepend(2, {3, 4}) == {2, 3, 4}
  280. */
  281. namespace detail {
  282. template <class T, size_t N, size_t... INDEX>
  283. constexpr inline array<T, N - 1> tail_(
  284. const array<T, N>& arg,
  285. std::index_sequence<INDEX...>) {
  286. static_assert(sizeof...(INDEX) == N - 1, "invariant");
  287. return {{get<INDEX + 1>(arg)...}};
  288. }
  289. } // namespace detail
  290. template <class T, size_t N>
  291. constexpr inline array<T, N - 1> tail(const array<T, N>& arg) {
  292. static_assert(
  293. N > 0, "Can only call tail() on an array with at least one element");
  294. return detail::tail_(arg, std::make_index_sequence<N - 1>());
  295. }
  296. namespace detail {
  297. template <class T, size_t N, size_t... INDEX>
  298. constexpr inline array<T, N + 1> prepend_(
  299. T&& head,
  300. const array<T, N>& tail,
  301. std::index_sequence<INDEX...>) {
  302. return {{std::forward<T>(head), get<INDEX>(tail)...}};
  303. }
  304. } // namespace detail
  305. template <class T, size_t N>
  306. constexpr inline array<T, N + 1> prepend(T&& head, const array<T, N>& tail) {
  307. return detail::prepend_(
  308. std::forward<T>(head), tail, std::make_index_sequence<N>());
  309. }
  310. /**
  311. * Convert a C array into a std::array.
  312. * Example:
  313. * int source[3] = {2, 3, 4};
  314. * std::array<int, 3> target = to_std_array(source);
  315. */
  316. namespace detail {
  317. template <class T, size_t N, size_t... INDEX>
  318. constexpr array<T, N> to_array_(
  319. const T (&arr)[N],
  320. std::index_sequence<INDEX...>) {
  321. return {{arr[INDEX]...}};
  322. }
  323. } // namespace detail
  324. template <class T, size_t N>
  325. constexpr array<T, N> to_array(const T (&arr)[N]) {
  326. return detail::to_array_(arr, std::make_index_sequence<N>());
  327. }
  328. } // namespace guts
  329. } // namespace c10