liebitarray.h

来自「某个实验事编写粗糙集智能信息处理的程序」· C头文件 代码 · 共 131 行

H
131
字号
//liebitarray.h

/*-----------------------------------------------------------------
 * 文 件 名:   liebitarray.h
 * 类    名: 	 ClieBitArray
 * 			 位串
 * 作    者:   李鄂 
 * 创建时间: 2001.9.23
 * 目    标: 实现跟内置数据类型(整形)一样的功能
 *           一样的操作接口
 * 实现功能:
 *			左,右移位操作 << >>(尚未完成! 不可用)
 *			位操作,与,或,非,异或 &  | ~ ^
 *			对某一位进行赋值或者清0  SetBit() ,ClearBit()
 *			比较运算(等于,不等) ==    !=
 *
 *			overloading operator []  
					//自能获取数据,不能改写数据
 *--------------------------------------------------*/

#if !defined _KYLIN_LIE_BIT_ARRAY_H_
#define _KYLIN_LIE_BIT_ARRAY_H_ 
class CLieBitArray
{
public:
	CLieBitArray(int length=32);
	CLieBitArray(const CLieBitArray& obj);			//copy constructor	
	virtual ~CLieBitArray();

	void ClearAll();//把位串的所有位清零
	void SetAll();//把位串的所有位置1

	inline int GetBitLength()const { return m_iLength;}//返回位串中bit位的长度
	int GetULLength()const {return m_iDataLength;}//返回位串中元素(unsigned long型)的个数

	CLieBitArray operator & (const CLieBitArray&)const;//返回位串之间的&(与)运算,并没有改变原位串的值
	CLieBitArray operator | (const CLieBitArray&)const;//返回位串之间的|(或)运算
	CLieBitArray operator |= (const CLieBitArray&);
	CLieBitArray operator ^ (const CLieBitArray&)const;//返回位串之间的^(异或)运算
	CLieBitArray operator = (const CLieBitArray& obj);//重载=运算符,功能和拷贝构造函数相同
	CLieBitArray Flip(int index);//把位串的第index位变反
private:	//the following methods are not complete
			// so i make it private
	CLieBitArray operator << (int step)const;//将位串左移step位
	CLieBitArray operator >> (int step)const;//将位串右移step位
public:
	CLieBitArray operator ~ ()const;//返回位串的~(求反)运算

	bool operator <(const CLieBitArray& obj)const;//比较两个位串的大小,ture表示该对象小于比较的对象
	bool operator >(const CLieBitArray& obj)const;

	int Get1s()const;//获得该位串中1的个数
	int Get0s()const;//获得该位串中0的个数
	bool SetBit(int index);			//将index位bit置为1
	bool ClearBit(int index);		//将index位清0
	unsigned long GetULAt(int index)const//返回位串中第index个元素(unsigned long型)
		{return m_pData[index];}
	unsigned long SetULAt(int index,unsigned long value)
		{return m_pData[index]=value;	}//将位串的第index个元素置为value,并把它返回
	bool operator == (const CLieBitArray&)const;//比较两个位串以否相等
	bool operator != (const CLieBitArray&)const;//判断两个位串是否不等
	bool operator  [](int index) const;//判断位串的第index位是否为1
private:
	bool SetValue(int index,unsigned long newValue);//给数组的第index的元素赋值newValue
	unsigned long *m_pData;
	int m_iLength;			//bit串长度
	int m_iDataLength;		//m_pData长度

public:	

#ifdef _DEBUG
	unsigned long getdata()const //供调试用,获取data值
	{return *m_pData;} 
#endif //_DEBUG

private:
	static unsigned long data_1s[32];
	static unsigned long data_0s[32];

};


/*-------------------------------------------------

  类名: CLieOutBitArray
  说明:
		这个类包装了CLieBitArray类,增加了一个成员num,
	记录一个数.并引出了CLieBitArray的几个成员函数.它
	使用CLieBitArray作为其成员,依赖于CLieBitArray.(为
	什么不用继承呢?因为该类不能算是CLieBitArray的一个
	子类.)
		该类实际上可以使用std::map替换

  方法:
     函数:
	    1. AddNum() 增加num值
		2. SetNum() 设置num值
		3. GetNum() 获取num值

     重载运算符:
		  1. ==		等于(自己和自己比较	    )
		  2. ==			(和CLieBitArray比较 )
--------------------------------------------------*/
/*
class CLieOutBitArray
{
	CLieBitArray array;
	unsigned num;			//记录一个数值,初值为 1
public:
	CLieOutBitArray(CLieBitArray x):array(x),num(1){}
	const CLieBitArray* GetInnerBitArray()const { return &array; }
	bool operator ==(const CLieOutBitArray& obj)
	{
		return array == obj.array;
	}
	bool operator ==(const CLieBitArray& obj)
	{
		return array == obj;
	}
	int AddNum(){return ++num;}
	int GetNum()const {return num;}
	int SetNum(int newValue) { return num=newValue;}

	
#ifdef _DEBUG
	unsigned long getdata()const {return array.getdata();}
#endif //_DEBUG

};
*/
#endif // _KYLIN_LIE_BIT_ARRAY__

⌨️ 快捷键说明

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