BitVector.cpp
上传用户:jxd368
上传日期:2013-06-20
资源大小:66k
文件大小:2k
源码类别:

文本生成

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include ".bitvector.h"
  3. CBitVector::CBitVector(int iSize)
  4. {m_uipArray=0;
  5.  m_iSize=0;
  6.  Resize(iSize);
  7. }
  8. // ----------------------------------------------------------------
  9. //  Name:           Resize
  10. //  Description:    Resizes the bitvector
  11. //  Arguments:      - iIndex: The size in BITS of the vector.
  12. //  Return Value:   None.
  13. // ----------------------------------------------------------------
  14. void CBitVector::Resize(int iSize)
  15. {
  16.  unsigned long int *ulpVector;
  17.  if(0==iSize%32)
  18.  iSize=iSize/32;
  19.  else
  20.  iSize=iSize/32+1;
  21.  ulpVector=new unsigned long int[iSize];
  22.  if(0==ulpVector)
  23.  return;
  24.  int iMin;
  25.  if(m_iSize<iSize)
  26.  iMin=m_iSize;
  27.  else
  28.  iMin=iSize;
  29.  for(int i=0;i<iMin;i++)
  30.      ulpVector[i]=m_uipArray[i];
  31.  if(m_uipArray)
  32.  {
  33.  delete []m_uipArray;
  34.  m_uipArray=0;
  35.  }
  36.  m_iSize=iSize;
  37.  m_uipArray=ulpVector;
  38. }
  39. // ----------------------------------------------------------------
  40. //  Name:           operator
  41. //  Description:    重载操作符
  42. //  Arguments:      - iIndex: 被操作的bit.
  43. //  Return Value:   第iIndex位的值 .
  44. // ----------------------------------------------------------------
  45. bool CBitVector::operator [](int iIndex)
  46. {
  47.  int cell=iIndex/32; 
  48.  int bit=iIndex%32;
  49.  return (m_uipArray[cell]&(1<<bit))>>bit;
  50. }
  51. void CBitVector::clearAll()
  52. {
  53.  //for(int i=0;i<m_iSize;i++)
  54.   // m_uipArray[i]=0;
  55.  memset(m_uipArray,0,m_iSize*sizeof(unsigned long int));
  56. }
  57. void CBitVector::setAll()
  58. {
  59.  //for(int i=0;i<m_iSize;i++)
  60. // m_uipArray[i]=0xFFFFFFFF;
  61.  memset(m_uipArray,0xFFFFFFFF,m_iSize*sizeof(unsigned long int));
  62. }
  63. void CBitVector::set(int iIndex,bool bValue)
  64. {
  65. int cell=iIndex/32; 
  66. int bit=iIndex%32;
  67. if(bValue)
  68. m_uipArray[cell]=m_uipArray[cell]|(1<<bit);
  69. else 
  70. m_uipArray[cell]=m_uipArray[cell]&(~(1<<bit));
  71. }
  72. CBitVector::~CBitVector(void)
  73. {
  74. delete []m_uipArray;
  75. }