C++ young 程序库——y_functional.hpp

文件位置:young/y_functional.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_FUNCTIONAL_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_FUNCTIONAL_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename Argument, typename Result >
class unary_function
{
public:
    typedef  Argument  argument_type;
    typedef  Result    result_type;
};

template< typename Argument1, typename Argument2, typename Result >
class binary_function
{
public:
    typedef  Argument1  first_argument_type;
    typedef  Argument2  second_argument_type;
    typedef  Result     result_type;
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

//逻辑运算类functor

template< typename T >
class logical_not : public unary_function<T, bool>
{
public:
    bool operator()( const T& x ) const
    {
        return !x;
    }
};

template< typename T >
class logical_and : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs && rhs );
    }
};

template< typename T >
class logical_or : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs || rhs );
    }
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

//算术类functor

template< typename T >
class negate : public unary_function<T, T>
{
public:
    T operator()( const T& x ) const
    {
        return -x;
    }
};

template< typename T >
class plus : public binary_function<T, T, T>
{
public:
    T operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs + rhs );
    }
};

template< typename T >
class minus : public binary_function<T, T, T>
{
public:
    T operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs - rhs );
    }
};

template< typename T >
class multiplies : public binary_function<T, T, T>
{
public:
    T operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs * rhs );
    }
};

template< typename T >
class divides : public binary_function<T, T, T>
{
public:
    T operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs / rhs );
    }
};

template< typename T >
class modulus : public binary_function<T, T, T>
{
public:
    T operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs % rhs );
    }
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

//关系运算类functor

template< typename T >
class less : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs < rhs );
    }
};

template< typename T >
class less_equal : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs <= rhs );
    }
};

template< typename T >
class greater : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs > rhs );
    }
};

template< typename T >
class greater_equal : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs >= rhs );
    }
};

template< typename T >
class equal_to : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs == rhs );
    }
};

template< typename T >
class not_equal_to : public binary_function<T, T, bool>
{
public:
    bool operator()( const T& lhs, const T& rhs ) const
    {
        return ( lhs != rhs );
    }
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class identity : public unary_function<T, T>
{
public:
   
const T& operator()( const T& x ) const
    {
        return x;
    }
};

template< typename PairType >
class select1st : public unary_function< PairType,
                                          typename PairType::first_type >
{
public:
    typedef  typename PairType::first_type  result_t;

    const result_t& operator()( const PairType& x ) const
    {
        return x.first;
    }
};

template< typename PairType >
class select2nd : public unary_function< PairType,
                                          typename PairType::second_type >
{
public:
    typedef  typename PairType::second_type  result_t;

    const result_t& operator()( const PairType& x ) const
    {
        return x.second;
    }
};

template< typename Argument1, typename Argument2 >
class project1st : public binary_function<Argument1, Argument2, Argument1>
{
public:
    Argument1 operator()( const Argument1& x, const Argument2& ) const
    {
        return x;
    }
};

template< typename Argument1, typename Argument2 >
class project2nd : public binary_function<Argument1, Argument2, Argument2>
{
public:
    Argument2 operator()( const Argument1&, const Argument2& y ) const
    {
        return y;
    }
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值