/*! @file Defines `boost::hana::minimum`. @copyright Louis Dionne 2013-2017 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_MINIMUM_HPP #define BOOST_HANA_MINIMUM_HPP #include #include #include #include #include // required by fwd decl #include #include #include BOOST_HANA_NAMESPACE_BEGIN //! @cond template constexpr decltype(auto) minimum_t::operator()(Xs&& xs) const { using S = typename hana::tag_of::type; using Minimum = BOOST_HANA_DISPATCH_IF(minimum_impl, hana::Foldable::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Foldable::value, "hana::minimum(xs) requires 'xs' to be Foldable"); #endif return Minimum::apply(static_cast(xs)); } template constexpr decltype(auto) minimum_t::operator()(Xs&& xs, Predicate&& pred) const { using S = typename hana::tag_of::type; using Minimum = BOOST_HANA_DISPATCH_IF(minimum_pred_impl, hana::Foldable::value ); #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS static_assert(hana::Foldable::value, "hana::minimum(xs, predicate) requires 'xs' to be Foldable"); #endif return Minimum::apply(static_cast(xs), static_cast(pred)); } //! @endcond ////////////////////////////////////////////////////////////////////////// // minimum (with a custom predicate) ////////////////////////////////////////////////////////////////////////// namespace detail { template struct min_by { Pred pred; template constexpr decltype(auto) operator()(X&& x, Y&& y) const { auto result = (*pred)(x, y); return hana::if_(result, static_cast(x), static_cast(y)); } }; } template struct minimum_pred_impl> : default_ { template static constexpr decltype(auto) apply(Xs&& xs, Pred const& pred) { // We use a pointer instead of a reference to avoid a Clang ICE. return hana::fold_left(static_cast(xs), detail::min_by{&pred} ); } }; ////////////////////////////////////////////////////////////////////////// // minimum (without a custom predicate) ////////////////////////////////////////////////////////////////////////// template struct minimum_impl> : default_ { template static constexpr decltype(auto) apply(Xs&& xs) { return hana::minimum(static_cast(xs), hana::less); } }; BOOST_HANA_NAMESPACE_END #endif // !BOOST_HANA_MINIMUM_HPP