From ffb3ccece8e0b83b51b6413d8336cbb941b76f86 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Wed, 4 Sep 2013 12:07:54 +0900 Subject: [PATCH] add functional/equiv fix is_bind_expression add polymorphic version not1, not2 --- sprout/functional/bind/bind.hpp | 8 +++ sprout/functional/equiv.hpp | 95 +++++++++++++++++++++++++++++++++ sprout/functional/functor.hpp | 1 + sprout/functional/not1.hpp | 53 ++++++++++++++---- sprout/functional/not2.hpp | 55 +++++++++++++++---- 5 files changed, 191 insertions(+), 21 deletions(-) create mode 100644 sprout/functional/equiv.hpp diff --git a/sprout/functional/bind/bind.hpp b/sprout/functional/bind/bind.hpp index 636e93fc..ca4fb640 100644 --- a/sprout/functional/bind/bind.hpp +++ b/sprout/functional/bind/bind.hpp @@ -804,6 +804,14 @@ namespace sprout { struct is_bind_expression > : public std::true_type {}; + template + struct is_bind_expression > + : public std::true_type + {}; + template + struct is_bind_expression > + : public std::true_type + {}; namespace detail { template diff --git a/sprout/functional/equiv.hpp b/sprout/functional/equiv.hpp new file mode 100644 index 00000000..9658bfe4 --- /dev/null +++ b/sprout/functional/equiv.hpp @@ -0,0 +1,95 @@ +/*============================================================================= + Copyright (c) 2011-2013 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_FUNCTIONAL_EQUIV_HPP +#define SPROUT_FUNCTIONAL_EQUIV_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template::value> + class equiv_adaptor_impl; + + template + class equiv_adaptor_impl { + public: + typedef typename Compare::first_argument_type first_argument_type; + typedef typename Compare::second_argument_type second_argument_type; + typedef bool result_type; + protected: + Compare fn; + public: + explicit SPROUT_CONSTEXPR equiv_adaptor_impl(Compare const& comp) + : fn(comp) + {} + SPROUT_CONSTEXPR bool operator()(typename Compare::first_argument_type const& x, typename Compare::second_argument_type const& y) const { + return !fn(x, y) && !fn(y, x); + } + }; + + template + class equiv_adaptor_impl { + protected: + Compare fn; + public: + explicit SPROUT_CONSTEXPR equiv_adaptor_impl(Compare const& comp) + : fn(comp) + {} + template + SPROUT_CONSTEXPR decltype(!std::declval()(std::declval(), std::declval()) && !std::declval()(std::declval(), std::declval())) + operator()(T&& x, U&& y) const { + return !fn(sprout::forward(x), sprout::forward(y)) && !fn(sprout::forward(y), sprout::forward(x)); + } + }; + } // namespace detail + + // + // equiv_adaptor + // + template + class equiv_adaptor + : public sprout::detail::equiv_adaptor_impl + { + public: + explicit SPROUT_CONSTEXPR equiv_adaptor(Compare const& comp) + : sprout::detail::equiv_adaptor_impl(comp) + {} + }; + template<> + class equiv_adaptor + : public sprout::detail::equiv_adaptor_impl > + { + public: + SPROUT_CONSTEXPR equiv_adaptor() + : sprout::detail::equiv_adaptor_impl >(sprout::less<>()) + {} + explicit SPROUT_CONSTEXPR equiv_adaptor(sprout::less<> const& comp) + : sprout::detail::equiv_adaptor_impl >(comp) + {} + }; + + // + // equiv + // + template + inline SPROUT_CONSTEXPR sprout::equiv_adaptor + equiv(Compare const& comp) { + return sprout::equiv_adaptor(comp); + } + inline SPROUT_CONSTEXPR sprout::equiv_adaptor<> + equiv() { + return sprout::equiv_adaptor<>(); + } +} // namespace sprout + +#endif // #ifndef SPROUT_FUNCTIONAL_EQUIV_HPP diff --git a/sprout/functional/functor.hpp b/sprout/functional/functor.hpp index 7a54e361..249adaeb 100644 --- a/sprout/functional/functor.hpp +++ b/sprout/functional/functor.hpp @@ -15,5 +15,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_FUNCTIONAL_FUNCTOR_HPP diff --git a/sprout/functional/not1.hpp b/sprout/functional/not1.hpp index 4173c0ad..3d034dfd 100644 --- a/sprout/functional/not1.hpp +++ b/sprout/functional/not1.hpp @@ -9,24 +9,57 @@ #define SPROUT_FUNCTIONAL_NOT1_HPP #include +#include #include +#include +#include namespace sprout { + namespace detail { + template::value> + class unary_negate_impl; + + template + class unary_negate_impl { + public: + typedef typename Predicate::argument_type argument_type; + typedef bool result_type; + protected: + Predicate fn; + public: + explicit SPROUT_CONSTEXPR unary_negate_impl(Predicate const& pred) + : fn(pred) + {} + SPROUT_CONSTEXPR bool operator()(typename Predicate::argument_type const& x) const { + return !fn(x); + } + }; + + template + class unary_negate_impl { + protected: + Predicate fn; + public: + explicit SPROUT_CONSTEXPR unary_negate_impl(Predicate const& pred) + : fn(pred) + {} + template + SPROUT_CONSTEXPR decltype(!std::declval()(std::declval())) + operator()(T&& x) const { + return !fn(sprout::forward(x)); + } + }; + } // namespace detail + // 20.8.8 negators template - class unary_negate { - public: - typedef typename Predicate::argument_type argument_type; - typedef bool result_type; - protected: - Predicate fn; + class unary_negate + : public sprout::detail::unary_negate_impl + { public: explicit SPROUT_CONSTEXPR unary_negate(Predicate const& pred) - : fn(pred) + : sprout::detail::unary_negate_impl(pred) {} - SPROUT_CONSTEXPR bool operator()(typename Predicate::argument_type const& x) const { - return !fn(x); - } }; template diff --git a/sprout/functional/not2.hpp b/sprout/functional/not2.hpp index 0e451d59..991c6ce5 100644 --- a/sprout/functional/not2.hpp +++ b/sprout/functional/not2.hpp @@ -9,25 +9,58 @@ #define SPROUT_FUNCTIONAL_NOT2_HPP #include +#include #include +#include +#include namespace sprout { + namespace detail { + template::value> + class binary_negate_impl; + + template + class binary_negate_impl { + public: + typedef typename Predicate::first_argument_type first_argument_type; + typedef typename Predicate::second_argument_type second_argument_type; + typedef bool result_type; + protected: + Predicate fn; + public: + explicit SPROUT_CONSTEXPR binary_negate_impl(Predicate const& pred) + : fn(pred) + {} + SPROUT_CONSTEXPR bool operator()(typename Predicate::first_argument_type const& x, typename Predicate::second_argument_type const& y) const { + return !fn(x, y); + } + }; + + template + class binary_negate_impl { + protected: + Predicate fn; + public: + explicit SPROUT_CONSTEXPR binary_negate_impl(Predicate const& pred) + : fn(pred) + {} + template + SPROUT_CONSTEXPR decltype(!std::declval()(std::declval(), std::declval())) + operator()(T&& x, U&& y) const { + return !fn(sprout::forward(x), sprout::forward(y)); + } + }; + } // namespace detail + // 20.8.8 negators template - class binary_negate { - public: - typedef typename Predicate::first_argument_type first_argument_type; - typedef typename Predicate::second_argument_type second_argument_type; - typedef bool result_type; - protected: - Predicate fn; + class binary_negate + : public sprout::detail::binary_negate_impl + { public: explicit SPROUT_CONSTEXPR binary_negate(Predicate const& pred) - : fn(pred) + : sprout::detail::binary_negate_impl(pred) {} - SPROUT_CONSTEXPR bool operator()(typename Predicate::first_argument_type const& x, typename Predicate::second_argument_type const& y) const { - return !fn(x, y); - } }; template