array.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*!
  2. @file
  3. Adapts `std::array` for use with Hana.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_EXT_STD_ARRAY_HPP
  9. #define BOOST_HANA_EXT_STD_ARRAY_HPP
  10. #include <boost/hana/bool.hpp>
  11. #include <boost/hana/config.hpp>
  12. #include <boost/hana/detail/algorithm.hpp>
  13. #include <boost/hana/fwd/at.hpp>
  14. #include <boost/hana/fwd/core/tag_of.hpp>
  15. #include <boost/hana/fwd/drop_front.hpp>
  16. #include <boost/hana/fwd/equal.hpp>
  17. #include <boost/hana/fwd/is_empty.hpp>
  18. #include <boost/hana/fwd/length.hpp>
  19. #include <boost/hana/fwd/less.hpp>
  20. #include <boost/hana/integral_constant.hpp>
  21. #include <array>
  22. #include <cstddef>
  23. #include <type_traits>
  24. #include <utility>
  25. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  26. namespace std {
  27. //! @ingroup group-ext-std
  28. //! Adaptation of `std::array` for Hana.
  29. //!
  30. //!
  31. //!
  32. //! Modeled concepts
  33. //! ----------------
  34. //! 1. `Comparable`\n
  35. //! `std::array`s are compared as per `std::equal`, except that two arrays
  36. //! with different sizes compare unequal instead of triggering an error
  37. //! and the result of the comparison is `constexpr` if both arrays are
  38. //! `constexpr`.
  39. //! @include example/ext/std/array/comparable.cpp
  40. //!
  41. //! 2. `Orderable`\n
  42. //! `std::array`s are ordered with the usual lexicographical ordering,
  43. //! except that two arrays with different size can be ordered instead
  44. //! of triggering an error and the result of the comparison is `constexpr`
  45. //! if both arrays are `constexpr`.
  46. //! @include example/ext/std/array/orderable.cpp
  47. //!
  48. //! 3. `Foldable`\n
  49. //! Folding an array from the left is equivalent to calling
  50. //! `std::accumulate` on it, except it can be `constexpr`.
  51. //! @include example/ext/std/array/foldable.cpp
  52. //!
  53. //! 4. `Iterable`\n
  54. //! Iterating over a `std::array` is equivalent to iterating over it with
  55. //! a normal `for` loop.
  56. //! @include example/ext/std/array/iterable.cpp
  57. template <typename T, std::size_t N>
  58. struct array { };
  59. }
  60. #endif
  61. BOOST_HANA_NAMESPACE_BEGIN
  62. namespace ext { namespace std { struct array_tag; }}
  63. template <typename T, std::size_t N>
  64. struct tag_of<std::array<T, N>> {
  65. using type = ext::std::array_tag;
  66. };
  67. //////////////////////////////////////////////////////////////////////////
  68. // Foldable
  69. //////////////////////////////////////////////////////////////////////////
  70. template <>
  71. struct length_impl<ext::std::array_tag> {
  72. template <typename Xs>
  73. static constexpr auto apply(Xs const&) {
  74. return hana::size_c<std::tuple_size<Xs>::type::value>;
  75. }
  76. };
  77. //////////////////////////////////////////////////////////////////////////
  78. // Iterable
  79. //////////////////////////////////////////////////////////////////////////
  80. template <>
  81. struct at_impl<ext::std::array_tag> {
  82. template <typename Xs, typename N>
  83. static constexpr decltype(auto) apply(Xs&& xs, N const&) {
  84. constexpr std::size_t n = N::value;
  85. return std::get<n>(static_cast<Xs&&>(xs));
  86. }
  87. };
  88. template <>
  89. struct drop_front_impl<ext::std::array_tag> {
  90. template <std::size_t n, typename Xs, std::size_t ...i>
  91. static constexpr auto drop_front_helper(Xs&& xs, std::index_sequence<i...>) {
  92. using T = typename std::remove_reference<Xs>::type::value_type;
  93. return std::array<T, sizeof...(i)>{{static_cast<Xs&&>(xs)[n + i]...}};
  94. }
  95. template <typename Xs, typename N>
  96. static constexpr auto apply(Xs&& xs, N const&) {
  97. constexpr std::size_t n = N::value;
  98. constexpr std::size_t len = std::tuple_size<
  99. typename std::remove_cv<
  100. typename std::remove_reference<Xs>::type
  101. >::type
  102. >::value;
  103. return drop_front_helper<n>(static_cast<Xs&&>(xs),
  104. std::make_index_sequence<(n < len ? len - n : 0)>{});
  105. }
  106. };
  107. template <>
  108. struct is_empty_impl<ext::std::array_tag> {
  109. template <typename T, std::size_t N>
  110. static constexpr auto apply(std::array<T, N> const&) {
  111. return hana::bool_c<N == 0>;
  112. }
  113. };
  114. //////////////////////////////////////////////////////////////////////////
  115. // Comparable
  116. //////////////////////////////////////////////////////////////////////////
  117. template <>
  118. struct equal_impl<ext::std::array_tag, ext::std::array_tag> {
  119. template <typename T, std::size_t n, typename U>
  120. static constexpr bool apply(std::array<T, n> const& xs, std::array<U, n> const& ys)
  121. { return detail::equal(&xs[0], &xs[0] + n, &ys[0], &ys[0] + n); }
  122. template <typename T, typename U>
  123. static constexpr auto apply(std::array<T, 0> const&, std::array<U, 0> const&)
  124. { return hana::true_c; }
  125. template <typename T, std::size_t n, typename U, std::size_t m>
  126. static constexpr auto apply(std::array<T, n> const&, std::array<U, m> const&)
  127. { return hana::false_c; }
  128. };
  129. //////////////////////////////////////////////////////////////////////////
  130. // Orderable
  131. //////////////////////////////////////////////////////////////////////////
  132. template <>
  133. struct less_impl<ext::std::array_tag, ext::std::array_tag> {
  134. template <typename T, std::size_t n, typename U, std::size_t m>
  135. static constexpr auto apply(std::array<T, n> const& xs, std::array<U, m> const& ys) {
  136. // This logic is more complex than it needs to be because we can't
  137. // use `.begin()` and `.end()`, which are not constexpr in C++14,
  138. // and because `&arr[0]` is UB when the array is empty.
  139. if (xs.empty()) {
  140. return !ys.empty();
  141. } else {
  142. if (ys.empty()) {
  143. return false;
  144. } else {
  145. return detail::lexicographical_compare(&xs[0], &xs[0] + n,
  146. &ys[0], &ys[0] + m);
  147. }
  148. }
  149. }
  150. };
  151. BOOST_HANA_NAMESPACE_END
  152. #endif // !BOOST_HANA_EXT_STD_ARRAY_HPP