- 浏览: 1285754 次
文章分类
最新评论
-
极限_裁决:
第一题,a 没有定义全局变量不属于 window
第二题,由于 ...
你真懂JavaScript吗? -
netsupper:
1)a=1,答错了."a" in wind ...
你真懂JavaScript吗?
MultiBoolean for C++/Python
MultiBoolean 是一个多值逻辑类,它兼容多种空值运算。最早在C#1.1上实现,包含在C#代码库March Library,并广泛应用于我以前开发的C#系统中,现在我编写了一个C++版,并为它封装了一个Python包。关于多值逻辑算法的讨论,见我在CSDN上发表的文章《March Library中的Multiboolean——多值逻辑实现》。这里,我主要希望可以通过这份源代码演示一个实用的boost::python应用。
以下为源代码文件的内容:
MultiBoolean.h
////////////////////////////////////////////////////////////////////////////////
//MultiBoolean C++ 实现
//基于C#源码库MarchLibrary中的MultiBoolean
//作者:刘鑫
//本代码库旨在演示Boost::Python的运算符重载技术,并提供一个实用的C++/Python
//类。
////////////////////////////////////////////////////////////////////////////////
#ifndef __MULTIBOOLEAN__
#define __MULTIBOOLEAN__
#include <string>
#include <complex>
#include <algorithm>
#include <stdexcept>
using namespace std;
namespace MarchLibrary{
class MultiBoolean
{
//友元定义
friend MultiBoolean operator ! (const MultiBoolean & x);
friend MultiBoolean operator == (const MultiBoolean & x, const MultiBoolean & y);
friend MultiBoolean operator == (const MultiBoolean & x, const bool & y);
friend MultiBoolean operator == (const bool & x, const MultiBoolean & y);
friend MultiBoolean operator != (const MultiBoolean & x, const MultiBoolean & y);
friend MultiBoolean operator != (const MultiBoolean & x, const bool & y);
friend MultiBoolean operator != (const bool & x, const MultiBoolean & y);
friend MultiBoolean operator && (const MultiBoolean & x, const MultiBoolean & y);
friend MultiBoolean operator && (const MultiBoolean & x, const bool & y);
friend MultiBoolean operator && (const bool & x, const MultiBoolean & y);
friend MultiBoolean operator || (const MultiBoolean & x, const MultiBoolean & y);
friend MultiBoolean operator || (const MultiBoolean & x, const bool & y);
friend MultiBoolean operator || (const bool & x, const MultiBoolean & y);
private:
//逻辑状态
complex<int> _value;
//禁用默认构造
MultiBoolean(){};
//禁止直接调用传值构造
MultiBoolean(complex<int> value)
{
_value = value;
}
//可选的5个状态值
static const complex<int> TrueValue;
static const complex<int> FalseValue;
static const complex<int> UnknownValue;
static const complex<int> UndefineValue;
static const complex<int> NilValue;
public:
//重载逻辑运算符 &= 、 |= 和 ^=
MultiBoolean& operator &= (const MultiBoolean& x)
{
if(this->_value == MultiBoolean::NilValue)
return (*this);
else if(x._value == MultiBoolean::NilValue)
this->_value = NilValue;
else if(this->_value == MultiBoolean::UndefineValue)
this->_value = x._value;
else if(this->_value.real() > x._value.real())
this->_value = x._value;
return (*this);
}
MultiBoolean& operator &= (const bool& x)
{
if(this->_value == MultiBoolean::NilValue)
return (*this);
else if(this->_value == MultiBoolean::UndefineValue)
this->_value = x ? TrueValue : FalseValue;
else if(!x)
this->_value = FalseValue;
return (*this);
}
MultiBoolean& operator |= (const MultiBoolean& x)
{
if(this->_value == MultiBoolean::NilValue)
return (*this);
else if(x._value == MultiBoolean::NilValue)
this->_value = NilValue;
else if(this->_value == MultiBoolean::UndefineValue)
this->_value = x._value;
else if(this->_value.real() < x._value.real())
this->_value = x._value;
return (*this);
}
MultiBoolean& operator |= (const bool& x)
{
if(this->_value == MultiBoolean::NilValue)
return (*this);
else if(this->_value == MultiBoolean::UndefineValue)
this->_value = x ? TrueValue : FalseValue;
else if(x)
this->_value = TrueValue;
return (*this);
}
MultiBoolean & operator ^= (const MultiBoolean & x)
{
this->_value = ((!x&&this)||(x&&!this))._value;
return (*this);
}
MultiBoolean & operator ^= (const bool & x)
{
this->_value = MultiBoolean((!x&&this)||(x&&!this))._value;
return (*this);
}
//由二元逻辑构造多值逻辑
MultiBoolean(bool value)
{
_value = value ? complex<int>(1, 0) : complex<int>(-1, 0);
};
//状态判定函数
bool IsTrue()
{
return _value == TrueValue;
};
bool IsFalse()
{
return _value == FalseValue;
};
bool IsUnknown()
{
return _value == UnknownValue;
};
bool IsUndefine()
{
return _value == UndefineValue;
};
bool IsNil()
{
return _value == NilValue;
};
//字符串与多值逻辑的相互转换
const char* ToString()
{
if(_value == TrueValue)
return "True";
if(_value == FalseValue)
return "False";
if(_value == UndefineValue)
return "Undefine";
if(_value == NilValue)
return "Nil";
return "Unknown";
};
string FullName()
{
string buf = string("MultiBoolean::");
buf += this->ToString();
return buf;
}
static MultiBoolean Parse(const char * input)
{
if("True" == input)
return True;
if("False" == input)
return False;
if("Unknown" == input)
return Unknown;
if("Undefine" == input)
return Undefine;
if("Nil" == input)
return Nil;
throw logic_error(string("The string isn't a available Value.") + string(input));
};
//可选的逻辑值,提供构造接口
static const MultiBoolean True;
static const MultiBoolean False;
static const MultiBoolean Unknown;
static const MultiBoolean Undefine;
static const MultiBoolean Nil;
};
//初始化状态值
const complex<int> MultiBoolean::TrueValue = complex<int>(1, 0);
const complex<int> MultiBoolean::FalseValue = complex<int>(-1, 0);
const complex<int> MultiBoolean::UnknownValue = complex<int>(0, 0);
const complex<int> MultiBoolean::UndefineValue = complex<int>(0, 1);
const complex<int> MultiBoolean::NilValue = complex<int>(0, -1);
//初始化逻辑值
const MultiBoolean MultiBoolean::True = MultiBoolean(TrueValue);
const MultiBoolean MultiBoolean::False = MultiBoolean(FalseValue);
const MultiBoolean MultiBoolean::Unknown = MultiBoolean(UnknownValue);
const MultiBoolean MultiBoolean::Undefine = MultiBoolean(UndefineValue);
const MultiBoolean MultiBoolean::Nil = MultiBoolean(NilValue);
//为C++版提供便捷的逻辑状态定义
const MultiBoolean True = MultiBoolean::True;
const MultiBoolean False = MultiBoolean::False;
const MultiBoolean Unknown = MultiBoolean::Unknown;
const MultiBoolean Undefine = MultiBoolean::Undefine;
const MultiBoolean Nil = MultiBoolean::Nil;
//非运算
MultiBoolean operator !(const MultiBoolean & x)
{
return MultiBoolean(-(x._value));
};
//相等比较
MultiBoolean operator == (const MultiBoolean & x, const MultiBoolean & y)
{
if(x._value.real() == 0 && y._value.real() == 0)
return MultiBoolean(complex<int>(0, min(x._value.imag(), y._value.imag())));
if(x._value.real() == 0 && y._value.real() != 0)
return MultiBoolean(x._value);
if(x._value.real() != 0 && y._value.real() == 0)
return MultiBoolean(y._value);
return MultiBoolean(x._value.real() == y._value.real() ? True : False);
};
MultiBoolean operator==(const MultiBoolean & x, const bool & y)
{
if(x._value.real() == 0)
return x;
return MultiBoolean((x._value.real() == 1) == y);
};
MultiBoolean operator==(const bool & x, const MultiBoolean & y)
{
if(y._value.real() == 0)
return y;
return MultiBoolean(x == (y._value.real() == 1));
};
//不等比较
MultiBoolean operator!=(const MultiBoolean & x, const MultiBoolean & y)
{
if(x._value.real() == 0 && y._value.real() == 0)
return MultiBoolean(complex<int>(0, min(x._value.imag(), y._value.imag())));
if(x._value.real() == 0 && y._value.real() != 0)
return MultiBoolean(x._value);
if(x._value.real() != 0 && y._value.real() == 0)
return MultiBoolean(y._value);
return MultiBoolean(x._value.real() == y._value.real() ? False : True);
};
MultiBoolean operator!=(const MultiBoolean & x, const bool & y)
{
if(x._value.real() == 0)
return x;
return MultiBoolean((x._value.real() == -1) == y);
};
MultiBoolean operator!=(const bool & x, const MultiBoolean & y)
{
if(y._value.real() == 0)
return y;
return MultiBoolean(x == (y._value.real() == -1));
};
//与运算
MultiBoolean operator && (const MultiBoolean & x, const MultiBoolean & y)
{
if((x._value == MultiBoolean::NilValue) || (y._value == MultiBoolean::NilValue))
return Nil;
if(x._value == MultiBoolean::UndefineValue)
return y;
if(y._value == MultiBoolean::UndefineValue)
return x;
return MultiBoolean(x._value.real() < y._value.real() ? x._value : y._value);
};
MultiBoolean operator && (const MultiBoolean & x, const bool & y)
{
return x && MultiBoolean(y);
}
MultiBoolean operator && (const bool & x, const MultiBoolean & y)
{
return MultiBoolean(x) && y;
};
//或运算
MultiBoolean operator || (const MultiBoolean & x, const MultiBoolean & y)
{
if(x._value == MultiBoolean::NilValue || y._value == MultiBoolean::NilValue)
return Nil;
if(x._value == MultiBoolean::UndefineValue)
return MultiBoolean(y._value);
if(y._value == MultiBoolean::UndefineValue)
return MultiBoolean(x._value);
return MultiBoolean(x._value.real() > y._value.real() ? x._value : y._value);
};
MultiBoolean operator || (const bool & x, const MultiBoolean & y)
{
return MultiBoolean(x) || y;
};
MultiBoolean operator || (const MultiBoolean & x, const bool & y)
{
return x || MultiBoolean(y);
};
//异或运算
MultiBoolean operator ^ (const MultiBoolean & x, const MultiBoolean & y)
{
return (!x&&y)||(x&&!y);
};
MultiBoolean operator ^ (const bool & x, const MultiBoolean & y)
{
return (!x&&y)||(x&&!y);
};
MultiBoolean operator ^ (const MultiBoolean & x, const bool & y)
{
return (!x&&y)||(x&&!y);
};
}
#endif
pyMultiBoolean.h
////////////////////////////////////////////////////////////
//与/或运算的Python接口封装
//作者:刘鑫
//Python中的与/或运算由&和|表达,为了不与C++中的按位与/或
//定义冲突,将这两个运算符定义放到单独的头文件中,仅供Python
//封装定义。
////////////////////////////////////////////////////////////
#ifndef __PYMULTIBOOLEAN__
#define __PYMULTIBOOLEAN__
#include "MultiBoolean.h"
namespace MarchLibrary{
//与运算
MultiBoolean operator & (MultiBoolean x, MultiBoolean y)
{
return x && y;
};
MultiBoolean operator & (MultiBoolean x, bool y)
{
return x && y;
}
MultiBoolean operator & (bool x, MultiBoolean y)
{
return x && y;
};
//或运算
MultiBoolean operator | (MultiBoolean x, MultiBoolean y)
{
return x || y;
};
MultiBoolean operator | (bool x, MultiBoolean y)
{
return x || y;
};
MultiBoolean operator | (MultiBoolean x, bool y)
{
return x || y;
};
}
#endif
wrapper.cpp
///////////////////////////////////////////////////////////
//MultiBoolean类的Python封装,使用boost::python技术
//作者:刘鑫
///////////////////////////////////////////////////////////
//引用boost库
#include <boost/python.hpp>
//引用多值逻辑定义
#include "MultiBoolean.h"
//引用与或运算的Python封装接口
#include "pyMultiBoolean.h"
//引用相关的命名空间
using namespace boost::python;
using namespace MarchLibrary;
//模块定义
BOOST_PYTHON_MODULE(MarchLibrary)
{
//类封装,这里用no_init表示没有可用的构造函数
class_<MultiBoolean>("MultiBoolean", no_init)
//可选的逻辑值接口
.def_readonly("True", &MultiBoolean::True)
.def_readonly("False", &MultiBoolean::False)
.def_readonly("Unknown", &MultiBoolean::Unknown)
.def_readonly("Undefine", &MultiBoolean::Undefine)
.def_readonly("Nil", &MultiBoolean::Nil)
//兼容C++版定义的字符串处理接口
.def("ToString", &MultiBoolean::ToString)
.def("FullName", &MultiBoolean::FullName)
.def("Parse", &MultiBoolean::Parse)
//逻辑值判定
.def("IsTrue", &MultiBoolean::IsTrue)
.def("IsFalse", &MultiBoolean::IsFalse)
.def("IsUnknown", &MultiBoolean::IsUnknown)
.def("IsUndefine", &MultiBoolean::IsUndefine)
.def("IsNil", &MultiBoolean::IsNil)
//运算符重载接口
.def(!self)
.def(self &= self)
.def(self &= bool())
.def(self |= self)
.def(self |= bool())
.def(self ^= self)
.def(self ^= bool())
.def(self == self)
.def(self == bool())
.def(bool() == self)
.def(self != self)
.def(self != bool())
.def(bool() != self)
.def(self & self)
.def(self & bool())
.def(bool() & self)
.def(self | self)
.def(self | bool())
.def(bool() | self)
.def(self ^ self)
.def(self ^ bool())
.def(bool() ^ self)
//提供给Python解释器的标准字符串输出接口
.def("__str__", &MultiBoolean::ToString)
.def("__repr__", &MultiBoolean::FullName)
;
}
在已编译和配置好Boost1.33.0的前提下,将以上三个源代码文件编译为一个名为 "MarchLibrary"的动态链接库(扩展名视具体的操作系统而定),放到Python的DLLs目录下,就可以使用,该模块的名称为 “MarchLibrary”,多值逻辑类名为“MultiBoolean”。
相关推荐
基于C/C++/Python的计算机视觉模型,使用OpenPose、OpenCV、DLIB、Keras和Tensorflow库。目标检测、跟踪、人脸识别、手势、情绪和姿势识别 C/C++/Python based computer DLIB:计算机视觉和其他机器学习的现代C++...
Hikyuu Quant Framework是一款基于C++/Python的开源量化交易研究框架,用于策略分析及回测(仅受限于数据,如有数据也可用于期货等)。其核心思想基于当前成熟的系统化交易方法,将整个系统化交易抽象为由市场环境...
基于C++/Python的开源量化交易研究框架
该工具支持常用C/C++/JAVA/PYTHON/PERL等流行语言的代码统计工具,便于项目统计和项目度量输出。内附工具使用教程和命令参数,可以支持代码差异统计
基于C++/Python的开源量化交易研究框架。Hikyuu Quant Framework是一款基于C++/Python的开源量化交易研究框架,用于策略分析及回测(目前主要用于国内A股市场)。其核心思想基于当前成熟的系统化交易方法,将整个...
基于OpenCV4.0 C++/Python SDK的案例代码演示程序
跌倒检测,YOLOV8S,只依赖OPENCV,支持C++/PYTHON
基于C++/Python的量化交易研究框架,用于策略分析及回测(仅受限于数据,如有数据也可用于期货等)。其核心思想基于当前成熟的系统化交易方法,将整个系统化交易抽象为由市场环境判断策略、系统有效条件、信号指示器...
C++/python opencv_traincascade 训练自己的检测器 包括了自动化脚本 自己获取图片路径
文件包含tensorflow1.14模型文件(VGG16),pb格式的,C++调用python脚本程序代码,代码中包含向python文件中传值,并从python文件中取值。系统:ubuntu系统,编译器:qt5。
免费软件,可自动分析C/C++/Java/Python/C#/Objective-C等项目的源代码并生成流程图和函数调用关系图。生成的流程图可以和源代码互相定位,有助于分析源代码,是阅读其他人写的代码或者自己很久前写的代码的利器。
支持实盘/仿真/回测的股票期货金融软件+支持C/C++/Python/Excel/VBA/麦语言的量化分析交易平台; 采用内存DB+多线程/多进程无锁化技术+全市场实时数据推送+K线图表分析+算法引擎+插件化设计支持全面定制和单机/...
手势识别,采用OPENCV调用YOLO4TINY模型,不需要其他依赖,支持C++/PYTHON
用C++实现的源码计算工具,能计算C++ python flex 的源码。压缩包中包括编译好的exe和源码(包括测试代码和类图),个人感觉C++代码写得还行,enjoy!
YDLidar激光雷达的开发,在win10和Linux下,使用C++/python开发 ,压缩包中包含SDK配置文件 ,ROS配置文件,USB驱动接口,以及官方给的TOOL即Lidarviewer雷达云图查看器,以及YDlidar使用说明和开发手册,保证小白也...
Hikyuu Quant Framework是一款基于C++/Python的开源量化交易研究框架,用于策略分析及回测(仅受限于数据,如有数据也可用于期货等)。其核心思想基于当前成熟的系统化交易方法,将整个系统化交易抽象为由市场环境...
资源包括python中文手册,C与C++库函数速查手册,C语言标准库函数大全和C语言资料大全等资源,给大家查找使用
主要包括:pp pid stanley lqr mpc 的c++版和python版代码实现