Replace new / delete by std::unique_ptr and std::vector in class Classify

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-10-10 14:08:12 +02:00
parent 5d903da1ce
commit 85cb6678fa

View File

@ -155,7 +155,7 @@ static int FindScoredUnichar(UNICHAR_ID id, const ADAPT_RESULTS &results) {
// Returns the current rating for a unichar id if we have rated it, defaulting
// to WORST_POSSIBLE_RATING.
static float ScoredUnichar(UNICHAR_ID id, const ADAPT_RESULTS &results) {
int index = FindScoredUnichar(id, results);
unsigned index = FindScoredUnichar(id, results);
if (index >= results.match.size()) {
return WORST_POSSIBLE_RATING;
}
@ -323,7 +323,7 @@ void Classify::LearnWord(const char *fontname, WERD_RES *word) {
pieces_all_natural);
std::string full_string;
for (int i = 0; i < tokens.size(); i++) {
for (unsigned i = 0; i < tokens.size(); i++) {
full_string += tokens[i];
if (i != tokens.size() - 1) {
full_string += ' ';
@ -979,7 +979,7 @@ void Classify::DisplayAdaptedChar(TBLOB *blob, INT_CLASS_STRUCT *int_class) {
* @param[out] results results to add new result to
*/
void Classify::AddNewResult(const UnicharRating &new_result, ADAPT_RESULTS *results) {
int old_match = FindScoredUnichar(new_result.unichar_id, *results);
unsigned old_match = FindScoredUnichar(new_result.unichar_id, *results);
if (new_result.rating + matcher_bad_match_pad < results->best_rating ||
(old_match < results->match.size() &&
@ -1120,7 +1120,7 @@ void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS_STRUCT **classes, boo
// by int_result. In this case, build a vector of UnicharRating to
// gather together different font-ids for each unichar. Also covers case1.
std::vector<UnicharRating> mapped_results;
for (int f = 0; f < int_result->fonts.size(); ++f) {
for (unsigned f = 0; f < int_result->fonts.size(); ++f) {
int shape_id = int_result->fonts[f].fontinfo_id;
const Shape &shape = shape_table_->GetShape(shape_id);
for (int c = 0; c < shape.size(); ++c) {
@ -1283,7 +1283,7 @@ int Classify::CharNormClassifier(TBLOB *blob, const TrainingSample &sample,
int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const TrainingSample &sample,
std::vector<UnicharRating> *results) {
results->clear();
auto *adapt_results = new ADAPT_RESULTS();
std::unique_ptr<ADAPT_RESULTS> adapt_results(new ADAPT_RESULTS());
adapt_results->Initialize();
// Compute the bounding box of the features.
uint32_t num_features = sample.num_features();
@ -1293,16 +1293,15 @@ int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const Trai
sample.geo_feature(GeoTop), sample.geo_feature(GeoTop));
// Compute the char_norm_array from the saved cn_feature.
FEATURE norm_feature = sample.GetCNFeature();
auto *char_norm_array = new uint8_t[unicharset.size()];
std::vector<uint8_t> char_norm_array(unicharset.size());
int num_pruner_classes = std::max(unicharset.size(), PreTrainedTemplates->NumClasses);
auto *pruner_norm_array = new uint8_t[num_pruner_classes];
std::vector<uint8_t> pruner_norm_array(num_pruner_classes);
adapt_results->BlobLength = static_cast<int>(ActualOutlineLength(norm_feature) * 20 + 0.5);
ComputeCharNormArrays(norm_feature, PreTrainedTemplates, char_norm_array, pruner_norm_array);
ComputeCharNormArrays(norm_feature, PreTrainedTemplates, &char_norm_array[0], &pruner_norm_array[0]);
PruneClasses(PreTrainedTemplates, num_features, keep_this, sample.features(), pruner_norm_array,
PruneClasses(PreTrainedTemplates, num_features, keep_this, sample.features(), &pruner_norm_array[0],
shape_table_ != nullptr ? &shapetable_cutoffs_[0] : CharNormCutoffs,
&adapt_results->CPResults);
delete[] pruner_norm_array;
if (keep_this >= 0) {
adapt_results->CPResults[0].Class = keep_this;
adapt_results->CPResults.resize(1);
@ -1314,9 +1313,9 @@ int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const Trai
results->push_back(UnicharRating(class_id, 1.0f - it.Rating));
}
} else {
MasterMatcher(PreTrainedTemplates, num_features, sample.features(), char_norm_array, nullptr,
MasterMatcher(PreTrainedTemplates, num_features, sample.features(), &char_norm_array[0], nullptr,
matcher_debug_flags, classify_integer_matcher_multiplier, blob_box,
adapt_results->CPResults, adapt_results);
adapt_results->CPResults, adapt_results.get());
// Convert master matcher results to output format.
for (auto &i : adapt_results->match) {
results->push_back(i);
@ -1325,8 +1324,6 @@ int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const Trai
std::sort(results->begin(), results->end(), SortDescendingRating);
}
}
delete[] char_norm_array;
delete adapt_results;
return num_features;
} /* CharNormTrainingSample */
@ -1627,17 +1624,17 @@ int Classify::GetCharNormFeature(const INT_FX_RESULT_STRUCT &fx_info, INT_TEMPLA
void Classify::ComputeCharNormArrays(FEATURE_STRUCT *norm_feature, INT_TEMPLATES_STRUCT *templates,
uint8_t *char_norm_array, uint8_t *pruner_array) {
ComputeIntCharNormArray(*norm_feature, char_norm_array);
if (pruner_array != nullptr) {
//if (pruner_array != nullptr) {
if (shape_table_ == nullptr) {
ComputeIntCharNormArray(*norm_feature, pruner_array);
} else {
memset(pruner_array, UINT8_MAX, templates->NumClasses * sizeof(pruner_array[0]));
memset(&pruner_array[0], UINT8_MAX, templates->NumClasses * sizeof(pruner_array[0]));
// Each entry in the pruner norm array is the MIN of all the entries of
// the corresponding unichars in the CharNormArray.
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) {
for (unsigned 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]) {
@ -1647,7 +1644,7 @@ void Classify::ComputeCharNormArrays(FEATURE_STRUCT *norm_feature, INT_TEMPLATES
}
}
}
}
//}
delete norm_feature;
}
@ -2121,7 +2118,7 @@ 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) {
for (unsigned config = 0; config < fs.size(); ++config) {
if (fs[config] == shape_id) {
return id;
}