Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:04

0001 /*!
0002 @file
0003 Defines `boost::hana::maximum`.
0004 
0005 Copyright Louis Dionne 2013-2022
0006 Distributed under the Boost Software License, Version 1.0.
0007 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
0008  */
0009 
0010 #ifndef BOOST_HANA_MAXIMUM_HPP
0011 #define BOOST_HANA_MAXIMUM_HPP
0012 
0013 #include <boost/hana/fwd/maximum.hpp>
0014 
0015 #include <boost/hana/concept/foldable.hpp>
0016 #include <boost/hana/config.hpp>
0017 #include <boost/hana/core/dispatch.hpp>
0018 #include <boost/hana/detail/nested_by.hpp> // required by fwd decl
0019 #include <boost/hana/fold_left.hpp>
0020 #include <boost/hana/if.hpp>
0021 #include <boost/hana/less.hpp>
0022 
0023 
0024 namespace boost { namespace hana {
0025     //! @cond
0026     template <typename Xs>
0027     constexpr decltype(auto) maximum_t::operator()(Xs&& xs) const {
0028         using S = typename hana::tag_of<Xs>::type;
0029         using Maximum = BOOST_HANA_DISPATCH_IF(maximum_impl<S>,
0030             hana::Foldable<S>::value
0031         );
0032 
0033     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0034         static_assert(hana::Foldable<S>::value,
0035         "hana::maximum(xs) requires 'xs' to be Foldable");
0036     #endif
0037 
0038         return Maximum::apply(static_cast<Xs&&>(xs));
0039     }
0040 
0041     template <typename Xs, typename Predicate>
0042     constexpr decltype(auto) maximum_t::operator()(Xs&& xs, Predicate&& pred) const {
0043         using S = typename hana::tag_of<Xs>::type;
0044         using Maximum = BOOST_HANA_DISPATCH_IF(maximum_pred_impl<S>,
0045             hana::Foldable<S>::value
0046         );
0047 
0048     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0049         static_assert(hana::Foldable<S>::value,
0050         "hana::maximum(xs, predicate) requires 'xs' to be Foldable");
0051     #endif
0052 
0053         return Maximum::apply(static_cast<Xs&&>(xs),
0054                               static_cast<Predicate&&>(pred));
0055     }
0056     //! @endcond
0057 
0058     //////////////////////////////////////////////////////////////////////////
0059     // maximum (with a custom predicate)
0060     //////////////////////////////////////////////////////////////////////////
0061     namespace detail {
0062         template <typename Pred>
0063         struct max_by {
0064             Pred pred;
0065 
0066             template <typename X, typename Y>
0067             constexpr decltype(auto) operator()(X&& x, Y&& y) const {
0068                 auto result = (*pred)(x, y);
0069                 return hana::if_(result, static_cast<Y&&>(y),
0070                                          static_cast<X&&>(x));
0071             }
0072         };
0073     }
0074 
0075     template <typename T, bool condition>
0076     struct maximum_pred_impl<T, when<condition>> : default_ {
0077         template <typename Xs, typename Pred>
0078         static constexpr decltype(auto) apply(Xs&& xs, Pred&& pred) {
0079             // We use a pointer instead of a reference to avoid a Clang ICE.
0080             return hana::fold_left(static_cast<Xs&&>(xs),
0081                 detail::max_by<decltype(&pred)>{&pred}
0082             );
0083         }
0084     };
0085 
0086     //////////////////////////////////////////////////////////////////////////
0087     // maximum (without a custom predicate)
0088     //////////////////////////////////////////////////////////////////////////
0089     template <typename T, bool condition>
0090     struct maximum_impl<T, when<condition>> : default_ {
0091         template <typename Xs>
0092         static constexpr decltype(auto) apply(Xs&& xs)
0093         { return hana::maximum(static_cast<Xs&&>(xs), hana::less); }
0094     };
0095 }} // end namespace boost::hana
0096 
0097 #endif // !BOOST_HANA_MAXIMUM_HPP