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