Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*!
0002 @file
0003 Defines `boost::hana::less`.
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_LESS_HPP
0011 #define BOOST_HANA_LESS_HPP
0012 
0013 #include <boost/hana/fwd/less.hpp>
0014 
0015 #include <boost/hana/and.hpp>
0016 #include <boost/hana/bool.hpp>
0017 #include <boost/hana/concept/constant.hpp>
0018 #include <boost/hana/concept/orderable.hpp>
0019 #include <boost/hana/concept/product.hpp>
0020 #include <boost/hana/concept/sequence.hpp>
0021 #include <boost/hana/config.hpp>
0022 #include <boost/hana/core/common.hpp>
0023 #include <boost/hana/core/to.hpp>
0024 #include <boost/hana/core/dispatch.hpp>
0025 #include <boost/hana/detail/concepts.hpp>
0026 #include <boost/hana/detail/has_common_embedding.hpp>
0027 #include <boost/hana/detail/nested_than.hpp> // required by fwd decl
0028 #include <boost/hana/equal.hpp>
0029 #include <boost/hana/first.hpp>
0030 #include <boost/hana/if.hpp>
0031 #include <boost/hana/less_equal.hpp>
0032 #include <boost/hana/lexicographical_compare.hpp>
0033 #include <boost/hana/or.hpp>
0034 #include <boost/hana/second.hpp>
0035 #include <boost/hana/value.hpp>
0036 
0037 
0038 namespace boost { namespace hana {
0039     //! @cond
0040     template <typename X, typename Y>
0041     constexpr auto less_t::operator()(X&& x, Y&& y) const {
0042         using T = typename hana::tag_of<X>::type;
0043         using U = typename hana::tag_of<Y>::type;
0044         using Less = BOOST_HANA_DISPATCH_IF(decltype(less_impl<T, U>{}),
0045             hana::Orderable<T>::value &&
0046             hana::Orderable<U>::value &&
0047             !is_default<less_impl<T, U>>::value
0048         );
0049 
0050     #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
0051         static_assert(hana::Orderable<T>::value,
0052         "hana::less(x, y) requires 'x' to be Orderable");
0053 
0054         static_assert(hana::Orderable<U>::value,
0055         "hana::less(x, y) requires 'y' to be Orderable");
0056 
0057         static_assert(!is_default<less_impl<T, U>>::value,
0058         "hana::less(x, y) requires 'x' and 'y' to be embeddable "
0059         "in a common Orderable");
0060     #endif
0061 
0062         return Less::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
0063     }
0064     //! @endcond
0065 
0066     template <typename T, typename U, bool condition>
0067     struct less_impl<T, U, when<condition>> : default_ {
0068         template <typename ...Args>
0069         static constexpr auto apply(Args&& ...) = delete;
0070     };
0071 
0072     // Cross-type overload
0073     template <typename T, typename U>
0074     struct less_impl<T, U, when<
0075         detail::has_nontrivial_common_embedding<Orderable, T, U>::value &&
0076         !detail::LessThanComparable<T, U>::value
0077     >> {
0078         using C = typename hana::common<T, U>::type;
0079         template <typename X, typename Y>
0080         static constexpr decltype(auto) apply(X&& x, Y&& y) {
0081             return hana::less(hana::to<C>(static_cast<X&&>(x)),
0082                               hana::to<C>(static_cast<Y&&>(y)));
0083         }
0084     };
0085 
0086     //////////////////////////////////////////////////////////////////////////
0087     // Model for LessThanComparable data types
0088     //////////////////////////////////////////////////////////////////////////
0089     template <typename T, typename U>
0090     struct less_impl<T, U, when<detail::LessThanComparable<T, U>::value>> {
0091         template <typename X, typename Y>
0092         static constexpr decltype(auto) apply(X&& x, Y&& y)
0093         { return static_cast<X&&>(x) < static_cast<Y&&>(y); }
0094     };
0095 
0096     //////////////////////////////////////////////////////////////////////////
0097     // Model for Constants wrapping an Orderable
0098     //////////////////////////////////////////////////////////////////////////
0099     template <typename C>
0100     struct less_impl<C, C, when<
0101         hana::Constant<C>::value &&
0102         Orderable<typename C::value_type>::value
0103     >> {
0104         template <typename X, typename Y>
0105         static constexpr auto apply(X const&, Y const&) {
0106             constexpr auto is_less = hana::less(hana::value<X>(), hana::value<Y>());
0107             constexpr bool truth_value = hana::if_(is_less, true, false);
0108             return hana::bool_c<truth_value>;
0109         }
0110     };
0111 
0112     //////////////////////////////////////////////////////////////////////////
0113     // Model for Products
0114     //////////////////////////////////////////////////////////////////////////
0115     template <typename T, typename U>
0116     struct less_impl<T, U, when<hana::Product<T>::value && hana::Product<U>::value>> {
0117         template <typename X, typename Y>
0118         static constexpr decltype(auto) apply(X const& x, Y const& y) {
0119             return hana::or_(
0120                 hana::less(hana::first(x), hana::first(y)),
0121                 hana::and_(
0122                     hana::less_equal(hana::first(x), hana::first(y)),
0123                     hana::less(hana::second(x), hana::second(y))
0124                 )
0125             );
0126         }
0127     };
0128 
0129     //////////////////////////////////////////////////////////////////////////
0130     // Model for Sequences
0131     //////////////////////////////////////////////////////////////////////////
0132     template <typename T, typename U>
0133     struct less_impl<T, U, when<
0134         hana::Sequence<T>::value && hana::Sequence<U>::value
0135     >> {
0136         template <typename Xs, typename Ys>
0137         static constexpr auto apply(Xs const& xs, Ys const& ys)
0138         { return hana::lexicographical_compare(xs, ys); }
0139     };
0140 }} // end namespace boost::hana
0141 
0142 #endif // !BOOST_HANA_LESS_HPP