152 lines
2.9 KiB
C++
152 lines
2.9 KiB
C++
/**
|
|
* @file dl_vector_bit.h
|
|
* @brief 位容器
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 26-02-03
|
|
*
|
|
* @note vector<bool>的替代品
|
|
* @note vector<bool>不保证内存连续,所以只能实现一个连续的
|
|
* @note std::bitset<8> 大小并非一定1字节
|
|
*/
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <cassert>
|
|
#include <bitset>
|
|
|
|
namespace dl
|
|
{
|
|
//! 位容器
|
|
class VectorBit
|
|
{
|
|
using Byte = unsigned char;
|
|
public:
|
|
// 二进制数据
|
|
void* GetData()
|
|
{
|
|
return _data.data();
|
|
}
|
|
size_t GetDataSize()
|
|
{
|
|
return _data.size();
|
|
}
|
|
|
|
// 容器意义
|
|
void SetBit(size_t index, bool v)
|
|
{
|
|
size_t j = index / 8;
|
|
size_t i = index % 8;
|
|
|
|
_set(j, i, v);
|
|
}
|
|
bool GetBit(size_t index)
|
|
{
|
|
size_t j = index / 8;
|
|
size_t i = index % 8;
|
|
|
|
return (_data[j] >> i) & 1;
|
|
}
|
|
|
|
size_t size() const
|
|
{
|
|
return _data.size() * 8 + _i;
|
|
}
|
|
void resize(size_t n, bool v)
|
|
{
|
|
// 旧
|
|
size_t j_old = _data.size();
|
|
if (j_old > 0)
|
|
--j_old;
|
|
// 新位置
|
|
size_t j = n / 8;
|
|
|
|
_data.resize(j + 1, v);
|
|
|
|
if (j < j_old)
|
|
{// 块容量缩小
|
|
;
|
|
}
|
|
else if (j > j_old)
|
|
{// 块容量扩大,旧的最后一块 (i_old, 8) 填充新值
|
|
for (size_t k = _i + 1; k < 8; ++k)
|
|
_set(j_old, k, v);
|
|
}
|
|
else
|
|
{// 块容量相等,旧的最后一块 从 (i_old, i] 填充新值
|
|
size_t i = n % 8;
|
|
for (size_t k = _i + 1; k < i; ++k)
|
|
_set(j_old, k, v);
|
|
}
|
|
}
|
|
// 注意 _i 的正确性
|
|
void assign(void* p, size_t n)
|
|
{
|
|
_data.resize(n);
|
|
memcpy(_data.data(), p, n);
|
|
}
|
|
VectorBit() :_i{ 0 }
|
|
{
|
|
}
|
|
private:
|
|
std::vector<unsigned char> _data; // size 为 j
|
|
size_t _i;
|
|
|
|
void _set(size_t j, size_t i, bool v)
|
|
{
|
|
assert(i < 8);
|
|
if (v)
|
|
_data[j] |= (1 << i);
|
|
else
|
|
_data[j] &= ~(1 << i);
|
|
// 反转 number ^ ((Uint)1 << n);
|
|
}
|
|
};
|
|
|
|
|
|
}
|
|
|
|
// 可参考boost的代码进一步优化
|
|
//resize(size_type num_bits, bool value) // strong guarantee
|
|
//{
|
|
// const size_type old_num_blocks = num_blocks();
|
|
// const size_type required_blocks = calc_num_blocks(num_bits);
|
|
//
|
|
//
|
|
// const block_type v = value ? Block(-1) : Block(0);
|
|
//
|
|
//
|
|
// if (required_blocks != old_num_blocks) {
|
|
// m_bits.resize(required_blocks, v); // s.g. (copy)
|
|
// }
|
|
//
|
|
//
|
|
// // At this point:
|
|
// //
|
|
// // - if the buffer was shrunk, we have nothing more to do,
|
|
// // except a call to m_zero_unused_bits()
|
|
// //
|
|
// // - if it was enlarged, all the (used) bits in the new blocks have
|
|
// // the correct value, but we have not yet touched those bits, if
|
|
// // any, that were 'unused bits' before enlarging: if value == true,
|
|
// // they must be set.
|
|
//
|
|
//
|
|
// if (value && (num_bits > m_num_bits)) {
|
|
// const int extra_bits = count_extra_bits();
|
|
// if (extra_bits) {
|
|
// BOOST_ASSERT(old_num_blocks >= 1 && old_num_blocks <= m_bits.size());
|
|
//
|
|
//
|
|
// // Set them.
|
|
// m_bits[old_num_blocks - 1] |= (v << extra_bits);
|
|
// }
|
|
// }
|
|
//
|
|
//
|
|
// m_num_bits = num_bits;
|
|
// m_zero_unused_bits();
|
|
//}
|