19#ifndef TESSERACT_CCUTIL_BITVECTOR_H_
20#define TESSERACT_CCUTIL_BITVECTOR_H_
38 static const uint8_t lsb_index_[256];
41 static const uint8_t lsb_eroded_[256];
43 static const int hamming_table_[256];
47 explicit BitVector(
int length) : bit_size_(length), array_(WordLength()) {
55 void Init(
int length);
58 return bit_size_ == 0;
79 array_[WordIndex(index)] |= BitMask(index);
82 array_[WordIndex(index)] &= ~BitMask(index);
91 bool At(
int index)
const {
92 return (array_[WordIndex(index)] & BitMask(index)) != 0;
95 return (array_[WordIndex(index)] & BitMask(index)) != 0;
100 int NextSetBit(
int prev_bit)
const;
103 int NumSetBits()
const;
115 void Alloc(
int length);
119 int WordIndex(
int index)
const {
120 assert(0 <= index && index < bit_size_);
121 return index / kBitFactor;
124 uint32_t BitMask(
int index)
const {
125 return 1 << (index & (kBitFactor - 1));
129 int WordLength()
const {
130 return (bit_size_ + kBitFactor - 1) / kBitFactor;
133 int ByteLength()
const {
134 return WordLength() *
sizeof(array_[0]);
138 int32_t bit_size_ = 0;
142 std::vector<uint32_t> array_;
144 static const int kBitFactor =
sizeof(array_[0]) * 8;
TBOX & operator&=(TBOX &op1, const TBOX &op2)
bool DeSerialize(bool swap, FILE *fp, std::vector< T > &data)
bool Serialize(FILE *fp, const std::vector< T > &data)
BitVector(const BitVector &src)
void SetValue(int index, bool value)
bool operator[](int index) const