1 /*! 2 @file 3 Defines `boost::hana::maximum`. 4 5 @copyright Louis Dionne 2013-2017 6 Distributed under the Boost Software License, Version 1.0. 7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) 8 */ 9 10 #ifndef BOOST_HANA_MAXIMUM_HPP 11 #define BOOST_HANA_MAXIMUM_HPP 12 13 #include <boost/hana/fwd/maximum.hpp> 14 15 #include <boost/hana/concept/foldable.hpp> 16 #include <boost/hana/config.hpp> 17 #include <boost/hana/core/dispatch.hpp> 18 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl 19 #include <boost/hana/fold_left.hpp> 20 #include <boost/hana/if.hpp> 21 #include <boost/hana/less.hpp> 22 23 24 BOOST_HANA_NAMESPACE_BEGIN 25 //! @cond 26 template <typename Xs> operator ()(Xs && xs) const27 constexpr decltype(auto) maximum_t::operator()(Xs&& xs) const { 28 using S = typename hana::tag_of<Xs>::type; 29 using Maximum = BOOST_HANA_DISPATCH_IF(maximum_impl<S>, 30 hana::Foldable<S>::value 31 ); 32 33 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 34 static_assert(hana::Foldable<S>::value, 35 "hana::maximum(xs) requires 'xs' to be Foldable"); 36 #endif 37 38 return Maximum::apply(static_cast<Xs&&>(xs)); 39 } 40 41 template <typename Xs, typename Predicate> operator ()(Xs && xs,Predicate && pred) const42 constexpr decltype(auto) maximum_t::operator()(Xs&& xs, Predicate&& pred) const { 43 using S = typename hana::tag_of<Xs>::type; 44 using Maximum = BOOST_HANA_DISPATCH_IF(maximum_pred_impl<S>, 45 hana::Foldable<S>::value 46 ); 47 48 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 49 static_assert(hana::Foldable<S>::value, 50 "hana::maximum(xs, predicate) requires 'xs' to be Foldable"); 51 #endif 52 53 return Maximum::apply(static_cast<Xs&&>(xs), 54 static_cast<Predicate&&>(pred)); 55 } 56 //! @endcond 57 58 ////////////////////////////////////////////////////////////////////////// 59 // maximum (with a custom predicate) 60 ////////////////////////////////////////////////////////////////////////// 61 namespace detail { 62 template <typename Pred> 63 struct max_by { 64 Pred pred; 65 66 template <typename X, typename Y> operator ()detail::max_by67 constexpr decltype(auto) operator()(X&& x, Y&& y) const { 68 auto result = (*pred)(x, y); 69 return hana::if_(result, static_cast<Y&&>(y), 70 static_cast<X&&>(x)); 71 } 72 }; 73 } 74 75 template <typename T, bool condition> 76 struct maximum_pred_impl<T, when<condition>> : default_ { 77 template <typename Xs, typename Pred> applymaximum_pred_impl78 static constexpr decltype(auto) apply(Xs&& xs, Pred&& pred) { 79 // We use a pointer instead of a reference to avoid a Clang ICE. 80 return hana::fold_left(static_cast<Xs&&>(xs), 81 detail::max_by<decltype(&pred)>{&pred} 82 ); 83 } 84 }; 85 86 ////////////////////////////////////////////////////////////////////////// 87 // maximum (without a custom predicate) 88 ////////////////////////////////////////////////////////////////////////// 89 template <typename T, bool condition> 90 struct maximum_impl<T, when<condition>> : default_ { 91 template <typename Xs> applymaximum_impl92 static constexpr decltype(auto) apply(Xs&& xs) 93 { return hana::maximum(static_cast<Xs&&>(xs), hana::less); } 94 }; 95 BOOST_HANA_NAMESPACE_END 96 97 #endif // !BOOST_HANA_MAXIMUM_HPP 98