tesseract  4.0.0-1-g2a2b
tesseract::BitVector Class Reference

#include <bitvector.h>

Public Member Functions

 BitVector ()
 
 BitVector (int length)
 
 BitVector (const BitVector &src)
 
BitVectoroperator= (const BitVector &src)
 
 ~BitVector ()
 
void Init (int length)
 
int size () const
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
void SetAllFalse ()
 
void SetAllTrue ()
 
void SetBit (int index)
 
void ResetBit (int index)
 
void SetValue (int index, bool value)
 
bool At (int index) const
 
bool operator[] (int index) const
 
int NextSetBit (int prev_bit) const
 
int NumSetBits () const
 
void operator|= (const BitVector &other)
 
void operator &= (const BitVector &other)
 
void operator^= (const BitVector &other)
 
void SetSubtract (const BitVector &v1, const BitVector &v2)
 

Static Public Attributes

static const uint8_t lsb_index_ [256]
 
static const uint8_t lsb_eroded_ [256]
 
static const int hamming_table_ [256]
 

Detailed Description

Definition at line 33 of file bitvector.h.

Constructor & Destructor Documentation

◆ BitVector() [1/3]

tesseract::BitVector::BitVector ( )

Definition at line 110 of file bitvector.cpp.

110 : bit_size_(0), array_(nullptr) {}

◆ BitVector() [2/3]

tesseract::BitVector::BitVector ( int  length)
explicit

Definition at line 112 of file bitvector.cpp.

112  : bit_size_(length) {
113  array_ = new uint32_t[WordLength()];
114  SetAllFalse();
115 }

◆ BitVector() [3/3]

tesseract::BitVector::BitVector ( const BitVector src)

Definition at line 117 of file bitvector.cpp.

117  : bit_size_(src.bit_size_) {
118  array_ = new uint32_t[WordLength()];
119  memcpy(array_, src.array_, ByteLength());
120 }

◆ ~BitVector()

tesseract::BitVector::~BitVector ( )

Definition at line 128 of file bitvector.cpp.

128  {
129  delete [] array_;
130 }

Member Function Documentation

◆ At()

bool tesseract::BitVector::At ( int  index) const
inline

Definition at line 84 of file bitvector.h.

84  {
85  return (array_[WordIndex(index)] & BitMask(index)) != 0;
86  }

◆ DeSerialize()

bool tesseract::BitVector::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 147 of file bitvector.cpp.

147  {
148  uint32_t new_bit_size;
149  if (!tesseract::DeSerialize(fp, &new_bit_size)) return false;
150  if (swap) {
151  ReverseN(&new_bit_size, sizeof(new_bit_size));
152  }
153  Alloc(new_bit_size);
154  int wordlen = WordLength();
155  if (!tesseract::DeSerialize(fp, &array_[0], wordlen)) return false;
156  if (swap) {
157  for (int i = 0; i < wordlen; ++i)
158  ReverseN(&array_[i], sizeof(array_[i]));
159  }
160  return true;
161 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
bool DeSerialize(FILE *fp, char *data, size_t n)
Definition: serialis.cpp:27

◆ Init()

void tesseract::BitVector::Init ( int  length)

Definition at line 133 of file bitvector.cpp.

133  {
134  Alloc(length);
135  SetAllFalse();
136 }

◆ NextSetBit()

int tesseract::BitVector::NextSetBit ( int  prev_bit) const

Definition at line 172 of file bitvector.cpp.

172  {
173  // Move on to the next bit.
174  int next_bit = prev_bit + 1;
175  if (next_bit >= bit_size_) return -1;
176  // Check the remains of the word containing the next_bit first.
177  int next_word = WordIndex(next_bit);
178  int bit_index = next_word * kBitFactor;
179  int word_end = bit_index + kBitFactor;
180  uint32_t word = array_[next_word];
181  uint8_t byte = word & 0xff;
182  while (bit_index < word_end) {
183  if (bit_index + 8 > next_bit && byte != 0) {
184  while (bit_index + lsb_index_[byte] < next_bit && byte != 0)
185  byte = lsb_eroded_[byte];
186  if (byte != 0)
187  return bit_index + lsb_index_[byte];
188  }
189  word >>= 8;
190  bit_index += 8;
191  byte = word & 0xff;
192  }
193  // next_word didn't contain a 1, so find the next word with set bit.
194  ++next_word;
195  int wordlen = WordLength();
196  while (next_word < wordlen && (word = array_[next_word]) == 0) {
197  ++next_word;
198  bit_index += kBitFactor;
199  }
200  if (bit_index >= bit_size_) return -1;
201  // Find the first non-zero byte within the word.
202  while ((word & 0xff) == 0) {
203  word >>= 8;
204  bit_index += 8;
205  }
206  return bit_index + lsb_index_[word & 0xff];
207 }
static const uint8_t lsb_eroded_[256]
Definition: bitvector.h:41
static const uint8_t lsb_index_[256]
Definition: bitvector.h:38

◆ NumSetBits()

int tesseract::BitVector::NumSetBits ( ) const

Definition at line 210 of file bitvector.cpp.

210  {
211  int wordlen = WordLength();
212  int total_bits = 0;
213  for (int w = 0; w < wordlen; ++w) {
214  uint32_t word = array_[w];
215  for (int i = 0; i < 4; ++i) {
216  total_bits += hamming_table_[word & 0xff];
217  word >>= 8;
218  }
219  }
220  return total_bits;
221 }
static const int hamming_table_[256]
Definition: bitvector.h:43

◆ operator &=()

void tesseract::BitVector::operator&= ( const BitVector other)

◆ operator=()

BitVector & tesseract::BitVector::operator= ( const BitVector src)

Definition at line 122 of file bitvector.cpp.

122  {
123  Alloc(src.bit_size_);
124  memcpy(array_, src.array_, ByteLength());
125  return *this;
126 }

◆ operator[]()

bool tesseract::BitVector::operator[] ( int  index) const
inline

Definition at line 87 of file bitvector.h.

87  {
88  return (array_[WordIndex(index)] & BitMask(index)) != 0;
89  }

◆ operator^=()

void tesseract::BitVector::operator^= ( const BitVector other)

Definition at line 237 of file bitvector.cpp.

237  {
238  int length = std::min(WordLength(), other.WordLength());
239  for (int w = 0; w < length; ++w)
240  array_[w] ^= other.array_[w];
241 }

◆ operator|=()

void tesseract::BitVector::operator|= ( const BitVector other)

Definition at line 225 of file bitvector.cpp.

225  {
226  int length = std::min(WordLength(), other.WordLength());
227  for (int w = 0; w < length; ++w)
228  array_[w] |= other.array_[w];
229 }

◆ ResetBit()

void tesseract::BitVector::ResetBit ( int  index)
inline

Definition at line 75 of file bitvector.h.

75  {
76  array_[WordIndex(index)] &= ~BitMask(index);
77  }

◆ Serialize()

bool tesseract::BitVector::Serialize ( FILE *  fp) const

Definition at line 139 of file bitvector.cpp.

139  {
140  if (!tesseract::Serialize(fp, &bit_size_)) return false;
141  int wordlen = WordLength();
142  return tesseract::Serialize(fp, &array_[0], wordlen);
143 }
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59

◆ SetAllFalse()

void tesseract::BitVector::SetAllFalse ( )

Definition at line 163 of file bitvector.cpp.

163  {
164  memset(array_, 0, ByteLength());
165 }

◆ SetAllTrue()

void tesseract::BitVector::SetAllTrue ( )

Definition at line 166 of file bitvector.cpp.

166  {
167  memset(array_, ~0, ByteLength());
168 }

◆ SetBit()

void tesseract::BitVector::SetBit ( int  index)
inline

Definition at line 72 of file bitvector.h.

72  {
73  array_[WordIndex(index)] |= BitMask(index);
74  }

◆ SetSubtract()

void tesseract::BitVector::SetSubtract ( const BitVector v1,
const BitVector v2 
)

Definition at line 243 of file bitvector.cpp.

243  {
244  Alloc(v1.size());
245  int length = std::min(v1.WordLength(), v2.WordLength());
246  for (int w = 0; w < length; ++w)
247  array_[w] = v1.array_[w] ^ (v1.array_[w] & v2.array_[w]);
248  for (int w = WordLength() - 1; w >= length; --w)
249  array_[w] = v1.array_[w];
250 }

◆ SetValue()

void tesseract::BitVector::SetValue ( int  index,
bool  value 
)
inline

Definition at line 78 of file bitvector.h.

78  {
79  if (value)
80  SetBit(index);
81  else
82  ResetBit(index);
83  }
void ResetBit(int index)
Definition: bitvector.h:75
void SetBit(int index)
Definition: bitvector.h:72

◆ size()

int tesseract::BitVector::size ( ) const
inline

Definition at line 56 of file bitvector.h.

56  {
57  return bit_size_;
58  }

Member Data Documentation

◆ hamming_table_

const int tesseract::BitVector::hamming_table_
static
Initial value:
= {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
}

Definition at line 43 of file bitvector.h.

◆ lsb_eroded_

const uint8_t tesseract::BitVector::lsb_eroded_
static

Definition at line 41 of file bitvector.h.

◆ lsb_index_

const uint8_t tesseract::BitVector::lsb_index_
static
Initial value:
= {
255, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
}

Definition at line 38 of file bitvector.h.


The documentation for this class was generated from the following files: