minimum.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. @file
  3. Defines `boost::hana::minimum`.
  4. Copyright Louis Dionne 2013-2022
  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_MINIMUM_HPP
  9. #define BOOST_HANA_MINIMUM_HPP
  10. #include <boost/hana/fwd/minimum.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
  15. #include <boost/hana/fold_left.hpp>
  16. #include <boost/hana/if.hpp>
  17. #include <boost/hana/less.hpp>
  18. namespace boost { namespace hana {
  19. //! @cond
  20. template <typename Xs>
  21. constexpr decltype(auto) minimum_t::operator()(Xs&& xs) const {
  22. using S = typename hana::tag_of<Xs>::type;
  23. using Minimum = BOOST_HANA_DISPATCH_IF(minimum_impl<S>,
  24. hana::Foldable<S>::value
  25. );
  26. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  27. static_assert(hana::Foldable<S>::value,
  28. "hana::minimum(xs) requires 'xs' to be Foldable");
  29. #endif
  30. return Minimum::apply(static_cast<Xs&&>(xs));
  31. }
  32. template <typename Xs, typename Predicate>
  33. constexpr decltype(auto) minimum_t::operator()(Xs&& xs, Predicate&& pred) const {
  34. using S = typename hana::tag_of<Xs>::type;
  35. using Minimum = BOOST_HANA_DISPATCH_IF(minimum_pred_impl<S>,
  36. hana::Foldable<S>::value
  37. );
  38. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  39. static_assert(hana::Foldable<S>::value,
  40. "hana::minimum(xs, predicate) requires 'xs' to be Foldable");
  41. #endif
  42. return Minimum::apply(static_cast<Xs&&>(xs),
  43. static_cast<Predicate&&>(pred));
  44. }
  45. //! @endcond
  46. //////////////////////////////////////////////////////////////////////////
  47. // minimum (with a custom predicate)
  48. //////////////////////////////////////////////////////////////////////////
  49. namespace detail {
  50. template <typename Pred>
  51. struct min_by {
  52. Pred pred;
  53. template <typename X, typename Y>
  54. constexpr decltype(auto) operator()(X&& x, Y&& y) const {
  55. auto result = (*pred)(x, y);
  56. return hana::if_(result, static_cast<X&&>(x),
  57. static_cast<Y&&>(y));
  58. }
  59. };
  60. }
  61. template <typename T, bool condition>
  62. struct minimum_pred_impl<T, when<condition>> : default_ {
  63. template <typename Xs, typename Pred>
  64. static constexpr decltype(auto) apply(Xs&& xs, Pred const& pred) {
  65. // We use a pointer instead of a reference to avoid a Clang ICE.
  66. return hana::fold_left(static_cast<Xs&&>(xs),
  67. detail::min_by<decltype(&pred)>{&pred}
  68. );
  69. }
  70. };
  71. //////////////////////////////////////////////////////////////////////////
  72. // minimum (without a custom predicate)
  73. //////////////////////////////////////////////////////////////////////////
  74. template <typename T, bool condition>
  75. struct minimum_impl<T, when<condition>> : default_ {
  76. template <typename Xs>
  77. static constexpr decltype(auto) apply(Xs&& xs)
  78. { return hana::minimum(static_cast<Xs&&>(xs), hana::less); }
  79. };
  80. }} // end namespace boost::hana
  81. #endif // !BOOST_HANA_MINIMUM_HPP