Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract::IndexMapBiDi Class Reference

#include <indexmapbidi.h>

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

List of all members.

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)

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

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

Definition at line 104 of file indexmapbidi.h.

{}

Member Function Documentation

void tesseract::IndexMapBiDi::CompleteMerges ( )

Definition at line 153 of file indexmapbidi.cpp.

{
// Ensure each sparse_map_entry contains a master compact_map_ index.
int compact_size = 0;
for (int i = 0; i < sparse_map_.size(); ++i) {
int compact_index = MasterCompactIndex(sparse_map_[i]);
sparse_map_[i] = compact_index;
if (compact_index >= compact_size)
compact_size = compact_index + 1;
}
// Re-generate the compact_map leaving holes for unused indices.
compact_map_.init_to_size(compact_size, -1);
for (int i = 0; i < sparse_map_.size(); ++i) {
if (sparse_map_[i] >= 0) {
if (compact_map_[sparse_map_[i]] == -1)
compact_map_[sparse_map_[i]] = i;
}
}
// Compact the compact_map, leaving tmp_compact_map saying where each
// index went to in the compacted map.
GenericVector<inT32> tmp_compact_map;
tmp_compact_map.init_to_size(compact_size, -1);
compact_size = 0;
for (int i = 0; i < compact_map_.size(); ++i) {
if (compact_map_[i] >= 0) {
tmp_compact_map[i] = compact_size;
compact_map_[compact_size++] = compact_map_[i];
}
}
compact_map_.truncate(compact_size);
// Now modify the entries in the sparse map to point to the new locations.
for (int i = 0; i < sparse_map_.size(); ++i) {
if (sparse_map_[i] >= 0) {
sparse_map_[i] = tmp_compact_map[sparse_map_[i]];
}
}
}
void tesseract::IndexMapBiDi::CopyFrom ( const IndexMapBiDi src)

Reimplemented from tesseract::IndexMap.

Definition at line 112 of file indexmapbidi.cpp.

{
sparse_map_ = src.sparse_map_;
compact_map_ = src.compact_map_;
sparse_size_ = sparse_map_.size();
}
bool tesseract::IndexMapBiDi::DeSerialize ( bool  swap,
FILE *  fp 
)

Reimplemented from tesseract::IndexMap.

Definition at line 209 of file indexmapbidi.cpp.

{
if (!IndexMap::DeSerialize(swap, fp)) return false;
GenericVector<inT32> remaining_pairs;
if (!remaining_pairs.DeSerialize(swap, fp)) return false;
sparse_map_.init_to_size(sparse_size_, -1);
for (int i = 0; i < compact_map_.size(); ++i) {
sparse_map_[compact_map_[i]] = i;
}
for (int i = 0; i < remaining_pairs.size(); ++i) {
int sparse_index = remaining_pairs[i++];
sparse_map_[sparse_index] = remaining_pairs[i];
}
return true;
}
void tesseract::IndexMapBiDi::Init ( int  size,
bool  all_mapped 
)

Definition at line 79 of file indexmapbidi.cpp.

{
sparse_map_.init_to_size(size, -1);
if (all_mapped) {
for (int i = 0; i < size; ++i)
sparse_map_[i] = i;
}
}
void tesseract::IndexMapBiDi::InitAndSetupRange ( int  sparse_size,
int  start,
int  end 
)

Definition at line 68 of file indexmapbidi.cpp.

{
Init(sparse_size, false);
for (int i = start; i < end; ++i)
SetMap(i, true);
Setup();
}
bool tesseract::IndexMapBiDi::IsCompactDeleted ( int  index) const
inline

Definition at line 130 of file indexmapbidi.h.

{
return MasterCompactIndex(index) < 0;
}
int tesseract::IndexMapBiDi::MapFeatures ( const GenericVector< int > &  sparse,
GenericVector< int > *  compact 
) const

Definition at line 229 of file indexmapbidi.cpp.

{
compact->truncate(0);
int num_features = sparse.size();
int missed_features = 0;
int prev_good_feature = -1;
for (int f = 0; f < num_features; ++f) {
int feature = sparse_map_[sparse[f]];
if (feature >= 0) {
if (feature != prev_good_feature) {
compact->push_back(feature);
prev_good_feature = feature;
}
} else {
++missed_features;
}
}
return missed_features;
}
bool tesseract::IndexMapBiDi::Merge ( int  compact_index1,
int  compact_index2 
)

Definition at line 121 of file indexmapbidi.cpp.

{
// Find the current master index for index1 and index2.
compact_index1 = MasterCompactIndex(compact_index1);
compact_index2 = MasterCompactIndex(compact_index2);
// Be sure that index1 < index2.
if (compact_index1 > compact_index2) {
int tmp = compact_index1;
compact_index1 = compact_index2;
compact_index2 = tmp;
} else if (compact_index1 == compact_index2) {
return false;
}
// To save iterating over all sparse_map_ entries, simply make the master
// entry for index2 point to index1.
// This leaves behind a potential chain of parents that needs to be chased,
// as above.
sparse_map_[compact_map_[compact_index2]] = compact_index1;
if (compact_index1 >= 0)
compact_map_[compact_index2] = compact_map_[compact_index1];
return true;
}
bool tesseract::IndexMapBiDi::Serialize ( FILE *  fp) const

Reimplemented from tesseract::IndexMap.

Definition at line 191 of file indexmapbidi.cpp.

{
if (!IndexMap::Serialize(fp)) return false;
// Make a vector containing the rest of the map. If the map is many-to-one
// then each additional sparse entry needs to be stored.
// Normally we store only the compact map to save space.
GenericVector<inT32> remaining_pairs;
for (int i = 0; i < sparse_map_.size(); ++i) {
if (sparse_map_[i] >= 0 && compact_map_[sparse_map_[i]] != i) {
remaining_pairs.push_back(i);
remaining_pairs.push_back(sparse_map_[i]);
}
}
if (!remaining_pairs.Serialize(fp)) return false;
return true;
}
void tesseract::IndexMapBiDi::SetMap ( int  sparse_index,
bool  mapped 
)

Definition at line 88 of file indexmapbidi.cpp.

{
sparse_map_[sparse_index] = mapped ? 0 : -1;
}
void tesseract::IndexMapBiDi::Setup ( )

Definition at line 95 of file indexmapbidi.cpp.

{
int compact_size = 0;
for (int i = 0; i < sparse_map_.size(); ++i) {
if (sparse_map_[i] >= 0) {
sparse_map_[i] = compact_size++;
}
}
compact_map_.init_to_size(compact_size, -1);
for (int i = 0; i < sparse_map_.size(); ++i) {
if (sparse_map_[i] >= 0) {
compact_map_[sparse_map_[i]] = i;
}
}
sparse_size_ = sparse_map_.size();
}
virtual int tesseract::IndexMapBiDi::SparseSize ( ) const
inlinevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 142 of file indexmapbidi.h.

{
return sparse_map_.size();
}
virtual int tesseract::IndexMapBiDi::SparseToCompact ( int  sparse_index) const
inlinevirtual

Reimplemented from tesseract::IndexMap.

Definition at line 138 of file indexmapbidi.h.

{
return sparse_map_[sparse_index];
}

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