9) 泛型數學運算符函數mathOperator [原創,泛型編程,自由下載轉載,需註明出處]

下面是一些算術操作符,其通常作爲一些接口的比較參數傳入。泛型的。

#ifndef mathOperator_h__
#define mathOperator_h__


#include "typeConvert.h"
#include "mplmacro.h"
/********************************************************************


Description : general math operator .
Author : Shen.Xiaolong (Shen Tony) (2010-2013)
Mail : [email protected],  [email protected]
verified platform : VS2008
copyright:          : latest Version of The Code Project Open License (CPOL : http://www.codeproject.com/).
*********************************************************************/


namespace MiniMPL
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename LeftT,typename RightT>
    bool equal(LeftT p1,RightT p2)
{
return p1==p2;
}


template <typename LeftT,typename RightT>
bool lesser(LeftT p1,RightT p2)
{
return p1 < p2;
}


    template<typename BinaryFunction_T,typename LeftT = typename BinaryFunction_T::TP1,typename RightT = typename BinaryFunction_T::TP2>
class NotBinary 
{
public:
NotBinary(BinaryFunction_T& pCmp) : m_cmp(pCmp){};
bool operator()(LeftT & p1,RightT & p2)
{
return !m_cmp(p1,p2);
}


protected:
BinaryFunction_T&   m_cmp;
};


    template<typename UnaryFunction_T,typename TP=typename UnaryFunction_T::Param_T>
    class NotUnary 
    {
    public:
        NotUnary(UnaryFunction_T& f) : m_f(f){};
        bool operator()(TP & p)
        {
            return !m_f(p);
        }


    protected:
        UnaryFunction_T&       m_f;
    };


template <typename LeftT,typename RightT>
bool notEqual(LeftT p1,RightT p2 )
{
return !equal<LeftT,RightT>(p1,p2);
}


template <typename LeftT,typename RightT>
bool lesserEqual(LeftT p1,RightT p2 )
{
return lesser<LeftT,RightT>(p1,p2) || equal<LeftT,RightT>(p1,p2);
}


template <typename LeftT,typename RightT>
bool greater(LeftT p1,RightT p2 )
{
return lesser<RightT,LeftT>(p2,p1);
}


template <typename LeftT,typename RightT>
bool greaterEqual(LeftT p1,RightT p2 )
{
return greater<LeftT,RightT>(p1,p2) || equal<LeftT,RightT>(p1,p2);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename DataType>
    void swap(DataType& left,DataType& right,SFINAE(logic::Not_T<IsStlContainer<DataType> >::value))
{  
        if (&left == &right) return;  //same element


DataType tmp = left;
left = right;
right = tmp;
}


    template<typename DataType>
    void swap(DataType& left,DataType& right,SFINAE(IsStlContainer<DataType>::value))
    {  
        if (&left == &right) return;  //same element
        left.swap(right);
    }


    template<typename DataType,unsigned int LEN>
    void swap(DataType(&left)[LEN],DataType(&right)[LEN] )
    {  
        if (&left == &right) return;  //same element
        for (unsigned int i=0;i<LEN;i++)
        {
            swap(left[i],right[i]);
        }
    }


template<typename DataType,typename Comparer_T>
bool swapIf(DataType& left,DataType& right,Comparer_T& cmp)
{
if (cmp(left,right))
{
swap(left,right);
return true;

return false;
}


template<typename DataType,typename Comparer_T>
bool swapNotIf(DataType& left,DataType& right,Comparer_T& cmp)
{
if (!cmp(left,right))
{
swap(left,right);
return true;
}
return false;
}
}




#endif // mathOperator_h__

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章