文件位置:young/y_functional.hpp
因为要实现黑红树和散列表的 modify_key 函数,所以 identity、select1st 、select2nd 和C++标准有所不同。
/*
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:
T& operator()( const T& x )
{
return const_cast<T&>( x );
}
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;
result_t& operator()( const PairType& x )
{
return const_cast<result_t&>( x.first );
}
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;
result_t& operator()( const PairType& x )
{
return const_cast<result_t&>( x.second );
}
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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------