9) 泛型数学运算符函数mathOperator [原创,泛型编程,自由下载转载,需注明出处]
2013-10-07 14:34
603 查看
下面是一些算术操作符,其通常作为一些接口的比较参数传入。泛型的。
#ifndef mathOperator_h__
#define mathOperator_h__
#include "typeConvert.h"
#include "mplmacro.h"
/********************************************************************
Description : general math operator .
Author : Shen.Xiaolong (Shen Tony) (2010-2013)
Mail : xlshen2002@hotmail.com, xlshen@126.com
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__
#ifndef mathOperator_h__
#define mathOperator_h__
#include "typeConvert.h"
#include "mplmacro.h"
/********************************************************************
Description : general math operator .
Author : Shen.Xiaolong (Shen Tony) (2010-2013)
Mail : xlshen2002@hotmail.com, xlshen@126.com
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__
相关文章推荐
- 10) 泛型工厂方法创建实例ClassRegister [原创,泛型编程,自由下载转载,需注明出处]
- 9) 常用函数对象functionobject [原创,泛型编程,自由下载转载,需注明出处]
- 5) 函数特征functionTraits [原创,泛型编程,自由下载转载,需注明出处]
- 4) 将多元函数转换为一元函数unaryFunctionConverter [原创,泛型编程,自由下载转载,需注明出处]
- 7) 占位符类型placeHolder [原创,泛型编程,自由下载转载,需注明出处]
- 11) 为STL容器和数组提供统一的接口DataSet [原创,泛型编程,自由下载转载,需注明出处]
- 13) 参数适配ParamterWrapper [原创,泛型编程,自由下载转载,需注明出处]
- 14) 常用的遍历/(条件)查找/收集数据/(拷贝2比较优先)排序算法algorithm [原创,泛型编程,自由下载转载,需注明出处]
- 8) 多级结构数据成员指针memberPtr [原创,泛型编程,自由下载转载,需注明出处]
- 1) 获取类型特征typeTraits [原创,泛型编程,自由下载转载,需注明出处]
- 6) 创建一元函数/二元函数functionCreater [原创,泛型编程,自由下载转载,需注明出处]
- 2) 转换类型typeConvert [原创,泛型编程,自由下载转载,需注明出处]
- 12) 类型安全的任意类CAnyObject [原创,泛型编程,自由下载转载,需注明出处]
- 3) 一元/二元函数抽象function [原创,泛型编程,自由下载转载,需注明出处]
- STL sort 函数实现详解 作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现。以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的
- Dropwizard框架下通过Rest接口实现多文件压缩下载(原创,转载请注明出处)
- 查看文件时去除注释说明内容(原创贴-转载请注明出处)
- 【原创】Spring-boot快速入门(二)JPA数据源--转载请注明出处
- Apache设置页面认证(原创贴-转载请注明出处)
- SilverLight学习笔记--如何在xaml文件中操作用户在后台代码定义的类(2)--示例篇:创建一个登录控件(原创)(转载本文请注明出处)