tesseract  5.0.0
tesseract::IndexMapBiDi Class Reference

#include <indexmapbidi.h>

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

Public Member Functions

 ~IndexMapBiDi () override
 
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 ()
 
int SparseToCompact (int sparse_index) const override
 
int SparseSize () const override
 
void CopyFrom (const IndexMapBiDi &src)
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
int MapFeatures (const std::vector< int > &sparse, std::vector< 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_
 
std::vector< int32_t > compact_map_
 

Detailed Description

Definition at line 104 of file indexmapbidi.h.

Constructor & Destructor Documentation

◆ ~IndexMapBiDi()

tesseract::IndexMapBiDi::~IndexMapBiDi ( )
overridedefault

Member Function Documentation

◆ CompleteMerges()

void tesseract::IndexMapBiDi::CompleteMerges ( )

Definition at line 177 of file indexmapbidi.cpp.

177  {
178  // Ensure each sparse_map_entry contains a master compact_map_ index.
179  int compact_size = 0;
180  for (int &i : sparse_map_) {
181  int compact_index = MasterCompactIndex(i);
182  i = compact_index;
183  if (compact_index >= compact_size) {
184  compact_size = compact_index + 1;
185  }
186  }
187  // Re-generate the compact_map leaving holes for unused indices.
188  compact_map_.clear();
189  compact_map_.resize(compact_size, -1);
190  for (size_t i = 0; i < sparse_map_.size(); ++i) {
191  if (sparse_map_[i] >= 0) {
192  if (compact_map_[sparse_map_[i]] == -1) {
193  compact_map_[sparse_map_[i]] = i;
194  }
195  }
196  }
197  // Compact the compact_map, leaving tmp_compact_map saying where each
198  // index went to in the compacted map.
199  std::vector<int32_t> tmp_compact_map(compact_size, -1);
200  compact_size = 0;
201  for (size_t i = 0; i < compact_map_.size(); ++i) {
202  if (compact_map_[i] >= 0) {
203  tmp_compact_map[i] = compact_size;
204  compact_map_[compact_size++] = compact_map_[i];
205  }
206  }
207  compact_map_.resize(compact_size);
208  // Now modify the entries in the sparse map to point to the new locations.
209  for (int &i : sparse_map_) {
210  if (i >= 0) {
211  i = tmp_compact_map[i];
212  }
213  }
214 }
std::vector< int32_t > compact_map_
Definition: indexmapbidi.h:82

◆ CopyFrom()

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

Definition at line 135 of file indexmapbidi.cpp.

135  {
136  sparse_map_ = src.sparse_map_;
137  compact_map_ = src.compact_map_;
138  sparse_size_ = sparse_map_.size();
139 }

◆ DeSerialize()

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

Definition at line 236 of file indexmapbidi.cpp.

236  {
237  if (!IndexMap::DeSerialize(swap, fp)) {
238  return false;
239  }
240  std::vector<int32_t> remaining_pairs;
241  if (!tesseract::DeSerialize(swap, fp, remaining_pairs)) {
242  return false;
243  }
244  sparse_map_.clear();
245  sparse_map_.resize(sparse_size_, -1);
246  for (unsigned i = 0; i < compact_map_.size(); ++i) {
247  sparse_map_[compact_map_[i]] = i;
248  }
249  for (size_t i = 0; i < remaining_pairs.size(); ++i) {
250  int sparse_index = remaining_pairs[i++];
251  sparse_map_[sparse_index] = remaining_pairs[i];
252  }
253  return true;
254 }
bool DeSerialize(bool swap, FILE *fp, std::vector< T > &data)
Definition: helpers.h:220
bool DeSerialize(bool swap, FILE *fp)

◆ Init()

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

Definition at line 97 of file indexmapbidi.cpp.

97  {
98  if (!all_mapped) {
99  sparse_map_.clear();
100  }
101  sparse_map_.resize(size, -1);
102  if (all_mapped) {
103  for (int i = 0; i < size; ++i) {
104  sparse_map_[i] = i;
105  }
106  }
107 }

◆ InitAndSetupRange()

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

Definition at line 85 of file indexmapbidi.cpp.

85  {
86  Init(sparse_size, false);
87  for (int i = start; i < end; ++i) {
88  SetMap(i, true);
89  }
90  Setup();
91 }
void Init(int size, bool all_mapped)
void SetMap(int sparse_index, bool mapped)

◆ IsCompactDeleted()

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

Definition at line 132 of file indexmapbidi.h.

132  {
133  return MasterCompactIndex(index) < 0;
134  }

◆ MapFeatures()

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

Definition at line 261 of file indexmapbidi.cpp.

261  {
262  compact->clear();
263  int num_features = sparse.size();
264  int missed_features = 0;
265  int prev_good_feature = -1;
266  for (int f = 0; f < num_features; ++f) {
267  int feature = sparse_map_[sparse[f]];
268  if (feature >= 0) {
269  if (feature != prev_good_feature) {
270  compact->push_back(feature);
271  prev_good_feature = feature;
272  }
273  } else {
274  ++missed_features;
275  }
276  }
277  return missed_features;
278 }

◆ Merge()

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

Definition at line 144 of file indexmapbidi.cpp.

144  {
145  // Find the current master index for index1 and index2.
146  compact_index1 = MasterCompactIndex(compact_index1);
147  compact_index2 = MasterCompactIndex(compact_index2);
148  // Be sure that index1 < index2.
149  if (compact_index1 > compact_index2) {
150  int tmp = compact_index1;
151  compact_index1 = compact_index2;
152  compact_index2 = tmp;
153  } else if (compact_index1 == compact_index2) {
154  return false;
155  }
156  // To save iterating over all sparse_map_ entries, simply make the master
157  // entry for index2 point to index1.
158  // This leaves behind a potential chain of parents that needs to be chased,
159  // as above.
160  sparse_map_[compact_map_[compact_index2]] = compact_index1;
161  if (compact_index1 >= 0) {
162  compact_map_[compact_index2] = compact_map_[compact_index1];
163  }
164  return true;
165 }

◆ Serialize()

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

Definition at line 217 of file indexmapbidi.cpp.

217  {
218  if (!IndexMap::Serialize(fp)) {
219  return false;
220  }
221  // Make a vector containing the rest of the map. If the map is many-to-one
222  // then each additional sparse entry needs to be stored.
223  // Normally we store only the compact map to save space.
224  std::vector<int32_t> remaining_pairs;
225  for (unsigned i = 0; i < sparse_map_.size(); ++i) {
226  if (sparse_map_[i] >= 0 && static_cast<unsigned>(compact_map_[sparse_map_[i]]) != i) {
227  remaining_pairs.push_back(i);
228  remaining_pairs.push_back(sparse_map_[i]);
229  }
230  }
231  return tesseract::Serialize(fp, remaining_pairs);
232 }
bool Serialize(FILE *fp, const std::vector< T > &data)
Definition: helpers.h:251
bool Serialize(FILE *fp) const

◆ SetMap()

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

Definition at line 110 of file indexmapbidi.cpp.

110  {
111  sparse_map_[sparse_index] = mapped ? 0 : -1;
112 }

◆ Setup()

void tesseract::IndexMapBiDi::Setup ( )

Definition at line 117 of file indexmapbidi.cpp.

117  {
118  int compact_size = 0;
119  for (int &i : sparse_map_) {
120  if (i >= 0) {
121  i = compact_size++;
122  }
123  }
124  compact_map_.clear();
125  compact_map_.resize(compact_size, -1);
126  for (size_t i = 0; i < sparse_map_.size(); ++i) {
127  if (sparse_map_[i] >= 0) {
128  compact_map_[sparse_map_[i]] = i;
129  }
130  }
131  sparse_size_ = sparse_map_.size();
132 }

◆ SparseSize()

int tesseract::IndexMapBiDi::SparseSize ( ) const
inlineoverridevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 144 of file indexmapbidi.h.

144  {
145  return sparse_map_.size();
146  }

◆ SparseToCompact()

int tesseract::IndexMapBiDi::SparseToCompact ( int  sparse_index) const
inlineoverridevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 140 of file indexmapbidi.h.

140  {
141  return sparse_map_[sparse_index];
142  }

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