tesseract  4.00.00dev
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 lsb_index_ [256]
 
static const uinT8 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 109 of file bitvector.cpp.

109 : bit_size_(0), array_(NULL) {}

◆ BitVector() [2/3]

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

Definition at line 111 of file bitvector.cpp.

111  : bit_size_(length) {
112  array_ = new uinT32[WordLength()];
113  SetAllFalse();
114 }
uint32_t uinT32
Definition: host.h:39

◆ BitVector() [3/3]

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

Definition at line 116 of file bitvector.cpp.

116  : bit_size_(src.bit_size_) {
117  array_ = new uinT32[WordLength()];
118  memcpy(array_, src.array_, ByteLength());
119 }
uint32_t uinT32
Definition: host.h:39

◆ ~BitVector()

tesseract::BitVector::~BitVector ( )

Definition at line 127 of file bitvector.cpp.

127  {
128  delete [] array_;
129 }

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 148 of file bitvector.cpp.

148  {
149  uinT32 new_bit_size;
150  if (fread(&new_bit_size, sizeof(new_bit_size), 1, fp) != 1) return false;
151  if (swap) {
152  ReverseN(&new_bit_size, sizeof(new_bit_size));
153  }
154  Alloc(new_bit_size);
155  int wordlen = WordLength();
156  if (static_cast<int>(fread(array_, sizeof(*array_), wordlen, fp)) != wordlen)
157  return false;
158  if (swap) {
159  for (int i = 0; i < wordlen; ++i)
160  ReverseN(&array_[i], sizeof(array_[i]));
161  }
162  return true;
163 }
uint32_t uinT32
Definition: host.h:39
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184

◆ Init()

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

Definition at line 132 of file bitvector.cpp.

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

◆ NextSetBit()

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

Definition at line 174 of file bitvector.cpp.

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

◆ NumSetBits()

int tesseract::BitVector::NumSetBits ( ) const

Definition at line 212 of file bitvector.cpp.

212  {
213  int wordlen = WordLength();
214  int total_bits = 0;
215  for (int w = 0; w < wordlen; ++w) {
216  uinT32 word = array_[w];
217  for (int i = 0; i < 4; ++i) {
218  total_bits += hamming_table_[word & 0xff];
219  word >>= 8;
220  }
221  }
222  return total_bits;
223 }
uint32_t uinT32
Definition: host.h:39
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 121 of file bitvector.cpp.

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

◆ 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 239 of file bitvector.cpp.

239  {
240  int length = MIN(WordLength(), other.WordLength());
241  for (int w = 0; w < length; ++w)
242  array_[w] ^= other.array_[w];
243 }
#define MIN(x, y)
Definition: ndminx.h:28

◆ operator|=()

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

Definition at line 227 of file bitvector.cpp.

227  {
228  int length = MIN(WordLength(), other.WordLength());
229  for (int w = 0; w < length; ++w)
230  array_[w] |= other.array_[w];
231 }
#define MIN(x, y)
Definition: ndminx.h:28

◆ 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 138 of file bitvector.cpp.

138  {
139  if (fwrite(&bit_size_, sizeof(bit_size_), 1, fp) != 1) return false;
140  int wordlen = WordLength();
141  if (static_cast<int>(fwrite(array_, sizeof(*array_), wordlen, fp)) != wordlen)
142  return false;
143  return true;
144 }

◆ SetAllFalse()

void tesseract::BitVector::SetAllFalse ( )

Definition at line 165 of file bitvector.cpp.

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

◆ SetAllTrue()

void tesseract::BitVector::SetAllTrue ( )

Definition at line 168 of file bitvector.cpp.

168  {
169  memset(array_, ~0, ByteLength());
170 }

◆ 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 245 of file bitvector.cpp.

245  {
246  Alloc(v1.size());
247  int length = MIN(v1.WordLength(), v2.WordLength());
248  for (int w = 0; w < length; ++w)
249  array_[w] = v1.array_[w] ^ (v1.array_[w] & v2.array_[w]);
250  for (int w = WordLength() - 1; w >= length; --w)
251  array_[w] = v1.array_[w];
252 }
#define MIN(x, y)
Definition: ndminx.h:28

◆ 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 tesseract::BitVector::lsb_eroded_
static

Definition at line 41 of file bitvector.h.

◆ lsb_index_

const uinT8 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: