//-----------------------------------------------------------------------------+ // Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd. // = FileName : TBitTable.cpp // = Version : ver2.0 // = Author : xlc // = CreateDate : 2002-09-09 // = Description: 类的定义 // = Maintainers: // //-----------------------------------------------------------------------------+ #include "StdAfx.h" #include "TBitTable.h" TBitTable::TBitTable(long unitNum,long unitSize) : bOwner(true),length(unitNum),unit(unitSize) { if(unitNum > 0) { pData = new char[GetByteSize()]; } else { pData = NULL; } Reset(); } TBitTable::TBitTable(void* p,long byteSize, bool bClient) : bOwner(!bClient),unit(1) { if(bOwner) { pData = new char[byteSize]; memcpy(p, pData, byteSize); } else { pData = (char*)p; } length = byteSize * 8; } TBitTable& TBitTable::operator = (const TBitTable& bTable) { SetUnitSize(bTable.GetUnitSize()); Realloc(bTable.GetLength()); memcpy(pData,(const char*)bTable,bTable.GetByteSize()); return *this; } void TBitTable::Attach(long byteSize,char *p) { if(pData != NULL && bOwner) { delete[] pData; } pData = p; length = byteSize * 8 / unit; } char* TBitTable::Detach() { char* p = pData; pData = NULL; length = 0; return p; } TBitTable::~TBitTable() { if(pData != NULL && bOwner) { delete[] pData; } } long TBitTable::BitToByte(long bitNum) { long byteSize = bitNum / 8; if(bitNum % 8 != 0) { byteSize++; } return byteSize; } bool TBitTable::IsValidIndex(long i) const { return i >= 0 && i < length; } long TBitTable::GetLength() const { return length; } long TBitTable::GetByteSize() const { return BitToByte(length * unit); } long TBitTable::GetBitSize() const { return length * unit; } void TBitTable::SetUnitSize(long size) { long oldUnit = unit; unit = size; length *= oldUnit / unit; } long TBitTable::GetUnitSize() const { return unit; } void TBitTable::Realloc(long unitNum) { if(unitNum != length) { char *p = pData; if(unitNum > 0) { pData = new char[BitToByte(unitNum * unit)]; } else { pData = NULL; } if(pData != NULL && p != NULL) { if(length < unitNum) { memcpy(pData, p, BitToByte(length * unit)); } else { memcpy(pData, p, BitToByte(unitNum * unit)); } } length = unitNum; if(p != NULL && bOwner) { delete[] p; } } } //bit read/write,使用位索引,非单元索引! bool TBitTable::operator[](long i) { long byteIndex,bitIndex; BitToByte(i, byteIndex, bitIndex); char mask = 1 << bitIndex; return (pData[byteIndex] & mask) != 0; } void TBitTable::Set(long i) { long byteIndex,bitIndex; BitToByte(i, byteIndex, bitIndex); char mask = 1 << bitIndex; (pData[byteIndex]) |= mask; } void TBitTable::Reset(long i) { long byteIndex,bitIndex; BitToByte(i, byteIndex, bitIndex); char mask = ~(1 << bitIndex); (pData[byteIndex]) &= mask; } //unit read/write long TBitTable::GetValue(long i) const { long byteIndex,bitIndex; UnitToByte(i,byteIndex,bitIndex); char res = (pData[byteIndex] >> bitIndex); if(unit == 1) { return res & 1; } else if(unit == 2) { return res & 3; } else { return res & 15; } } void TBitTable::SetValue(long i,char value) { long byteIndex,bitIndex; UnitToByte(i,byteIndex,bitIndex); (pData[byteIndex])|=value << bitIndex; } void TBitTable::SetAll(char value) { for(long i = 0; i < length; i++) { SetValue(i,value); } } void TBitTable::SetAll() { if(pData != NULL) { memset(pData,255,GetByteSize()); } } void TBitTable::Reset() { if(pData != NULL) { memset(pData,0,GetByteSize()); } } //data transfer TBitTable::operator char() const { return *(pData); } TBitTable::operator int() const { return *((int*)pData); } TBitTable::operator float() const { return *((float*)pData); } TBitTable::operator double() const { return *((double*)pData); } TBitTable::operator const char*() const { return pData; } TBitTable::operator const int*() const { return (int*)pData; } TBitTable::operator const float*() const { return (float*)pData; } TBitTable::operator const double*() const { return (double*)pData; } void TBitTable::BitToByte(long i,long& byteIndex, long& bitIndex) const { byteIndex = i / 8; bitIndex = i % 8; } //单元定位 void TBitTable::UnitToByte(long i, long& byteIndex, long& bitIndex) const { BitToByte( i * unit, byteIndex, bitIndex); }