📄 bitary.cpp
字号:
/************************************************************************
*
* 文件名:bitary.cpp
*
* 文件描述:位数组类
*
* 创建人: jinxiongye@163.com 2006-3-6
*
* 版本号:1.0
*
* 修改记录:
*
************************************************************************/
#include "OSHeaders.h"
#include "./bitary.h"
#include <string.h>
unsigned char Cbitary::s_aryAndMark[ONE_BYTE_OWN_BIT_SUM] = \
{ 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F };
unsigned char Cbitary::s_aryOrMark[ONE_BYTE_OWN_BIT_SUM] = \
{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
unsigned char Cbitary::s_aryOrMarkForRemainder[ONE_BYTE_OWN_BIT_SUM] = \
{ 0x00, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
Cbitary::Cbitary( unsigned long ulArySize )
{
m_ulBufTotalSize = 0;
m_ulCurrUseBufSize = 0;
m_pBuf = NULL;
m_ulBitSum = 0;
if ( ulArySize != 0 )
{
Alloc( ulArySize );
}
}
Cbitary::~Cbitary(void)
{
delete[] m_pBuf;
}
Cbitary::Cbitary(const Cbitary& bits)
{
m_ulBufTotalSize = bits.m_ulBufTotalSize;
m_ulCurrUseBufSize = bits.m_ulCurrUseBufSize;
m_ulBitSum = bits.m_ulBitSum;
m_pBuf = new unsigned char[m_ulBufTotalSize];
memcpy( m_pBuf, bits.m_pBuf, m_ulBufTotalSize );
}
Cbitary& Cbitary::operator=(const Cbitary& bits)
{
if ( this != &bits )
{
delete[] m_pBuf;
m_ulBufTotalSize = bits.m_ulBufTotalSize;
m_ulCurrUseBufSize = bits.m_ulCurrUseBufSize;
m_ulBitSum = bits.m_ulBitSum;
m_pBuf = new unsigned char[m_ulBufTotalSize];
memcpy( m_pBuf, bits.m_pBuf, m_ulBufTotalSize );
}
return *this;
}
bool Cbitary::Alloc( unsigned long ulArySize )
{
bool bSuccess = false;
if ( ulArySize > 0 )
{
m_ulBitSum = ulArySize;
m_ulBufTotalSize = m_ulBitSum>>3;
unsigned long ulRemainder = m_ulBitSum & 0x07;
if ( ulRemainder != 0 )
{
m_ulBufTotalSize += 1;
}
m_ulCurrUseBufSize = m_ulBufTotalSize;
delete[] m_pBuf;
m_pBuf = new unsigned char[m_ulBufTotalSize];
ResetAll();
bSuccess = true;
}
return bSuccess;
}
void Cbitary::Push( unsigned char uVal )
{
if ( m_ulCurrUseBufSize >= m_ulBufTotalSize )
{
unsigned long ulNewAllocSize = m_ulBufTotalSize/ALLOC_MULTIPLE;
ulNewAllocSize = ( (ulNewAllocSize>MIN_ALLOC_NUM)?ulNewAllocSize:MIN_ALLOC_NUM );
m_ulBufTotalSize += ulNewAllocSize;
unsigned char* pBuf = new unsigned char[m_ulBufTotalSize];
memset( pBuf, 0xff, m_ulBufTotalSize );
memcpy( pBuf, m_pBuf, ( m_ulBufTotalSize - ulNewAllocSize ) );
delete[] m_pBuf;
m_pBuf = pBuf;
}
m_ulBitSum++;
m_ulCurrUseBufSize = ( m_ulBitSum >> 3 ) + ( ( m_ulBitSum & 0x07 )?1:0 );
if ( uVal == 0 )
{
Reset( m_ulBitSum-1 );
}
else
{
Set( m_ulBitSum-1 );
}
}
void Cbitary::Pop()
{
if ( m_ulBitSum > 0 )
{
unsigned long ulUnuseBufSize = m_ulBufTotalSize - m_ulCurrUseBufSize;
unsigned long ulDeleteSize = m_ulBufTotalSize/ALLOC_MULTIPLE;
ulDeleteSize = ( (ulDeleteSize>MIN_ALLOC_NUM)?ulDeleteSize:MIN_ALLOC_NUM );
if ( ulUnuseBufSize > ulDeleteSize )
{
m_ulBufTotalSize -= ulDeleteSize;
unsigned char* pBuf = new unsigned char[m_ulBufTotalSize];
// memset( pBuf, 0xff, m_ulBufTotalSize );
memcpy( pBuf, m_pBuf, m_ulBufTotalSize );
delete[] m_pBuf;
m_pBuf = pBuf;
}
Set( m_ulBitSum-1 );
m_ulBitSum--;
m_ulCurrUseBufSize = ( m_ulBitSum >> 3 ) + ( ( m_ulBitSum & 0x07 )?1:0 );
}
}
void Cbitary::Construct( const unsigned char* pBuf, unsigned long ulBufSize, unsigned long ulBitSum )
{
m_ulBufTotalSize = ulBufSize;
m_ulCurrUseBufSize = m_ulBufTotalSize;
m_ulBitSum = ulBitSum;
delete[] m_pBuf;
m_pBuf = new unsigned char[m_ulBufTotalSize];
memcpy( m_pBuf, pBuf, m_ulBufTotalSize );
}
void Cbitary::SetAll()
{
memset( m_pBuf, 0xFF, m_ulCurrUseBufSize );
}
void Cbitary::ResetAll()
{
memset( m_pBuf, 0x00, m_ulCurrUseBufSize );
// for ( unsigned long i = m_ulCurrUseBufSize; i < m_ulBufTotalSize; i++ )
// {
// m_pBuf[i] = 0xFF;
// }
m_pBuf[m_ulCurrUseBufSize-1] |= s_aryOrMarkForRemainder[m_ulBitSum & 0x07];
}
bool Cbitary::Set( unsigned long ulIndex )
{
bool bSuccess = false;
unsigned long ulBYTEIndex = ulIndex>>3;
if ( ( ulBYTEIndex < m_ulBufTotalSize ) && ( ulIndex < m_ulBitSum ) )
{
m_pBuf[ulBYTEIndex] |= s_aryOrMark[ulIndex & 0x07];
bSuccess = true;
}
return bSuccess;
}
bool Cbitary::Reset( unsigned long ulIndex )
{
bool bSuccess = false;
unsigned long ulBYTEIndex = ulIndex>>3;
if ( ( ulBYTEIndex < m_ulBufTotalSize ) && ( ulIndex < m_ulBitSum ) )
{
m_pBuf[ulBYTEIndex] &= s_aryAndMark[ulIndex & 0x07];
bSuccess = true;
}
return bSuccess;
}
bool Cbitary::Test( unsigned long ulIndex ) const
{
bool bSuccess = false;
unsigned long ulBYTEIndex = ulIndex>>3;
if ( ( ulBYTEIndex < m_ulBufTotalSize ) && ( ulIndex < m_ulBitSum ) )
{
#ifdef _WIN32
#pragma warning(disable:4800)
#endif
bSuccess = (bool)(m_pBuf[ulBYTEIndex] & s_aryOrMark[ulIndex & 0x07]);
#ifdef _WIN32
#pragma warning(default:4800)
#endif
}
return bSuccess;
}
bool Cbitary::TestAll() const
{
for ( unsigned long i = 0; i < m_ulCurrUseBufSize; i++ )
{
if ( ( m_pBuf[i] & 0xFF ) != 0xFF )
{
return false;
}
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -