⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstl_functional.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 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.
*/

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation 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.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 */

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_FUNCTIONAL_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_FUNCTIONAL_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "mstl_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_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_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -