tesseract  4.00.00dev
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
int sparse_size_
 
GenericVector< inT32compact_map_
 

Detailed Description

Definition at line 102 of file indexmapbidi.h.

Constructor & Destructor Documentation

◆ ~IndexMapBiDi()

virtual tesseract::IndexMapBiDi::~IndexMapBiDi ( )
inlinevirtual

Definition at line 104 of file indexmapbidi.h.

104 {}

Member Function Documentation

◆ CompleteMerges()

void tesseract::IndexMapBiDi::CompleteMerges ( )

Definition at line 153 of file indexmapbidi.cpp.

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

◆ CopyFrom()

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

Definition at line 112 of file indexmapbidi.cpp.

112  {
113  sparse_map_ = src.sparse_map_;
114  compact_map_ = src.compact_map_;
115  sparse_size_ = sparse_map_.size();
116 }
GenericVector< inT32 > compact_map_
Definition: indexmapbidi.h:80
int size() const
Definition: genericvector.h:72

◆ DeSerialize()

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

Definition at line 209 of file indexmapbidi.cpp.

209  {
210  if (!IndexMap::DeSerialize(swap, fp)) return false;
211  GenericVector<inT32> remaining_pairs;
212  if (!remaining_pairs.DeSerialize(swap, fp)) return false;
213  sparse_map_.init_to_size(sparse_size_, -1);
214  for (int i = 0; i < compact_map_.size(); ++i) {
215  sparse_map_[compact_map_[i]] = i;
216  }
217  for (int i = 0; i < remaining_pairs.size(); ++i) {
218  int sparse_index = remaining_pairs[i++];
219  sparse_map_[sparse_index] = remaining_pairs[i];
220  }
221  return true;
222 }
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
void init_to_size(int size, T t)
GenericVector< inT32 > compact_map_
Definition: indexmapbidi.h:80
int size() const
Definition: genericvector.h:72

◆ Init()

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

Definition at line 79 of file indexmapbidi.cpp.

79  {
80  sparse_map_.init_to_size(size, -1);
81  if (all_mapped) {
82  for (int i = 0; i < size; ++i)
83  sparse_map_[i] = i;
84  }
85 }
void init_to_size(int size, T t)
voidpf void uLong size
Definition: ioapi.h:39

◆ InitAndSetupRange()

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

Definition at line 68 of file indexmapbidi.cpp.

68  {
69  Init(sparse_size, false);
70  for (int i = start; i < end; ++i)
71  SetMap(i, true);
72  Setup();
73 }
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 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 229 of file indexmapbidi.cpp.

230  {
231  compact->truncate(0);
232  int num_features = sparse.size();
233  int missed_features = 0;
234  int prev_good_feature = -1;
235  for (int f = 0; f < num_features; ++f) {
236  int feature = sparse_map_[sparse[f]];
237  if (feature >= 0) {
238  if (feature != prev_good_feature) {
239  compact->push_back(feature);
240  prev_good_feature = feature;
241  }
242  } else {
243  ++missed_features;
244  }
245  }
246  return missed_features;
247 }
int push_back(T object)
void truncate(int size)
int size() const
Definition: genericvector.h:72

◆ Merge()

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

Definition at line 121 of file indexmapbidi.cpp.

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

◆ Serialize()

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

Definition at line 191 of file indexmapbidi.cpp.

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

◆ SetMap()

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

Definition at line 88 of file indexmapbidi.cpp.

88  {
89  sparse_map_[sparse_index] = mapped ? 0 : -1;
90 }

◆ Setup()

void tesseract::IndexMapBiDi::Setup ( )

Definition at line 95 of file indexmapbidi.cpp.

95  {
96  int compact_size = 0;
97  for (int i = 0; i < sparse_map_.size(); ++i) {
98  if (sparse_map_[i] >= 0) {
99  sparse_map_[i] = compact_size++;
100  }
101  }
102  compact_map_.init_to_size(compact_size, -1);
103  for (int i = 0; i < sparse_map_.size(); ++i) {
104  if (sparse_map_[i] >= 0) {
105  compact_map_[sparse_map_[i]] = i;
106  }
107  }
108  sparse_size_ = sparse_map_.size();
109 }
void init_to_size(int size, T t)
GenericVector< inT32 > compact_map_
Definition: indexmapbidi.h:80
int size() const
Definition: genericvector.h:72

◆ 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:72

◆ 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: