BitVector.cpp
资源名称:CText2.rar [点击查看]
上传用户:jxd368
上传日期:2013-06-20
资源大小:66k
文件大小:2k
源码类别:
文本生成
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".bitvector.h"
- CBitVector::CBitVector(int iSize)
- {m_uipArray=0;
- m_iSize=0;
- Resize(iSize);
- }
- // ----------------------------------------------------------------
- // Name: Resize
- // Description: Resizes the bitvector
- // Arguments: - iIndex: The size in BITS of the vector.
- // Return Value: None.
- // ----------------------------------------------------------------
- void CBitVector::Resize(int iSize)
- {
- unsigned long int *ulpVector;
- if(0==iSize%32)
- iSize=iSize/32;
- else
- iSize=iSize/32+1;
- ulpVector=new unsigned long int[iSize];
- if(0==ulpVector)
- return;
- int iMin;
- if(m_iSize<iSize)
- iMin=m_iSize;
- else
- iMin=iSize;
- for(int i=0;i<iMin;i++)
- ulpVector[i]=m_uipArray[i];
- if(m_uipArray)
- {
- delete []m_uipArray;
- m_uipArray=0;
- }
- m_iSize=iSize;
- m_uipArray=ulpVector;
- }
- // ----------------------------------------------------------------
- // Name: operator
- // Description: 重载操作符
- // Arguments: - iIndex: 被操作的bit.
- // Return Value: 第iIndex位的值 .
- // ----------------------------------------------------------------
- bool CBitVector::operator [](int iIndex)
- {
- int cell=iIndex/32;
- int bit=iIndex%32;
- return (m_uipArray[cell]&(1<<bit))>>bit;
- }
- void CBitVector::clearAll()
- {
- //for(int i=0;i<m_iSize;i++)
- // m_uipArray[i]=0;
- memset(m_uipArray,0,m_iSize*sizeof(unsigned long int));
- }
- void CBitVector::setAll()
- {
- //for(int i=0;i<m_iSize;i++)
- // m_uipArray[i]=0xFFFFFFFF;
- memset(m_uipArray,0xFFFFFFFF,m_iSize*sizeof(unsigned long int));
- }
- void CBitVector::set(int iIndex,bool bValue)
- {
- int cell=iIndex/32;
- int bit=iIndex%32;
- if(bValue)
- m_uipArray[cell]=m_uipArray[cell]|(1<<bit);
- else
- m_uipArray[cell]=m_uipArray[cell]&(~(1<<bit));
- }
- CBitVector::~CBitVector(void)
- {
- delete []m_uipArray;
- }
English
