add functional/equiv

fix is_bind_expression
add polymorphic version not1, not2
This commit is contained in:
bolero-MURAKAMI 2013-09-04 12:07:54 +09:00
parent d9e9c23f67
commit ffb3ccece8
5 changed files with 191 additions and 21 deletions

View file

@ -804,6 +804,14 @@ namespace sprout {
struct is_bind_expression<sprout::res_binder<Result, Signature> > struct is_bind_expression<sprout::res_binder<Result, Signature> >
: public std::true_type : public std::true_type
{}; {};
template<typename Signature>
struct is_bind_expression<sprout::cbinder<Signature> >
: public std::true_type
{};
template<typename Result, typename Signature>
struct is_bind_expression<sprout::res_cbinder<Result, Signature> >
: public std::true_type
{};
namespace detail { namespace detail {
template<std::size_t N, typename BoundArg, typename = void> template<std::size_t N, typename BoundArg, typename = void>

View file

@ -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 <functional>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/functional/type_traits/is_strict_function.hpp>
#include <sprout/functional/less.hpp>
#include <sprout/utility/forward.hpp>
namespace sprout {
namespace detail {
template<typename Compare, bool IsStrict = sprout::is_strict_binary_function<Compare>::value>
class equiv_adaptor_impl;
template<typename Compare>
class equiv_adaptor_impl<Compare, true> {
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<typename Compare>
class equiv_adaptor_impl<Compare, false> {
protected:
Compare fn;
public:
explicit SPROUT_CONSTEXPR equiv_adaptor_impl(Compare const& comp)
: fn(comp)
{}
template<typename T, typename U>
SPROUT_CONSTEXPR decltype(!std::declval<Compare const&>()(std::declval<T>(), std::declval<U>()) && !std::declval<Compare const&>()(std::declval<U>(), std::declval<T>()))
operator()(T&& x, U&& y) const {
return !fn(sprout::forward<T>(x), sprout::forward<U>(y)) && !fn(sprout::forward<U>(y), sprout::forward<T>(x));
}
};
} // namespace detail
//
// equiv_adaptor
//
template<typename Compare = void>
class equiv_adaptor
: public sprout::detail::equiv_adaptor_impl<Compare>
{
public:
explicit SPROUT_CONSTEXPR equiv_adaptor(Compare const& comp)
: sprout::detail::equiv_adaptor_impl<Compare>(comp)
{}
};
template<>
class equiv_adaptor<void>
: public sprout::detail::equiv_adaptor_impl<sprout::less<> >
{
public:
SPROUT_CONSTEXPR equiv_adaptor()
: sprout::detail::equiv_adaptor_impl<sprout::less<> >(sprout::less<>())
{}
explicit SPROUT_CONSTEXPR equiv_adaptor(sprout::less<> const& comp)
: sprout::detail::equiv_adaptor_impl<sprout::less<> >(comp)
{}
};
//
// equiv
//
template<typename Compare>
inline SPROUT_CONSTEXPR sprout::equiv_adaptor<Compare>
equiv(Compare const& comp) {
return sprout::equiv_adaptor<Compare>(comp);
}
inline SPROUT_CONSTEXPR sprout::equiv_adaptor<>
equiv() {
return sprout::equiv_adaptor<>();
}
} // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_EQUIV_HPP

View file

@ -15,5 +15,6 @@
#include <sprout/functional/bitwise.hpp> #include <sprout/functional/bitwise.hpp>
#include <sprout/functional/binder.hpp> #include <sprout/functional/binder.hpp>
#include <sprout/functional/negator.hpp> #include <sprout/functional/negator.hpp>
#include <sprout/functional/equiv.hpp>
#endif // #ifndef SPROUT_FUNCTIONAL_FUNCTOR_HPP #endif // #ifndef SPROUT_FUNCTIONAL_FUNCTOR_HPP

View file

@ -9,24 +9,57 @@
#define SPROUT_FUNCTIONAL_NOT1_HPP #define SPROUT_FUNCTIONAL_NOT1_HPP
#include <functional> #include <functional>
#include <utility>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/functional/type_traits/is_strict_function.hpp>
#include <sprout/utility/forward.hpp>
namespace sprout { namespace sprout {
namespace detail {
template<typename Predicate, bool IsStrict = sprout::is_strict_unary_function<Predicate>::value>
class unary_negate_impl;
template<typename Predicate>
class unary_negate_impl<Predicate, true> {
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<typename Predicate>
class unary_negate_impl<Predicate, false> {
protected:
Predicate fn;
public:
explicit SPROUT_CONSTEXPR unary_negate_impl(Predicate const& pred)
: fn(pred)
{}
template<typename T>
SPROUT_CONSTEXPR decltype(!std::declval<Predicate const&>()(std::declval<T>()))
operator()(T&& x) const {
return !fn(sprout::forward<T>(x));
}
};
} // namespace detail
// 20.8.8 negators // 20.8.8 negators
template<typename Predicate> template<typename Predicate>
class unary_negate { class unary_negate
public: : public sprout::detail::unary_negate_impl<Predicate>
typedef typename Predicate::argument_type argument_type; {
typedef bool result_type;
protected:
Predicate fn;
public: public:
explicit SPROUT_CONSTEXPR unary_negate(Predicate const& pred) explicit SPROUT_CONSTEXPR unary_negate(Predicate const& pred)
: fn(pred) : sprout::detail::unary_negate_impl<Predicate>(pred)
{} {}
SPROUT_CONSTEXPR bool operator()(typename Predicate::argument_type const& x) const {
return !fn(x);
}
}; };
template<typename Predicate> template<typename Predicate>

View file

@ -9,25 +9,58 @@
#define SPROUT_FUNCTIONAL_NOT2_HPP #define SPROUT_FUNCTIONAL_NOT2_HPP
#include <functional> #include <functional>
#include <utility>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/functional/type_traits/is_strict_function.hpp>
#include <sprout/utility/forward.hpp>
namespace sprout { namespace sprout {
namespace detail {
template<typename Predicate, bool IsStrict = sprout::is_strict_binary_function<Predicate>::value>
class binary_negate_impl;
template<typename Predicate>
class binary_negate_impl<Predicate, true> {
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<typename Predicate>
class binary_negate_impl<Predicate, false> {
protected:
Predicate fn;
public:
explicit SPROUT_CONSTEXPR binary_negate_impl(Predicate const& pred)
: fn(pred)
{}
template<typename T, typename U>
SPROUT_CONSTEXPR decltype(!std::declval<Predicate const&>()(std::declval<T>(), std::declval<U>()))
operator()(T&& x, U&& y) const {
return !fn(sprout::forward<T>(x), sprout::forward<U>(y));
}
};
} // namespace detail
// 20.8.8 negators // 20.8.8 negators
template<typename Predicate> template<typename Predicate>
class binary_negate { class binary_negate
public: : public sprout::detail::binary_negate_impl<Predicate>
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: public:
explicit SPROUT_CONSTEXPR binary_negate(Predicate const& pred) explicit SPROUT_CONSTEXPR binary_negate(Predicate const& pred)
: fn(pred) : sprout::detail::binary_negate_impl<Predicate>(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<typename Predicate> template<typename Predicate>