compblock.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:4k
- #include "stdafx.h"
- #include <compblock.h>
- //#include <mmintrin.h>
- //#include <xmmintrin.h>
- namespace CompMasks {
- const UINT32 NumMasks = 31;
- UINT32 masks[CompMasks::NumMasks] =
- {
- 1, 3, 7, 15, 31, 63, 127, 255,
- 511, 1023, 2047, 4095, 8191, 16383,
- 32767, 65535, 131072, 262144,
- 524288, 1048576, 2097152, 4194304,
- 8388608, 16777216, 33554432, 67108864,
- 134217728, 268435456, 536870912,
- 1073741824, 2147483648
- };
- };
- CompBlock::CompBlock(void)
- {
- InitBlock();
- }
- CompBlock::CompBlock(UINT32 length, unsigned char * ptr)
- {
- InitBlock();
- SetData(length,ptr);
- }
- CompBlock::~CompBlock(void)
- {
- }
- UINT32 CompBlock::CalculateMinBits(UINT32 num)
- {
- UINT32 bits = 1;
- UINT32 i = 0;
- UINT32 found = 0;
- while (!found){
- if ((num > CompMasks::masks[i]) && (i < CompMasks::NumMasks))
- {
- bits++;
- i++;
- }
- else
- {
- found++;
- }
- }
- return bits;
- }
- void CompBlock::SetData(UINT32 length, unsigned char * ptr)
- {
- InitBlock();
- m_bits = ptr;
- m_length = length;
- }
- void CompBlock::PopNumber(UINT32 num_bits, short& num)
- {
- if (!m_popped)
- {
- m_popped++;
- m_current_bit = 0;
- }
- if (m_bits)
- {
- m_off = m_current_bit / 8;
- INT32 tmp = 0;
- unsigned char * tmp_ptr = (unsigned char *)&tmp;
- if ((m_off+3) < m_length)
- tmp_ptr[0] = m_bits[m_off+3];
- if ((m_off+2) < m_length)
- tmp_ptr[1] = m_bits[m_off+2];
- if ((m_off+1) < m_length)
- tmp_ptr[2] = m_bits[m_off+1];
- if (m_off < m_length)
- tmp_ptr[3] = m_bits[m_off];
- m_pre = m_current_bit - (m_off * 8);
- m_fol = 32 - m_pre - num_bits;
- tmp = tmp >> m_fol;
- num = (short)(tmp & CompMasks::masks[num_bits-1]);
- m_current_bit += num_bits;
- }
- }
- void CompBlock::PushNumber(UINT32 num_bits, short num)
- {
- if (!m_pushed)
- {
- m_pushed++;
- m_current_bit = 0;
- }
- UINT32 final_bit = (m_current_bit + num_bits);
- UINT32 final_byte = (final_bit/8) + ((final_bit%8) ? 1 : 0);
- if (final_byte <= m_length)
- {
- m_bucket = num;
- m_bucket &= CompMasks::masks[num_bits-1];
- m_off = m_current_bit/8;
- m_pre = m_current_bit - (m_off * 8);
- m_fol = 32 - m_pre - num_bits;
- m_bucket = m_bucket << m_fol;
- char * cptr = (char *)&m_bucket;
- m_bits[m_off+3] |= cptr[0];
- m_bits[m_off+2] |= cptr[1];
- m_bits[m_off+1] |= cptr[2];
- m_bits[m_off] |= cptr[3];
- m_current_bit += num_bits;
- }
- }
- void CompBlock::PushNumber(UINT32 num_bits, UINT32 num)
- {
- if (!m_pushed)
- {
- m_pushed++;
- m_current_bit = 0;
- }
- UINT32 final_bit = (m_current_bit + num_bits);
- UINT32 final_byte = (final_bit/8) + ((final_bit%8) ? 1 : 0);
- if (final_byte <= m_length)
- {
- m_bucket = num;
- m_bucket &= CompMasks::masks[num_bits-1];
- m_off = m_current_bit/8;
- m_pre = m_current_bit - (m_off * 8);
- m_fol = 32 - m_pre - num_bits;
- m_bucket = m_bucket << m_fol;
- char * cptr = (char *)&m_bucket;
- m_bits[m_off+3] |= cptr[0];
- m_bits[m_off+2] |= cptr[1];
- m_bits[m_off+1] |= cptr[2];
- m_bits[m_off] |= cptr[3];
- m_current_bit += num_bits;
- }
- }
- void CompBlock::PushNumber(UINT32 num_bits, unsigned char num)
- {
- if (!m_pushed)
- {
- m_pushed++;
- m_current_bit = 0;
- }
- UINT32 final_bit = (m_current_bit + num_bits);
- UINT32 final_byte = (final_bit/8) + ((final_bit%8) ? 1 : 0);
- if (final_byte <= m_length)
- {
- m_bucket = num;
- m_bucket &= CompMasks::masks[num_bits-1];
- m_off = m_current_bit/8;
- m_pre = m_current_bit - (m_off * 8);
- m_fol = 32 - m_pre - num_bits;
- m_bucket = m_bucket << m_fol;
- char * cptr = (char *)&m_bucket;
- m_bits[m_off+3] |= cptr[0];
- m_bits[m_off+2] |= cptr[1];
- m_bits[m_off+1] |= cptr[2];
- m_bits[m_off] |= cptr[3];
- m_current_bit += num_bits;
- }
- }
- UINT32 CompBlock::GetCompressedSize(void)const
- {
- UINT32 result = (m_current_bit/8) + ((m_current_bit%8) ? 1 : 0);
- return result;
- }