区间泛型类支持区间(通用数据类型)的交、并、减欧拉操作(目前版本不提供补),使用方式参见测试函数
//**********************************************************************
// Copyright (c) 2010
// 迪斯特软件开发小组.
// 文件: Range.hpp
// 内容:
// 历史:
// 序号 修改时间 修改人 修改内容
// 1 2010-7-21 hlq 首次生成
//*********************************************************************
//声明本头文件宏
#ifndef _RANGE_HPP
#define _RANGE_HPP
//包含头文件
#include <algorithm>
#include <iterator>
#include <set>
#include <cassert>
#include "FloatCompare.hpp"
//禁用Window定义的宏min、max
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
//类前向声明
template <typename T> class CRangeT;
template <typename T> class CRangeSetT;
//**********************************************************************
// 类名: CRangeT
// 目的: 区间类
//*********************************************************************
template <typename T>
class CRangeT
{
////类型声明
public:
typedef CRangeT<T> my_type;
typedef T value_type;
typedef std::pair<my_type,my_type> range_pair_type;
////业务处理
public:
//两个区间是否相连
bool IsAdjacent(const my_type& rhs) const { return Equal(m_maxValue,rhs.m_minValue) || Equal(m_minValue,rhs.m_maxValue);}
//两个区间是否相交
bool IsIntersect(const my_type& rhs) const {return Great(m_maxValue,rhs.m_minValue) && Less(m_minValue,rhs.m_maxValue);}
//是否包含目标对象
bool IsContain(const my_type& rhs) const {return GreatEqual(rhs.m_minValue,m_minValue) && LessEqual(rhs.m_maxValue,m_maxValue);}
//得到区间交
const my_type GetIntersect(const my_type& rhs) const;
//得到区间并集
const range_pair_type GetUnion(const my_type& rhs) const;
//得到区间差差集
const range_pair_type GetDifference(const my_type& rhs) const;
//得到区间异或集
const range_pair_type GetExclusiveOr(const my_type& rhs) const;
////操作符重载
public:
//等于操作符重载
bool operator==(const my_type& rhs) const {return Equal(m_minValue,rhs.m_minValue) && Equal(m_maxValue,rhs.m_maxValue);}
//不等于操作符重载
bool operator!=(const my_type& rhs) const {return Unequal(m_minValue,rhs.m_minValue) || Unequal(m_maxValue,rhs.m_maxValue);}
//小于操作符重载
bool operator <(const my_type& rhs) const {return Less(m_minValue,rhs.m_minValue);}
//小于等于操作符重载
bool operator <=(const my_type& rhs) const {return LessEqaul(m_minValue,rhs.m_minValue);}
//大于操作符重载
bool operator >(const my_type& rhs) const {return Great(m_minValue,rhs.m_minValue);}
//大于等于操作符重载
bool operator >=(const my_type& rhs) const {return GreatEqaul(m_minValue,rhs.m_minValue);}
////基本查询
public:
//得到最小值
const value_type& GetMinValue(void) const {return m_minValue;}
//得到最大值
const value_type& GetMaxValue(void) const {return m_maxValue;}
//得到区间值
value_type GetRange(void) const {return m_maxValue - m_minValue;}
//是否为空
bool IsEmpty(void) const {return Equal(GetRange(),0);}
////命令
public:
//设置区间
void SetValues(const value_type aMinValue, const value_type aMaxValue) {m_minValue = aMinValue;m_maxValue = aMaxValue;VerifyConstraint();}
////构造、析构函数
public:
//默认构造函数
CRangeT(const value_type aMinValue = 0, const value_type aMaxValue = 0) : m_minValue(aMinValue),m_maxValue(aMaxValue){VerifyConstraint();}
//零区间
static const my_type& Zero(void) { static my_type s_aZeroInstance(0,0);return s_aZeroInstance;}
////执行函数
private:
//确保区间约束
void VerifyConstraint(void) { if(m_minValue > m_maxValue) std::swap(m_minValue,m_maxValue);}
////数据成员
private:
value_type m_minValue;
value_type m_maxValue;
};
//**********************************************************************
// 函数: GetIntersect
// 功能: 得到区间相交
//*********************************************************************
template <typename T>
inline const CRangeT<T> CRangeT<T>::GetIntersect(const my_type& rhs) const
{
//检查是否相交
if(!IsIntersect(rhs))
return Zero();
//计算相交区间
value_type dMinValue = std::max(m_minValue,rhs.m_minValue);
value_type dMaxValue = std::min(m_maxValue,rhs.m_maxValue);
return my_type(dMinValue,dMaxValue);
}
//**********************************************************************
// 函数: GetUnion
// 功能: 得到并集
//*********************************************************************
template <typename T>
inline const typename CRangeT<T>::range_pair_type CRangeT<T>::GetUnion(const my_type& rhs) const
{
if(IsIntersect(rhs) || IsAdjacent(rhs))
return range_pair_type(my_type(std::min(m_minValue,rhs.m_minValue),std::max(m_maxValue,rhs.m_maxValue)),Zero());
else
return (*this < rhs) ? range_pair_type(*this,rhs) : range_pair_type(rhs,*this);
}
//**********************************************************************
// 函数: GetDifference
// 功能: 区间差
//*********************************************************************
template <typename T>
inline const typename CRangeT<T>::range_pair_type CRangeT<T>::GetDifference(const my_type& rhs) const
{
//检查是否相交
if(!IsIntersect(rhs))
return range_pair_type(*this,Zero());
else if(rhs.IsContain(*this))
return range_pair_type(Zero(),Zero());
//得到相交区间
my_type aTemp = GetIntersect(rhs);
if(Equal(aTemp.m_minValue,m_minValue))
return range_pair_type(my_type(aTemp.m_maxValue,m_maxValue),Zero());
else if(Equal(aTemp.m_maxValue,m_maxValue))
return range_pair_type(my_type(m_minValue,aTemp.m_minValue),Zero());
else
return range_pair_type(my_type(m_minValue,aTemp.m_minValue),my_type(aTemp.m_maxValue,m_maxValue));
}
//**********************************************************************
// 函数: GetExclusiveOr
// 功能: 得到区间异或集
//*********************************************************************
template <typename T>
inline const typename CRangeT<T>::range_pair_type CRangeT<T>::GetExclusiveOr(const my_type& rhs) const
{
//检查是否邻接或相交
if(IsAdjacent(rhs))
return range_pair_type(my_type(std::min(m_minValue,rhs.m_minValue),std::max(m_maxValue,rhs.m_maxValue)),Zero());
else if(!IsIntersect(rhs))
return (*this < rhs) ? range_pair_type(*this,rhs) : range_pair_type(rhs,*this);
//得到差集
range_pair_type aResult = GetDifference(rhs);
if(aResult.first.IsEmpty() || aResult.second.IsEmpty())
{
range_pair_type aTempPair = rhs.GetDifference(*this);
if(!aTempPair.first.IsEmpty())
{
if(aResult.first.IsEmpty())
aResult.first = aTempPair.first;
else
aResult.second = aTempPair.first;
}
if(!aTempPair.second.IsEmpty())
{
if(aResult.first.IsEmpty())
aResult.first = aTempPair.second;
else
aResult.second = aTempPair.second;
}
}
//返回
if((!aResult.first.IsEmpty()) && (!aResult.second.IsEmpty()) && (aResult.first > aResult.second))
std::swap(aResult.first,aResult.second);
return aResult;
}
//**********************************************************************
// 类名: CRangeSetT
// 目的: 区间集合类
//*********************************************************************
template <typename T>
class CRangeSetT
{
////类型声明
public:
typedef CRangeSetT<T> my_type;
typedef std::set<CRangeT<T> > container_type;
typedef typename container_type::value_type range_type;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;
typedef typename container_type::size_type size_type;
typedef size_type index_type;
typedef typename range_type::value_type value_type;
typedef typename range_type::range_pair_type range_pair_type;
////业务处理
public:
//区间并
void Union(const range_type& aRange);
//区间并
void Union(const value_type dMinValue,const value_type dMaxValue) { Union(range_type(dMinValue,dMaxValue));}
//区间差
void Difference(const range_type& aRange);
//区间差
void Difference(const value_type dMinValue,const value_type dMaxValue) { Difference(range_type(dMinValue,dMaxValue));}
//区间合并
const my_type GetUnion(const my_type& aRangeSet) const;
//区间差
const my_type GetDifference(const my_type& aRangeSet) const;
////操作符重载
public:
//等于操作符重载
bool operator==(const my_type& rhs) const {return m_aRanges == rhs.m_aRanges;}
//不等于操作符重载
bool operator!=(const my_type& rhs) const {return m_aRanges != rhs.m_aRanges;}
////基本查询
public:
//得到集合数量
size_type GetCount(void) const {return m_aRanges.size();}
//得到指定区间
const range_type& Get(size_type nIndex) const { const_iterator p = begin(); std::advance(p,nIndex);return *p;}
////命令
public:
//清除
void Clear(void) { m_aRanges.clear();}
////迭代器实现
public:
iterator begin(void) { return m_aRanges.begin();}
const_iterator begin(void) const { return m_aRanges.begin();}
iterator end(void) { return m_aRanges.end();}
const_iterator end(void) const { return m_aRanges.end();}
////执行函数
private:
//合并相邻区间
void CombineAdjacentRanges(void);
////数据成员
private:
container_type m_aRanges; //区间集合
};
//**********************************************************************
// 函数: CombineAdjacentRange
// 功能: 合并相邻区间
//*********************************************************************
template <typename T>
inline void CRangeSetT<T>::CombineAdjacentRanges(void)
{
bool bFound = true;
do
{
bFound = false;
iterator pEnd = end();
iterator p = begin();
iterator pNext = p;
for(++pNext; (p != pEnd) && (pNext != pEnd);++p,++pNext)
{
if(p->IsAdjacent(*pNext))
{
*p = p->GetUnion(*pNext).first;
m_aRanges.erase(pNext);
bFound = true;
break;
}
}
}while(bFound);
}
//**********************************************************************
// 函数: Union
// 功能: 区间并
//*********************************************************************
template <typename T>
inline void CRangeSetT<T>::Union(const range_type& aRange)
{
//假如不存在元素,直接添加
if(m_aRanges.empty())
{
m_aRanges.insert(aRange);
return;
}
//假如某个区间包含当前区间,忽略返回
iterator pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(p->IsContain(aRange))
return;
}
//剔除本区间完整包含的已有区间
bool bFound = true;
do
{
bFound = false;
iterator pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(aRange.IsContain(*p))
{
m_aRanges.erase(p);
bFound = true;
break;
}
}
}while(bFound);
//调整与本区间相交的已有区间
pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(p->IsIntersect(aRange))
{
range_type aTemp = p->GetIntersect(aRange);
*p = p->GetDifference(aTemp).first;
}
}
//添加本区间
m_aRanges.insert(aRange);
//合并相邻区间
CombineAdjacentRanges();
}
//**********************************************************************
// 函数: Difference
// 功能: 区间差
//*********************************************************************
template <typename T>
inline void CRangeSetT<T>::Difference(const range_type& aRange)
{
//剔除当前区间包含的子区间
bool bFound = true;
do
{
bFound = false;
iterator pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(aRange.IsContain(*p))
{
m_aRanges.erase(p);
bFound = true;
break;
}
}
}while(bFound);
//处理子区间完全包含当前区间的情况
iterator pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(p->IsContain(aRange))
{
range_pair_type aPair = p->GetDifference(aRange);
m_aRanges.erase(p);
if(!aPair.first.IsEmpty())
m_aRanges.insert(aPair.first);
if(!aPair.second.IsEmpty())
m_aRanges.insert(aPair.second);
return;
}
}
//调整处理交叉区间
pEnd = end();
for(iterator p = begin(); p != pEnd;++p)
{
if(p->IsIntersect(aRange))
{
range_pair_type aPair = p->GetDifference(aRange);
*p = aPair.first;
}
}
}
//**********************************************************************
// 函数: GetUnion
// 功能: 区间合并
//*********************************************************************
template <typename T>
inline const CRangeSetT<T> CRangeSetT<T>::GetUnion(const my_type& aRangeSet) const
{
my_type aTemp(aRangeSet);
const_iterator pEnd = end();
for(const_iterator p = begin(); p != pEnd;++p)
aTemp.Union(*p);
return aTemp;
}
//**********************************************************************
// 函数: GetDifference
// 功能: 区间差
//*********************************************************************
template <typename T>
inline const CRangeSetT<T> CRangeSetT<T>::GetDifference(const my_type& aRangeSet) const
{
my_type aTemp(aRangeSet);
const_iterator pEnd = end();
for(const_iterator p = begin(); p != pEnd;++p)
aTemp.Difference(*p);
return aTemp;
}
//**********************************************************************
// 函数: CRangeT_Test
// 功能: 区间测试
//*********************************************************************
template<typename T>
inline void CRangeT_Test(void)
{
//类型声明
typedef CRangeT<T> range_type;
typedef typename range_type::value_type value_type;
typedef typename range_type::range_pair_type range_pair_type;
//声明变量
value_type dMinValue = 100;
value_type dMaxValue = 200;
value_type dValueLess1 = 60;
value_type dValueLess2 = 80;
value_type dValue1 = 120;
value_type dValue2 = 140;
value_type dValue3 = 160;
value_type dValue4 = 180;
value_type dValueGreat1 = 220;
value_type dValueGreat2 = 240;
range_type aRange(dMinValue,dMaxValue);
//测试用例1: 交测试
assert(aRange.GetIntersect(range_type(dValueLess1,dValueLess2)) == range_type::Zero());
assert(aRange.GetIntersect(range_type(dValueLess1,dMinValue)) == range_type::Zero());
assert(aRange.GetIntersect(range_type(dValueLess1,dValue1)) == range_type(dMinValue,dValue1));
assert(aRange.GetIntersect(range_type(dValueLess1,dMaxValue)) == range_type(dMinValue,dMaxValue));
assert(aRange.GetIntersect(range_type(dValueLess1,dValueGreat1)) == range_type(dMinValue,dMaxValue));
assert(aRange.GetIntersect(range_type(dMinValue,dMinValue)) == range_type::Zero());
assert(aRange.GetIntersect(range_type(dMinValue,dValue1)) == range_type(dMinValue,dValue1));
assert(aRange.GetIntersect(range_type(dMinValue,dMaxValue)) == range_type(dMinValue,dMaxValue));
assert(aRange.GetIntersect(range_type(dMinValue,dValueGreat1)) == range_type(dMinValue,dMaxValue));
assert(aRange.GetIntersect(range_type(dValue1,dValue2)) == range_type(dValue1,dValue2));
assert(aRange.GetIntersect(range_type(dValue1,dMaxValue)) == range_type(dValue1,dMaxValue));
assert(aRange.GetIntersect(range_type(dValue1,dValueGreat1)) == range_type(dValue1,dMaxValue));
assert(aRange.GetIntersect(range_type(dMaxValue,dValueGreat1)) == range_type::Zero());
assert(aRange.GetIntersect(range_type(dValueGreat1,dValueGreat2)) == range_type::Zero());
//测试用例2: 并测试
assert(aRange.GetUnion(range_type(dValueLess1,dValueLess2)) == std::make_pair(range_type(dValueLess1,dValueLess2),range_type(dMinValue,dMaxValue)));
assert(aRange.GetUnion(range_type(dValueLess1,dMinValue)) == std::make_pair(range_type(dValueLess1,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValueLess1,dValue1)) == std::make_pair(range_type(dValueLess1,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValueLess1,dMaxValue)) == std::make_pair(range_type(dValueLess1,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValueLess1,dValueGreat1)) == std::make_pair(range_type(dValueLess1,dValueGreat1),range_type::Zero()));
assert(aRange.GetUnion(range_type(dMinValue,dMinValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dMinValue,dValue1)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dMinValue,dMaxValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dMinValue,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValueGreat1),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValue1,dValue2)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValue1,dMaxValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValue1,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValueGreat1),range_type::Zero()));
assert(aRange.GetUnion(range_type(dMaxValue,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValueGreat1),range_type::Zero()));
assert(aRange.GetUnion(range_type(dValueGreat1,dValueGreat2)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type(dValueGreat1,dValueGreat2)));
//测试用例3: 差测试
assert(aRange.GetDifference(range_type(dValueLess1,dValueLess2)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValueLess1,dMinValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValueLess1,dValue1)) == std::make_pair(range_type(dValue1,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValueLess1,dMaxValue)) == std::make_pair(range_type::Zero(),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValueLess1,dValueGreat1)) == std::make_pair(range_type::Zero(),range_type::Zero()));
assert(aRange.GetDifference(range_type(dMinValue,dMinValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dMinValue,dValue1)) == std::make_pair(range_type(dValue1,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dMinValue,dMaxValue)) == std::make_pair(range_type::Zero(),range_type::Zero()));
assert(aRange.GetDifference(range_type(dMinValue,dValueGreat1)) == std::make_pair(range_type::Zero(),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValue1,dValue2)) == std::make_pair(range_type(dMinValue,dValue1),range_type(dValue2,dMaxValue)));
assert(aRange.GetDifference(range_type(dValue1,dMaxValue)) == std::make_pair(range_type(dMinValue,dValue1),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValue1,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValue1),range_type::Zero()));
assert(aRange.GetDifference(range_type(dMaxValue,dValueGreat1)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetDifference(range_type(dValueGreat1,dValueGreat2)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
//测试用例4: 异或
assert(aRange.GetExclusiveOr(range_type(dValueLess1,dValueLess2)) == std::make_pair(range_type(dValueLess1,dValueLess2),range_type(dMinValue,dMaxValue)));
assert(aRange.GetExclusiveOr(range_type(dValueLess1,dMinValue)) == std::make_pair(range_type(dValueLess1,dMaxValue),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dValueLess1,dValue1)) == std::make_pair(range_type(dValueLess1,dMinValue),range_type(dValue1,dMaxValue)));
assert(aRange.GetExclusiveOr(range_type(dValueLess1,dMaxValue)) == std::make_pair(range_type(dValueLess1,dMinValue),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dValueLess1,dValueGreat1)) == std::make_pair(range_type(dValueLess1,dMinValue),range_type(dMaxValue,dValueGreat1)));
assert(aRange.GetExclusiveOr(range_type(dMinValue,dMinValue)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dMinValue,dValue1)) == std::make_pair(range_type(dValue1,dMaxValue),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dMinValue,dMaxValue)) == std::make_pair(range_type::Zero(),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dMinValue,dValueGreat1)) == std::make_pair(range_type(dMaxValue,dValueGreat1),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dValue1,dValue2)) == std::make_pair(range_type(dMinValue,dValue1),range_type(dValue2,dMaxValue)));
assert(aRange.GetExclusiveOr(range_type(dValue1,dMaxValue)) == std::make_pair(range_type(dMinValue,dValue1),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dValue1,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValue1),range_type(dMaxValue,dValueGreat1)));
assert(aRange.GetExclusiveOr(range_type(dMaxValue,dValueGreat1)) == std::make_pair(range_type(dMinValue,dValueGreat1),range_type::Zero()));
assert(aRange.GetExclusiveOr(range_type(dValueGreat1,dValueGreat2)) == std::make_pair(range_type(dMinValue,dMaxValue),range_type(dValueGreat1,dValueGreat2)));
}
//**********************************************************************
// 函数: CRangeSetT_Test
// 功能: 区间集合测试
//*********************************************************************
template<typename T>
inline void CRangeSetT_Test(void)
{
//执行区间测试
CRangeT_Test<T>();
//类型声明
typedef CRangeSetT<T> range_set_type;
typedef typename range_set_type::range_type range_type;
typedef typename range_type::value_type value_type;
//声明变量
value_type dMinValue = 100;
value_type dMaxValue = 200;
value_type dValueLess1 = 60;
value_type dValueLess2 = 80;
value_type dValue1 = 120;
value_type dValue2 = 140;
value_type dValue3 = 160;
value_type dValue4 = 180;
value_type dValueGreat1 = 220;
value_type dValueGreat2 = 240;
range_set_type aDashedSet,aSolidSet,aFullSet;
aDashedSet.Union(range_type(dMinValue,dMaxValue));
aFullSet.Union(range_type(dMinValue,dMaxValue));
//测试用例1: 区间集并
aSolidSet.Union(range_type(dValue1,dValue2));
aSolidSet.Union(range_type(dValue3,dValue4));
aSolidSet.Union(range_type(dValueGreat1,dValueGreat2));
assert(aSolidSet.GetCount() == 3);
assert(aSolidSet.Get(0) == range_type(dValue1,dValue2));
assert(aSolidSet.Get(1) == range_type(dValue3,dValue4));
assert(aSolidSet.Get(2) == range_type(dValueGreat1,dValueGreat2));
aSolidSet.Union(range_type((dValue1 + dValue2) / 2,(dValue2 + dValue3) / 2));
assert(aSolidSet.GetCount() == 3);
assert(aSolidSet.Get(0) == range_type(dValue1,(dValue2 + dValue3) / 2));
assert(aSolidSet.Get(1) == range_type(dValue3,dValue4));
assert(aSolidSet.Get(2) == range_type(dValueGreat1,dValueGreat2));
aSolidSet.Union(range_type(dValue2,(dValueGreat1 + dValueGreat2) / 2));
assert(aSolidSet.GetCount() == 1);
assert(aSolidSet.Get(0) == range_type(dValue1,dValueGreat2));
//测试用例2: 区间集减
aDashedSet.Difference(range_type(dValue1,dValue2));
assert(aDashedSet.GetCount() == 2);
assert(aDashedSet.Get(0) == range_type(dMinValue,dValue1));
assert(aDashedSet.Get(1) == range_type(dValue2,dMaxValue));
aDashedSet.Difference(range_type(dValue3,dValue4));
assert(aDashedSet.GetCount() == 3);
assert(aDashedSet.Get(0) == range_type(dMinValue,dValue1));
assert(aDashedSet.Get(1) == range_type(dValue2,dValue3));
assert(aDashedSet.Get(2) == range_type(dValue4,dMaxValue));
aDashedSet.Difference(range_type((dMinValue + dValue1) / 2,(dValue4 + dMaxValue) / 2));
assert(aDashedSet.GetCount() == 2);
assert(aDashedSet.Get(0) == range_type(dMinValue,(dMinValue + dValue1) / 2));
assert(aDashedSet.Get(1) == range_type((dValue4 + dMaxValue) / 2,dMaxValue));
//测试用例3: 演示
aDashedSet.Clear();
aDashedSet.Union(range_type(dMinValue,dMaxValue));
aSolidSet.Clear();
assert(aDashedSet.GetUnion(aSolidSet) == aFullSet);
aDashedSet.Difference(range_type(dValue1,dValue2));
aSolidSet.Union(range_type(dValue1,dValue2));
assert(aDashedSet.GetUnion(aSolidSet) == aFullSet);
aDashedSet.Difference(range_type(dValue3,dValue4));
aSolidSet.Union(range_type(dValue3,dValue4));
assert(aDashedSet.GetUnion(aSolidSet) == aFullSet);
aDashedSet.Difference(range_type(dValue2,dValue3));
aSolidSet.Union(range_type(dValue2,dValue3));
assert(aDashedSet.GetUnion(aSolidSet) == aFullSet);
assert(aDashedSet.GetCount() == 2);
assert(aDashedSet.Get(0) == range_type(dMinValue,dValue1));
assert(aDashedSet.Get(1) == range_type(dValue4,dMaxValue));
assert(aSolidSet.GetCount() == 1);
assert(aSolidSet.Get(0) == range_type(dValue1,dValue4));
}
#endif //假如未定义_RANGE_HPP宏
胡乐秋
2010/8/3
http://blog.csdn.net/hlqyq