maximum.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. @file
  3. Defines `boost::hana::maximum`.
  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_MAXIMUM_HPP
  9. #define BOOST_HANA_MAXIMUM_HPP
  10. #include <boost/hana/fwd/maximum.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. BOOST_HANA_NAMESPACE_BEGIN
  19. //! @cond
  20. template <typename Xs>
  21. constexpr decltype(auto) maximum_t::operator()(Xs&& xs) const {
  22. using S = typename hana::tag_of<Xs>::type;
  23. using Maximum = BOOST_HANA_DISPATCH_IF(maximum_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::maximum(xs) requires 'xs' to be Foldable");
  29. #endif
  30. return Maximum::apply(static_cast<Xs&&>(xs));
  31. }
  32. template <typename Xs, typename Predicate>
  33. constexpr decltype(auto) maximum_t::operator()(Xs&& xs, Predicate&& pred) const {
  34. using S = typename hana::tag_of<Xs>::type;
  35. using Maximum = BOOST_HANA_DISPATCH_IF(maximum_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::maximum(xs, predicate) requires 'xs' to be Foldable");
  41. #endif
  42. return Maximum::apply(static_cast<Xs&&>(xs),
  43. static_cast<Predicate&&>(pred));
  44. }
  45. //! @endcond
  46. //////////////////////////////////////////////////////////////////////////
  47. // maximum (with a custom predicate)
  48. //////////////////////////////////////////////////////////////////////////
  49. namespace detail {
  50. template <typename Pred>
  51. struct max_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<Y&&>(y),
  57. static_cast<X&&>(x));
  58. }
  59. };
  60. }
  61. template <typename T, bool condition>
  62. struct maximum_pred_impl<T, when<condition>> : default_ {
  63. template <typename Xs, typename Pred>
  64. static constexpr decltype(auto) apply(Xs&& xs, Pred&& 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::max_by<decltype(&pred)>{&pred}
  68. );
  69. }
  70. };
  71. //////////////////////////////////////////////////////////////////////////
  72. // maximum (without a custom predicate)
  73. //////////////////////////////////////////////////////////////////////////
  74. template <typename T, bool condition>
  75. struct maximum_impl<T, when<condition>> : default_ {
  76. template <typename Xs>
  77. static constexpr decltype(auto) apply(Xs&& xs)
  78. { return hana::maximum(static_cast<Xs&&>(xs), hana::less); }
  79. };
  80. BOOST_HANA_NAMESPACE_END
  81. #endif // !BOOST_HANA_MAXIMUM_HPP