From 048e63c02be42e83d5ad8ca1866e697433215a22 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Fri, 23 Apr 2021 02:38:25 +0300 Subject: [PATCH] Replace FontSet struct with vector. It may be improved further (remove pointer?). --- src/ccstruct/fontinfo.cpp | 14 ++++++++------ src/ccstruct/fontinfo.h | 21 ++++----------------- src/classify/adaptmatch.cpp | 12 ++++++------ src/classify/intproto.cpp | 14 ++++++-------- 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/ccstruct/fontinfo.cpp b/src/ccstruct/fontinfo.cpp index 300974b7..23abadc8 100644 --- a/src/ccstruct/fontinfo.cpp +++ b/src/ccstruct/fontinfo.cpp @@ -138,7 +138,7 @@ void FontInfoDeleteCallback(FontInfo f) { f.name = nullptr; } void FontSetDeleteCallback(FontSet fs) { - delete[] fs.configs; + delete fs; } /*---------------------------------------------------------------------------*/ @@ -222,16 +222,18 @@ bool write_spacing_info(FILE *f, const FontInfo &fi) { return true; } -bool read_set(TFile *f, FontSet *fs) { - if (!f->DeSerialize(&fs->size)) { +bool read_set(TFile *f, FontSet &fs) { + int size; + if (!f->DeSerialize(&size)) { return false; } - fs->configs = new int[fs->size]; - return f->DeSerialize(&fs->configs[0], fs->size); + fs->resize(size); + return f->DeSerialize(&(*fs)[0], size); } bool write_set(FILE *f, const FontSet &fs) { - return tesseract::Serialize(f, &fs.size) && tesseract::Serialize(f, &fs.configs[0], fs.size); + int size = fs->size(); + return tesseract::Serialize(f, &size) && tesseract::Serialize(f, &(*fs)[0], size); } } // namespace tesseract. diff --git a/src/ccstruct/fontinfo.h b/src/ccstruct/fontinfo.h index 07c435ac..56b01ee7 100644 --- a/src/ccstruct/fontinfo.h +++ b/src/ccstruct/fontinfo.h @@ -26,6 +26,7 @@ #include // for uint16_t, uint32_t #include // for FILE +#include namespace tesseract { @@ -151,22 +152,8 @@ struct FontInfo { // lot of FontSet that differ only by one font. Rather than storing directly // the FontInfo in the FontSet structure, it's better to share FontInfos among // FontSets (Classify::fontinfo_table_). -struct FontSet { - int size; - int *configs; // FontInfo ids - - bool operator==(const FontSet &rhs) const { - if (size != rhs.size) { - return false; - } - for (int i = 0; i < size; ++i) { - if (configs[i] != rhs.configs[i]) { - return false; - } - } - return true; - } -}; +using FontSetBase = std::vector; +using FontSet = FontSetBase *; // Class that adds a bit of functionality on top of GenericVector to // implement a table of FontInfo that replaces UniCityTable. @@ -213,7 +200,7 @@ bool read_info(TFile *f, FontInfo *fi); bool write_info(FILE *f, const FontInfo &fi); bool read_spacing_info(TFile *f, FontInfo *fi); bool write_spacing_info(FILE *f, const FontInfo &fi); -bool read_set(TFile *f, FontSet *fs); +bool read_set(TFile *f, FontSet &fs); bool write_set(FILE *f, const FontSet &fs); } // namespace tesseract. diff --git a/src/classify/adaptmatch.cpp b/src/classify/adaptmatch.cpp index e803a293..17043ee3 100644 --- a/src/classify/adaptmatch.cpp +++ b/src/classify/adaptmatch.cpp @@ -1637,8 +1637,8 @@ void Classify::ComputeCharNormArrays(FEATURE_STRUCT *norm_feature, INT_TEMPLATES for (int id = 0; id < templates->NumClasses; ++id) { int font_set_id = templates->Class[id]->font_set_id; const FontSet &fs = fontset_table_.at(font_set_id); - for (int config = 0; config < fs.size; ++config) { - const Shape &shape = shape_table_->GetShape(fs.configs[config]); + for (int config = 0; config < fs->size(); ++config) { + const Shape &shape = shape_table_->GetShape((*fs)[config]); for (int c = 0; c < shape.size(); ++c) { if (char_norm_array[shape[c].unichar_id] < pruner_array[id]) { pruner_array[id] = char_norm_array[shape[c].unichar_id]; @@ -2111,8 +2111,8 @@ int Classify::ClassAndConfigIDToFontOrShapeID(int class_id, int int_result_confi return kBlankFontinfoId; } const FontSet &fs = fontset_table_.at(font_set_id); - ASSERT_HOST(int_result_config >= 0 && int_result_config < fs.size); - return fs.configs[int_result_config]; + ASSERT_HOST(int_result_config >= 0 && int_result_config < fs->size()); + return (*fs)[int_result_config]; } // Converts a shape_table_ index to a classifier class_id index (not a @@ -2122,8 +2122,8 @@ int Classify::ShapeIDToClassID(int shape_id) const { int font_set_id = PreTrainedTemplates->Class[id]->font_set_id; ASSERT_HOST(font_set_id >= 0); const FontSet &fs = fontset_table_.at(font_set_id); - for (int config = 0; config < fs.size; ++config) { - if (fs.configs[config] == shape_id) { + for (int config = 0; config < fs->size(); ++config) { + if ((*fs)[config] == shape_id) { return id; } } diff --git a/src/classify/intproto.cpp b/src/classify/intproto.cpp index e249aee3..3227f095 100644 --- a/src/classify/intproto.cpp +++ b/src/classify/intproto.cpp @@ -506,15 +506,13 @@ INT_TEMPLATES_STRUCT *Classify::CreateIntTemplates(CLASSES FloatProtos, } assert(UnusedClassIdIn(IntTemplates, ClassId)); IClass = new INT_CLASS_STRUCT(FClass->NumProtos, FClass->NumConfigs); - FontSet fs; - fs.size = FClass->font_set.size(); - fs.configs = new int[fs.size]; - for (int i = 0; i < fs.size; ++i) { - fs.configs[i] = FClass->font_set.at(i); + FontSet fs = new FontSetBase{FClass->font_set.size()}; + for (int i = 0; i < fs->size(); ++i) { + (*fs)[i] = FClass->font_set.at(i); } if (this->fontset_table_.contains(fs)) { IClass->font_set_id = this->fontset_table_.get_id(fs); - delete[] fs.configs; + delete fs; } else { IClass->font_set_id = this->fontset_table_.push_back(fs); } @@ -854,7 +852,7 @@ INT_TEMPLATES_STRUCT *Classify::ReadIntTemplates(TFile *fp) { if (version_id >= 5) { this->fontinfo_table_.read(fp, std::bind(read_spacing_info, _1, _2)); } - this->fontset_table_.read(fp, std::bind(read_set, _1, _2)); + this->fontset_table_.read(fp, [](auto *f, auto *fs) { return read_set(f, *fs); } ); } return (Templates); @@ -952,7 +950,7 @@ void Classify::WriteIntTemplates(FILE *File, INT_TEMPLATES_STRUCT *Templates, /* first write out the high level struct for the class */ fwrite(&Class->NumProtos, sizeof(Class->NumProtos), 1, File); fwrite(&Class->NumProtoSets, sizeof(Class->NumProtoSets), 1, File); - ASSERT_HOST(Class->NumConfigs == this->fontset_table_.at(Class->font_set_id).size); + ASSERT_HOST(Class->NumConfigs == this->fontset_table_.at(Class->font_set_id)->size()); fwrite(&Class->NumConfigs, sizeof(Class->NumConfigs), 1, File); for (j = 0; j < Class->NumConfigs; ++j) { fwrite(&Class->ConfigLengths[j], sizeof(uint16_t), 1, File);