/usr/include/dawgdic/bit-pool.h is in libdawgdic-dev 0.4.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #ifndef DAWGDIC_BIT_POOL_H
#define DAWGDIC_BIT_POOL_H
#include "object-pool.h"
namespace dawgdic {
// This class works as an array of bit flags with compact memory management.
template <SizeType BLOCK_SIZE = 1 << 10>
class BitPool {
public:
BitPool() : pool_(), size_(0) {}
// Accessors.
void set(SizeType index, bool bit) {
SizeType pool_index = PoolIndex(index);
UCharType bit_flag = BitFlag(index);
if (bit) {
pool_[pool_index] |= bit_flag;
} else {
pool_[pool_index] &= ~bit_flag;
}
}
bool get(SizeType index) const {
SizeType pool_index = PoolIndex(index);
UCharType bit_flag = BitFlag(index);
return (pool_[pool_index] & bit_flag) ? true : false;
}
// Deletes all bits and frees memory.
void Clear() {
pool_.Clear();
size_ = 0;
}
// Swaps bit pools.
void Swap(BitPool *bit_pool) {
pool_.Swap(&bit_pool->pool_);
}
// Allocates memory for a new bit and returns its ID.
// Note: Allocated bits are filled with false.
SizeType Allocate() {
SizeType pool_index = PoolIndex(size_);
if (pool_index == pool_.size()) {
pool_.Allocate();
pool_[pool_index] = '\0';
}
return size_++;
}
private:
ObjectPool<UCharType> pool_;
SizeType size_;
// Disallows copies.
BitPool(const BitPool &);
BitPool &operator=(const BitPool &);
static SizeType PoolIndex(SizeType index) {
return index / 8;
}
static UCharType BitFlag(BaseType index) {
return static_cast<UCharType>(1) << (index % 8);
}
};
} // namespace dawgdic
#endif // DAWGDIC_BIT_POOL_H
|