简单的BitVector

class BitVector { public: explicit BitVector(int size) { __size = size; __memlen = (size>>3) + 1; __table = new unsigned char[__memlen]; } ~BitVector() { if(__table) delete[] __table; } void SetBit(int pos) { __table[pos>>3] |= __mask[pos&7]; } void UnSetBit(int pos) { __table[pos>>3] &= ~__mask[pos&7]; } bool TestBit(int pos) { return (__table[pos>>3] & __mask[pos&7]); } void SetVector() { memset(__table, 1, __memlen); } void UnSetVector() { memset(__table, 0, __memlen); } void PushBack(bool value) { if(__size + 1 >= (__memlen<<3)) { unsigned char* tmp = new unsigned char[__memlen+DefaultVectorSize]; memcpy(tmp, __table, __memlen); __memlen += DefaultVectorSize; delete __table; __table = tmp; } if(value) SetBit(__size + 1); else UnSetBit(__size + 1); __size++; } int GetSize(){ return __size; } void Destroy() { delete[] __table; __table = 0; } void Clear() { __size = 0; } private: unsigned int __memlen; unsigned int __size; static const unsigned char/ __mask[8]; unsigned char* __table; }; const unsigned char BitVector::__mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };

 

你可能感兴趣的:(table,delete,Class)