]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::minimum`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
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_MINIMUM_HPP | |
11 | #define BOOST_HANA_MINIMUM_HPP | |
12 | ||
13 | #include <boost/hana/fwd/minimum.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> | |
27 | constexpr decltype(auto) minimum_t::operator()(Xs&& xs) const { | |
28 | using S = typename hana::tag_of<Xs>::type; | |
29 | using Minimum = BOOST_HANA_DISPATCH_IF(minimum_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::minimum(xs) requires 'xs' to be Foldable"); | |
36 | #endif | |
37 | ||
38 | return Minimum::apply(static_cast<Xs&&>(xs)); | |
39 | } | |
40 | ||
41 | template <typename Xs, typename Predicate> | |
42 | constexpr decltype(auto) minimum_t::operator()(Xs&& xs, Predicate&& pred) const { | |
43 | using S = typename hana::tag_of<Xs>::type; | |
44 | using Minimum = BOOST_HANA_DISPATCH_IF(minimum_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::minimum(xs, predicate) requires 'xs' to be Foldable"); | |
51 | #endif | |
52 | ||
53 | return Minimum::apply(static_cast<Xs&&>(xs), | |
54 | static_cast<Predicate&&>(pred)); | |
55 | } | |
56 | //! @endcond | |
57 | ||
58 | ////////////////////////////////////////////////////////////////////////// | |
59 | // minimum (with a custom predicate) | |
60 | ////////////////////////////////////////////////////////////////////////// | |
61 | namespace detail { | |
62 | template <typename Pred> | |
63 | struct min_by { | |
64 | Pred pred; | |
65 | ||
66 | template <typename X, typename Y> | |
67 | constexpr decltype(auto) operator()(X&& x, Y&& y) const { | |
68 | auto result = (*pred)(x, y); | |
69 | return hana::if_(result, static_cast<X&&>(x), | |
70 | static_cast<Y&&>(y)); | |
71 | } | |
72 | }; | |
73 | } | |
74 | ||
75 | template <typename T, bool condition> | |
76 | struct minimum_pred_impl<T, when<condition>> : default_ { | |
77 | template <typename Xs, typename Pred> | |
78 | static constexpr decltype(auto) apply(Xs&& xs, Pred const& 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::min_by<decltype(&pred)>{&pred} | |
82 | ); | |
83 | } | |
84 | }; | |
85 | ||
86 | ////////////////////////////////////////////////////////////////////////// | |
87 | // minimum (without a custom predicate) | |
88 | ////////////////////////////////////////////////////////////////////////// | |
89 | template <typename T, bool condition> | |
90 | struct minimum_impl<T, when<condition>> : default_ { | |
91 | template <typename Xs> | |
92 | static constexpr decltype(auto) apply(Xs&& xs) | |
93 | { return hana::minimum(static_cast<Xs&&>(xs), hana::less); } | |
94 | }; | |
95 | BOOST_HANA_NAMESPACE_END | |
96 | ||
97 | #endif // !BOOST_HANA_MINIMUM_HPP |