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

#include <indexmapbidi.h>

Inheritance diagram for tesseract::IndexMapBiDi:
tesseract::IndexMap

Public Member Functions

virtual ~IndexMapBiDi ()
 
void InitAndSetupRange (int sparse_size, int start, int end)
 
void Init (int size, bool all_mapped)
 
void SetMap (int sparse_index, bool mapped)
 
void Setup ()
 
bool Merge (int compact_index1, int compact_index2)
 
bool IsCompactDeleted (int index) const
 
void CompleteMerges ()
 
virtual int SparseToCompact (int sparse_index) const
 
virtual int SparseSize () const
 
void CopyFrom (const IndexMapBiDi &src)
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
int MapFeatures (const GenericVector< int > &sparse, GenericVector< int > *compact) const
 
- Public Member Functions inherited from tesseract::IndexMap
virtual ~IndexMap ()
 
int CompactToSparse (int compact_index) const
 
int CompactSize () const
 
void CopyFrom (const IndexMap &src)
 
void CopyFrom (const IndexMapBiDi &src)
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::IndexMap
int32_t sparse_size_
 
GenericVector< int32_t > compact_map_
 

Detailed Description

Definition at line 102 of file indexmapbidi.h.

Constructor & Destructor Documentation

◆ ~IndexMapBiDi()

tesseract::IndexMapBiDi::~IndexMapBiDi ( )
virtualdefault

Member Function Documentation

◆ CompleteMerges()

void tesseract::IndexMapBiDi::CompleteMerges ( )

Definition at line 160 of file indexmapbidi.cpp.

160  {
161  // Ensure each sparse_map_entry contains a master compact_map_ index.
162  int compact_size = 0;
163  for (int i = 0; i < sparse_map_.size(); ++i) {
164  int compact_index = MasterCompactIndex(sparse_map_[i]);
165  sparse_map_[i] = compact_index;
166  if (compact_index >= compact_size)
167  compact_size = compact_index + 1;
168  }
169  // Re-generate the compact_map leaving holes for unused indices.
170  compact_map_.init_to_size(compact_size, -1);
171  for (int i = 0; i < sparse_map_.size(); ++i) {
172  if (sparse_map_[i] >= 0) {
173  if (compact_map_[sparse_map_[i]] == -1)
174  compact_map_[sparse_map_[i]] = i;
175  }
176  }
177  // Compact the compact_map, leaving tmp_compact_map saying where each
178  // index went to in the compacted map.
179  GenericVector<int32_t> tmp_compact_map;
180  tmp_compact_map.init_to_size(compact_size, -1);
181  compact_size = 0;
182  for (int i = 0; i < compact_map_.size(); ++i) {
183  if (compact_map_[i] >= 0) {
184  tmp_compact_map[i] = compact_size;
185  compact_map_[compact_size++] = compact_map_[i];
186  }
187  }
188  compact_map_.truncate(compact_size);
189  // Now modify the entries in the sparse map to point to the new locations.
190  for (int i = 0; i < sparse_map_.size(); ++i) {
191  if (sparse_map_[i] >= 0) {
192  sparse_map_[i] = tmp_compact_map[sparse_map_[i]];
193  }
194  }
195 }
int size() const
Definition: genericvector.h:71
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80
void init_to_size(int size, const T &t)
void truncate(int size)

◆ CopyFrom()

void tesseract::IndexMapBiDi::CopyFrom ( const IndexMapBiDi src)

Definition at line 119 of file indexmapbidi.cpp.

119  {
120  sparse_map_ = src.sparse_map_;
121  compact_map_ = src.compact_map_;
122  sparse_size_ = sparse_map_.size();
123 }
int size() const
Definition: genericvector.h:71
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80

◆ DeSerialize()

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

Definition at line 216 of file indexmapbidi.cpp.

216  {
217  if (!IndexMap::DeSerialize(swap, fp)) return false;
218  GenericVector<int32_t> remaining_pairs;
219  if (!remaining_pairs.DeSerialize(swap, fp)) return false;
220  sparse_map_.init_to_size(sparse_size_, -1);
221  for (int i = 0; i < compact_map_.size(); ++i) {
222  sparse_map_[compact_map_[i]] = i;
223  }
224  for (int i = 0; i < remaining_pairs.size(); ++i) {
225  int sparse_index = remaining_pairs[i++];
226  sparse_map_[sparse_index] = remaining_pairs[i];
227  }
228  return true;
229 }
int size() const
Definition: genericvector.h:71
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80
bool DeSerialize(bool swap, FILE *fp)
void init_to_size(int size, const T &t)
bool DeSerialize(bool swap, FILE *fp)

◆ Init()

void tesseract::IndexMapBiDi::Init ( int  size,
bool  all_mapped 
)

Definition at line 86 of file indexmapbidi.cpp.

86  {
87  sparse_map_.init_to_size(size, -1);
88  if (all_mapped) {
89  for (int i = 0; i < size; ++i)
90  sparse_map_[i] = i;
91  }
92 }
void init_to_size(int size, const T &t)

◆ InitAndSetupRange()

void tesseract::IndexMapBiDi::InitAndSetupRange ( int  sparse_size,
int  start,
int  end 
)

Definition at line 75 of file indexmapbidi.cpp.

75  {
76  Init(sparse_size, false);
77  for (int i = start; i < end; ++i)
78  SetMap(i, true);
79  Setup();
80 }
void SetMap(int sparse_index, bool mapped)
void Init(int size, bool all_mapped)

◆ IsCompactDeleted()

bool tesseract::IndexMapBiDi::IsCompactDeleted ( int  index) const
inline

Definition at line 130 of file indexmapbidi.h.

130  {
131  return MasterCompactIndex(index) < 0;
132  }

◆ MapFeatures()

int tesseract::IndexMapBiDi::MapFeatures ( const GenericVector< int > &  sparse,
GenericVector< int > *  compact 
) const

Definition at line 236 of file indexmapbidi.cpp.

237  {
238  compact->truncate(0);
239  int num_features = sparse.size();
240  int missed_features = 0;
241  int prev_good_feature = -1;
242  for (int f = 0; f < num_features; ++f) {
243  int feature = sparse_map_[sparse[f]];
244  if (feature >= 0) {
245  if (feature != prev_good_feature) {
246  compact->push_back(feature);
247  prev_good_feature = feature;
248  }
249  } else {
250  ++missed_features;
251  }
252  }
253  return missed_features;
254 }
int size() const
Definition: genericvector.h:71
int push_back(T object)
void truncate(int size)

◆ Merge()

bool tesseract::IndexMapBiDi::Merge ( int  compact_index1,
int  compact_index2 
)

Definition at line 128 of file indexmapbidi.cpp.

128  {
129  // Find the current master index for index1 and index2.
130  compact_index1 = MasterCompactIndex(compact_index1);
131  compact_index2 = MasterCompactIndex(compact_index2);
132  // Be sure that index1 < index2.
133  if (compact_index1 > compact_index2) {
134  int tmp = compact_index1;
135  compact_index1 = compact_index2;
136  compact_index2 = tmp;
137  } else if (compact_index1 == compact_index2) {
138  return false;
139  }
140  // To save iterating over all sparse_map_ entries, simply make the master
141  // entry for index2 point to index1.
142  // This leaves behind a potential chain of parents that needs to be chased,
143  // as above.
144  sparse_map_[compact_map_[compact_index2]] = compact_index1;
145  if (compact_index1 >= 0)
146  compact_map_[compact_index2] = compact_map_[compact_index1];
147  return true;
148 }
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80

◆ Serialize()

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

Definition at line 198 of file indexmapbidi.cpp.

198  {
199  if (!IndexMap::Serialize(fp)) return false;
200  // Make a vector containing the rest of the map. If the map is many-to-one
201  // then each additional sparse entry needs to be stored.
202  // Normally we store only the compact map to save space.
203  GenericVector<int32_t> remaining_pairs;
204  for (int i = 0; i < sparse_map_.size(); ++i) {
205  if (sparse_map_[i] >= 0 && compact_map_[sparse_map_[i]] != i) {
206  remaining_pairs.push_back(i);
207  remaining_pairs.push_back(sparse_map_[i]);
208  }
209  }
210  if (!remaining_pairs.Serialize(fp)) return false;
211  return true;
212 }
int size() const
Definition: genericvector.h:71
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80
bool Serialize(FILE *fp) const
bool Serialize(FILE *fp) const
int push_back(T object)

◆ SetMap()

void tesseract::IndexMapBiDi::SetMap ( int  sparse_index,
bool  mapped 
)

Definition at line 95 of file indexmapbidi.cpp.

95  {
96  sparse_map_[sparse_index] = mapped ? 0 : -1;
97 }

◆ Setup()

void tesseract::IndexMapBiDi::Setup ( )

Definition at line 102 of file indexmapbidi.cpp.

102  {
103  int compact_size = 0;
104  for (int i = 0; i < sparse_map_.size(); ++i) {
105  if (sparse_map_[i] >= 0) {
106  sparse_map_[i] = compact_size++;
107  }
108  }
109  compact_map_.init_to_size(compact_size, -1);
110  for (int i = 0; i < sparse_map_.size(); ++i) {
111  if (sparse_map_[i] >= 0) {
112  compact_map_[sparse_map_[i]] = i;
113  }
114  }
115  sparse_size_ = sparse_map_.size();
116 }
int size() const
Definition: genericvector.h:71
GenericVector< int32_t > compact_map_
Definition: indexmapbidi.h:80
void init_to_size(int size, const T &t)

◆ SparseSize()

virtual int tesseract::IndexMapBiDi::SparseSize ( ) const
inlinevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 142 of file indexmapbidi.h.

142  {
143  return sparse_map_.size();
144  }
int size() const
Definition: genericvector.h:71

◆ SparseToCompact()

virtual int tesseract::IndexMapBiDi::SparseToCompact ( int  sparse_index) const
inlinevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 138 of file indexmapbidi.h.

138  {
139  return sparse_map_[sparse_index];
140  }

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