您的位置:首页 > 编程语言 > C语言/C++

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

2005-08-14 10:42 471 查看
文件位置: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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: