sort.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. @file
  3. Defines `boost::hana::sort`.
  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_SORT_HPP
  9. #define BOOST_HANA_SORT_HPP
  10. #include <boost/hana/fwd/sort.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
  17. #include <boost/hana/length.hpp>
  18. #include <boost/hana/less.hpp>
  19. #include <utility> // std::declval, std::index_sequence
  20. BOOST_HANA_NAMESPACE_BEGIN
  21. //! @cond
  22. template <typename Xs, typename Predicate>
  23. constexpr auto sort_t::operator()(Xs&& xs, Predicate&& pred) const {
  24. using S = typename hana::tag_of<Xs>::type;
  25. using Sort = BOOST_HANA_DISPATCH_IF(sort_impl<S>,
  26. hana::Sequence<S>::value
  27. );
  28. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  29. static_assert(hana::Sequence<S>::value,
  30. "hana::sort(xs, predicate) requires 'xs' to be a Sequence");
  31. #endif
  32. return Sort::apply(static_cast<Xs&&>(xs),
  33. static_cast<Predicate&&>(pred));
  34. }
  35. template <typename Xs>
  36. constexpr auto sort_t::operator()(Xs&& xs) const {
  37. using S = typename hana::tag_of<Xs>::type;
  38. using Sort = BOOST_HANA_DISPATCH_IF(sort_impl<S>,
  39. hana::Sequence<S>::value
  40. );
  41. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  42. static_assert(hana::Sequence<S>::value,
  43. "hana::sort(xs) requires 'xs' to be a Sequence");
  44. #endif
  45. return Sort::apply(static_cast<Xs&&>(xs));
  46. }
  47. //! @endcond
  48. namespace detail {
  49. template <typename Xs, typename Pred>
  50. struct sort_predicate {
  51. template <std::size_t I, std::size_t J>
  52. using apply = decltype(std::declval<Pred>()(
  53. hana::at_c<I>(std::declval<Xs>()),
  54. hana::at_c<J>(std::declval<Xs>())
  55. ));
  56. };
  57. template <typename Pred, std::size_t Insert, bool IsInsertionPoint,
  58. typename Left,
  59. std::size_t ...Right>
  60. struct insert;
  61. // We did not find the insertion point; continue processing elements
  62. // recursively.
  63. template <
  64. typename Pred, std::size_t Insert,
  65. std::size_t ...Left,
  66. std::size_t Right1, std::size_t Right2, std::size_t ...Right
  67. >
  68. struct insert<Pred, Insert, false,
  69. std::index_sequence<Left...>,
  70. Right1, Right2, Right...
  71. > {
  72. using type = typename insert<
  73. Pred, Insert, (bool)Pred::template apply<Insert, Right2>::value,
  74. std::index_sequence<Left..., Right1>,
  75. Right2, Right...
  76. >::type;
  77. };
  78. // We did not find the insertion point, but there is only one element
  79. // left. We insert at the end of the list, and we're done.
  80. template <typename Pred, std::size_t Insert, std::size_t ...Left, std::size_t Last>
  81. struct insert<Pred, Insert, false, std::index_sequence<Left...>, Last> {
  82. using type = std::index_sequence<Left..., Last, Insert>;
  83. };
  84. // We found the insertion point, we're done.
  85. template <typename Pred, std::size_t Insert, std::size_t ...Left, std::size_t ...Right>
  86. struct insert<Pred, Insert, true, std::index_sequence<Left...>, Right...> {
  87. using type = std::index_sequence<Left..., Insert, Right...>;
  88. };
  89. template <typename Pred, typename Result, std::size_t ...T>
  90. struct insertion_sort_impl;
  91. template <typename Pred,
  92. std::size_t Result1, std::size_t ...Result,
  93. std::size_t T, std::size_t ...Ts>
  94. struct insertion_sort_impl<Pred, std::index_sequence<Result1, Result...>, T, Ts...> {
  95. using type = typename insertion_sort_impl<
  96. Pred,
  97. typename insert<
  98. Pred, T, (bool)Pred::template apply<T, Result1>::value,
  99. std::index_sequence<>,
  100. Result1, Result...
  101. >::type,
  102. Ts...
  103. >::type;
  104. };
  105. template <typename Pred, std::size_t T, std::size_t ...Ts>
  106. struct insertion_sort_impl<Pred, std::index_sequence<>, T, Ts...> {
  107. using type = typename insertion_sort_impl<
  108. Pred, std::index_sequence<T>, Ts...
  109. >::type;
  110. };
  111. template <typename Pred, typename Result>
  112. struct insertion_sort_impl<Pred, Result> {
  113. using type = Result;
  114. };
  115. template <typename Pred, typename Indices>
  116. struct sort_helper;
  117. template <typename Pred, std::size_t ...i>
  118. struct sort_helper<Pred, std::index_sequence<i...>> {
  119. using type = typename insertion_sort_impl<
  120. Pred, std::index_sequence<>, i...
  121. >::type;
  122. };
  123. } // end namespace detail
  124. template <typename S, bool condition>
  125. struct sort_impl<S, when<condition>> : default_ {
  126. template <typename Xs, std::size_t ...i>
  127. static constexpr auto apply_impl(Xs&& xs, std::index_sequence<i...>) {
  128. return hana::make<S>(hana::at_c<i>(static_cast<Xs&&>(xs))...);
  129. }
  130. template <typename Xs, typename Pred>
  131. static constexpr auto apply(Xs&& xs, Pred const&) {
  132. constexpr std::size_t Len = decltype(hana::length(xs))::value;
  133. using Indices = typename detail::sort_helper<
  134. detail::sort_predicate<Xs&&, Pred>,
  135. std::make_index_sequence<Len>
  136. >::type;
  137. return apply_impl(static_cast<Xs&&>(xs), Indices{});
  138. }
  139. template <typename Xs>
  140. static constexpr auto apply(Xs&& xs)
  141. { return sort_impl::apply(static_cast<Xs&&>(xs), hana::less); }
  142. };
  143. BOOST_HANA_NAMESPACE_END
  144. #endif // !BOOST_HANA_SORT_HPP