From 187ac4136a71bfd0a9f7a8381c567c1bf9c90ed2 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 18:20:09 +0100 Subject: [PATCH 1/9] Fix LGTM alert (local variable hides a parameter) LGTM alert: Local variable 'correct_text' hides a parameter of the same name. Signed-off-by: Stefan Weil --- src/ccmain/applybox.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ccmain/applybox.cpp b/src/ccmain/applybox.cpp index f08b1e52..1d7db24a 100644 --- a/src/ccmain/applybox.cpp +++ b/src/ccmain/applybox.cpp @@ -385,8 +385,8 @@ bool Tesseract::ResegmentCharBox(PAGE_RES *page_res, const TBOX *prev_box, const } tprintf("\n"); tprintf("Correct text = [[ "); - for (auto &correct_text : word_res->correct_text) { - tprintf("%s ", correct_text.c_str()); + for (auto &it : word_res->correct_text) { + tprintf("%s ", it.c_str()); } tprintf("]]\n"); } From 719dc1d7da4c8a398913565b5a9cd074c472b7be Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:06:34 +0100 Subject: [PATCH 2/9] Modernize code using override The modifications were made using this command: run-clang-tidy -header-filter='.*' -checks='-*,modernize-use-override' -fix Signed-off-by: Stefan Weil --- src/training/unicharset/icuerrorcode.h | 4 ++-- src/training/unicharset/validate_grapheme.h | 2 +- src/training/unicharset/validate_indic.h | 2 +- src/training/unicharset/validate_javanese.h | 2 +- src/training/unicharset/validate_khmer.h | 2 +- src/training/unicharset/validate_myanmar.h | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/training/unicharset/icuerrorcode.h b/src/training/unicharset/icuerrorcode.h index 5a027df1..c02c74be 100644 --- a/src/training/unicharset/icuerrorcode.h +++ b/src/training/unicharset/icuerrorcode.h @@ -45,10 +45,10 @@ namespace tesseract { class IcuErrorCode : public icu::ErrorCode { public: IcuErrorCode() {} - virtual ~IcuErrorCode(); + ~IcuErrorCode() override; protected: - virtual void handleFailure() const { + void handleFailure() const override { tprintf("ICU ERROR: %s\n", errorName()); exit(errorCode); } diff --git a/src/training/unicharset/validate_grapheme.h b/src/training/unicharset/validate_grapheme.h index 7a739502..6d8ddabc 100644 --- a/src/training/unicharset/validate_grapheme.h +++ b/src/training/unicharset/validate_grapheme.h @@ -10,7 +10,7 @@ namespace tesseract { class ValidateGrapheme : public Validator { public: ValidateGrapheme(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateGrapheme() {} + ~ValidateGrapheme() override {} protected: // Consumes the next Grapheme in codes_[codes_used_++...] and copies it to diff --git a/src/training/unicharset/validate_indic.h b/src/training/unicharset/validate_indic.h index 4d9c7d8b..055ecf11 100644 --- a/src/training/unicharset/validate_indic.h +++ b/src/training/unicharset/validate_indic.h @@ -10,7 +10,7 @@ namespace tesseract { class ValidateIndic : public Validator { public: ValidateIndic(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateIndic() {} + ~ValidateIndic() override {} protected: // Returns whether codes matches the pattern for an Indic Grapheme. diff --git a/src/training/unicharset/validate_javanese.h b/src/training/unicharset/validate_javanese.h index 79766924..c2fcd53d 100644 --- a/src/training/unicharset/validate_javanese.h +++ b/src/training/unicharset/validate_javanese.h @@ -28,7 +28,7 @@ namespace tesseract { class ValidateJavanese : public Validator { public: ValidateJavanese(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateJavanese() {} + ~ValidateJavanese() override {} protected: // Returns whether codes matches the pattern for an Javanese Grapheme. diff --git a/src/training/unicharset/validate_khmer.h b/src/training/unicharset/validate_khmer.h index 12f3fb94..e921be38 100644 --- a/src/training/unicharset/validate_khmer.h +++ b/src/training/unicharset/validate_khmer.h @@ -9,7 +9,7 @@ namespace tesseract { class ValidateKhmer : public Validator { public: ValidateKhmer(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateKhmer() {} + ~ValidateKhmer() override {} protected: // Returns whether codes matches the pattern for an Khmer Grapheme. diff --git a/src/training/unicharset/validate_myanmar.h b/src/training/unicharset/validate_myanmar.h index 8ad600a2..662728e4 100644 --- a/src/training/unicharset/validate_myanmar.h +++ b/src/training/unicharset/validate_myanmar.h @@ -9,7 +9,7 @@ namespace tesseract { class ValidateMyanmar : public Validator { public: ValidateMyanmar(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateMyanmar() {} + ~ValidateMyanmar() override {} protected: // Returns whether codes matches the pattern for a Myanmar Grapheme. From 02774bda6ed7d88926afd465cf047bc18e02ecfe Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:24:28 +0100 Subject: [PATCH 3/9] Modernize code (clang-tidy -checks='-*,modernize-loop-convert') Signed-off-by: Stefan Weil --- src/ccmain/equationdetect.cpp | 95 +++++++++++------------ src/ccmain/par_control.cpp | 18 ++--- src/ccmain/paragraphs.cpp | 91 +++++++++++----------- src/ccmain/pgedit.cpp | 8 +- src/ccmain/resultiterator.cpp | 12 +-- src/ccmain/tessedit.cpp | 21 +++-- src/ccmain/tesseractclass.cpp | 36 ++++----- src/ccstruct/blobs.cpp | 19 +++-- src/ccstruct/boxread.cpp | 4 +- src/ccstruct/detlinefit.cpp | 10 +-- src/ccstruct/fontinfo.cpp | 4 +- src/ccstruct/imagedata.cpp | 6 +- src/ccstruct/pageres.cpp | 3 +- src/ccutil/ambigs.cpp | 3 +- src/ccutil/indexmapbidi.cpp | 18 ++--- src/ccutil/params.cpp | 32 ++++---- src/ccutil/unicharcompress.cpp | 10 +-- src/classify/adaptmatch.cpp | 25 +++--- src/classify/shapeclassifier.cpp | 22 +++--- src/classify/shapetable.cpp | 93 +++++++++++----------- src/dict/dawg.cpp | 12 +-- src/dict/dict.cpp | 22 +++--- src/dict/trie.cpp | 12 +-- src/lstm/plumbing.cpp | 36 ++++----- src/lstm/recodebeam.cpp | 32 ++++---- src/lstm/series.cpp | 26 +++---- src/lstm/weightmatrix.cpp | 4 +- src/textord/baselinedetect.cpp | 37 ++++----- src/textord/colfind.cpp | 3 +- src/textord/tabfind.cpp | 15 ++-- src/textord/tablerecog.cpp | 24 +++--- src/training/cntraining.cpp | 4 +- src/training/common/intfeaturedist.cpp | 3 +- src/training/common/intfeaturemap.cpp | 4 +- src/training/common/mastertrainer.cpp | 4 +- src/training/common/trainingsampleset.cpp | 22 +++--- src/training/pango/boxchar.cpp | 25 +++--- src/training/pango/pango_font_info.cpp | 23 +++--- src/training/pango/stringrenderer.cpp | 41 +++++----- src/training/text2image.cpp | 4 +- src/training/unicharset/lstmtrainer.cpp | 6 +- src/training/unicharset/normstrngs.cpp | 12 +-- src/training/unicharset_extractor.cpp | 6 +- src/wordrec/chopper.cpp | 13 ++-- src/wordrec/lm_pain_points.cpp | 3 +- src/wordrec/segsearch.cpp | 4 +- 46 files changed, 443 insertions(+), 484 deletions(-) diff --git a/src/ccmain/equationdetect.cpp b/src/ccmain/equationdetect.cpp index 8525f200..d34ecad3 100644 --- a/src/ccmain/equationdetect.cpp +++ b/src/ccmain/equationdetect.cpp @@ -134,8 +134,8 @@ int EquationDetect::LabelSpecialText(TO_BLOCK *to_block) { std::vector blob_lists; blob_lists.push_back(&(to_block->blobs)); blob_lists.push_back(&(to_block->large_blobs)); - for (int i = 0; i < blob_lists.size(); ++i) { - BLOBNBOX_IT bbox_it(blob_lists[i]); + for (auto &blob_list : blob_lists) { + BLOBNBOX_IT bbox_it(blob_list); for (bbox_it.mark_cycle_pt(); !bbox_it.cycled_list(); bbox_it.forward()) { bbox_it.data()->set_special_text_type(BSTT_NONE); } @@ -227,8 +227,8 @@ BlobSpecialTextType EquationDetect::EstimateTypeForUnichar(const UNICHARSET &uni if (ids_to_exclude.empty()) { static const char *kCharsToEx[] = {"'", "`", "\"", "\\", ",", ".", "〈", "〉", "《", "》", "」", "「"}; - for (auto i = 0; i < countof(kCharsToEx); i++) { - ids_to_exclude.push_back(unicharset.unichar_to_id(kCharsToEx[i])); + for (auto &i : kCharsToEx) { + ids_to_exclude.push_back(unicharset.unichar_to_id(i)); } std::sort(ids_to_exclude.begin(), ids_to_exclude.end()); } @@ -379,16 +379,16 @@ int EquationDetect::FindEquationParts(ColPartitionGrid *part_grid, ColPartitionS // Pass 3: expand block equation seeds. while (!cp_seeds_.empty()) { std::vector seeds_expanded; - for (int i = 0; i < cp_seeds_.size(); ++i) { - if (ExpandSeed(cp_seeds_[i])) { + for (auto &cp_seed : cp_seeds_) { + if (ExpandSeed(cp_seed)) { // If this seed is expanded, then we add it into seeds_expanded. Note // this seed has been removed from part_grid_ if it is expanded. - seeds_expanded.push_back(cp_seeds_[i]); + seeds_expanded.push_back(cp_seed); } } // Add seeds_expanded back into part_grid_ and reset cp_seeds_. - for (int i = 0; i < seeds_expanded.size(); ++i) { - InsertPartAfterAbsorb(seeds_expanded[i]); + for (auto &i : seeds_expanded) { + InsertPartAfterAbsorb(i); } cp_seeds_ = seeds_expanded; } @@ -423,9 +423,9 @@ void EquationDetect::MergePartsByLocation() { // Merge parts_to_merge with part, and remove them from part_grid_. part_grid_->RemoveBBox(part); - for (int i = 0; i < parts_to_merge.size(); ++i) { - ASSERT_HOST(parts_to_merge[i] != nullptr && parts_to_merge[i] != part); - part->Absorb(parts_to_merge[i], nullptr); + for (auto &i : parts_to_merge) { + ASSERT_HOST(i != nullptr && i != part); + part->Absorb(i, nullptr); } gsearch.RepositionIterator(); @@ -437,8 +437,8 @@ void EquationDetect::MergePartsByLocation() { } // Re-insert parts_updated into part_grid_. - for (int i = 0; i < parts_updated.size(); ++i) { - InsertPartAfterAbsorb(parts_updated[i]); + for (auto &i : parts_updated) { + InsertPartAfterAbsorb(i); } } } @@ -561,23 +561,23 @@ void EquationDetect::IdentifySeedParts() { foreground_density_th = 0.8 * texts_foreground_density[texts_foreground_density.size() / 2]; } - for (int i = 0; i < seeds1.size(); ++i) { - const TBOX &box = seeds1[i]->bounding_box(); - if (CheckSeedFgDensity(foreground_density_th, seeds1[i]) && - !(IsLeftIndented(IsIndented(seeds1[i])) && + for (auto &i : seeds1) { + const TBOX &box = i->bounding_box(); + if (CheckSeedFgDensity(foreground_density_th, i) && + !(IsLeftIndented(IsIndented(i)) && CountAlignment(indented_texts_left, box.left()) >= kLeftIndentAlignmentCountTh)) { // Mark as PT_EQUATION type. - seeds1[i]->set_type(PT_EQUATION); - cp_seeds_.push_back(seeds1[i]); + i->set_type(PT_EQUATION); + cp_seeds_.push_back(i); } else { // Mark as PT_INLINE_EQUATION type. - seeds1[i]->set_type(PT_INLINE_EQUATION); + i->set_type(PT_INLINE_EQUATION); } } - for (int i = 0; i < seeds2.size(); ++i) { - if (CheckForSeed2(indented_texts_left, foreground_density_th, seeds2[i])) { - seeds2[i]->set_type(PT_EQUATION); - cp_seeds_.push_back(seeds2[i]); + for (auto &i : seeds2) { + if (CheckForSeed2(indented_texts_left, foreground_density_th, i)) { + i->set_type(PT_EQUATION); + cp_seeds_.push_back(i); } } } @@ -602,8 +602,8 @@ bool EquationDetect::CheckSeedFgDensity(const float density_th, ColPartition *pa std::vector sub_boxes; SplitCPHorLite(part, &sub_boxes); float parts_passed = 0.0; - for (int i = 0; i < sub_boxes.size(); ++i) { - const float density = ComputeForegroundDensity(sub_boxes[i]); + for (auto &sub_boxe : sub_boxes) { + const float density = ComputeForegroundDensity(sub_boxe); if (density < density_th) { parts_passed++; } @@ -777,8 +777,7 @@ void EquationDetect::IdentifyInlinePartsHorizontal() { search.SetUniqueMode(true); // The center x coordinate of the cp_super_bbox_. const int cps_cx = cps_super_bbox_->left() + cps_super_bbox_->width() / 2; - for (int i = 0; i < cp_seeds_.size(); ++i) { - ColPartition *part = cp_seeds_[i]; + for (auto part : cp_seeds_) { const TBOX &part_box(part->bounding_box()); const int left_margin = part_box.left() - cps_super_bbox_->left(), right_margin = cps_super_bbox_->right() - part_box.right(); @@ -879,8 +878,7 @@ void EquationDetect::IdentifyInlinePartsVertical(const bool top_to_bottom, } std::vector new_seeds; - for (int i = 0; i < cp_seeds_.size(); ++i) { - ColPartition *part = cp_seeds_[i]; + for (auto part : cp_seeds_) { // If we sort cp_seeds_ from top to bottom, then for each cp_seeds_, we look // for its top neighbors, so that if two/more inline regions are connected // to each other, then we will identify the top one, and then use it to @@ -1057,14 +1055,13 @@ bool EquationDetect::ExpandSeed(ColPartition *seed) { // from part_grid_ as its bounding box is going to expand. Then we add it // back after it absorbs all parts_to_merge partitions. part_grid_->RemoveBBox(seed); - for (int i = 0; i < parts_to_merge.size(); ++i) { - ColPartition *part = parts_to_merge[i]; + for (auto part : parts_to_merge) { if (part->type() == PT_EQUATION) { // If part is in cp_seeds_, then we mark it as nullptr so that we won't // process it again. - for (int j = 0; j < cp_seeds_.size(); ++j) { - if (part == cp_seeds_[j]) { - cp_seeds_[j] = nullptr; + for (auto &cp_seed : cp_seeds_) { + if (part == cp_seed) { + cp_seed = nullptr; break; } } @@ -1199,15 +1196,15 @@ void EquationDetect::ExpandSeedVertical(const bool search_bottom, ColPartition * // seed: ****************** | part: ********** // skipped: xxx | skipped: xxx // part: ********** | seed: *********** - for (int i = 0; i < parts.size(); i++) { - const TBOX &part_box(parts[i]->bounding_box()); + for (auto &part : parts) { + const TBOX &part_box(part->bounding_box()); if ((search_bottom && part_box.top() <= skipped_max_bottom) || (!search_bottom && part_box.bottom() >= skipped_min_top)) { continue; } // Add parts[i] into parts_to_merge, and delete it from part_grid_. - parts_to_merge->push_back(parts[i]); - part_grid_->RemoveBBox(parts[i]); + parts_to_merge->push_back(part); + part_grid_->RemoveBBox(part); } } @@ -1272,24 +1269,24 @@ void EquationDetect::ProcessMathBlockSatelliteParts() { } // Iterate every text_parts and check if it is a math block satellite. - for (int i = 0; i < text_parts.size(); ++i) { - const TBOX &text_box(text_parts[i]->bounding_box()); + for (auto &text_part : text_parts) { + const TBOX &text_box(text_part->bounding_box()); if (text_box.height() > med_height) { continue; } std::vector math_blocks; - if (!IsMathBlockSatellite(text_parts[i], &math_blocks)) { + if (!IsMathBlockSatellite(text_part, &math_blocks)) { continue; } // Found. merge text_parts[i] with math_blocks. - part_grid_->RemoveBBox(text_parts[i]); - text_parts[i]->set_type(PT_EQUATION); - for (int j = 0; j < math_blocks.size(); ++j) { - part_grid_->RemoveBBox(math_blocks[j]); - text_parts[i]->Absorb(math_blocks[j], nullptr); + part_grid_->RemoveBBox(text_part); + text_part->set_type(PT_EQUATION); + for (auto &math_block : math_blocks) { + part_grid_->RemoveBBox(math_block); + text_part->Absorb(math_block, nullptr); } - InsertPartAfterAbsorb(text_parts[i]); + InsertPartAfterAbsorb(text_part); } } diff --git a/src/ccmain/par_control.cpp b/src/ccmain/par_control.cpp index 23a76a03..1dcd23e4 100644 --- a/src/ccmain/par_control.cpp +++ b/src/ccmain/par_control.cpp @@ -38,11 +38,11 @@ struct BlobData { void Tesseract::PrerecAllWordsPar(const std::vector &words) { // Prepare all the blobs. std::vector blobs; - for (size_t w = 0; w < words.size(); ++w) { - if (words[w].word->ratings != nullptr && words[w].word->ratings->get(0, 0) == nullptr) { - for (int s = 0; s < words[w].lang_words.size(); ++s) { + for (const auto &w : words) { + if (w.word->ratings != nullptr && w.word->ratings->get(0, 0) == nullptr) { + for (int s = 0; s < w.lang_words.size(); ++s) { Tesseract *sub = s < sub_langs_.size() ? sub_langs_[s] : this; - const WERD_RES &word = *words[w].lang_words[s]; + const WERD_RES &word = *w.lang_words[s]; for (int b = 0; b < word.chopped_word->NumBlobs(); ++b) { blobs.push_back(BlobData(b, sub, word)); } @@ -54,15 +54,13 @@ void Tesseract::PrerecAllWordsPar(const std::vector &words) { #ifdef _OPENMP # pragma omp parallel for num_threads(10) #endif // _OPENMP - for (size_t b = 0; b < blobs.size(); ++b) { - *blobs[b].choices = - blobs[b].tesseract->classify_blob(blobs[b].blob, "par", ScrollView::WHITE, nullptr); + for (auto &blob : blobs) { + *blob.choices = blob.tesseract->classify_blob(blob.blob, "par", ScrollView::WHITE, nullptr); } } else { // TODO(AMD) parallelize this. - for (size_t b = 0; b < blobs.size(); ++b) { - *blobs[b].choices = - blobs[b].tesseract->classify_blob(blobs[b].blob, "par", ScrollView::WHITE, nullptr); + for (auto &blob : blobs) { + *blob.choices = blob.tesseract->classify_blob(blob.blob, "par", ScrollView::WHITE, nullptr); } } } diff --git a/src/ccmain/paragraphs.cpp b/src/ccmain/paragraphs.cpp index 9293faa0..3772498f 100644 --- a/src/ccmain/paragraphs.cpp +++ b/src/ccmain/paragraphs.cpp @@ -97,8 +97,8 @@ static void PrintTable(const std::vector> &rows, const int num_columns = row.size(); for (int c = 0; c < num_columns; c++) { int num_unicodes = 0; - for (int i = 0; i < row[c].size(); i++) { - if ((row[c][i] & 0xC0) != 0x80) + for (char i : row[c]) { + if ((i & 0xC0) != 0x80) num_unicodes++; } if (c >= max_col_widths.size()) { @@ -111,15 +111,15 @@ static void PrintTable(const std::vector> &rows, const } std::vector col_width_patterns; - for (int c = 0; c < max_col_widths.size(); c++) { - col_width_patterns.push_back(std::string("%-") + std::to_string(max_col_widths[c]) + "s"); + for (int max_col_width : max_col_widths) { + col_width_patterns.push_back(std::string("%-") + std::to_string(max_col_width) + "s"); } - for (int r = 0; r < rows.size(); r++) { - for (int c = 0; c < rows[r].size(); c++) { + for (const auto &row : rows) { + for (int c = 0; c < row.size(); c++) { if (c > 0) tprintf("%s", colsep); - tprintf(col_width_patterns[c].c_str(), rows[r][c].c_str()); + tprintf(col_width_patterns[c].c_str(), row[c].c_str()); } tprintf("\n"); } @@ -505,16 +505,16 @@ void RowScratchRegisters::AppendDebugInfo(const ParagraphTheory &theory, model_string += ":"; int model_numbers = 0; - for (int h = 0; h < hypotheses_.size(); h++) { - if (hypotheses_[h].model == nullptr) + for (const auto &hypothese : hypotheses_) { + if (hypothese.model == nullptr) continue; if (model_numbers > 0) model_string += ","; - if (StrongModel(hypotheses_[h].model)) { - model_string += std::to_string(1 + theory.IndexOf(hypotheses_[h].model)); - } else if (hypotheses_[h].model == kCrownLeft) { + if (StrongModel(hypothese.model)) { + model_string += std::to_string(1 + theory.IndexOf(hypothese.model)); + } else if (hypothese.model == kCrownLeft) { model_string += "CrL"; - } else if (hypotheses_[h].model == kCrownRight) { + } else if (hypothese.model == kCrownRight) { model_string += "CrR"; } model_numbers++; @@ -538,8 +538,8 @@ LineType RowScratchRegisters::GetLineType() const { return LT_UNKNOWN; bool has_start = false; bool has_body = false; - for (int i = 0; i < hypotheses_.size(); i++) { - switch (hypotheses_[i].ty) { + for (const auto &hypothese : hypotheses_) { + switch (hypothese.ty) { case LT_START: has_start = true; break; @@ -547,7 +547,7 @@ LineType RowScratchRegisters::GetLineType() const { has_body = true; break; default: - tprintf("Encountered bad value in hypothesis list: %c\n", hypotheses_[i].ty); + tprintf("Encountered bad value in hypothesis list: %c\n", hypothese.ty); break; } } @@ -561,10 +561,10 @@ LineType RowScratchRegisters::GetLineType(const ParagraphModel *model) const { return LT_UNKNOWN; bool has_start = false; bool has_body = false; - for (int i = 0; i < hypotheses_.size(); i++) { - if (hypotheses_[i].model != model) + for (const auto &hypothese : hypotheses_) { + if (hypothese.model != model) continue; - switch (hypotheses_[i].ty) { + switch (hypothese.ty) { case LT_START: has_start = true; break; @@ -572,7 +572,7 @@ LineType RowScratchRegisters::GetLineType(const ParagraphModel *model) const { has_body = true; break; default: - tprintf("Encountered bad value in hypothesis list: %c\n", hypotheses_[i].ty); + tprintf("Encountered bad value in hypothesis list: %c\n", hypothese.ty); break; } } @@ -618,23 +618,23 @@ void RowScratchRegisters::AddBodyLine(const ParagraphModel *model) { } void RowScratchRegisters::StartHypotheses(SetOfModels *models) const { - for (int h = 0; h < hypotheses_.size(); h++) { - if (hypotheses_[h].ty == LT_START && StrongModel(hypotheses_[h].model)) - push_back_new(*models, hypotheses_[h].model); + for (const auto &hypothese : hypotheses_) { + if (hypothese.ty == LT_START && StrongModel(hypothese.model)) + push_back_new(*models, hypothese.model); } } void RowScratchRegisters::StrongHypotheses(SetOfModels *models) const { - for (int h = 0; h < hypotheses_.size(); h++) { - if (StrongModel(hypotheses_[h].model)) - push_back_new(*models, hypotheses_[h].model); + for (const auto &hypothese : hypotheses_) { + if (StrongModel(hypothese.model)) + push_back_new(*models, hypothese.model); } } void RowScratchRegisters::NonNullHypotheses(SetOfModels *models) const { - for (int h = 0; h < hypotheses_.size(); h++) { - if (hypotheses_[h].model != nullptr) - push_back_new(*models, hypotheses_[h].model); + for (const auto &hypothese : hypotheses_) { + if (hypothese.model != nullptr) + push_back_new(*models, hypothese.model); } } @@ -1344,12 +1344,12 @@ void ParagraphModelSmearer::CalculateOpenModels(int row_start, int row_end) { // Which models survive the transition from row to row + 1? SetOfModels still_open; - for (int m = 0; m < opened.size(); m++) { - if (ValidFirstLine(rows_, row, opened[m]) || ValidBodyLine(rows_, row, opened[m])) { + for (auto &m : opened) { + if (ValidFirstLine(rows_, row, m) || ValidBodyLine(rows_, row, m)) { // This is basic filtering; we check likely paragraph starty-ness down // below in Smear() -- you know, whether the first word would have fit // and such. - push_back_new(still_open, opened[m]); + push_back_new(still_open, m); } } OpenModels(row + 1) = still_open; @@ -1375,8 +1375,8 @@ void ParagraphModelSmearer::Smear() { // "first" word in a row would fit at the "end" of the previous row. bool left_align_open = false; bool right_align_open = false; - for (int m = 0; m < OpenModels(i).size(); m++) { - switch (OpenModels(i)[m]->justification()) { + for (auto &m : OpenModels(i)) { + switch (m->justification()) { case JUSTIFICATION_LEFT: left_align_open = true; break; @@ -1423,8 +1423,7 @@ void ParagraphModelSmearer::Smear() { } else { theory_->NonCenteredModels(&last_line_models); } - for (int m = 0; m < last_line_models.size(); m++) { - const ParagraphModel *model = last_line_models[m]; + for (auto model : last_line_models) { if (ValidBodyLine(rows_, i, model)) row.AddBodyLine(model); } @@ -1438,9 +1437,9 @@ void ParagraphModelSmearer::Smear() { (row.GetLineType() == LT_START && !row.UniqueStartHypothesis())) { SetOfModels all_models; theory_->NonCenteredModels(&all_models); - for (int m = 0; m < all_models.size(); m++) { - if (ValidFirstLine(rows_, i, all_models[m])) { - row.AddStartLine(all_models[m]); + for (auto &all_model : all_models) { + if (ValidFirstLine(rows_, i, all_model)) { + row.AddStartLine(all_model); } } } @@ -1460,8 +1459,8 @@ void ParagraphModelSmearer::Smear() { static void DiscardUnusedModels(const std::vector &rows, ParagraphTheory *theory) { SetOfModels used_models; - for (int i = 0; i < rows.size(); i++) { - rows[i].StrongHypotheses(&used_models); + for (const auto &row : rows) { + row.StrongHypotheses(&used_models); } theory->DiscardUnusedModels(used_models); } @@ -2102,14 +2101,14 @@ static bool RowIsStranded(const std::vector &rows, int row) SetOfModels row_models; rows[row].StrongHypotheses(&row_models); - for (int m = 0; m < row_models.size(); m++) { + for (auto &row_model : row_models) { bool all_starts = rows[row].GetLineType(); int run_length = 1; bool continues = true; for (int i = row - 1; i >= 0 && continues; i--) { SetOfModels models; rows[i].NonNullHypotheses(&models); - switch (rows[i].GetLineType(row_models[m])) { + switch (rows[i].GetLineType(row_model)) { case LT_START: run_length++; break; @@ -2127,7 +2126,7 @@ static bool RowIsStranded(const std::vector &rows, int row) for (int i = row + 1; i < rows.size() && continues; i++) { SetOfModels models; rows[i].NonNullHypotheses(&models); - switch (rows[i].GetLineType(row_models[m])) { + switch (rows[i].GetLineType(row_model)) { case LT_START: run_length++; break; @@ -2195,8 +2194,8 @@ static void LeftoverSegments(const std::vector &rows, } } // Convert inclusive intervals to half-open intervals. - for (int i = 0; i < to_fix->size(); i++) { - (*to_fix)[i].end = (*to_fix)[i].end + 1; + for (auto &i : *to_fix) { + i.end = i.end + 1; } } diff --git a/src/ccmain/pgedit.cpp b/src/ccmain/pgedit.cpp index 347e1983..27e3c8f3 100644 --- a/src/ccmain/pgedit.cpp +++ b/src/ccmain/pgedit.cpp @@ -912,14 +912,14 @@ void Tesseract::blob_feature_display(PAGE_RES *page_res, const TBOX &selection_b // Display baseline features. ScrollView *bl_win = CreateFeatureSpaceWindow("BL Features", 512, 0); ClearFeatureSpaceWindow(baseline, bl_win); - for (int f = 0; f < bl_features.size(); ++f) - RenderIntFeature(bl_win, &bl_features[f], ScrollView::GREEN); + for (auto &bl_feature : bl_features) + RenderIntFeature(bl_win, &bl_feature, ScrollView::GREEN); bl_win->Update(); // Display cn features. ScrollView *cn_win = CreateFeatureSpaceWindow("CN Features", 512, 0); ClearFeatureSpaceWindow(character, cn_win); - for (int f = 0; f < cn_features.size(); ++f) - RenderIntFeature(cn_win, &cn_features[f], ScrollView::GREEN); + for (auto &cn_feature : cn_features) + RenderIntFeature(cn_win, &cn_feature, ScrollView::GREEN); cn_win->Update(); it->DeleteCurrentWord(); diff --git a/src/ccmain/resultiterator.cpp b/src/ccmain/resultiterator.cpp index 485c3016..d8bb805f 100644 --- a/src/ccmain/resultiterator.cpp +++ b/src/ccmain/resultiterator.cpp @@ -221,8 +221,8 @@ void ResultIterator::CalculateBlobOrder(std::vector *blob_indices) const { } static void PrintScriptDirs(const std::vector &dirs) { - for (int i = 0; i < dirs.size(); i++) { - switch (dirs[i]) { + for (auto dir : dirs) { + switch (dir) { case DIR_NEUTRAL: tprintf("N "); break; @@ -669,8 +669,8 @@ void ResultIterator::AppendUTF8WordText(std::string *text) const { std::vector blob_order; CalculateBlobOrder(&blob_order); - for (int i = 0; i < blob_order.size(); i++) { - *text += it_->word()->BestUTF8(blob_order[i], false); + for (int i : blob_order) { + *text += it_->word()->BestUTF8(i, false); } AppendSuffixMarks(text); } @@ -689,8 +689,8 @@ void ResultIterator::IterateAndAppendUTF8TextlineText(std::string *text) { PrintScriptDirs(dirs); tprintf("Logical textline order [%p/P=%s]: ", it_->row(), current_paragraph_is_ltr_ ? "ltr" : "rtl"); - for (int i = 0; i < textline_order.size(); i++) { - tprintf("%d ", textline_order[i]); + for (int i : textline_order) { + tprintf("%d ", i); } tprintf("\n"); } diff --git a/src/ccmain/tessedit.cpp b/src/ccmain/tessedit.cpp index 1d2f1766..ae08bd60 100644 --- a/src/ccmain/tessedit.cpp +++ b/src/ccmain/tessedit.cpp @@ -230,8 +230,8 @@ bool Tesseract::init_tesseract_lang_data(const std::string &arg0, const std::str // Helper returns true if the given string is in the vector of strings. static bool IsStrInList(const std::string &str, const std::vector &str_list) { - for (int i = 0; i < str_list.size(); ++i) { - if (str_list[i] == str) + for (const auto &i : str_list) { + if (i == str) return true; } return false; @@ -341,15 +341,14 @@ int Tesseract::init_tesseract(const std::string &arg0, const std::string &textba // tessedit_use_primary_params_model is set, // otherwise use default language model weights. if (tessedit_use_primary_params_model) { - for (int s = 0; s < sub_langs_.size(); ++s) { - sub_langs_[s]->language_model_->getParamsModel().Copy( - this->language_model_->getParamsModel()); + for (auto &sub_lang : sub_langs_) { + sub_lang->language_model_->getParamsModel().Copy(this->language_model_->getParamsModel()); } tprintf("Using params model of the primary language\n"); } else { this->language_model_->getParamsModel().Clear(); - for (int s = 0; s < sub_langs_.size(); ++s) { - sub_langs_[s]->language_model_->getParamsModel().Clear(); + for (auto &sub_lang : sub_langs_) { + sub_lang->language_model_->getParamsModel().Clear(); } } } @@ -424,13 +423,13 @@ void Tesseract::SetupUniversalFontIds() { // Create the universal ID table. CollectFonts(get_fontinfo_table(), &all_fonts); - for (int i = 0; i < sub_langs_.size(); ++i) { - CollectFonts(sub_langs_[i]->get_fontinfo_table(), &all_fonts); + for (auto &sub_lang : sub_langs_) { + CollectFonts(sub_lang->get_fontinfo_table(), &all_fonts); } // Assign ids from the table to each font table. AssignIds(all_fonts, &get_fontinfo_table()); - for (int i = 0; i < sub_langs_.size(); ++i) { - AssignIds(all_fonts, &sub_langs_[i]->get_fontinfo_table()); + for (auto &sub_lang : sub_langs_) { + AssignIds(all_fonts, &sub_lang->get_fontinfo_table()); } font_table_size_ = all_fonts.size(); } diff --git a/src/ccmain/tesseractclass.cpp b/src/ccmain/tesseractclass.cpp index db8f139e..b9cd80bb 100644 --- a/src/ccmain/tesseractclass.cpp +++ b/src/ccmain/tesseractclass.cpp @@ -450,8 +450,8 @@ void Tesseract::Clear() { reskew_ = FCOORD(1.0f, 0.0f); splitter_.Clear(); scaled_factor_ = -1; - for (int i = 0; i < sub_langs_.size(); ++i) - sub_langs_[i]->Clear(); + for (auto &sub_lang : sub_langs_) + sub_lang->Clear(); } #ifndef DISABLED_LEGACY_ENGINE @@ -464,8 +464,8 @@ void Tesseract::SetEquationDetect(EquationDetect *detector) { // Clear all memory of adaption for this and all subclassifiers. void Tesseract::ResetAdaptiveClassifier() { ResetAdaptiveClassifierInternal(); - for (int i = 0; i < sub_langs_.size(); ++i) { - sub_langs_[i]->ResetAdaptiveClassifierInternal(); + for (auto &sub_lang : sub_langs_) { + sub_lang->ResetAdaptiveClassifierInternal(); } } @@ -474,8 +474,8 @@ void Tesseract::ResetAdaptiveClassifier() { // Clear the document dictionary for this and all subclassifiers. void Tesseract::ResetDocumentDictionary() { getDict().ResetDocumentDictionary(); - for (int i = 0; i < sub_langs_.size(); ++i) { - sub_langs_[i]->getDict().ResetDocumentDictionary(); + for (auto &sub_lang : sub_langs_) { + sub_lang->getDict().ResetDocumentDictionary(); } } @@ -491,12 +491,12 @@ void Tesseract::SetBlackAndWhitelist() { tessedit_char_unblacklist.c_str()); } // Black and white lists should apply to all loaded classifiers. - for (int i = 0; i < sub_langs_.size(); ++i) { - sub_langs_[i]->unicharset.set_black_and_whitelist(tessedit_char_blacklist.c_str(), - tessedit_char_whitelist.c_str(), - tessedit_char_unblacklist.c_str()); - if (sub_langs_[i]->lstm_recognizer_) { - UNICHARSET &lstm_unicharset = sub_langs_[i]->lstm_recognizer_->GetUnicharset(); + for (auto &sub_lang : sub_langs_) { + sub_lang->unicharset.set_black_and_whitelist(tessedit_char_blacklist.c_str(), + tessedit_char_whitelist.c_str(), + tessedit_char_unblacklist.c_str()); + if (sub_lang->lstm_recognizer_) { + UNICHARSET &lstm_unicharset = sub_lang->lstm_recognizer_->GetUnicharset(); lstm_unicharset.set_black_and_whitelist(tessedit_char_blacklist.c_str(), tessedit_char_whitelist.c_str(), tessedit_char_unblacklist.c_str()); @@ -511,13 +511,13 @@ void Tesseract::PrepareForPageseg() { // Find the max splitter strategy over all langs. auto max_pageseg_strategy = static_cast( static_cast(pageseg_devanagari_split_strategy)); - for (int i = 0; i < sub_langs_.size(); ++i) { + for (auto &sub_lang : sub_langs_) { auto pageseg_strategy = static_cast( - static_cast(sub_langs_[i]->pageseg_devanagari_split_strategy)); + static_cast(sub_lang->pageseg_devanagari_split_strategy)); if (pageseg_strategy > max_pageseg_strategy) max_pageseg_strategy = pageseg_strategy; - pixDestroy(&sub_langs_[i]->pix_binary_); - sub_langs_[i]->pix_binary_ = pixClone(pix_binary()); + pixDestroy(&sub_lang->pix_binary_); + sub_lang->pix_binary_ = pixClone(pix_binary()); } // Perform shiro-rekha (top-line) splitting and replace the current image by // the newly split image. @@ -539,9 +539,9 @@ void Tesseract::PrepareForTessOCR(BLOCK_LIST *block_list, Tesseract *osd_tess, O // Find the max splitter strategy over all langs. auto max_ocr_strategy = static_cast( static_cast(ocr_devanagari_split_strategy)); - for (int i = 0; i < sub_langs_.size(); ++i) { + for (auto &sub_lang : sub_langs_) { auto ocr_strategy = static_cast( - static_cast(sub_langs_[i]->ocr_devanagari_split_strategy)); + static_cast(sub_lang->ocr_devanagari_split_strategy)); if (ocr_strategy > max_ocr_strategy) max_ocr_strategy = ocr_strategy; } diff --git a/src/ccstruct/blobs.cpp b/src/ccstruct/blobs.cpp index 536ea71c..bb348c11 100644 --- a/src/ccstruct/blobs.cpp +++ b/src/ccstruct/blobs.cpp @@ -784,8 +784,7 @@ void TWERD::BLNormalize(const BLOCK *block, const ROW *row, Pix *pix, bool inver } else { input_y_offset = row->base_line(word_middle) + baseline_shift; } - for (int b = 0; b < blobs.size(); ++b) { - TBLOB *blob = blobs[b]; + for (auto blob : blobs) { TBOX blob_box = blob->bounding_box(); float mid_x = (blob_box.left() + blob_box.right()) / 2.0f; float baseline = input_y_offset; @@ -817,8 +816,8 @@ void TWERD::BLNormalize(const BLOCK *block, const ROW *row, Pix *pix, bool inver void TWERD::CopyFrom(const TWERD &src) { Clear(); latin_script = src.latin_script; - for (int b = 0; b < src.blobs.size(); ++b) { - auto *new_blob = new TBLOB(*src.blobs[b]); + for (auto blob : src.blobs) { + auto *new_blob = new TBLOB(*blob); blobs.push_back(new_blob); } } @@ -833,15 +832,15 @@ void TWERD::Clear() { // Recomputes the bounding boxes of the blobs. void TWERD::ComputeBoundingBoxes() { - for (int b = 0; b < blobs.size(); ++b) { - blobs[b]->ComputeBoundingBoxes(); + for (auto &blob : blobs) { + blob->ComputeBoundingBoxes(); } } TBOX TWERD::bounding_box() const { TBOX result; - for (int b = 0; b < blobs.size(); ++b) { - TBOX box = blobs[b]->bounding_box(); + for (auto blob : blobs) { + TBOX box = blob->bounding_box(); result += box; } return result; @@ -879,8 +878,8 @@ void TWERD::MergeBlobs(int start, int end) { #ifndef GRAPHICS_DISABLED void TWERD::plot(ScrollView *window) { ScrollView::Color color = WERD::NextColor(ScrollView::BLACK); - for (int b = 0; b < blobs.size(); ++b) { - blobs[b]->plot(window, color, ScrollView::BROWN); + for (auto &blob : blobs) { + blob->plot(window, color, ScrollView::BROWN); color = WERD::NextColor(color); } } diff --git a/src/ccstruct/boxread.cpp b/src/ccstruct/boxread.cpp index a8483b0d..9044eca8 100644 --- a/src/ccstruct/boxread.cpp +++ b/src/ccstruct/boxread.cpp @@ -94,11 +94,11 @@ bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, bool if (lines.empty()) return false; int num_boxes = 0; - for (int i = 0; i < lines.size(); ++i) { + for (auto &line : lines) { int page = 0; std::string utf8_str; TBOX box; - if (!ParseBoxFileStr(lines[i].c_str(), &page, utf8_str, &box)) { + if (!ParseBoxFileStr(line.c_str(), &page, utf8_str, &box)) { if (continue_on_failure) continue; else diff --git a/src/ccstruct/detlinefit.cpp b/src/ccstruct/detlinefit.cpp index fec690a6..fc01f4b1 100644 --- a/src/ccstruct/detlinefit.cpp +++ b/src/ccstruct/detlinefit.cpp @@ -150,8 +150,8 @@ double DetLineFit::ConstrainedFit(const FCOORD &direction, double min_dist, doub } // Center distances on the fitted point. double dist_origin = direction * *line_pt; - for (int i = 0; i < distances_.size(); ++i) { - distances_[i].key() -= dist_origin; + for (auto &distance : distances_) { + distance.key() -= dist_origin; } return sqrt(EvaluateLineFit()); } @@ -282,12 +282,12 @@ void DetLineFit::ComputeConstrainedDistances(const FCOORD &direction, double min distances_.clear(); square_length_ = direction.sqlength(); // Compute the distance of each point from the line. - for (int i = 0; i < pts_.size(); ++i) { - FCOORD pt_vector = pts_[i].pt; + for (auto &pt : pts_) { + FCOORD pt_vector = pt.pt; // Compute |line_vector||pt_vector|sin(angle between) double dist = direction * pt_vector; if (min_dist <= dist && dist <= max_dist) - distances_.push_back(DistPointPair(dist, pts_[i].pt)); + distances_.push_back(DistPointPair(dist, pt.pt)); } } diff --git a/src/ccstruct/fontinfo.cpp b/src/ccstruct/fontinfo.cpp index 615d3376..a6a32557 100644 --- a/src/ccstruct/fontinfo.cpp +++ b/src/ccstruct/fontinfo.cpp @@ -63,8 +63,8 @@ bool FontInfoTable::DeSerialize(TFile *fp) { bool FontInfoTable::SetContainsFontProperties(int font_id, const std::vector &font_set) const { uint32_t properties = at(font_id).properties; - for (int f = 0; f < font_set.size(); ++f) { - if (at(font_set[f].fontinfo_id).properties == properties) + for (auto f : font_set) { + if (at(f.fontinfo_id).properties == properties) return true; } return false; diff --git a/src/ccstruct/imagedata.cpp b/src/ccstruct/imagedata.cpp index e28a4a82..58e48bb4 100644 --- a/src/ccstruct/imagedata.cpp +++ b/src/ccstruct/imagedata.cpp @@ -220,8 +220,7 @@ Pix *ImageData::PreScale(int target_height, int max_height, float *scale_factor, if (boxes != nullptr) { // Get the boxes. boxes->clear(); - for (int b = 0; b < boxes_.size(); ++b) { - TBOX box = boxes_[b]; + for (auto box : boxes_) { box.scale(im_factor); boxes->push_back(box); } @@ -572,8 +571,7 @@ bool DocumentCache::LoadDocuments(const std::vector &filenames, // determines which DocumentDatas are held entirely in memory. if (cache_strategy_ == CS_ROUND_ROBIN) fair_share_memory = max_memory_ / filenames.size(); - for (int arg = 0; arg < filenames.size(); ++arg) { - std::string filename = filenames[arg]; + for (auto filename : filenames) { auto *document = new DocumentData(filename); document->SetDocument(filename.c_str(), fair_share_memory, reader); AddToCache(document); diff --git a/src/ccstruct/pageres.cpp b/src/ccstruct/pageres.cpp index e357f500..e654bd9b 100644 --- a/src/ccstruct/pageres.cpp +++ b/src/ccstruct/pageres.cpp @@ -1215,8 +1215,7 @@ WERD_RES *PAGE_RES_IT::InsertSimpleCloneWord(const WERD_RES &clone_res, WERD *ne static void ComputeBlobEnds(const WERD_RES &word, const TBOX &clip_box, C_BLOB_LIST *next_word_blobs, std::vector *blob_ends) { C_BLOB_IT blob_it(word.word->cblob_list()); - for (int i = 0; i < word.best_state.size(); ++i) { - int length = word.best_state[i]; + for (int length : word.best_state) { // Get the bounding box of the fake blobs TBOX blob_box = blob_it.data()->bounding_box(); blob_it.forward(); diff --git a/src/ccutil/ambigs.cpp b/src/ccutil/ambigs.cpp index b76b6fa9..caa421e9 100644 --- a/src/ccutil/ambigs.cpp +++ b/src/ccutil/ambigs.cpp @@ -132,8 +132,7 @@ void UnicharAmbigs::LoadUnicharAmbigs(const UNICHARSET &encoder_set, TFile *ambi ambigs_for_adaption_[test_unichar_ids[i]] = new UnicharIdVector(); } adaption_ambigs_entry = ambigs_for_adaption_[test_unichar_ids[i]]; - for (int r = 0; r < encoding.size(); ++r) { - UNICHAR_ID id_to_insert = encoding[r]; + for (int id_to_insert : encoding) { ASSERT_HOST(id_to_insert != INVALID_UNICHAR_ID); // Add the new unichar id to adaption_ambigs_entry (only if the // vector does not already contain it) keeping it in sorted order. diff --git a/src/ccutil/indexmapbidi.cpp b/src/ccutil/indexmapbidi.cpp index 569afbd7..b0e20311 100644 --- a/src/ccutil/indexmapbidi.cpp +++ b/src/ccutil/indexmapbidi.cpp @@ -111,9 +111,9 @@ void IndexMapBiDi::SetMap(int sparse_index, bool mapped) { // in the forward map to the compact space. void IndexMapBiDi::Setup() { int compact_size = 0; - for (int i = 0; i < sparse_map_.size(); ++i) { - if (sparse_map_[i] >= 0) { - sparse_map_[i] = compact_size++; + for (int &i : sparse_map_) { + if (i >= 0) { + i = compact_size++; } } compact_map_.clear(); @@ -171,9 +171,9 @@ bool IndexMapBiDi::Merge(int compact_index1, int compact_index2) { void IndexMapBiDi::CompleteMerges() { // 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; + for (int &i : sparse_map_) { + int compact_index = MasterCompactIndex(i); + i = compact_index; if (compact_index >= compact_size) compact_size = compact_index + 1; } @@ -198,9 +198,9 @@ void IndexMapBiDi::CompleteMerges() { } compact_map_.resize(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]]; + for (int &i : sparse_map_) { + if (i >= 0) { + i = tmp_compact_map[i]; } } } diff --git a/src/ccutil/params.cpp b/src/ccutil/params.cpp index 13fc240c..e1191b2e 100644 --- a/src/ccutil/params.cpp +++ b/src/ccutil/params.cpp @@ -166,21 +166,21 @@ void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) { stream.imbue(std::locale::classic()); for (int v = 0; v < num_iterations; ++v) { const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params; - for (int i = 0; i < vec->int_params.size(); ++i) { - stream << vec->int_params[i]->name_str() << '\t' << (int32_t)(*vec->int_params[i]) << '\t' - << vec->int_params[i]->info_str() << '\n'; + for (auto int_param : vec->int_params) { + stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t' + << int_param->info_str() << '\n'; } - for (int i = 0; i < vec->bool_params.size(); ++i) { - stream << vec->bool_params[i]->name_str() << '\t' << bool(*vec->bool_params[i]) << '\t' - << vec->bool_params[i]->info_str() << '\n'; + for (auto bool_param : vec->bool_params) { + stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t' + << bool_param->info_str() << '\n'; } - for (int i = 0; i < vec->string_params.size(); ++i) { - stream << vec->string_params[i]->name_str() << '\t' << vec->string_params[i]->c_str() << '\t' - << vec->string_params[i]->info_str() << '\n'; + for (auto string_param : vec->string_params) { + stream << string_param->name_str() << '\t' << string_param->c_str() << '\t' + << string_param->info_str() << '\n'; } - for (int i = 0; i < vec->double_params.size(); ++i) { - stream << vec->double_params[i]->name_str() << '\t' << (double)(*vec->double_params[i]) - << '\t' << vec->double_params[i]->info_str() << '\n'; + for (auto double_param : vec->double_params) { + stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t' + << double_param->info_str() << '\n'; } } fprintf(fp, "%s", stream.str().c_str()); @@ -198,11 +198,11 @@ void ParamUtils::ResetToDefaults(ParamsVectors *member_params) { for (i = 0; i < vec->bool_params.size(); ++i) { vec->bool_params[i]->ResetToDefault(); } - for (int i = 0; i < vec->string_params.size(); ++i) { - vec->string_params[i]->ResetToDefault(); + for (auto &string_param : vec->string_params) { + string_param->ResetToDefault(); } - for (int i = 0; i < vec->double_params.size(); ++i) { - vec->double_params[i]->ResetToDefault(); + for (auto &double_param : vec->double_params) { + double_param->ResetToDefault(); } } } diff --git a/src/ccutil/unicharcompress.cpp b/src/ccutil/unicharcompress.cpp index 9c4a9509..60759753 100644 --- a/src/ccutil/unicharcompress.cpp +++ b/src/ccutil/unicharcompress.cpp @@ -247,8 +247,7 @@ void UnicharCompress::DefragmentCodeValues(int encoded_null) { ComputeCodeRange(); std::vector offsets(code_range_); // Find which codes are used - for (int c = 0; c < encoder_.size(); ++c) { - const RecodedCharID &code = encoder_[c]; + for (auto &code : encoder_) { for (int i = 0; i < code.length(); ++i) { offsets[code(i)] = 1; } @@ -270,8 +269,8 @@ void UnicharCompress::DefragmentCodeValues(int encoded_null) { offsets[encoded_null] = offsets.size() + offsets.back() - encoded_null; } // Now apply the offsets. - for (int c = 0; c < encoder_.size(); ++c) { - RecodedCharID *code = &encoder_[c]; + for (auto &c : encoder_) { + RecodedCharID *code = &c; for (int i = 0; i < code->length(); ++i) { int value = (*code)(i); code->Set(i, value + offsets[value]); @@ -366,8 +365,7 @@ bool UnicharCompress::DecomposeHangul(int unicode, int *leading, int *vowel, int // Computes the value of code_range_ from the encoder_. void UnicharCompress::ComputeCodeRange() { code_range_ = -1; - for (int c = 0; c < encoder_.size(); ++c) { - const RecodedCharID &code = encoder_[c]; + for (auto &code : encoder_) { for (int i = 0; i < code.length(); ++i) { if (code(i) > code_range_) code_range_ = code(i); diff --git a/src/classify/adaptmatch.cpp b/src/classify/adaptmatch.cpp index 8f952987..36fc0f9f 100644 --- a/src/classify/adaptmatch.cpp +++ b/src/classify/adaptmatch.cpp @@ -1056,8 +1056,8 @@ void Classify::MasterMatcher(INT_TEMPLATES templates, int16_t num_features, int top = blob_box.top(); int bottom = blob_box.bottom(); UnicharRating int_result; - for (int c = 0; c < results.size(); c++) { - CLASS_ID class_id = results[c].Class; + for (auto result : results) { + CLASS_ID class_id = result.Class; BIT_VECTOR protos = classes != nullptr ? classes[class_id]->PermProtos : AllProtosOn; BIT_VECTOR configs = classes != nullptr ? classes[class_id]->PermConfigs : AllConfigsOn; @@ -1065,7 +1065,7 @@ void Classify::MasterMatcher(INT_TEMPLATES templates, int16_t num_features, im_.Match(ClassForClassId(templates, class_id), protos, configs, num_features, features, &int_result, classify_adapt_feature_threshold, debug, matcher_debug_separate_windows); bool is_debug = matcher_debug_level >= 2 || classify_debug_level > 1; - ExpandShapesAndApplyCorrections(classes, is_debug, class_id, bottom, top, results[c].Rating, + ExpandShapesAndApplyCorrections(classes, is_debug, class_id, bottom, top, result.Rating, final_results->BlobLength, matcher_multiplier, norm_factors, &int_result, final_results); } @@ -1084,16 +1084,14 @@ void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS *classes, bool debug, if (classes != nullptr) { // Adapted result. Convert configs to fontinfo_ids. int_result->adapted = true; - for (int f = 0; f < int_result->fonts.size(); ++f) { - int_result->fonts[f].fontinfo_id = - GetFontinfoId(classes[class_id], int_result->fonts[f].fontinfo_id); + for (auto &font : int_result->fonts) { + font.fontinfo_id = GetFontinfoId(classes[class_id], font.fontinfo_id); } } else { // Pre-trained result. Map fonts using font_sets_. int_result->adapted = false; - for (int f = 0; f < int_result->fonts.size(); ++f) { - int_result->fonts[f].fontinfo_id = - ClassAndConfigIDToFontOrShapeID(class_id, int_result->fonts[f].fontinfo_id); + for (auto &font : int_result->fonts) { + font.fontinfo_id = ClassAndConfigIDToFontOrShapeID(class_id, font.fontinfo_id); } if (shape_table_ != nullptr) { // Two possible cases: @@ -1121,9 +1119,8 @@ void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS *classes, bool debug, mapped_results[r].unichar_id = unichar_id; mapped_results[r].fonts.clear(); } - for (int i = 0; i < shape[c].font_ids.size(); ++i) { - mapped_results[r].fonts.push_back( - ScoredFont(shape[c].font_ids[i], int_result->fonts[f].score)); + for (int font_id : shape[c].font_ids) { + mapped_results[r].fonts.push_back(ScoredFont(font_id, int_result->fonts[f].score)); } } } @@ -1298,8 +1295,8 @@ int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const Trai matcher_debug_flags, classify_integer_matcher_multiplier, blob_box, adapt_results->CPResults, adapt_results); // Convert master matcher results to output format. - for (unsigned i = 0; i < adapt_results->match.size(); i++) { - results->push_back(adapt_results->match[i]); + for (auto &i : adapt_results->match) { + results->push_back(i); } if (results->size() > 1) { std::sort(results->begin(), results->end(), SortDescendingRating); diff --git a/src/classify/shapeclassifier.cpp b/src/classify/shapeclassifier.cpp index 553f7248..86b0c1a2 100644 --- a/src/classify/shapeclassifier.cpp +++ b/src/classify/shapeclassifier.cpp @@ -169,13 +169,13 @@ int ShapeClassifier::DisplayClassifyAs(const TrainingSample &sample, Pix *page_p void ShapeClassifier::UnicharPrintResults(const char *context, const std::vector &results) const { tprintf("%s\n", context); - for (int i = 0; i < results.size(); ++i) { - tprintf("%g: c_id=%d=%s", results[i].rating, results[i].unichar_id, - GetUnicharset().id_to_unichar(results[i].unichar_id)); - if (!results[i].fonts.empty()) { + for (const auto &result : results) { + tprintf("%g: c_id=%d=%s", result.rating, result.unichar_id, + GetUnicharset().id_to_unichar(result.unichar_id)); + if (!result.fonts.empty()) { tprintf(" Font Vector:"); - for (int f = 0; f < results[i].fonts.size(); ++f) { - tprintf(" %d", results[i].fonts[f].fontinfo_id); + for (int f = 0; f < result.fonts.size(); ++f) { + tprintf(" %d", result.fonts[f].fontinfo_id); } } tprintf("\n"); @@ -184,13 +184,13 @@ void ShapeClassifier::UnicharPrintResults(const char *context, void ShapeClassifier::PrintResults(const char *context, const std::vector &results) const { tprintf("%s\n", context); - for (int i = 0; i < results.size(); ++i) { - tprintf("%g:", results[i].rating); - if (results[i].joined) + for (const auto &result : results) { + tprintf("%g:", result.rating); + if (result.joined) tprintf("[J]"); - if (results[i].broken) + if (result.broken) tprintf("[B]"); - tprintf(" %s\n", GetShapeTable()->DebugStr(results[i].shape_id).c_str()); + tprintf(" %s\n", GetShapeTable()->DebugStr(result.shape_id).c_str()); } } diff --git a/src/classify/shapetable.cpp b/src/classify/shapetable.cpp index 7a189011..ed8566a1 100644 --- a/src/classify/shapetable.cpp +++ b/src/classify/shapetable.cpp @@ -99,12 +99,12 @@ bool Shape::DeSerialize(TFile *fp) { // Adds a font_id for the given unichar_id. If the unichar_id is not // in the shape, it is added. void Shape::AddToShape(int unichar_id, int font_id) { - for (int c = 0; c < unichars_.size(); ++c) { - if (unichars_[c].unichar_id == unichar_id) { + for (auto &unichar : unichars_) { + if (unichar.unichar_id == unichar_id) { // Found the unichar in the shape table. - std::vector &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (font_list[f] == font_id) + std::vector &font_list = unichar.font_ids; + for (int f : font_list) { + if (f == font_id) return; // Font is already there. } font_list.push_back(font_id); @@ -118,9 +118,9 @@ void Shape::AddToShape(int unichar_id, int font_id) { // Adds everything in other to this. void Shape::AddShape(const Shape &other) { - for (int c = 0; c < other.unichars_.size(); ++c) { - for (int f = 0; f < other.unichars_[c].font_ids.size(); ++f) { - AddToShape(other.unichars_[c].unichar_id, other.unichars_[c].font_ids[f]); + for (const auto &unichar : other.unichars_) { + for (int f = 0; f < unichar.font_ids.size(); ++f) { + AddToShape(unichar.unichar_id, unichar.font_ids[f]); } } unichars_sorted_ = unichars_.size() <= 1; @@ -128,12 +128,12 @@ void Shape::AddShape(const Shape &other) { // Returns true if the shape contains the given unichar_id, font_id pair. bool Shape::ContainsUnicharAndFont(int unichar_id, int font_id) const { - for (int c = 0; c < unichars_.size(); ++c) { - if (unichars_[c].unichar_id == unichar_id) { + for (const auto &unichar : unichars_) { + if (unichar.unichar_id == unichar_id) { // Found the unichar, so look for the font. - auto &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (font_list[f] == font_id) + auto &font_list = unichar.font_ids; + for (int f : font_list) { + if (f == font_id) return true; } return false; @@ -144,8 +144,8 @@ bool Shape::ContainsUnicharAndFont(int unichar_id, int font_id) const { // Returns true if the shape contains the given unichar_id, ignoring font. bool Shape::ContainsUnichar(int unichar_id) const { - for (int c = 0; c < unichars_.size(); ++c) { - if (unichars_[c].unichar_id == unichar_id) { + for (const auto &unichar : unichars_) { + if (unichar.unichar_id == unichar_id) { return true; } } @@ -154,10 +154,10 @@ bool Shape::ContainsUnichar(int unichar_id) const { // Returns true if the shape contains the given font, ignoring unichar_id. bool Shape::ContainsFont(int font_id) const { - for (int c = 0; c < unichars_.size(); ++c) { - auto &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (font_list[f] == font_id) + for (const auto &unichar : unichars_) { + auto &font_list = unichar.font_ids; + for (int f : font_list) { + if (f == font_id) return true; } } @@ -166,10 +166,10 @@ bool Shape::ContainsFont(int font_id) const { // Returns true if the shape contains the given font properties, ignoring // unichar_id. bool Shape::ContainsFontProperties(const FontInfoTable &font_table, uint32_t properties) const { - for (int c = 0; c < unichars_.size(); ++c) { - auto &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (font_table.at(font_list[f]).properties == properties) + for (const auto &unichar : unichars_) { + auto &font_list = unichar.font_ids; + for (int f : font_list) { + if (font_table.at(f).properties == properties) return true; } } @@ -179,10 +179,10 @@ bool Shape::ContainsFontProperties(const FontInfoTable &font_table, uint32_t pro // ignoring unichar_id. bool Shape::ContainsMultipleFontProperties(const FontInfoTable &font_table) const { uint32_t properties = font_table.at(unichars_[0].font_ids[0]).properties; - for (int c = 0; c < unichars_.size(); ++c) { - auto &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (font_table.at(font_list[f]).properties != properties) + for (const auto &unichar : unichars_) { + auto &font_list = unichar.font_ids; + for (int f : font_list) { + if (font_table.at(f).properties != properties) return true; } } @@ -197,11 +197,11 @@ bool Shape::operator==(const Shape &other) const { // Returns true if this is a subset (including equal) of other. bool Shape::IsSubsetOf(const Shape &other) const { - for (int c = 0; c < unichars_.size(); ++c) { - int unichar_id = unichars_[c].unichar_id; - const std::vector &font_list = unichars_[c].font_ids; - for (int f = 0; f < font_list.size(); ++f) { - if (!other.ContainsUnicharAndFont(unichar_id, font_list[f])) + for (const auto &unichar : unichars_) { + int unichar_id = unichar.unichar_id; + const std::vector &font_list = unichar.font_ids; + for (int f : font_list) { + if (!other.ContainsUnicharAndFont(unichar_id, f)) return false; } } @@ -251,12 +251,12 @@ bool ShapeTable::DeSerialize(TFile *fp) { // necessary. int ShapeTable::NumFonts() const { if (num_fonts_ <= 0) { - for (int shape_id = 0; shape_id < shape_table_.size(); ++shape_id) { - const Shape &shape = *shape_table_[shape_id]; + for (auto shape_id : shape_table_) { + const Shape &shape = *shape_id; for (int c = 0; c < shape.size(); ++c) { - for (int f = 0; f < shape[c].font_ids.size(); ++f) { - if (shape[c].font_ids[f] >= num_fonts_) - num_fonts_ = shape[c].font_ids[f] + 1; + for (int font_id : shape[c].font_ids) { + if (font_id >= num_fonts_) + num_fonts_ = font_id + 1; } } } @@ -267,8 +267,7 @@ int ShapeTable::NumFonts() const { // Re-indexes the class_ids in the shapetable according to the given map. // Useful in conjunction with set_unicharset. void ShapeTable::ReMapClassIds(const std::vector &unicharset_map) { - for (int shape_id = 0; shape_id < shape_table_.size(); ++shape_id) { - Shape *shape = shape_table_[shape_id]; + for (auto shape : shape_table_) { for (int c = 0; c < shape->size(); ++c) { shape->SetUnicharId(c, unicharset_map[(*shape)[c].unichar_id]); } @@ -386,8 +385,8 @@ int ShapeTable::FindShape(int unichar_id, int font_id) const { if (shape[c].unichar_id == unichar_id) { if (font_id < 0) return s; // We don't care about the font. - for (int f = 0; f < shape[c].font_ids.size(); ++f) { - if (shape[c].font_ids[f] == font_id) + for (int f : shape[c].font_ids) { + if (f == font_id) return s; } } @@ -633,8 +632,8 @@ bool ShapeTable::CommonFont(int shape_id1, int shape_id2) const { const Shape &shape2 = GetShape(shape_id2); for (int c1 = 0; c1 < shape1.size(); ++c1) { const std::vector &font_list1 = shape1[c1].font_ids; - for (int f = 0; f < font_list1.size(); ++f) { - if (shape2.ContainsFont(font_list1[f])) + for (int f : font_list1) { + if (shape2.ContainsFont(f)) return true; } } @@ -658,8 +657,8 @@ void ShapeTable::AppendMasterShapes(const ShapeTable &other, std::vector *s // Returns the number of master shapes remaining after merging. int ShapeTable::NumMasterShapes() const { int num_shapes = 0; - for (int s = 0; s < shape_table_.size(); ++s) { - if (shape_table_[s]->destination_index() < 0) + for (auto s : shape_table_) { + if (s->destination_index() < 0) ++num_shapes; } return num_shapes; @@ -684,9 +683,9 @@ void ShapeTable::AddShapeToResults(const ShapeRating &shape_rating, std::vector< for (int u = 0; u < shape.size(); ++u) { int result_index = AddUnicharToResults(shape[u].unichar_id, shape_rating.rating, unichar_map, results); - for (int f = 0; f < shape[u].font_ids.size(); ++f) { + for (int font_id : shape[u].font_ids) { (*results)[result_index].fonts.push_back( - ScoredFont(shape[u].font_ids[f], IntCastRounded(shape_rating.rating * INT16_MAX))); + ScoredFont(font_id, IntCastRounded(shape_rating.rating * INT16_MAX))); } } } diff --git a/src/dict/dawg.cpp b/src/dict/dawg.cpp index 78c8d08e..1495ef9f 100644 --- a/src/dict/dawg.cpp +++ b/src/dict/dawg.cpp @@ -120,13 +120,13 @@ void Dawg::iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore std::function cb) const { NodeChildVector children; this->unichar_ids_of(to_explore, &children, false); - for (int i = 0; i < children.size(); i++) { + for (auto &i : children) { WERD_CHOICE next_word(word_so_far); - next_word.append_unichar_id(children[i].unichar_id, 1, 0.0, 0.0); - if (this->end_of_word(children[i].edge_ref)) { + next_word.append_unichar_id(i.unichar_id, 1, 0.0, 0.0); + if (this->end_of_word(i.edge_ref)) { cb(&next_word); } - NODE_REF next = next_node(children[i].edge_ref); + NODE_REF next = next_node(i.edge_ref); if (next != 0) { iterate_words_rec(next_word, next, cb); } @@ -141,8 +141,8 @@ bool Dawg::match_words(WERD_CHOICE *word, int32_t index, NODE_REF node, UNICHAR_ bool any_matched = false; NodeChildVector vec; this->unichar_ids_of(node, &vec, false); - for (int i = 0; i < vec.size(); ++i) { - word->set_unichar_id(vec[i].unichar_id, index); + for (auto &i : vec) { + word->set_unichar_id(i.unichar_id, index); if (match_words(word, index, node, wildcard)) any_matched = true; } diff --git a/src/dict/dict.cpp b/src/dict/dict.cpp index f3948ae8..80cc6685 100644 --- a/src/dict/dict.cpp +++ b/src/dict/dict.cpp @@ -351,8 +351,7 @@ bool Dict::FinishLoad() { // in the successors_ vector is a vector of integers that represent the // indices into the dawgs_ vector of the successors for dawg i. successors_.reserve(dawgs_.size()); - for (int i = 0; i < dawgs_.size(); ++i) { - const Dawg *dawg = dawgs_[i]; + for (auto dawg : dawgs_) { auto *lst = new SuccessorList(); for (int j = 0; j < dawgs_.size(); ++j) { const Dawg *other = dawgs_[j]; @@ -368,9 +367,9 @@ bool Dict::FinishLoad() { void Dict::End() { if (dawgs_.size() == 0) return; // Not safe to call twice. - for (int i = 0; i < dawgs_.size(); i++) { - if (!dawg_cache_->FreeDawg(dawgs_[i])) { - delete dawgs_[i]; + for (auto &dawg : dawgs_) { + if (!dawg_cache_->FreeDawg(dawg)) { + delete dawg; } } dawg_cache_->FreeDawg(bigram_dawg_); @@ -438,8 +437,7 @@ int Dict::def_letter_is_okay(void *void_dawg_args, const UNICHARSET &unicharset, if (punc_transition_edge != NO_EDGE) { // Find all successors, and see which can transition. const SuccessorList &slist = *(successors_[pos.punc_index]); - for (int s = 0; s < slist.size(); ++s) { - int sdawg_index = slist[s]; + for (int sdawg_index : slist) { const Dawg *sdawg = dawgs_[sdawg_index]; UNICHAR_ID ch = char_for_dawg(unicharset, unichar_id, sdawg); EDGE_REF dawg_edge = sdawg->edge_char_of(0, ch, word_end); @@ -555,13 +553,13 @@ void Dict::ProcessPatternEdges(const Dawg *dawg, const DawgPosition &pos, UNICHA std::vector unichar_id_patterns; unichar_id_patterns.push_back(unichar_id); dawg->unichar_id_to_patterns(unichar_id, getUnicharset(), &unichar_id_patterns); - for (int i = 0; i < unichar_id_patterns.size(); ++i) { + for (int unichar_id_pattern : unichar_id_patterns) { // On the first iteration check all the outgoing edges. // On the second iteration check all self-loops. for (int k = 0; k < 2; ++k) { EDGE_REF edge = (k == 0) - ? dawg->edge_char_of(node, unichar_id_patterns[i], word_end) - : dawg->pattern_loop_edge(pos.dawg_ref, unichar_id_patterns[i], word_end); + ? dawg->edge_char_of(node, unichar_id_pattern, word_end) + : dawg->pattern_loop_edge(pos.dawg_ref, unichar_id_pattern, word_end); if (edge == NO_EDGE) continue; if (dawg_debug_level >= 3) { @@ -832,8 +830,8 @@ bool Dict::valid_bigram(const WERD_CHOICE &word1, const WERD_CHOICE &word2) cons bigram_string.insert(bigram_string.end(), normed_ids.begin(), normed_ids.end()); } WERD_CHOICE normalized_word(&uchset, bigram_string.size()); - for (int i = 0; i < bigram_string.size(); ++i) { - normalized_word.append_unichar_id_space_allocated(bigram_string[i], 1, 0.0f, 0.0f); + for (int i : bigram_string) { + normalized_word.append_unichar_id_space_allocated(i, 1, 0.0f, 0.0f); } return bigram_dawg_->word_in_dawg(normalized_word); } diff --git a/src/dict/trie.cpp b/src/dict/trie.cpp index 95917f41..cad1158d 100644 --- a/src/dict/trie.cpp +++ b/src/dict/trie.cpp @@ -295,8 +295,8 @@ bool Trie::read_word_list(const char *filename, std::vector *words) bool Trie::add_word_list(const std::vector &words, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse_policy) { - for (int i = 0; i < words.size(); ++i) { - WERD_CHOICE word(words[i].c_str(), unicharset); + for (const auto &i : words) { + WERD_CHOICE word(i.c_str(), unicharset); if (word.length() == 0 || word.contains_unichar_id(INVALID_UNICHAR_ID)) continue; if ((reverse_policy == RRP_REVERSE_IF_HAS_RTL && word.has_rtl_unichar_id()) || @@ -306,7 +306,7 @@ bool Trie::add_word_list(const std::vector &words, const UNICHARSET if (!word_in_dawg(word)) { add_word_to_dawg(word); if (!word_in_dawg(word)) { - tprintf("Error: word '%s' not in DAWG after adding it\n", words[i].c_str()); + tprintf("Error: word '%s' not in DAWG after adding it\n", i.c_str()); return false; } } @@ -665,10 +665,10 @@ void Trie::reduce_node_input(NODE_REF node, NODE_MARKER reduced_nodes) { print_node(node, MAX_NODE_EDGES_DISPLAY); } - for (int i = 0; i < backward_edges.size(); ++i) { - if (DeadEdge(backward_edges[i])) + for (auto &backward_edge : backward_edges) { + if (DeadEdge(backward_edge)) continue; - NODE_REF next_node = next_node_from_edge_rec(backward_edges[i]); + NODE_REF next_node = next_node_from_edge_rec(backward_edge); if (next_node != 0 && !reduced_nodes[next_node]) { reduce_node_input(next_node, reduced_nodes); } diff --git a/src/lstm/plumbing.cpp b/src/lstm/plumbing.cpp index cf6a1d46..6d565a63 100644 --- a/src/lstm/plumbing.cpp +++ b/src/lstm/plumbing.cpp @@ -27,16 +27,16 @@ Plumbing::Plumbing(const std::string &name) : Network(NT_PARALLEL, name, 0, 0) { // DeSerialize only operate on the run-time data if state is false. void Plumbing::SetEnableTraining(TrainingState state) { Network::SetEnableTraining(state); - for (int i = 0; i < stack_.size(); ++i) - stack_[i]->SetEnableTraining(state); + for (auto &i : stack_) + i->SetEnableTraining(state); } // Sets flags that control the action of the network. See NetworkFlags enum // for bit values. void Plumbing::SetNetworkFlags(uint32_t flags) { Network::SetNetworkFlags(flags); - for (int i = 0; i < stack_.size(); ++i) - stack_[i]->SetNetworkFlags(flags); + for (auto &i : stack_) + i->SetNetworkFlags(flags); } // Sets up the network for training. Initializes weights using weights of @@ -46,8 +46,8 @@ void Plumbing::SetNetworkFlags(uint32_t flags) { // Returns the number of weights initialized. int Plumbing::InitWeights(float range, TRand *randomizer) { num_weights_ = 0; - for (int i = 0; i < stack_.size(); ++i) - num_weights_ += stack_[i]->InitWeights(range, randomizer); + for (auto &i : stack_) + num_weights_ += i->InitWeights(range, randomizer); return num_weights_; } @@ -55,24 +55,24 @@ int Plumbing::InitWeights(float range, TRand *randomizer) { // and remaps their outputs according to code_map. See network.h for details. int Plumbing::RemapOutputs(int old_no, const std::vector &code_map) { num_weights_ = 0; - for (int i = 0; i < stack_.size(); ++i) { - num_weights_ += stack_[i]->RemapOutputs(old_no, code_map); + for (auto &i : stack_) { + num_weights_ += i->RemapOutputs(old_no, code_map); } return num_weights_; } // Converts a float network to an int network. void Plumbing::ConvertToInt() { - for (int i = 0; i < stack_.size(); ++i) - stack_[i]->ConvertToInt(); + for (auto &i : stack_) + i->ConvertToInt(); } // Provides a pointer to a TRand for any networks that care to use it. // Note that randomizer is a borrowed pointer that should outlive the network // and should not be deleted by any of the networks. void Plumbing::SetRandomizer(TRand *randomizer) { - for (int i = 0; i < stack_.size(); ++i) - stack_[i]->SetRandomizer(randomizer); + for (auto &i : stack_) + i->SetRandomizer(randomizer); } // Adds the given network to the stack. @@ -98,8 +98,8 @@ bool Plumbing::SetupNeedsBackprop(bool needs_backprop) { if (IsTraining()) { needs_to_backprop_ = needs_backprop; bool retval = needs_backprop; - for (int i = 0; i < stack_.size(); ++i) { - if (stack_[i]->SetupNeedsBackprop(needs_backprop)) + for (auto &i : stack_) { + if (i->SetupNeedsBackprop(needs_backprop)) retval = true; } return retval; @@ -122,15 +122,15 @@ int Plumbing::XScaleFactor() const { // Provides the (minimum) x scale factor to the network (of interest only to // input units) so they can determine how to scale bounding boxes. void Plumbing::CacheXScaleFactor(int factor) { - for (int i = 0; i < stack_.size(); ++i) { - stack_[i]->CacheXScaleFactor(factor); + for (auto &i : stack_) { + i->CacheXScaleFactor(factor); } } // Provides debug output on the weights. void Plumbing::DebugWeights() { - for (int i = 0; i < stack_.size(); ++i) - stack_[i]->DebugWeights(); + for (auto &i : stack_) + i->DebugWeights(); } // Returns a set of strings representing the layer-ids of all layers below. diff --git a/src/lstm/recodebeam.cpp b/src/lstm/recodebeam.cpp index b171dc16..24c89eba 100644 --- a/src/lstm/recodebeam.cpp +++ b/src/lstm/recodebeam.cpp @@ -169,9 +169,9 @@ std::vector>> RecodeBeamSearch::combineSegmentedTimesteps( std::vector>>> *segmentedTimesteps) { std::vector>> combined_timesteps; - for (int i = 0; i < segmentedTimesteps->size(); ++i) { - for (int j = 0; j < (*segmentedTimesteps)[i].size(); ++j) { - combined_timesteps.push_back((*segmentedTimesteps)[i][j]); + for (auto &segmentedTimestep : *segmentedTimesteps) { + for (int j = 0; j < segmentedTimestep.size(); ++j) { + combined_timesteps.push_back(segmentedTimestep[j]); } } return combined_timesteps; @@ -436,9 +436,9 @@ void RecodeBeamSearch::extractSymbolChoices(const UNICHARSET *unicharset) { } // Exclude the best choice for the followup decoding. std::unordered_set excludeCodeList; - for (int node = 0; node < best_nodes.size(); ++node) { - if (best_nodes[node]->code != null_char_) { - excludeCodeList.insert(best_nodes[node]->code); + for (auto &best_node : best_nodes) { + if (best_node->code != null_char_) { + excludeCodeList.insert(best_node->code); } } if (j - 1 < excludedUnichars.size()) { @@ -514,9 +514,9 @@ void RecodeBeamSearch::DebugBeamPos(const UNICHARSET &unicharset, const RecodeHe } } } - for (int u = 0; u < unichar_bests.size(); ++u) { - if (unichar_bests[u] != nullptr) { - const RecodeNode &node = *unichar_bests[u]; + for (auto &unichar_best : unichar_bests) { + if (unichar_best != nullptr) { + const RecodeNode &node = *unichar_best; node.Print(null_char_, unicharset, 1); } } @@ -870,8 +870,7 @@ void RecodeBeamSearch::ContinueContext(const RecodeNode *prev, int index, const } const std::vector *final_codes = recoder_.GetFinalCodes(prefix); if (final_codes != nullptr) { - for (int i = 0; i < final_codes->size(); ++i) { - int code = (*final_codes)[i]; + for (int code : *final_codes) { if (top_n_flags_[code] != top_n_flag) continue; if (prev != nullptr && prev->code == code && !is_simple_text_) @@ -904,8 +903,7 @@ void RecodeBeamSearch::ContinueContext(const RecodeNode *prev, int index, const } const std::vector *next_codes = recoder_.GetNextCodes(prefix); if (next_codes != nullptr) { - for (int i = 0; i < next_codes->size(); ++i) { - int code = (*next_codes)[i]; + for (int code : *next_codes) { if (top_n_flags_[code] != top_n_flag) continue; if (prev != nullptr && prev->code == code && !is_simple_text_) @@ -1132,16 +1130,16 @@ bool RecodeBeamSearch::UpdateHeapIfMatched(RecodeNode *new_node, RecodeHeap *hea // It might not be faster because the hash map would have to be updated // every time a heap reshuffle happens, and that would be a lot of overhead. std::vector &nodes = heap->heap(); - for (int i = 0; i < nodes.size(); ++i) { - RecodeNode &node = nodes[i].data(); + for (auto &i : nodes) { + RecodeNode &node = i.data(); if (node.code == new_node->code && node.code_hash == new_node->code_hash && node.permuter == new_node->permuter && node.start_of_dawg == new_node->start_of_dawg) { if (new_node->score > node.score) { // The new one is better. Update the entire node in the heap and // reshuffle. node = *new_node; - nodes[i].key() = node.score; - heap->Reshuffle(&nodes[i]); + i.key() = node.score; + heap->Reshuffle(&i); } return true; } diff --git a/src/lstm/series.cpp b/src/lstm/series.cpp index 23abccf9..d6e52fa0 100644 --- a/src/lstm/series.cpp +++ b/src/lstm/series.cpp @@ -46,9 +46,9 @@ StaticShape Series::OutputShape(const StaticShape &input_shape) const { int Series::InitWeights(float range, TRand *randomizer) { num_weights_ = 0; tprintf("Num outputs,weights in Series:\n"); - for (int i = 0; i < stack_.size(); ++i) { - int weights = stack_[i]->InitWeights(range, randomizer); - tprintf(" %s:%d, %d\n", stack_[i]->spec().c_str(), stack_[i]->NumOutputs(), weights); + for (auto &i : stack_) { + int weights = i->InitWeights(range, randomizer); + tprintf(" %s:%d, %d\n", i->spec().c_str(), i->NumOutputs(), weights); num_weights_ += weights; } tprintf("Total weights = %d\n", num_weights_); @@ -60,9 +60,9 @@ int Series::InitWeights(float range, TRand *randomizer) { int Series::RemapOutputs(int old_no, const std::vector &code_map) { num_weights_ = 0; tprintf("Num (Extended) outputs,weights in Series:\n"); - for (int i = 0; i < stack_.size(); ++i) { - int weights = stack_[i]->RemapOutputs(old_no, code_map); - tprintf(" %s:%d, %d\n", stack_[i]->spec().c_str(), stack_[i]->NumOutputs(), weights); + for (auto &i : stack_) { + int weights = i->RemapOutputs(old_no, code_map); + tprintf(" %s:%d, %d\n", i->spec().c_str(), i->NumOutputs(), weights); num_weights_ += weights; } tprintf("Total weights = %d\n", num_weights_); @@ -75,8 +75,8 @@ int Series::RemapOutputs(int old_no, const std::vector &code_map) { // can be told to produce backprop for this layer if needed. bool Series::SetupNeedsBackprop(bool needs_backprop) { needs_to_backprop_ = needs_backprop; - for (int i = 0; i < stack_.size(); ++i) - needs_backprop = stack_[i]->SetupNeedsBackprop(needs_backprop); + for (auto &i : stack_) + needs_backprop = i->SetupNeedsBackprop(needs_backprop); return needs_backprop; } @@ -88,8 +88,8 @@ bool Series::SetupNeedsBackprop(bool needs_backprop) { // the minimum scale factor of the paths through the GlobalMinimax. int Series::XScaleFactor() const { int factor = 1; - for (int i = 0; i < stack_.size(); ++i) - factor *= stack_[i]->XScaleFactor(); + for (auto i : stack_) + factor *= i->XScaleFactor(); return factor; } @@ -183,9 +183,9 @@ void Series::SplitAt(int last_start, Series **start, Series **end) { void Series::AppendSeries(Network *src) { ASSERT_HOST(src->type() == NT_SERIES); auto *src_series = static_cast(src); - for (int s = 0; s < src_series->stack_.size(); ++s) { - AddToStack(src_series->stack_[s]); - src_series->stack_[s] = nullptr; + for (auto &s : src_series->stack_) { + AddToStack(s); + s = nullptr; } delete src; } diff --git a/src/lstm/weightmatrix.cpp b/src/lstm/weightmatrix.cpp index ebb72ff0..937b86f8 100644 --- a/src/lstm/weightmatrix.cpp +++ b/src/lstm/weightmatrix.cpp @@ -260,8 +260,8 @@ bool WeightMatrix::DeSerializeOld(bool training, TFile *fp) { if (!fp->DeSerialize(old_scales)) return false; scales_.reserve(old_scales.size()); - for (int i = 0; i < old_scales.size(); ++i) { - scales_.push_back(old_scales[i]); + for (float old_scale : old_scales) { + scales_.push_back(old_scale); } } else { if (!float_array.DeSerialize(fp)) diff --git a/src/textord/baselinedetect.cpp b/src/textord/baselinedetect.cpp index e272255e..8cdf8800 100644 --- a/src/textord/baselinedetect.cpp +++ b/src/textord/baselinedetect.cpp @@ -307,8 +307,8 @@ void BaselineRow::SetupBlobDisplacements(const FCOORD &direction) { // Set up a histogram using disp_quant_factor_ as the bucket size. STATS dist_stats(IntCastRounded(min_dist / disp_quant_factor_), IntCastRounded(max_dist / disp_quant_factor_) + 1); - for (int i = 0; i < perp_blob_dists.size(); ++i) { - dist_stats.add(IntCastRounded(perp_blob_dists[i] / disp_quant_factor_), 1); + for (double perp_blob_dist : perp_blob_dists) { + dist_stats.add(IntCastRounded(perp_blob_dist / disp_quant_factor_), 1); } std::vector> scaled_modes; dist_stats.top_n_modes(kMaxDisplacementsModes, scaled_modes); @@ -320,8 +320,8 @@ void BaselineRow::SetupBlobDisplacements(const FCOORD &direction) { } } #endif - for (int i = 0; i < scaled_modes.size(); ++i) - displacement_modes_.push_back(disp_quant_factor_ * scaled_modes[i].key()); + for (auto &scaled_mode : scaled_modes) + displacement_modes_.push_back(disp_quant_factor_ * scaled_mode.key()); } // Fits a line in the given direction to blobs that are close to the given @@ -429,8 +429,7 @@ bool BaselineBlock::FitBaselinesAndFindSkew(bool use_box_bottoms) { if (non_text_block_) return false; std::vector angles; - for (int r = 0; r < rows_.size(); ++r) { - BaselineRow *row = rows_[r]; + for (auto row : rows_) { if (row->FitBaseline(use_box_bottoms)) { double angle = row->BaselineAngle(); angles.push_back(angle); @@ -462,8 +461,7 @@ void BaselineBlock::ParallelizeBaselines(double default_block_skew) { if (debug_level_ > 0) tprintf("Adjusting block to skew angle %g\n", skew_angle_); FCOORD direction(cos(skew_angle_), sin(skew_angle_)); - for (int r = 0; r < rows_.size(); ++r) { - BaselineRow *row = rows_[r]; + for (auto row : rows_) { row->AdjustBaselineToParallel(debug_level_, direction); if (debug_level_ > 1) row->Print(); @@ -646,8 +644,7 @@ bool BaselineBlock::ComputeLineSpacing() { void BaselineBlock::ComputeBaselinePositions(const FCOORD &direction, std::vector *positions) { positions->clear(); - for (int r = 0; r < rows_.size(); ++r) { - BaselineRow *row = rows_[r]; + for (auto row : rows_) { const TBOX &row_box = row->bounding_box(); float x_middle = (row_box.left() + row_box.right()) / 2.0f; FCOORD row_pos(x_middle, static_cast(row->StraightYAtX(x_middle))); @@ -739,16 +736,15 @@ double BaselineBlock::FitLineSpacingModel(const std::vector &positions, } std::vector offsets; // Get the offset (remainder) linespacing for each line and choose the median. - for (int i = 0; i < positions.size(); ++i) - offsets.push_back(fmod(positions[i], m_in)); + for (double position : positions) + offsets.push_back(fmod(position, m_in)); // Get the median offset. double median_offset = MedianOfCircularValues(m_in, offsets); // Now fit a line to quantized line number and offset. LLSQ llsq; int min_index = INT32_MAX; int max_index = -INT32_MAX; - for (int i = 0; i < positions.size(); ++i) { - double y_pos = positions[i]; + for (double y_pos : positions) { int row_index = IntCastRounded((y_pos - median_offset) / m_in); UpdateRange(row_index, &min_index, &max_index); llsq.add(row_index, y_pos); @@ -758,8 +754,8 @@ double BaselineBlock::FitLineSpacingModel(const std::vector &positions, // Use the median offset rather than the mean. offsets.clear(); if (*m_out != 0.0) { - for (int i = 0; i < positions.size(); ++i) { - offsets.push_back(fmod(positions[i], *m_out)); + for (double position : positions) { + offsets.push_back(fmod(position, *m_out)); } // Get the median offset. if (debug_level_ > 2) { @@ -810,8 +806,7 @@ BaselineDetect::BaselineDetect(int debug_level, const FCOORD &page_skew, TO_BLOC // smoothing based on block/page-level skew and block-level linespacing. void BaselineDetect::ComputeStraightBaselines(bool use_box_bottoms) { std::vector block_skew_angles; - for (int i = 0; i < blocks_.size(); ++i) { - BaselineBlock *bl_block = blocks_[i]; + for (auto bl_block : blocks_) { if (debug_level_ > 0) tprintf("Fitting initial baselines...\n"); if (bl_block->FitBaselinesAndFindSkew(use_box_bottoms)) { @@ -828,8 +823,7 @@ void BaselineDetect::ComputeStraightBaselines(bool use_box_bottoms) { } // Set bad lines in each block to the default block skew and then force fit // a linespacing model where it makes sense to do so. - for (int i = 0; i < blocks_.size(); ++i) { - BaselineBlock *bl_block = blocks_[i]; + for (auto bl_block : blocks_) { bl_block->ParallelizeBaselines(default_block_skew); bl_block->SetupBlockParameters(); // This replaced compute_row_stats. } @@ -843,8 +837,7 @@ void BaselineDetect::ComputeStraightBaselines(bool use_box_bottoms) { void BaselineDetect::ComputeBaselineSplinesAndXheights(const ICOORD &page_tr, bool enable_splines, bool remove_noise, bool show_final_rows, Textord *textord) { - for (int i = 0; i < blocks_.size(); ++i) { - BaselineBlock *bl_block = blocks_[i]; + for (auto bl_block : blocks_) { if (enable_splines) bl_block->PrepareForSplineFitting(page_tr, remove_noise); bl_block->FitBaselineSplines(enable_splines, show_final_rows, textord); diff --git a/src/textord/colfind.cpp b/src/textord/colfind.cpp index ab2a8c3e..0aae8eaa 100644 --- a/src/textord/colfind.cpp +++ b/src/textord/colfind.cpp @@ -592,8 +592,7 @@ bool ColumnFinder::MakeColumns(bool single_column) { #endif ComputeMeanColumnGap(any_multi_column); } - for (int i = 0; i < part_sets.size(); ++i) { - ColPartitionSet *line_set = part_sets.at(i); + for (auto line_set : part_sets) { if (line_set != nullptr) { line_set->RelinquishParts(); delete line_set; diff --git a/src/textord/tabfind.cpp b/src/textord/tabfind.cpp index 629dda6f..ed842051 100644 --- a/src/textord/tabfind.cpp +++ b/src/textord/tabfind.cpp @@ -517,13 +517,13 @@ ScrollView *TabFind::FindInitialTabVectors(BLOBNBOX_LIST *image_blobs, int min_g // Helper displays all the boxes in the given vector on the given window. static void DisplayBoxVector(const std::vector &boxes, ScrollView *win) { - for (int i = 0; i < boxes.size(); ++i) { - TBOX box = boxes[i]->bounding_box(); + for (auto boxe : boxes) { + TBOX box = boxe->bounding_box(); int left_x = box.left(); int right_x = box.right(); int top_y = box.top(); int bottom_y = box.bottom(); - ScrollView::Color box_color = boxes[i]->BoxColor(); + ScrollView::Color box_color = boxe->BoxColor(); win->Pen(box_color); win->Rectangle(left_x, bottom_y, right_x, top_y); } @@ -793,13 +793,11 @@ void TabFind::FindAllTabVectors(int min_gutter_width) { } // Get rid of the test vectors and reset the types of the tabs. dummy_vectors.clear(); - for (int i = 0; i < left_tab_boxes_.size(); ++i) { - BLOBNBOX *bbox = left_tab_boxes_[i]; + for (auto bbox : left_tab_boxes_) { if (bbox->left_tab_type() == TT_CONFIRMED) bbox->set_left_tab_type(TT_MAYBE_ALIGNED); } - for (int i = 0; i < right_tab_boxes_.size(); ++i) { - BLOBNBOX *bbox = right_tab_boxes_[i]; + for (auto bbox : right_tab_boxes_) { if (bbox->right_tab_type() == TT_CONFIRMED) bbox->set_right_tab_type(TT_MAYBE_ALIGNED); } @@ -832,8 +830,7 @@ int TabFind::FindTabVectors(int search_size_multiple, TabAlignment alignment, in // Search the right or left tab boxes, looking for tab vectors. bool right = alignment == TA_RIGHT_ALIGNED || alignment == TA_RIGHT_RAGGED; const std::vector &boxes = right ? right_tab_boxes_ : left_tab_boxes_; - for (int i = 0; i < boxes.size(); ++i) { - BLOBNBOX *bbox = boxes[i]; + for (auto bbox : boxes) { if ((!right && bbox->left_tab_type() == TT_MAYBE_ALIGNED) || (right && bbox->right_tab_type() == TT_MAYBE_ALIGNED)) { TabVector *vector = FindTabVector(search_size_multiple, min_gutter_width, alignment, bbox, diff --git a/src/textord/tablerecog.cpp b/src/textord/tablerecog.cpp index 1f800a61..6ceb2e79 100644 --- a/src/textord/tablerecog.cpp +++ b/src/textord/tablerecog.cpp @@ -209,11 +209,11 @@ bool StructuredTable::FindWhitespacedStructure() { // throughout the code is that "0" distance is a very very small space. bool StructuredTable::DoesPartitionFit(const ColPartition &part) const { const TBOX &box = part.bounding_box(); - for (int i = 0; i < cell_x_.size(); ++i) - if (box.left() < cell_x_[i] && cell_x_[i] < box.right()) + for (int i : cell_x_) + if (box.left() < i && i < box.right()) return false; - for (int i = 0; i < cell_y_.size(); ++i) - if (box.bottom() < cell_y_[i] && cell_y_[i] < box.top()) + for (int i : cell_y_) + if (box.bottom() < i && i < box.top()) return false; return true; } @@ -290,11 +290,11 @@ void StructuredTable::Display(ScrollView *window, ScrollView::Color color) { window->Pen(color); window->Rectangle(bounding_box_.left(), bounding_box_.bottom(), bounding_box_.right(), bounding_box_.top()); - for (int i = 0; i < cell_x_.size(); i++) { - window->Line(cell_x_[i], bounding_box_.bottom(), cell_x_[i], bounding_box_.top()); + for (int i : cell_x_) { + window->Line(i, bounding_box_.bottom(), i, bounding_box_.top()); } - for (int i = 0; i < cell_y_.size(); i++) { - window->Line(bounding_box_.left(), cell_y_[i], bounding_box_.right(), cell_y_[i]); + for (int i : cell_y_) { + window->Line(bounding_box_.left(), i, bounding_box_.right(), i); } window->UpdateWindow(); } @@ -350,12 +350,12 @@ void StructuredTable::ClearStructure() { bool StructuredTable::VerifyLinedTableCells() { // Function only called when lines exist. ASSERT_HOST(cell_y_.size() >= 2 && cell_x_.size() >= 2); - for (int i = 0; i < cell_y_.size(); ++i) { - if (CountHorizontalIntersections(cell_y_[i]) > 0) + for (int i : cell_y_) { + if (CountHorizontalIntersections(i) > 0) return false; } - for (int i = 0; i < cell_x_.size(); ++i) { - if (CountVerticalIntersections(cell_x_[i]) > 0) + for (int i : cell_x_) { + if (CountVerticalIntersections(i) > 0) return false; } return true; diff --git a/src/training/cntraining.cpp b/src/training/cntraining.cpp index bd0883db..81de5dba 100644 --- a/src/training/cntraining.cpp +++ b/src/training/cntraining.cpp @@ -171,8 +171,8 @@ int main(int argc, char *argv[]) { int desc_index = ShortNameToFeatureType(FeatureDefs, PROGRAM_FEATURE_TYPE); WriteNormProtos(FLAGS_D.c_str(), NormProtoList, FeatureDefs.FeatureDesc[desc_index]); FreeNormProtoList(NormProtoList); - for (int i = 0; i < freeable_protos.size(); ++i) { - FreeProtoList(&freeable_protos[i]); + for (auto &freeable_proto : freeable_protos) { + FreeProtoList(&freeable_proto); } printf("\n"); return 0; diff --git a/src/training/common/intfeaturedist.cpp b/src/training/common/intfeaturedist.cpp index 48e35876..67b0b705 100644 --- a/src/training/common/intfeaturedist.cpp +++ b/src/training/common/intfeaturedist.cpp @@ -52,8 +52,7 @@ void IntFeatureDist::Init(const IntFeatureMap *feature_map) { void IntFeatureDist::Set(const std::vector &indexed_features, int canonical_count, bool value) { total_feature_weight_ = canonical_count; - for (int i = 0; i < indexed_features.size(); ++i) { - const int f = indexed_features[i]; + for (int f : indexed_features) { features_[f] = value; for (int dir = -kNumOffsetMaps; dir <= kNumOffsetMaps; ++dir) { if (dir == 0) diff --git a/src/training/common/intfeaturemap.cpp b/src/training/common/intfeaturemap.cpp index da5f9625..8a862444 100644 --- a/src/training/common/intfeaturemap.cpp +++ b/src/training/common/intfeaturemap.cpp @@ -164,8 +164,8 @@ int IntFeatureMap::FinalizeMapping(SampleIterator *it) { // Prints the map features from the set in human-readable form. void IntFeatureMap::DebugMapFeatures(const std::vector &map_features) const { - for (int i = 0; i < map_features.size(); ++i) { - INT_FEATURE_STRUCT f = InverseMapFeature(map_features[i]); + for (int map_feature : map_features) { + INT_FEATURE_STRUCT f = InverseMapFeature(map_feature); f.print(); } } diff --git a/src/training/common/mastertrainer.cpp b/src/training/common/mastertrainer.cpp index 2d54112a..bb563ad6 100644 --- a/src/training/common/mastertrainer.cpp +++ b/src/training/common/mastertrainer.cpp @@ -62,8 +62,8 @@ MasterTrainer::MasterTrainer(NormalizationMode norm_mode, bool shape_analysis, MasterTrainer::~MasterTrainer() { delete[] fragments_; - for (int p = 0; p < page_images_.size(); ++p) - pixDestroy(&page_images_[p]); + for (auto &page_image : page_images_) + pixDestroy(&page_image); } // WARNING! Serialize/DeSerialize are only partial, providing diff --git a/src/training/common/trainingsampleset.cpp b/src/training/common/trainingsampleset.cpp index 9b4ffd18..ca48bdaf 100644 --- a/src/training/common/trainingsampleset.cpp +++ b/src/training/common/trainingsampleset.cpp @@ -430,8 +430,7 @@ int TrainingSampleSet::ReliablySeparable(int font_id1, int class_id1, int font_i return canonical2.size(); // There are no cloud features. // Find a canonical2 feature that is not in cloud1. - for (int f = 0; f < canonical2.size(); ++f) { - const int feature = canonical2[f]; + for (int feature : canonical2) { if (cloud1[feature]) continue; // Gather the near neighbours of f. @@ -488,8 +487,8 @@ float TrainingSampleSet::GetCanonicalDist(int font_id, int class_id) const { // Generates indexed features for all samples with the supplied feature_space. void TrainingSampleSet::IndexFeatures(const IntFeatureSpace &feature_space) { - for (int s = 0; s < samples_.size(); ++s) - samples_[s]->IndexFeatures(feature_space); + for (auto &sample : samples_) + sample->IndexFeatures(feature_space); } // Marks the given sample index for deletion. @@ -560,8 +559,8 @@ void TrainingSampleSet::OrganizeByFontAndClass() { void TrainingSampleSet::SetupFontIdMap() { // Number of samples for each font_id. std::vector font_counts; - for (int s = 0; s < samples_.size(); ++s) { - const int font_id = samples_[s]->font_id(); + for (auto &sample : samples_) { + const int font_id = sample->font_id(); while (font_id >= font_counts.size()) font_counts.push_back(0); ++font_counts[font_id]; @@ -618,8 +617,7 @@ void TrainingSampleSet::ComputeCanonicalSamples(const IntFeatureMap &map, bool d // reasonably fast because f_table.FeatureDistance is fast, but we // may have to reconsider if we start playing with too many samples // of a single char/font. - for (int j = 0; j < fcinfo.samples.size(); ++j) { - int s2 = fcinfo.samples[j]; + for (int s2 : fcinfo.samples) { if (samples_[s2]->class_id() != c || samples_[s2]->font_id() != font_id || s2 == s1) continue; std::vector features2 = samples_[s2]->indexed_features(); @@ -733,8 +731,8 @@ void TrainingSampleSet::ComputeCloudFeatures(int feature_space_size) { for (int s = 0; s < num_samples; ++s) { const TrainingSample *sample = GetSample(font_id, c, s); const std::vector &sample_features = sample->indexed_features(); - for (int i = 0; i < sample_features.size(); ++i) - fcinfo.cloud_features.SetBit(sample_features[i]); + for (int sample_feature : sample_features) + fcinfo.cloud_features.SetBit(sample_feature); } } } @@ -761,8 +759,8 @@ void TrainingSampleSet::DisplaySamplesWithFeature(int f_index, const Shape &shap if (shape.ContainsUnichar(sample->class_id())) { std::vector indexed_features; space.IndexAndSortFeatures(sample->features(), sample->num_features(), &indexed_features); - for (int f = 0; f < indexed_features.size(); ++f) { - if (indexed_features[f] == f_index) { + for (int indexed_feature : indexed_features) { + if (indexed_feature == f_index) { sample->DisplayFeatures(color, window); } } diff --git a/src/training/pango/boxchar.cpp b/src/training/pango/boxchar.cpp index fcece484..193b0e85 100644 --- a/src/training/pango/boxchar.cpp +++ b/src/training/pango/boxchar.cpp @@ -54,8 +54,8 @@ void BoxChar::GetDirection(int *num_rtl, int *num_ltr) const { std::vector uni_vector = UNICHAR::UTF8ToUTF32(ch_.c_str()); if (uni_vector.empty()) { tprintf("Illegal utf8 in boxchar string:%s = ", ch_.c_str()); - for (size_t c = 0; c < ch_.size(); ++c) { - tprintf(" 0x%x", ch_[c]); + for (char c : ch_) { + tprintf(" 0x%x", c); } tprintf("\n"); return; @@ -81,8 +81,8 @@ void BoxChar::ReverseUnicodesInBox() { /* static */ void BoxChar::TranslateBoxes(int xshift, int yshift, std::vector *boxes) { - for (size_t i = 0; i < boxes->size(); ++i) { - BOX *box = (*boxes)[i]->box_; + for (auto &boxe : *boxes) { + BOX *box = boxe->box_; if (box != nullptr) { box->x += xshift; box->y += yshift; @@ -257,8 +257,8 @@ void BoxChar::ReorderRTLText(std::vector *boxes) { /* static */ bool BoxChar::ContainsMostlyRTL(const std::vector &boxes) { int num_rtl = 0, num_ltr = 0; - for (size_t i = 0; i < boxes.size(); ++i) { - boxes[i]->GetDirection(&num_rtl, &num_ltr); + for (auto boxe : boxes) { + boxe->GetDirection(&num_rtl, &num_ltr); } return num_rtl > num_ltr; } @@ -285,8 +285,8 @@ bool BoxChar::MostlyVertical(const std::vector &boxes) { /* static */ int BoxChar::TotalByteLength(const std::vector &boxes) { int total_length = 0; - for (size_t i = 0; i < boxes.size(); ++i) - total_length += boxes[i]->ch_.size(); + for (auto boxe : boxes) + total_length += boxe->ch_.size(); return total_length; } @@ -324,15 +324,14 @@ void BoxChar::WriteTesseractBoxFile(const std::string &filename, int height, std::string BoxChar::GetTesseractBoxStr(int height, const std::vector &boxes) { std::string output; char buffer[kMaxLineLength]; - for (size_t i = 0; i < boxes.size(); ++i) { - const Box *box = boxes[i]->box_; + for (auto boxe : boxes) { + const Box *box = boxe->box_; if (box == nullptr) { tprintf("Error: Call PrepareToWrite before WriteTesseractBoxFile!!\n"); return ""; } - int nbytes = - snprintf(buffer, kMaxLineLength, "%s %d %d %d %d %d\n", boxes[i]->ch_.c_str(), box->x, - height - box->y - box->h, box->x + box->w, height - box->y, boxes[i]->page_); + int nbytes = snprintf(buffer, kMaxLineLength, "%s %d %d %d %d %d\n", boxe->ch_.c_str(), box->x, + height - box->y - box->h, box->x + box->w, height - box->y, boxe->page_); output.append(buffer, nbytes); } return output; diff --git a/src/training/pango/pango_font_info.cpp b/src/training/pango/pango_font_info.cpp index 5001d5b2..b4ba8c3e 100644 --- a/src/training/pango/pango_font_info.cpp +++ b/src/training/pango/pango_font_info.cpp @@ -583,14 +583,13 @@ int FontUtils::FontScore(const std::unordered_map &ch_map, } *raw_score = 0; int ok_chars = 0; - for (std::unordered_map::const_iterator it = ch_map.begin(); it != ch_map.end(); - ++it) { - bool covered = (coverage != nullptr) && - (IsWhitespace(it->first) || - (pango_coverage_get(coverage, it->first) == PANGO_COVERAGE_EXACT)); + for (auto it : ch_map) { + bool covered = + (coverage != nullptr) && (IsWhitespace(it.first) || + (pango_coverage_get(coverage, it.first) == PANGO_COVERAGE_EXACT)); if (covered) { ++(*raw_score); - ok_chars += it->second; + ok_chars += it.second; } if (ch_flags) { ch_flags->push_back(covered); @@ -616,10 +615,10 @@ std::string FontUtils::BestFonts(const std::unordered_map &ch_m int most_ok_chars = 0; int best_raw_score = 0; const std::vector &font_names = FontUtils::ListAvailableFonts(); - for (unsigned i = 0; i < font_names.size(); ++i) { + for (const auto &font_name : font_names) { std::vector ch_flags; int raw_score = 0; - int ok_chars = FontScore(ch_map, font_names[i], &raw_score, &ch_flags); + int ok_chars = FontScore(ch_map, font_name, &raw_score, &ch_flags); most_ok_chars = std::max(ok_chars, most_ok_chars); best_raw_score = std::max(raw_score, best_raw_score); @@ -672,16 +671,16 @@ bool FontUtils::SelectFont(const char *utf8_word, const int utf8_len, font_name->clear(); if (graphemes) graphemes->clear(); - for (unsigned i = 0; i < all_fonts.size(); ++i) { + for (const auto &all_font : all_fonts) { PangoFontInfo font; std::vector found_graphemes; - ASSERT_HOST_MSG(font.ParseFontDescriptionName(all_fonts[i]), - "Could not parse font desc name %s\n", all_fonts[i].c_str()); + ASSERT_HOST_MSG(font.ParseFontDescriptionName(all_font), "Could not parse font desc name %s\n", + all_font.c_str()); if (font.CanRenderString(utf8_word, utf8_len, &found_graphemes)) { if (graphemes) graphemes->swap(found_graphemes); if (font_name) - *font_name = all_fonts[i]; + *font_name = all_font; return true; } } diff --git a/src/training/pango/stringrenderer.cpp b/src/training/pango/stringrenderer.cpp index 9ca62a71..e3dfa5ed 100644 --- a/src/training/pango/stringrenderer.cpp +++ b/src/training/pango/stringrenderer.cpp @@ -336,8 +336,8 @@ void StringRenderer::RotatePageBoxes(float rotation) { } void StringRenderer::ClearBoxes() { - for (size_t i = 0; i < boxchars_.size(); ++i) - delete boxchars_[i]; + for (auto &boxchar : boxchars_) + delete boxchar; boxchars_.clear(); boxaDestroy(&page_boxes_); } @@ -408,10 +408,10 @@ bool StringRenderer::GetClusterStrings(std::vector *cluster_text) { static void MergeBoxCharsToWords(std::vector *boxchars) { std::vector result; bool started_word = false; - for (size_t i = 0; i < boxchars->size(); ++i) { - if (boxchars->at(i)->ch() == " " || boxchars->at(i)->box() == nullptr) { - result.push_back(boxchars->at(i)); - boxchars->at(i) = nullptr; + for (auto &boxchar : *boxchars) { + if (boxchar->ch() == " " || boxchar->box() == nullptr) { + result.push_back(boxchar); + boxchar = nullptr; started_word = false; continue; } @@ -419,12 +419,12 @@ static void MergeBoxCharsToWords(std::vector *boxchars) { if (!started_word) { // Begin new word started_word = true; - result.push_back(boxchars->at(i)); - boxchars->at(i) = nullptr; + result.push_back(boxchar); + boxchar = nullptr; } else { BoxChar *last_boxchar = result.back(); // Compute bounding box union - const Box *box = boxchars->at(i)->box(); + const Box *box = boxchar->box(); Box *last_box = last_boxchar->mutable_box(); int left = std::min(last_box->x, box->x); int right = std::max(last_box->x + last_box->w, box->x + box->w); @@ -438,18 +438,18 @@ static void MergeBoxCharsToWords(std::vector *boxchars) { // Insert a fake interword space and start a new word with the current // boxchar. result.push_back(new BoxChar(" ", 1)); - result.push_back(boxchars->at(i)); - boxchars->at(i) = nullptr; + result.push_back(boxchar); + boxchar = nullptr; continue; } // Append to last word - last_boxchar->mutable_ch()->append(boxchars->at(i)->ch()); + last_boxchar->mutable_ch()->append(boxchar->ch()); last_box->x = left; last_box->w = right - left; last_box->y = top; last_box->h = bottom - top; - delete boxchars->at(i); - boxchars->at(i) = nullptr; + delete boxchar; + boxchar = nullptr; } } boxchars->swap(result); @@ -558,11 +558,10 @@ void StringRenderer::ComputeClusterBoxes() { CorrectBoxPositionsToLayout(&page_boxchars); if (render_fullwidth_latin_) { - for (std::map::iterator it = start_byte_to_box.begin(); - it != start_byte_to_box.end(); ++it) { + for (auto &it : start_byte_to_box) { // Convert fullwidth Latin characters to their halfwidth forms. - std::string half(ConvertFullwidthLatinToBasicLatin(it->second->ch())); - it->second->mutable_ch()->swap(half); + std::string half(ConvertFullwidthLatinToBasicLatin(it.second->ch())); + it.second->mutable_ch()->swap(half); } } @@ -576,12 +575,12 @@ void StringRenderer::ComputeClusterBoxes() { // Compute the page bounding box Box *page_box = nullptr; Boxa *all_boxes = nullptr; - for (size_t i = 0; i < page_boxchars.size(); ++i) { - if (page_boxchars[i]->box() == nullptr) + for (auto &page_boxchar : page_boxchars) { + if (page_boxchar->box() == nullptr) continue; if (all_boxes == nullptr) all_boxes = boxaCreate(0); - boxaAddBox(all_boxes, page_boxchars[i]->mutable_box(), L_CLONE); + boxaAddBox(all_boxes, page_boxchar->mutable_box(), L_CLONE); } if (all_boxes != nullptr) { boxaGetExtent(all_boxes, nullptr, nullptr, &page_box); diff --git a/src/training/text2image.cpp b/src/training/text2image.cpp index 51311323..b43981f4 100644 --- a/src/training/text2image.cpp +++ b/src/training/text2image.cpp @@ -694,8 +694,8 @@ static int Main() { if (fp == nullptr) { tprintf("Failed to create output font list %s\n", filename.c_str()); } else { - for (size_t i = 0; i < font_names.size(); ++i) { - fprintf(fp, "%s\n", font_names[i].c_str()); + for (auto &font_name : font_names) { + fprintf(fp, "%s\n", font_name.c_str()); } fclose(fp); } diff --git a/src/training/unicharset/lstmtrainer.cpp b/src/training/unicharset/lstmtrainer.cpp index fb67cf48..1573c651 100644 --- a/src/training/unicharset/lstmtrainer.cpp +++ b/src/training/unicharset/lstmtrainer.cpp @@ -1233,9 +1233,9 @@ double LSTMTrainer::ComputeWordError(std::string *truth_str, std::string *ocr_st --it->second; } int word_recall_errs = 0; - for (StrMap::const_iterator it = word_counts.begin(); it != word_counts.end(); ++it) { - if (it->second > 0) - word_recall_errs += it->second; + for (const auto &word_count : word_counts) { + if (word_count.second > 0) + word_recall_errs += word_count.second; } return static_cast(word_recall_errs) / truth_words.size(); } diff --git a/src/training/unicharset/normstrngs.cpp b/src/training/unicharset/normstrngs.cpp index 7f537778..01a556ef 100644 --- a/src/training/unicharset/normstrngs.cpp +++ b/src/training/unicharset/normstrngs.cpp @@ -46,8 +46,8 @@ static bool is_hyphen_punc(const char32 ch) { 0xfe63, // small hyphen-minus 0xff0d, // fullwidth hyphen-minus }; - for (int i = 0; i < kNumHyphenPuncUnicodes; ++i) { - if (kHyphenPuncUnicodes[i] == ch) + for (int kHyphenPuncUnicode : kHyphenPuncUnicodes) { + if (kHyphenPuncUnicode == ch) return true; } return false; @@ -65,8 +65,8 @@ static bool is_single_quote(const char32 ch) { 0x300C, // left corner bracket (East Asian languages) 0xFF07, // fullwidth apostrophe }; - for (int i = 0; i < kNumSingleQuoteUnicodes; ++i) { - if (kSingleQuoteUnicodes[i] == ch) + for (int kSingleQuoteUnicode : kSingleQuoteUnicodes) { + if (kSingleQuoteUnicode == ch) return true; } return false; @@ -85,8 +85,8 @@ static bool is_double_quote(const char32 ch) { 0x301E, // close double prime (East Asian languages written horizontally) 0xFF02, // fullwidth quotation mark }; - for (int i = 0; i < kNumDoubleQuoteUnicodes; ++i) { - if (kDoubleQuoteUnicodes[i] == ch) + for (int kDoubleQuoteUnicode : kDoubleQuoteUnicodes) { + if (kDoubleQuoteUnicode == ch) return true; } return false; diff --git a/src/training/unicharset_extractor.cpp b/src/training/unicharset_extractor.cpp index 486f093a..0abc77d9 100644 --- a/src/training/unicharset_extractor.cpp +++ b/src/training/unicharset_extractor.cpp @@ -42,11 +42,11 @@ namespace tesseract { // adds the segmented parts to unicharset. static void AddStringsToUnicharset(const std::vector &strings, int norm_mode, UNICHARSET *unicharset) { - for (int i = 0; i < strings.size(); ++i) { + for (const auto &string : strings) { std::vector normalized; if (NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone, static_cast(norm_mode), - /*report_errors*/ true, strings[i].c_str(), &normalized)) { + /*report_errors*/ true, string.c_str(), &normalized)) { for (const std::string &normed : normalized) { // normed is a UTF-8 encoded string if (normed.empty() || IsUTF8Whitespace(normed.c_str())) @@ -54,7 +54,7 @@ static void AddStringsToUnicharset(const std::vector &strings, int unicharset->unichar_insert(normed.c_str()); } } else { - tprintf("Normalization failed for string '%s'\n", strings[i].c_str()); + tprintf("Normalization failed for string '%s'\n", string.c_str()); } } } diff --git a/src/wordrec/chopper.cpp b/src/wordrec/chopper.cpp index 27d07e95..1dc5f04d 100644 --- a/src/wordrec/chopper.cpp +++ b/src/wordrec/chopper.cpp @@ -271,10 +271,10 @@ SEAM *Wordrec::chop_overlapping_blob(const std::vector &boxes, bool italic bool almost_equal_box = false; int num_overlap = 0; - for (int i = 0; i < boxes.size(); i++) { - if (original_box.overlap_fraction(boxes[i]) > 0.125) + for (auto boxe : boxes) { + if (original_box.overlap_fraction(boxe) > 0.125) num_overlap++; - if (original_box.almost_equal(boxes[i], 3)) + if (original_box.almost_equal(boxe, 3)) almost_equal_box = true; } @@ -585,10 +585,9 @@ int Wordrec::select_blob_to_split(const std::vector &blob_choices int Wordrec::select_blob_to_split_from_fixpt(DANGERR *fixpt) { if (!fixpt) return -1; - for (int i = 0; i < fixpt->size(); i++) { - if ((*fixpt)[i].begin + 1 == (*fixpt)[i].end && (*fixpt)[i].dangerous && - (*fixpt)[i].correct_is_ngram) { - return (*fixpt)[i].begin; + for (auto &i : *fixpt) { + if (i.begin + 1 == i.end && i.dangerous && i.correct_is_ngram) { + return i.begin; } } return -1; diff --git a/src/wordrec/lm_pain_points.cpp b/src/wordrec/lm_pain_points.cpp index 886b86f7..ed87b336 100644 --- a/src/wordrec/lm_pain_points.cpp +++ b/src/wordrec/lm_pain_points.cpp @@ -128,8 +128,7 @@ void LMPainPoints::GenerateFromAmbigs(const DANGERR &fixpt, ViterbiStateEntry *v WERD_RES *word_res) { // Begins and ends in DANGERR vector now record the blob indices as used // by the ratings matrix. - for (int d = 0; d < fixpt.size(); ++d) { - const DANGERR_INFO &danger = fixpt[d]; + for (auto danger : fixpt) { // Only use dangerous ambiguities. if (danger.dangerous) { GeneratePainPoint(danger.begin, danger.end - 1, LM_PPTYPE_AMBIG, vse->cost, true, diff --git a/src/wordrec/segsearch.cpp b/src/wordrec/segsearch.cpp index 29fec817..c069e8f3 100644 --- a/src/wordrec/segsearch.cpp +++ b/src/wordrec/segsearch.cpp @@ -281,8 +281,8 @@ void Wordrec::ResetNGramSearch(WERD_RES *word_res, BestChoiceBundle *best_choice std::vector *pending) { // TODO(rays) More refactoring required here. // Delete existing viterbi states. - for (int col = 0; col < best_choice_bundle->beam.size(); ++col) { - best_choice_bundle->beam[col]->Clear(); + for (auto &col : best_choice_bundle->beam) { + col->Clear(); } // Reset best_choice_bundle. word_res->ClearWordChoices(); From 1439efa73474a86b167f440fefbf9886b20ff150 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:26:20 +0100 Subject: [PATCH 4/9] Modernize code (clang-tidy -checks='-*,modernize-make-unique') Signed-off-by: Stefan Weil --- src/ccstruct/rejctmap.cpp | 4 +++- src/textord/edgblob.cpp | 4 +++- src/wordrec/wordrec.cpp | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ccstruct/rejctmap.cpp b/src/ccstruct/rejctmap.cpp index e5827811..9887c0ee 100644 --- a/src/ccstruct/rejctmap.cpp +++ b/src/ccstruct/rejctmap.cpp @@ -18,6 +18,8 @@ #include "rejctmap.h" +#include + #include "params.h" namespace tesseract { @@ -219,7 +221,7 @@ REJMAP &REJMAP::operator=(const REJMAP &source) { } void REJMAP::initialise(int16_t length) { - ptr.reset(new REJ[length]); + ptr = std::make_unique(length); len = length; } diff --git a/src/textord/edgblob.cpp b/src/textord/edgblob.cpp index 44e6c3e7..7541528d 100644 --- a/src/textord/edgblob.cpp +++ b/src/textord/edgblob.cpp @@ -23,6 +23,8 @@ #include "edgblob.h" +#include + #include "edgloop.h" #include "scanedg.h" @@ -60,7 +62,7 @@ OL_BUCKETS::OL_BUCKETS(ICOORD bleft, // corners bxdim = (tright.x() - bleft.x()) / BUCKETSIZE + 1; bydim = (tright.y() - bleft.y()) / BUCKETSIZE + 1; // make array - buckets.reset(new C_OUTLINE_LIST[bxdim * bydim]); + buckets = std::make_unique(bxdim * bydim); index = 0; } diff --git a/src/wordrec/wordrec.cpp b/src/wordrec/wordrec.cpp index b87ffd69..ceaad044 100644 --- a/src/wordrec/wordrec.cpp +++ b/src/wordrec/wordrec.cpp @@ -18,6 +18,8 @@ #include "wordrec.h" +#include + #ifdef DISABLED_LEGACY_ENGINE # include "params.h" @@ -99,7 +101,7 @@ Wordrec::Wordrec() params()) , pass2_ok_split(0.0f) { prev_word_best_choice_ = nullptr; - language_model_.reset(new LanguageModel(&get_fontinfo_table(), &(getDict()))); + language_model_ = std::make_unique(&get_fontinfo_table(), &(getDict())); fill_lattice_ = nullptr; } From 35e143ddfc327cdcb5ee19531fd87a8fd384b8a4 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:33:40 +0100 Subject: [PATCH 5/9] Modernize code (clang-tidy -checks='-*,modernize-use-auto') Signed-off-by: Stefan Weil --- src/api/baseapi.cpp | 12 +++++----- src/ccstruct/coutln.cpp | 2 +- src/classify/mfoutline.cpp | 2 +- src/classify/mfx.cpp | 2 +- src/lstm/functions.h | 4 ++-- src/lstm/recodebeam.cpp | 2 +- src/lstm/series.cpp | 4 ++-- src/textord/colfind.cpp | 4 ++-- src/textord/colpartition.cpp | 4 ++-- src/textord/strokewidth.cpp | 10 ++++----- src/textord/tablefind.cpp | 10 ++++----- src/training/common/commandlineflags.cpp | 24 ++++++++++---------- src/training/common/commontraining.cpp | 6 ++--- src/training/common/networkbuilder.cpp | 28 ++++++++++++------------ src/training/mftraining.cpp | 8 +++---- src/training/pango/boxchar.cpp | 2 +- src/training/pango/ligature_table.cpp | 4 ++-- src/training/pango/stringrenderer.cpp | 13 +++++------ src/training/unicharset/validator.cpp | 2 +- 19 files changed, 70 insertions(+), 73 deletions(-) diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp index 0e24565a..ccaaeecc 100644 --- a/src/api/baseapi.cpp +++ b/src/api/baseapi.cpp @@ -1329,8 +1329,8 @@ size_t TessBaseAPI::GetNumberOfTables() std::tuple TessBaseAPI::GetTableBoundingBox(unsigned i) { - const std::vector& t = constUniqueInstance>(); - + const auto &t = constUniqueInstance>(); + if(i >= t.size()) return std::tuple(0, 0, 0, 0); @@ -1344,8 +1344,8 @@ std::tuple TessBaseAPI::GetTableBoundingBox(unsigned i) std::vector> TessBaseAPI::GetTableRows(unsigned i) { - const std::vector& t = constUniqueInstance>(); - + const auto &t = constUniqueInstance>(); + if(i >= t.size()) return std::vector>(); @@ -1362,8 +1362,8 @@ std::vector> TessBaseAPI::GetTableRows(unsigned i) std::vector > TessBaseAPI::GetTableCols(unsigned i) { - const std::vector& t = constUniqueInstance>(); - + const auto &t = constUniqueInstance>(); + if(i >= t.size()) return std::vector>(); diff --git a/src/ccstruct/coutln.cpp b/src/ccstruct/coutln.cpp index 4347fa00..2832048f 100644 --- a/src/ccstruct/coutln.cpp +++ b/src/ccstruct/coutln.cpp @@ -243,7 +243,7 @@ void C_OUTLINE::FakeOutline(const TBOX &box, C_OUTLINE_LIST *outlines) { // as there is no outline, just a bounding box, but it works nicely. CRACKEDGE start; start.pos = box.topleft(); - C_OUTLINE *outline = new C_OUTLINE(&start, box.topleft(), box.botright(), 0); + auto *outline = new C_OUTLINE(&start, box.topleft(), box.botright(), 0); ol_it.add_to_end(outline); } diff --git a/src/classify/mfoutline.cpp b/src/classify/mfoutline.cpp index 73a749c5..0b25f593 100644 --- a/src/classify/mfoutline.cpp +++ b/src/classify/mfoutline.cpp @@ -40,7 +40,7 @@ LIST ConvertBlob(TBLOB *blob) { /** Convert a TESSLINE into the float-based MFOUTLINE micro-feature format. */ MFOUTLINE ConvertOutline(TESSLINE *outline) { MFEDGEPT *NewPoint; - MFOUTLINE MFOutline = NIL_LIST; + auto MFOutline = NIL_LIST; EDGEPT *EdgePoint; EDGEPT *StartPoint; EDGEPT *NextPoint; diff --git a/src/classify/mfx.cpp b/src/classify/mfx.cpp index ec00b130..1d38d6e5 100644 --- a/src/classify/mfx.cpp +++ b/src/classify/mfx.cpp @@ -52,7 +52,7 @@ MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End); * @return List of micro-features extracted from the blob. */ MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm) { - MICROFEATURES MicroFeatures = NIL_LIST; + auto MicroFeatures = NIL_LIST; LIST Outlines; LIST RemainingOutlines; MFOUTLINE Outline; diff --git a/src/lstm/functions.h b/src/lstm/functions.h index 40c7cfec..10ad3af2 100644 --- a/src/lstm/functions.h +++ b/src/lstm/functions.h @@ -44,7 +44,7 @@ inline double Tanh(double x) { if (x < 0.0) return -Tanh(-x); x *= kScaleFactor; - unsigned index = static_cast(x); + auto index = static_cast(x); if (index >= (kTableSize - 1)) return 1.0; double tanh_i0 = TanhTable[index]; @@ -57,7 +57,7 @@ inline double Logistic(double x) { if (x < 0.0) return 1.0 - Logistic(-x); x *= kScaleFactor; - unsigned index = static_cast(x); + auto index = static_cast(x); if (index >= (kTableSize - 1)) return 1.0; double l0 = LogisticTable[index]; diff --git a/src/lstm/recodebeam.cpp b/src/lstm/recodebeam.cpp index 24c89eba..53a70f5f 100644 --- a/src/lstm/recodebeam.cpp +++ b/src/lstm/recodebeam.cpp @@ -794,7 +794,7 @@ void RecodeBeamSearch::DecodeSecondaryStep(const float *outputs, int t, double d // first, which may have an empty intersection with the valid codes, so we // fall back to the rest if the beam is empty. for (int tn = 0; tn < TN_COUNT && total_beam == 0; ++tn) { - TopNState top_n = static_cast(tn); + auto top_n = static_cast(tn); for (int index = 0; index < kNumBeams; ++index) { // Working backwards through the heaps doesn't guarantee that we see the // best first, but it comes before a lot of the worst, so it is slightly diff --git a/src/lstm/series.cpp b/src/lstm/series.cpp index d6e52fa0..35284262 100644 --- a/src/lstm/series.cpp +++ b/src/lstm/series.cpp @@ -158,8 +158,8 @@ void Series::SplitAt(int last_start, Series **start, Series **end) { tprintf("Invalid split index %d must be in range [0,%zu]!\n", last_start, stack_.size() - 1); return; } - Series *master_series = new Series("MasterSeries"); - Series *boosted_series = new Series("BoostedSeries"); + auto *master_series = new Series("MasterSeries"); + auto *boosted_series = new Series("BoostedSeries"); for (int s = 0; s <= last_start; ++s) { if (s + 1 == stack_.size() && stack_[s]->type() == NT_SOFTMAX) { // Change the softmax to a tanh. diff --git a/src/textord/colfind.cpp b/src/textord/colfind.cpp index 0aae8eaa..b5160a1c 100644 --- a/src/textord/colfind.cpp +++ b/src/textord/colfind.cpp @@ -1530,8 +1530,8 @@ void ColumnFinder::RotateAndReskewBlocks(bool input_is_rtl, TO_BLOCK_LIST *block if (textord_debug_tabfind >= 2) tprintf("Block median size = (%d, %d)\n", block->median_size().x(), block->median_size().y()); } - - std::vector& tables = uniqueInstance>(); + + auto &tables = uniqueInstance>(); for(TessTable& mt: tables) mt.box.rotate_large(reskew_); } diff --git a/src/textord/colpartition.cpp b/src/textord/colpartition.cpp index 90b809da..994b8bad 100644 --- a/src/textord/colpartition.cpp +++ b/src/textord/colpartition.cpp @@ -110,7 +110,7 @@ ColPartition::ColPartition(BlobRegionType blob_type, const ICOORD &vertical) // Call DeleteBoxes before deleting the ColPartition. ColPartition *ColPartition::FakePartition(const TBOX &box, PolyBlockType block_type, BlobRegionType blob_type, BlobTextFlowType flow) { - ColPartition *part = new ColPartition(blob_type, ICOORD(0, 1)); + auto *part = new ColPartition(blob_type, ICOORD(0, 1)); part->set_type(block_type); part->set_flow(flow); part->AddBox(new BLOBNBOX(C_BLOB::FakeBlob(box))); @@ -129,7 +129,7 @@ ColPartition *ColPartition::FakePartition(const TBOX &box, PolyBlockType block_t // If the given list is not nullptr, the partition is also added to the list. ColPartition *ColPartition::MakeBigPartition(BLOBNBOX *box, ColPartition_LIST *big_part_list) { box->set_owner(nullptr); - ColPartition *single = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1)); + auto *single = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1)); single->set_flow(BTFT_NONE); single->AddBox(box); single->ComputeLimits(); diff --git a/src/textord/strokewidth.cpp b/src/textord/strokewidth.cpp index 5abb79c9..38fdd238 100644 --- a/src/textord/strokewidth.cpp +++ b/src/textord/strokewidth.cpp @@ -460,7 +460,7 @@ void StrokeWidth::FindLeadersAndMarkNoise(TO_BLOCK *block, ColPartition_LIST *le if (bbox->neighbour(BND_RIGHT) == nullptr && bbox->neighbour(BND_LEFT) == nullptr) continue; // Put all the linked blobs into a ColPartition. - ColPartition *part = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1)); + auto *part = new ColPartition(BRT_UNKNOWN, ICOORD(0, 1)); BLOBNBOX *blob; for (blob = bbox; blob != nullptr && blob->flow() == BTFT_NONE; blob = blob->neighbour(BND_RIGHT)) @@ -1365,7 +1365,7 @@ void StrokeWidth::FindVerticalTextChains(ColPartitionGrid *part_grid) { if (bbox->owner() == nullptr && bbox->UniquelyVertical() && (blob = MutualUnusedVNeighbour(bbox, BND_ABOVE)) != nullptr) { // Put all the linked blobs into a ColPartition. - ColPartition *part = new ColPartition(BRT_VERT_TEXT, ICOORD(0, 1)); + auto *part = new ColPartition(BRT_VERT_TEXT, ICOORD(0, 1)); part->AddBox(bbox); while (blob != nullptr) { part->AddBox(blob); @@ -1407,7 +1407,7 @@ void StrokeWidth::FindHorizontalTextChains(ColPartitionGrid *part_grid) { if (bbox->owner() == nullptr && bbox->UniquelyHorizontal() && (blob = MutualUnusedHNeighbour(bbox, BND_RIGHT)) != nullptr) { // Put all the linked blobs into a ColPartition. - ColPartition *part = new ColPartition(BRT_TEXT, ICOORD(0, 1)); + auto *part = new ColPartition(BRT_TEXT, ICOORD(0, 1)); part->AddBox(bbox); while (blob != nullptr) { part->AddBox(blob); @@ -1786,7 +1786,7 @@ void StrokeWidth::MakePartitionsFromCellList(PageSegMode pageseg_mode, bool comb BLOBNBOX_C_IT cell_it(cell_list); if (combine) { BLOBNBOX *bbox = cell_it.extract(); - ColPartition *part = new ColPartition(bbox->region_type(), ICOORD(0, 1)); + auto *part = new ColPartition(bbox->region_type(), ICOORD(0, 1)); part->AddBox(bbox); part->set_flow(bbox->flow()); for (cell_it.forward(); !cell_it.empty(); cell_it.forward()) { @@ -1796,7 +1796,7 @@ void StrokeWidth::MakePartitionsFromCellList(PageSegMode pageseg_mode, bool comb } else { for (; !cell_it.empty(); cell_it.forward()) { BLOBNBOX *bbox = cell_it.extract(); - ColPartition *part = new ColPartition(bbox->region_type(), ICOORD(0, 1)); + auto *part = new ColPartition(bbox->region_type(), ICOORD(0, 1)); part->set_flow(bbox->flow()); part->AddBox(bbox); CompletePartition(pageseg_mode, part, part_grid); diff --git a/src/textord/tablefind.cpp b/src/textord/tablefind.cpp index ba017aaf..6b0028f9 100644 --- a/src/textord/tablefind.cpp +++ b/src/textord/tablefind.cpp @@ -2018,11 +2018,11 @@ void TableFinder::MakeTableBlocks(ColPartitionGrid *grid, ColPartitionSet **all_ } #endif // GRAPHICS_DISABLED - std::vector& tables = uniqueInstance>(); - tables.push_back(TessTable{table_box, table_structure->getRows(), - table_structure->getCols()}); - - delete table_structure; + auto &tables = uniqueInstance>(); + tables.push_back( + TessTable{table_box, table_structure->getRows(), table_structure->getCols()}); + + delete table_structure; } } } diff --git a/src/training/common/commandlineflags.cpp b/src/training/common/commandlineflags.cpp index fc46dda1..f79b13cc 100644 --- a/src/training/common/commandlineflags.cpp +++ b/src/training/common/commandlineflags.cpp @@ -22,7 +22,7 @@ static bool IntFlagExists(const char *flag_name, int32_t *value) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - IntParam *p = + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), GlobalParams()->int_params, empty); if (p == nullptr) return false; @@ -34,8 +34,8 @@ static bool DoubleFlagExists(const char *flag_name, double *value) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - DoubleParam *p = ParamUtils::FindParam(full_flag_name.c_str(), - GlobalParams()->double_params, empty); + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), + GlobalParams()->double_params, empty); if (p == nullptr) return false; *value = static_cast(*p); @@ -46,7 +46,7 @@ static bool BoolFlagExists(const char *flag_name, bool *value) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - BoolParam *p = + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), GlobalParams()->bool_params, empty); if (p == nullptr) return false; @@ -58,8 +58,8 @@ static bool StringFlagExists(const char *flag_name, const char **value) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - StringParam *p = ParamUtils::FindParam(full_flag_name.c_str(), - GlobalParams()->string_params, empty); + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), + GlobalParams()->string_params, empty); *value = (p != nullptr) ? p->c_str() : nullptr; return p != nullptr; } @@ -68,7 +68,7 @@ static void SetIntFlagValue(const char *flag_name, const int32_t new_val) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - IntParam *p = + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), GlobalParams()->int_params, empty); ASSERT_HOST(p != nullptr); p->set_value(new_val); @@ -78,8 +78,8 @@ static void SetDoubleFlagValue(const char *flag_name, const double new_val) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - DoubleParam *p = ParamUtils::FindParam(full_flag_name.c_str(), - GlobalParams()->double_params, empty); + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), + GlobalParams()->double_params, empty); ASSERT_HOST(p != nullptr); p->set_value(new_val); } @@ -88,7 +88,7 @@ static void SetBoolFlagValue(const char *flag_name, const bool new_val) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - BoolParam *p = + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), GlobalParams()->bool_params, empty); ASSERT_HOST(p != nullptr); p->set_value(new_val); @@ -98,8 +98,8 @@ static void SetStringFlagValue(const char *flag_name, const char *new_val) { std::string full_flag_name("FLAGS_"); full_flag_name += flag_name; std::vector empty; - StringParam *p = ParamUtils::FindParam(full_flag_name.c_str(), - GlobalParams()->string_params, empty); + auto *p = ParamUtils::FindParam(full_flag_name.c_str(), + GlobalParams()->string_params, empty); ASSERT_HOST(p != nullptr); p->set_value(std::string(new_val)); } diff --git a/src/training/common/commontraining.cpp b/src/training/common/commontraining.cpp index 644e8e42..503b1532 100644 --- a/src/training/common/commontraining.cpp +++ b/src/training/common/commontraining.cpp @@ -518,7 +518,7 @@ void MergeInsignificantProtos(LIST ProtoList, const char *label, CLUSTERER *Clus // Find the nearest alive prototype. LIST list_it = ProtoList; iterate(list_it) { - PROTOTYPE *test_p = reinterpret_cast first_node(list_it); + auto *test_p = reinterpret_cast first_node(list_it); if (test_p != Prototype && !test_p->Merged) { float dist = ComputeDistance(Clusterer->SampleSize, Clusterer->ParamDesc, Prototype->Mean, test_p->Mean); @@ -691,7 +691,7 @@ CLASS_STRUCT *SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassL // printf("Float2Int ...\n"); - CLASS_STRUCT *float_classes = new CLASS_STRUCT[unicharset.size()]; + auto *float_classes = new CLASS_STRUCT[unicharset.size()]; iterate(LabeledClassList) { UnicityTable font_set; MergeClass = reinterpret_cast first_node(LabeledClassList); @@ -782,7 +782,7 @@ void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName) { int NumberOfProtos(LIST ProtoList, bool CountSigProtos, bool CountInsigProtos) { int N = 0; iterate(ProtoList) { - PROTOTYPE *Proto = reinterpret_cast first_node(ProtoList); + auto *Proto = reinterpret_cast first_node(ProtoList); if ((Proto->Significant && CountSigProtos) || (!Proto->Significant && CountInsigProtos)) N++; } diff --git a/src/training/common/networkbuilder.cpp b/src/training/common/networkbuilder.cpp index f51e4d1a..0c5486ca 100644 --- a/src/training/common/networkbuilder.cpp +++ b/src/training/common/networkbuilder.cpp @@ -132,7 +132,7 @@ Network *NetworkBuilder::ParseInput(const char **str) { return nullptr; } *str += length; - Input *input = new Input("Input", shape); + auto *input = new Input("Input", shape); // We want to allow [rest of net... or [rest of net... so we // have to check explicitly for '[' here. SkipWhitespace(str); @@ -145,7 +145,7 @@ Network *NetworkBuilder::ParseInput(const char **str) { Network *NetworkBuilder::ParseSeries(const StaticShape &input_shape, Input *input_layer, const char **str) { StaticShape shape = input_shape; - Series *series = new Series("Series"); + auto *series = new Series("Series"); ++*str; if (input_layer != nullptr) { series->AddToStack(input_layer); @@ -167,7 +167,7 @@ Network *NetworkBuilder::ParseSeries(const StaticShape &input_shape, Input *inpu // Parses a parallel set of networks, defined by (...). Network *NetworkBuilder::ParseParallel(const StaticShape &input_shape, const char **str) { - Parallel *parallel = new Parallel("Parallel", NT_PARALLEL); + auto *parallel = new Parallel("Parallel", NT_PARALLEL); ++*str; Network *network = nullptr; while (**str != '\0' && **str != ')' && @@ -204,7 +204,7 @@ Network *NetworkBuilder::ParseR(const StaticShape &input_shape, const char **str tprintf("Invalid R spec!:%s\n", end); return nullptr; } - Parallel *parallel = new Parallel("Replicated", NT_REPLICATED); + auto *parallel = new Parallel("Replicated", NT_REPLICATED); const char *str_copy = *str; for (int i = 0; i < replicas; ++i) { str_copy = *str; @@ -284,8 +284,8 @@ Network *NetworkBuilder::ParseC(const StaticShape &input_shape, const char **str // be slid over all batch,y,x. return new FullyConnected("Conv1x1", input_shape.depth(), d, type); } - Series *series = new Series("ConvSeries"); - Convolve *convolve = new Convolve("Convolve", input_shape.depth(), x / 2, y / 2); + auto *series = new Series("ConvSeries"); + auto *convolve = new Convolve("Convolve", input_shape.depth(), x / 2, y / 2); series->AddToStack(convolve); StaticShape fc_input = convolve->OutputShape(input_shape); series->AddToStack(new FullyConnected("ConvNL", fc_input.depth(), d, type)); @@ -358,13 +358,13 @@ Network *NetworkBuilder::ParseLSTM(const StaticShape &input_shape, const char ** std::string name(spec_start, *str - spec_start); lstm = new LSTM(name, input_shape.depth(), num_states, num_outputs, false, type); if (dir != 'f') { - Reversed *rev = new Reversed("RevLSTM", NT_XREVERSED); + auto *rev = new Reversed("RevLSTM", NT_XREVERSED); rev->SetNetwork(lstm); lstm = rev; } if (dir == 'b') { name += "LTR"; - Parallel *parallel = new Parallel("BidiLSTM", NT_PAR_RL_LSTM); + auto *parallel = new Parallel("BidiLSTM", NT_PAR_RL_LSTM); parallel->AddToStack( new LSTM(name, input_shape.depth(), num_states, num_outputs, false, type)); parallel->AddToStack(lstm); @@ -372,7 +372,7 @@ Network *NetworkBuilder::ParseLSTM(const StaticShape &input_shape, const char ** } } if (dim == 'y') { - Reversed *rev = new Reversed("XYTransLSTM", NT_XYTRANSPOSE); + auto *rev = new Reversed("XYTransLSTM", NT_XYTRANSPOSE); rev->SetNetwork(lstm); lstm = rev; } @@ -381,14 +381,14 @@ Network *NetworkBuilder::ParseLSTM(const StaticShape &input_shape, const char ** // Builds a set of 4 lstms with x and y reversal, running in true parallel. Network *NetworkBuilder::BuildLSTMXYQuad(int num_inputs, int num_states) { - Parallel *parallel = new Parallel("2DLSTMQuad", NT_PAR_2D_LSTM); + auto *parallel = new Parallel("2DLSTMQuad", NT_PAR_2D_LSTM); parallel->AddToStack(new LSTM("L2DLTRDown", num_inputs, num_states, num_states, true, NT_LSTM)); - Reversed *rev = new Reversed("L2DLTRXRev", NT_XREVERSED); + auto *rev = new Reversed("L2DLTRXRev", NT_XREVERSED); rev->SetNetwork(new LSTM("L2DRTLDown", num_inputs, num_states, num_states, true, NT_LSTM)); parallel->AddToStack(rev); rev = new Reversed("L2DRTLYRev", NT_YREVERSED); rev->SetNetwork(new LSTM("L2DRTLUp", num_inputs, num_states, num_states, true, NT_LSTM)); - Reversed *rev2 = new Reversed("L2DXRevU", NT_XREVERSED); + auto *rev2 = new Reversed("L2DXRevU", NT_XREVERSED); rev2->SetNetwork(rev); parallel->AddToStack(rev2); rev = new Reversed("L2DXRevY", NT_YREVERSED); @@ -409,7 +409,7 @@ static Network *BuildFullyConnected(const StaticShape &input_shape, NetworkType int input_depth = input_size * input_shape.depth(); Network *fc = new FullyConnected(name, input_depth, depth, type); if (input_size > 1) { - Series *series = new Series("FCSeries"); + auto *series = new Series("FCSeries"); series->AddToStack( new Reconfig("FCReconfig", input_shape.depth(), input_shape.width(), input_shape.height())); series->AddToStack(fc); @@ -478,7 +478,7 @@ Network *NetworkBuilder::ParseOutput(const StaticShape &input_shape, const char int input_depth = input_size * input_shape.depth(); Network *fc = new FullyConnected("Output", input_depth, depth, type); if (input_size > 1) { - Series *series = new Series("FCSeries"); + auto *series = new Series("FCSeries"); series->AddToStack(new Reconfig("FCReconfig", input_shape.depth(), 1, input_shape.height())); series->AddToStack(fc); fc = series; diff --git a/src/training/mftraining.cpp b/src/training/mftraining.cpp index 5a45797d..0744cd06 100644 --- a/src/training/mftraining.cpp +++ b/src/training/mftraining.cpp @@ -58,7 +58,7 @@ static void DisplayProtoList(const char *ch, LIST protolist) { auto window = std::make_unique("Char samples", 50, 200, 520, 520, 260, 260, true); LIST proto = protolist; iterate(proto) { - PROTOTYPE *prototype = reinterpret_cast(first_node(proto)); + auto *prototype = reinterpret_cast(first_node(proto)); if (prototype->Significant) window->Pen(ScrollView::GREEN); else if (prototype->NumSamples == 0) @@ -70,8 +70,8 @@ static void DisplayProtoList(const char *ch, LIST protolist) { float x = CenterX(prototype->Mean); float y = CenterY(prototype->Mean); double angle = OrientationOf(prototype->Mean) * 2 * M_PI; - float dx = static_cast(LengthOf(prototype->Mean) * cos(angle) / 2); - float dy = static_cast(LengthOf(prototype->Mean) * sin(angle) / 2); + auto dx = static_cast(LengthOf(prototype->Mean) * cos(angle) / 2); + auto dy = static_cast(LengthOf(prototype->Mean) * sin(angle) / 2); window->SetCursor((x - dx) * 256, (y - dy) * 256); window->DrawTo((x + dx) * 256, (y + dy) * 256); if (prototype->Significant) @@ -113,7 +113,7 @@ static LIST ClusterOneConfig(int shape_id, const char *class_label, LIST mf_clas merge_class->Class->font_set.push_back(shape_id); LIST proto_it = proto_list; iterate(proto_it) { - PROTOTYPE *prototype = reinterpret_cast(first_node(proto_it)); + auto *prototype = reinterpret_cast(first_node(proto_it)); // See if proto can be approximated by existing proto. int p_id = FindClosestExistingProto(merge_class->Class, merge_class->NumMerged, prototype); if (p_id == NO_PROTO) { diff --git a/src/training/pango/boxchar.cpp b/src/training/pango/boxchar.cpp index 193b0e85..ff14252e 100644 --- a/src/training/pango/boxchar.cpp +++ b/src/training/pango/boxchar.cpp @@ -154,7 +154,7 @@ void BoxChar::InsertNewlines(bool rtl_rules, bool vertical_rules, std::vectorAddBox(x, y, width, height); new_box->page_ = (*boxes)[i]->page_; boxes->insert(boxes->begin() + i, new_box); diff --git a/src/training/pango/ligature_table.cpp b/src/training/pango/ligature_table.cpp index 6c98075e..05f3d178 100644 --- a/src/training/pango/ligature_table.cpp +++ b/src/training/pango/ligature_table.cpp @@ -114,7 +114,7 @@ std::string LigatureTable::RemoveLigatures(const std::string &str) const { for (UNICHAR::const_iterator it = it_begin; it != it_end; ++it) { len = it.get_utf8(tmp); tmp[len] = '\0'; - LigHash::const_iterator lig_it = lig_to_norm_table_.find(tmp); + auto lig_it = lig_to_norm_table_.find(tmp); if (lig_it != lig_to_norm_table_.end()) { result += lig_it->second; } else { @@ -159,7 +159,7 @@ std::string LigatureTable::AddLigatures(const std::string &str, const PangoFontI for (int liglen = max_norm_length_; liglen >= min_norm_length_; --liglen) { if (i + liglen <= len) { std::string lig_cand = str.substr(i, liglen); - LigHash::const_iterator it = norm_to_lig_table_.find(lig_cand); + auto it = norm_to_lig_table_.find(lig_cand); if (it != norm_to_lig_table_.end()) { tlog(3, "Considering %s -> %s\n", lig_cand.c_str(), it->second.c_str()); if (font) { diff --git a/src/training/pango/stringrenderer.cpp b/src/training/pango/stringrenderer.cpp index e3dfa5ed..ba7092ab 100644 --- a/src/training/pango/stringrenderer.cpp +++ b/src/training/pango/stringrenderer.cpp @@ -388,8 +388,7 @@ bool StringRenderer::GetClusterStrings(std::vector *cluster_text) { pango_layout_iter_free(run_iter); cluster_text->clear(); - for (std::map::const_iterator it = start_byte_to_text.begin(); - it != start_byte_to_text.end(); ++it) { + for (auto it = start_byte_to_text.begin(); it != start_byte_to_text.end(); ++it) { cluster_text->push_back(it->second); } return !cluster_text->empty(); @@ -495,7 +494,7 @@ void StringRenderer::ComputeClusterBoxes() { if (!cluster_rect.width || !cluster_rect.height || IsUTF8Whitespace(cluster_text.c_str())) { tlog(2, "Skipping whitespace with boxdim (%d,%d) '%s'\n", cluster_rect.width, cluster_rect.height, cluster_text.c_str()); - BoxChar *boxchar = new BoxChar(" ", 1); + auto *boxchar = new BoxChar(" ", 1); boxchar->set_page(page_); start_byte_to_box[start_byte_index] = boxchar; continue; @@ -519,7 +518,7 @@ void StringRenderer::ComputeClusterBoxes() { // decided to use an unmapped glyph. cluster_text = LigatureTable::Get()->AddLigatures(cluster_text, nullptr); } - BoxChar *boxchar = new BoxChar(cluster_text.c_str(), cluster_text.size()); + auto *boxchar = new BoxChar(cluster_text.c_str(), cluster_text.size()); boxchar->set_page(page_); boxchar->AddBox(cluster_rect.x, cluster_rect.y, cluster_rect.width, cluster_rect.height); start_byte_to_box[start_byte_index] = boxchar; @@ -536,8 +535,7 @@ void StringRenderer::ComputeClusterBoxes() { if (GetClusterStrings(&cluster_text)) { ASSERT_HOST(cluster_text.size() == start_byte_to_box.size()); int ind = 0; - for (std::map::iterator it = start_byte_to_box.begin(); - it != start_byte_to_box.end(); ++it, ++ind) { + for (auto it = start_byte_to_box.begin(); it != start_byte_to_box.end(); ++it, ++ind) { it->second->mutable_ch()->swap(cluster_text[ind]); } } @@ -546,8 +544,7 @@ void StringRenderer::ComputeClusterBoxes() { std::vector page_boxchars; page_boxchars.reserve(start_byte_to_box.size()); std::string last_ch; - for (std::map::const_iterator it = start_byte_to_box.begin(); - it != start_byte_to_box.end(); ++it) { + for (auto it = start_byte_to_box.begin(); it != start_byte_to_box.end(); ++it) { if (it->second->ch() == kWordJoinerUTF8) { // Skip zero-width joiner characters (ZWJs) here. delete it->second; diff --git a/src/training/unicharset/validator.cpp b/src/training/unicharset/validator.cpp index 34600d93..1b8a9afb 100644 --- a/src/training/unicharset/validator.cpp +++ b/src/training/unicharset/validator.cpp @@ -148,7 +148,7 @@ ViramaScript Validator::MostFrequentViramaScript(const std::vector &utf3 } if (!histogram.empty()) { int base = std::max_element(histogram.begin(), histogram.end(), CmpPairSecond)->first; - char32 codebase = static_cast(base * kIndicCodePageSize); + auto codebase = static_cast(base * kIndicCodePageSize); // Check for validity. if (codebase == static_cast(ViramaScript::kMyanmar) || codebase == static_cast(ViramaScript::kJavanese) || From 6fc31c44f80cb36a90bac4e7406e85eb4ec9566a Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:35:24 +0100 Subject: [PATCH 6/9] Modernize code (clang-tidy -checks='-*,modernize-use-bool-literals') Signed-off-by: Stefan Weil --- src/ccmain/equationdetect.cpp | 4 ++-- src/ccmain/recogtraining.cpp | 4 ++-- src/ccmain/tessbox.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ccmain/equationdetect.cpp b/src/ccmain/equationdetect.cpp index d34ecad3..988cf023 100644 --- a/src/ccmain/equationdetect.cpp +++ b/src/ccmain/equationdetect.cpp @@ -248,8 +248,8 @@ BlobSpecialTextType EquationDetect::EstimateTypeForUnichar(const UNICHARSET &uni void EquationDetect::IdentifySpecialText() { // Set configuration for Tesseract::AdaptiveClassifier. - equ_tesseract_.tess_cn_matching.set_value(1); // turn it on - equ_tesseract_.tess_bn_matching.set_value(0); + equ_tesseract_.tess_cn_matching.set_value(true); // turn it on + equ_tesseract_.tess_bn_matching.set_value(false); // Set the multiplier to zero for lang_tesseract_ to improve the accuracy. const int classify_class_pruner = lang_tesseract_->classify_class_pruner_multiplier; diff --git a/src/ccmain/recogtraining.cpp b/src/ccmain/recogtraining.cpp index 093f0528..f5527d1c 100644 --- a/src/ccmain/recogtraining.cpp +++ b/src/ccmain/recogtraining.cpp @@ -36,9 +36,9 @@ const int16_t kMaxBoxEdgeDiff = 2; FILE *Tesseract::init_recog_training(const char *filename) { if (tessedit_ambigs_training) { tessedit_tess_adaption_mode.set_value(0); // turn off adaption - tessedit_enable_doc_dict.set_value(0); // turn off document dictionary + tessedit_enable_doc_dict.set_value(false); // turn off document dictionary // Explore all segmentations. - getDict().stopper_no_acceptable_choices.set_value(1); + getDict().stopper_no_acceptable_choices.set_value(true); } std::string output_fname = filename; diff --git a/src/ccmain/tessbox.cpp b/src/ccmain/tessbox.cpp index a80058bd..ce13ef18 100644 --- a/src/ccmain/tessbox.cpp +++ b/src/ccmain/tessbox.cpp @@ -36,8 +36,8 @@ void Tesseract::tess_segment_pass_n(int pass_n, WERD_RES *word) { if (word->word->flag(W_DONT_CHOP)) { saved_enable_assoc = wordrec_enable_assoc; saved_chop_enable = chop_enable; - wordrec_enable_assoc.set_value(0); - chop_enable.set_value(0); + wordrec_enable_assoc.set_value(false); + chop_enable.set_value(false); } if (pass_n == 1) set_pass1(); From 27293fad62d0a4674b98d41ad0f1e79df4571efe Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:42:20 +0100 Subject: [PATCH 7/9] Modernize code (clang-tidy -checks='-*,modernize-use-emplace') Signed-off-by: Stefan Weil --- src/api/capi.cpp | 4 ++-- src/ccmain/applybox.cpp | 2 +- src/ccmain/par_control.cpp | 2 +- src/ccmain/paragraphs.cpp | 14 +++++++------- src/ccstruct/detlinefit.cpp | 8 ++++---- src/ccstruct/imagedata.cpp | 2 +- src/ccstruct/pageres.cpp | 4 ++-- src/ccstruct/params_training_featdef.h | 2 +- src/classify/adaptmatch.cpp | 2 +- src/classify/intmatcher.cpp | 2 +- src/classify/shapetable.cpp | 6 +++--- src/dict/trie.cpp | 3 +-- src/lstm/recodebeam.cpp | 2 +- src/training/ambiguous_words.cpp | 4 ++-- src/training/common/mastertrainer.cpp | 2 +- src/training/pango/pango_font_info.cpp | 2 +- src/training/text2image.cpp | 2 +- src/training/unicharset/validator.cpp | 2 +- 18 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/api/capi.cpp b/src/api/capi.cpp index 5455c0e3..12459a3a 100644 --- a/src/api/capi.cpp +++ b/src/api/capi.cpp @@ -205,8 +205,8 @@ int TessBaseAPIInit4(TessBaseAPI *handle, const char *datapath, const char *lang std::vector varValues; if (vars_vec != nullptr && vars_values != nullptr) { for (size_t i = 0; i < vars_vec_size; i++) { - varNames.push_back(vars_vec[i]); - varValues.push_back(vars_values[i]); + varNames.emplace_back(vars_vec[i]); + varValues.emplace_back(vars_values[i]); } } diff --git a/src/ccmain/applybox.cpp b/src/ccmain/applybox.cpp index 1d7db24a..63277877 100644 --- a/src/ccmain/applybox.cpp +++ b/src/ccmain/applybox.cpp @@ -578,7 +578,7 @@ bool Tesseract::FindSegmentation(const std::vector &target_text, WER } word_res->correct_text.clear(); for (auto &text : target_text) { - word_res->correct_text.push_back(unicharset.id_to_unichar(text)); + word_res->correct_text.emplace_back(unicharset.id_to_unichar(text)); } return true; } diff --git a/src/ccmain/par_control.cpp b/src/ccmain/par_control.cpp index 1dcd23e4..ed0d032b 100644 --- a/src/ccmain/par_control.cpp +++ b/src/ccmain/par_control.cpp @@ -44,7 +44,7 @@ void Tesseract::PrerecAllWordsPar(const std::vector &words) { Tesseract *sub = s < sub_langs_.size() ? sub_langs_[s] : this; const WERD_RES &word = *w.lang_words[s]; for (int b = 0; b < word.chopped_word->NumBlobs(); ++b) { - blobs.push_back(BlobData(b, sub, word)); + blobs.emplace_back(b, sub, word); } } } diff --git a/src/ccmain/paragraphs.cpp b/src/ccmain/paragraphs.cpp index 3772498f..b9ebafa7 100644 --- a/src/ccmain/paragraphs.cpp +++ b/src/ccmain/paragraphs.cpp @@ -135,7 +135,7 @@ static std::string RtlEmbed(const std::string &word, bool rtlify) { static void PrintDetectorState(const ParagraphTheory &theory, const std::vector &rows) { std::vector> output; - output.push_back(std::vector()); + output.emplace_back(); output.back().push_back("#row"); output.back().push_back("space"); output.back().push_back(".."); @@ -145,12 +145,12 @@ static void PrintDetectorState(const ParagraphTheory &theory, output.back().push_back("text"); for (int i = 0; i < rows.size(); i++) { - output.push_back(std::vector()); + output.emplace_back(); std::vector &row = output.back(); const RowInfo &ri = *rows[i].ri_; row.push_back(std::to_string(i)); row.push_back(std::to_string(ri.average_interword_space)); - row.push_back(ri.has_leaders ? ".." : " "); + row.emplace_back(ri.has_leaders ? ".." : " "); row.push_back(RtlEmbed(ri.lword_text, !ri.ltr) + "[" + std::to_string(ri.lword_box.width()) + (ri.lword_likely_starts_idea ? "S" : "s") + (ri.lword_likely_ends_idea ? "E" : "e") + @@ -491,15 +491,15 @@ void RightWordAttributes(const UNICHARSET *unicharset, const WERD_CHOICE *werd, // =============== Implementation of RowScratchRegisters ===================== /* static */ void RowScratchRegisters::AppendDebugHeaderFields(std::vector &header) { - header.push_back("[lmarg,lind;rind,rmarg]"); - header.push_back("model"); + header.emplace_back("[lmarg,lind;rind,rmarg]"); + header.emplace_back("model"); } void RowScratchRegisters::AppendDebugInfo(const ParagraphTheory &theory, std::vector &dbg) const { char s[30]; snprintf(s, sizeof(s), "[%3d,%3d;%3d,%3d]", lmargin_, lindent_, rindent_, rmargin_); - dbg.push_back(s); + dbg.emplace_back(s); std::string model_string; model_string += static_cast(GetLineType()); model_string += ":"; @@ -2487,7 +2487,7 @@ void DetectParagraphs(int debug_level, bool after_text_recognition, if (!row.PageResIt()->row()) continue; // empty row. row.PageResIt()->row()->row->set_para(nullptr); - row_infos.push_back(RowInfo()); + row_infos.emplace_back(); RowInfo &ri = row_infos.back(); InitializeRowInfo(after_text_recognition, row, &ri); } while (!row.IsAtFinalElement(RIL_BLOCK, RIL_TEXTLINE) && row.Next(RIL_TEXTLINE)); diff --git a/src/ccstruct/detlinefit.cpp b/src/ccstruct/detlinefit.cpp index fc01f4b1..30b735a4 100644 --- a/src/ccstruct/detlinefit.cpp +++ b/src/ccstruct/detlinefit.cpp @@ -48,14 +48,14 @@ void DetLineFit::Clear() { // Add a new point. Takes a copy - the pt doesn't need to stay in scope. void DetLineFit::Add(const ICOORD &pt) { - pts_.push_back(PointWidth(pt, 0)); + pts_.emplace_back(pt, 0); } // Associates a half-width with the given point if a point overlaps the // previous point by more than half the width, and its distance is further // than the previous point, then the more distant point is ignored in the // distance calculation. Useful for ignoring i dots and other diacritics. void DetLineFit::Add(const ICOORD &pt, int halfwidth) { - pts_.push_back(PointWidth(pt, halfwidth)); + pts_.emplace_back(pt, halfwidth); } // Fits a line to the points, ignoring the skip_first initial points and the @@ -268,7 +268,7 @@ void DetLineFit::ComputeDistances(const ICOORD &start, const ICOORD &end) { separation < line_length * pts_[i - 1].halfwidth) continue; } - distances_.push_back(DistPointPair(dist, pts_[i].pt)); + distances_.emplace_back(dist, pts_[i].pt); prev_abs_dist = abs_dist; prev_dot = dot; } @@ -287,7 +287,7 @@ void DetLineFit::ComputeConstrainedDistances(const FCOORD &direction, double min // Compute |line_vector||pt_vector|sin(angle between) double dist = direction * pt_vector; if (min_dist <= dist && dist <= max_dist) - distances_.push_back(DistPointPair(dist, pt.pt)); + distances_.emplace_back(dist, pt.pt); } } diff --git a/src/ccstruct/imagedata.cpp b/src/ccstruct/imagedata.cpp index 58e48bb4..37a2069e 100644 --- a/src/ccstruct/imagedata.cpp +++ b/src/ccstruct/imagedata.cpp @@ -73,7 +73,7 @@ ImageData *ImageData::Build(const char *name, int page_number, const char *lang, } image_data->transcription_ = truth_text; // If we have no boxes, the transcription is in the 0th box_texts_. - image_data->box_texts_.push_back(truth_text); + image_data->box_texts_.emplace_back(truth_text); // We will create a box for the whole image on PreScale, to save unpacking // the image now. } else if (truth_text != nullptr && truth_text[0] != '\0' && diff --git a/src/ccstruct/pageres.cpp b/src/ccstruct/pageres.cpp index e654bd9b..65c784a5 100644 --- a/src/ccstruct/pageres.cpp +++ b/src/ccstruct/pageres.cpp @@ -832,7 +832,7 @@ void WERD_RES::CloneChoppedToRebuild() { correct_text.reserve(word_len); for (int i = 0; i < word_len; ++i) { best_state.push_back(1); - correct_text.push_back(std::string("")); + correct_text.emplace_back(""); } } @@ -917,7 +917,7 @@ void WERD_RES::BestChoiceToCorrectText() { for (int i = 0; i < best_choice->length(); ++i) { UNICHAR_ID choice_id = best_choice->unichar_id(i); const char *blob_choice = uch_set->id_to_unichar(choice_id); - correct_text.push_back(std::string(blob_choice)); + correct_text.emplace_back(blob_choice); } } diff --git a/src/ccstruct/params_training_featdef.h b/src/ccstruct/params_training_featdef.h index 18c9c726..505d2bb8 100644 --- a/src/ccstruct/params_training_featdef.h +++ b/src/ccstruct/params_training_featdef.h @@ -134,7 +134,7 @@ public: // Starts a new hypothesis list. // Should be called at the beginning of a new run of the segmentation search. void StartHypothesisList() { - hyp_list_vec.push_back(ParamsTrainingHypothesisList()); + hyp_list_vec.emplace_back(); } // Adds a new ParamsTrainingHypothesis to the current hypothesis list // and returns the reference to the newly added entry. diff --git a/src/classify/adaptmatch.cpp b/src/classify/adaptmatch.cpp index 36fc0f9f..6b12ebc9 100644 --- a/src/classify/adaptmatch.cpp +++ b/src/classify/adaptmatch.cpp @@ -1120,7 +1120,7 @@ void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS *classes, bool debug, mapped_results[r].fonts.clear(); } for (int font_id : shape[c].font_ids) { - mapped_results[r].fonts.push_back(ScoredFont(font_id, int_result->fonts[f].score)); + mapped_results[r].fonts.emplace_back(font_id, int_result->fonts[f].score); } } } diff --git a/src/classify/intmatcher.cpp b/src/classify/intmatcher.cpp index 2a3ce4c0..c0eb0f02 100644 --- a/src/classify/intmatcher.cpp +++ b/src/classify/intmatcher.cpp @@ -1096,7 +1096,7 @@ int IntegerMatcher::FindBestMatch(INT_CLASS class_template, const ScratchEvidenc result->config = c; best_match = rating; } - result->fonts.push_back(ScoredFont(c, rating)); + result->fonts.emplace_back(c, rating); } // Compute confidence on a Probability scale. diff --git a/src/classify/shapetable.cpp b/src/classify/shapetable.cpp index ed8566a1..51bcba09 100644 --- a/src/classify/shapetable.cpp +++ b/src/classify/shapetable.cpp @@ -112,7 +112,7 @@ void Shape::AddToShape(int unichar_id, int font_id) { } } // Unichar_id is not in shape, so add it to shape. - unichars_.push_back(UnicharAndFonts(unichar_id, font_id)); + unichars_.emplace_back(unichar_id, font_id); unichars_sorted_ = unichars_.size() <= 1; } @@ -684,8 +684,8 @@ void ShapeTable::AddShapeToResults(const ShapeRating &shape_rating, std::vector< int result_index = AddUnicharToResults(shape[u].unichar_id, shape_rating.rating, unichar_map, results); for (int font_id : shape[u].font_ids) { - (*results)[result_index].fonts.push_back( - ScoredFont(font_id, IntCastRounded(shape_rating.rating * INT16_MAX))); + (*results)[result_index].fonts.emplace_back(font_id, + IntCastRounded(shape_rating.rating * INT16_MAX)); } } } diff --git a/src/dict/trie.cpp b/src/dict/trie.cpp index cad1158d..35652c81 100644 --- a/src/dict/trie.cpp +++ b/src/dict/trie.cpp @@ -629,8 +629,7 @@ void Trie::sort_edges(EDGE_VECTOR *edges) { std::vector> sort_vec; sort_vec.reserve(num_edges); for (int i = 0; i < num_edges; ++i) { - sort_vec.push_back( - KDPairInc(unichar_id_from_edge_rec((*edges)[i]), (*edges)[i])); + sort_vec.emplace_back(unichar_id_from_edge_rec((*edges)[i]), (*edges)[i]); } std::sort(sort_vec.begin(), sort_vec.end()); for (int i = 0; i < num_edges; ++i) diff --git a/src/lstm/recodebeam.cpp b/src/lstm/recodebeam.cpp index 53a70f5f..6560bd30 100644 --- a/src/lstm/recodebeam.cpp +++ b/src/lstm/recodebeam.cpp @@ -459,7 +459,7 @@ void RecodeBeamSearch::extractSymbolChoices(const UNICHARSET *unicharset) { int id = unichar_ids[bestPos]; const char *result = unicharset->id_to_unichar_ext(id); float rating = ratings[bestPos]; - choice.push_back(std::pair(result, rating)); + choice.emplace_back(result, rating); ctc_choices.push_back(choice); } // fill the blank spot with an empty array diff --git a/src/training/ambiguous_words.cpp b/src/training/ambiguous_words.cpp index ce340506..909113eb 100644 --- a/src/training/ambiguous_words.cpp +++ b/src/training/ambiguous_words.cpp @@ -57,8 +57,8 @@ int main(int argc, char **argv) { tesseract::TessBaseAPI api; std::vector vars_vec; std::vector vars_values; - vars_vec.push_back("output_ambig_words_file"); - vars_values.push_back(output_file_str); + vars_vec.emplace_back("output_ambig_words_file"); + vars_values.emplace_back(output_file_str); api.Init(tessdata_dir, lang.c_str(), tesseract::OEM_TESSERACT_ONLY, nullptr, 0, &vars_vec, &vars_values, false); tesseract::Dict &dict = api.tesseract()->getDict(); diff --git a/src/training/common/mastertrainer.cpp b/src/training/common/mastertrainer.cpp index bb563ad6..afa39570 100644 --- a/src/training/common/mastertrainer.cpp +++ b/src/training/common/mastertrainer.cpp @@ -134,7 +134,7 @@ void MasterTrainer::ReadTrainingSamples(const char *page_name, tprintf("Failed to open tr file: %s\n", page_name); return; } - tr_filenames_.push_back(std::string(page_name)); + tr_filenames_.emplace_back(page_name); while (fgets(buffer, sizeof(buffer), fp) != nullptr) { if (buffer[0] == '\n') continue; diff --git a/src/training/pango/pango_font_info.cpp b/src/training/pango/pango_font_info.cpp index b4ba8c3e..77f7e9f7 100644 --- a/src/training/pango/pango_font_info.cpp +++ b/src/training/pango/pango_font_info.cpp @@ -552,7 +552,7 @@ const std::vector &FontUtils::ListAvailableFonts() { char *desc_str = pango_font_description_to_string(desc); // "synthesized" font faces that are not truly loadable, so we skip it if (!pango_font_face_is_synthesized(faces[j]) && IsAvailableFont(desc_str)) { - available_fonts_.push_back(desc_str); + available_fonts_.emplace_back(desc_str); } pango_font_description_free(desc); g_free(desc_str); diff --git a/src/training/text2image.cpp b/src/training/text2image.cpp index b43981f4..ed9ce33e 100644 --- a/src/training/text2image.cpp +++ b/src/training/text2image.cpp @@ -561,7 +561,7 @@ static int Main() { int offset = SpanUTF8Whitespace(str8); while (offset < len) { step = SpanUTF8NotWhitespace(str8 + offset); - offsets.push_back(std::make_pair(offset, step)); + offsets.emplace_back(offset, step); offset += step; offset += SpanUTF8Whitespace(str8 + offset); } diff --git a/src/training/unicharset/validator.cpp b/src/training/unicharset/validator.cpp index 1b8a9afb..35075b2c 100644 --- a/src/training/unicharset/validator.cpp +++ b/src/training/unicharset/validator.cpp @@ -187,7 +187,7 @@ bool Validator::IsSubscriptScript() const { void Validator::ComputeClassCodes(const std::vector &text) { codes_.reserve(text.size()); for (char32 c : text) { - codes_.push_back(std::make_pair(UnicodeToCharClass(c), c)); + codes_.emplace_back(UnicodeToCharClass(c), c); } } From 406233f1aec916832ab43bf6cca04a7d31aa25cc Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:43:23 +0100 Subject: [PATCH 8/9] Modernize code (clang-tidy -checks='-*,modernize-use-equals-default') Signed-off-by: Stefan Weil --- src/ccmain/paragraphs_internal.h | 8 ++------ src/ccstruct/fontinfo.cpp | 2 +- src/ccstruct/matrix.h | 2 +- src/ccstruct/ratngs.cpp | 3 +-- src/ccstruct/rejctmap.h | 8 ++------ src/classify/kdtree.cpp | 2 +- src/lstm/networkscratch.h | 3 +-- src/textord/cjkpitch.cpp | 6 +++--- src/textord/tabfind.cpp | 2 +- src/textord/tablerecog.cpp | 2 +- src/training/unicharset/icuerrorcode.h | 2 +- src/training/unicharset/validate_grapheme.h | 2 +- src/training/unicharset/validate_indic.h | 2 +- src/training/unicharset/validate_javanese.h | 2 +- src/training/unicharset/validate_khmer.h | 2 +- src/training/unicharset/validate_myanmar.h | 2 +- src/viewer/svmnode.cpp | 2 +- src/wordrec/lm_pain_points.h | 2 +- src/wordrec/lm_state.h | 3 +-- 19 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/ccmain/paragraphs_internal.h b/src/ccmain/paragraphs_internal.h index 3c6c0162..516fd553 100644 --- a/src/ccmain/paragraphs_internal.h +++ b/src/ccmain/paragraphs_internal.h @@ -76,14 +76,10 @@ inline bool StrongModel(const ParagraphModel *model) { struct LineHypothesis { LineHypothesis() : ty(LT_UNKNOWN), model(nullptr) {} LineHypothesis(LineType line_type, const ParagraphModel *m) : ty(line_type), model(m) {} - LineHypothesis(const LineHypothesis &other) : ty(other.ty), model(other.model) {} + LineHypothesis(const LineHypothesis &other) = default; // Copy assignment operator. - LineHypothesis &operator=(const LineHypothesis &other) { - ty = other.ty; - model = other.model; - return *this; - } + LineHypothesis &operator=(const LineHypothesis &other) = default; bool operator==(const LineHypothesis &other) const { return ty == other.ty && model == other.model; diff --git a/src/ccstruct/fontinfo.cpp b/src/ccstruct/fontinfo.cpp index a6a32557..8224a4fe 100644 --- a/src/ccstruct/fontinfo.cpp +++ b/src/ccstruct/fontinfo.cpp @@ -45,7 +45,7 @@ FontInfoTable::FontInfoTable() { set_clear_callback(std::bind(FontInfoDeleteCallback, _1)); } -FontInfoTable::~FontInfoTable() {} +FontInfoTable::~FontInfoTable() = default; // Writes to the given file. Returns false in case of error. bool FontInfoTable::Serialize(FILE *fp) const { diff --git a/src/ccstruct/matrix.h b/src/ccstruct/matrix.h index 18b32ec6..99b89502 100644 --- a/src/ccstruct/matrix.h +++ b/src/ccstruct/matrix.h @@ -652,7 +652,7 @@ struct MATRIX_COORD { // Default constructor required by GenericHeap. MATRIX_COORD() : col(0), row(0) {} MATRIX_COORD(int c, int r) : col(c), row(r) {} - ~MATRIX_COORD() {} + ~MATRIX_COORD() = default; bool Valid(const MATRIX &m) const { return 0 <= col && col < m.dimension() && col <= row && row < col + m.bandwidth() && diff --git a/src/ccstruct/ratngs.cpp b/src/ccstruct/ratngs.cpp index d36b6598..92d6ad51 100644 --- a/src/ccstruct/ratngs.cpp +++ b/src/ccstruct/ratngs.cpp @@ -264,8 +264,7 @@ void WERD_CHOICE::init(const char *src_string, const char *src_lengths, float sr /** * WERD_CHOICE::~WERD_CHOICE */ -WERD_CHOICE::~WERD_CHOICE() { -} +WERD_CHOICE::~WERD_CHOICE() = default; const char *WERD_CHOICE::permuter_name() const { return kPermuterTypeNames[permuter_]; diff --git a/src/ccstruct/rejctmap.h b/src/ccstruct/rejctmap.h index 80187bdf..47f9af71 100644 --- a/src/ccstruct/rejctmap.h +++ b/src/ccstruct/rejctmap.h @@ -124,12 +124,8 @@ public: flags2 = source.flags2; } - REJ &operator=( // assign REJ - const REJ &source) { // from this - flags1 = source.flags1; - flags2 = source.flags2; - return *this; - } + REJ &operator=( // assign REJ + const REJ &source) = default; bool flag(REJ_FLAGS rej_flag) { if (rej_flag < 16) diff --git a/src/classify/kdtree.cpp b/src/classify/kdtree.cpp index cddcf591..238801de 100644 --- a/src/classify/kdtree.cpp +++ b/src/classify/kdtree.cpp @@ -55,7 +55,7 @@ public: ~MinK(); struct Element { - Element() {} + Element() = default; Element(const Key &k, const Value &v) : key(k), value(v) {} Key key; diff --git a/src/lstm/networkscratch.h b/src/lstm/networkscratch.h index fd305e27..66f321d2 100644 --- a/src/lstm/networkscratch.h +++ b/src/lstm/networkscratch.h @@ -214,8 +214,7 @@ public: template class Stack { public: - Stack() { - } + Stack() = default; ~Stack() { for (auto data : stack_) { diff --git a/src/textord/cjkpitch.cpp b/src/textord/cjkpitch.cpp index 4f284b24..c62ed5f3 100644 --- a/src/textord/cjkpitch.cpp +++ b/src/textord/cjkpitch.cpp @@ -105,7 +105,7 @@ public: }; LocalCorrelation() : finalized_(false) {} - ~LocalCorrelation() {} + ~LocalCorrelation() = default; void Finish() { std::sort(values_.begin(), values_.end(), float_pair_compare); @@ -275,7 +275,7 @@ class FPRow { public: FPRow() : all_pitches_(), all_gaps_(), good_pitches_(), good_gaps_(), heights_(), characters_() {} - ~FPRow() {} + ~FPRow() = default; // Initialize from TD_ROW. void Init(TO_ROW *row); @@ -916,7 +916,7 @@ void FPRow::FinalizeLargeChars() { class FPAnalyzer { public: FPAnalyzer(ICOORD page_tr, TO_BLOCK_LIST *port_blocks); - ~FPAnalyzer() {} + ~FPAnalyzer() = default; void Pass1Analyze() { for (auto &row : rows_) diff --git a/src/textord/tabfind.cpp b/src/textord/tabfind.cpp index ed842051..1b1af703 100644 --- a/src/textord/tabfind.cpp +++ b/src/textord/tabfind.cpp @@ -75,7 +75,7 @@ TabFind::TabFind(int gridsize, const ICOORD &bleft, const ICOORD &tright, TabVec width_cb_ = std::bind(&TabFind::CommonWidth, this, _1); } -TabFind::~TabFind() {} +TabFind::~TabFind() = default; ///////////////// PUBLIC functions (mostly used by TabVector). ////////////// diff --git a/src/textord/tablerecog.cpp b/src/textord/tablerecog.cpp index 6ceb2e79..785a489a 100644 --- a/src/textord/tablerecog.cpp +++ b/src/textord/tablerecog.cpp @@ -723,7 +723,7 @@ TableRecognizer::TableRecognizer() , min_width_(0) , max_text_height_(INT32_MAX) {} -TableRecognizer::~TableRecognizer() {} +TableRecognizer::~TableRecognizer() = default; void TableRecognizer::Init() {} diff --git a/src/training/unicharset/icuerrorcode.h b/src/training/unicharset/icuerrorcode.h index c02c74be..bed3886d 100644 --- a/src/training/unicharset/icuerrorcode.h +++ b/src/training/unicharset/icuerrorcode.h @@ -44,7 +44,7 @@ namespace tesseract { class IcuErrorCode : public icu::ErrorCode { public: - IcuErrorCode() {} + IcuErrorCode() = default; ~IcuErrorCode() override; protected: diff --git a/src/training/unicharset/validate_grapheme.h b/src/training/unicharset/validate_grapheme.h index 6d8ddabc..3c4e4e69 100644 --- a/src/training/unicharset/validate_grapheme.h +++ b/src/training/unicharset/validate_grapheme.h @@ -10,7 +10,7 @@ namespace tesseract { class ValidateGrapheme : public Validator { public: ValidateGrapheme(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateGrapheme() override {} + ~ValidateGrapheme() override = default; protected: // Consumes the next Grapheme in codes_[codes_used_++...] and copies it to diff --git a/src/training/unicharset/validate_indic.h b/src/training/unicharset/validate_indic.h index 055ecf11..e80f8766 100644 --- a/src/training/unicharset/validate_indic.h +++ b/src/training/unicharset/validate_indic.h @@ -10,7 +10,7 @@ namespace tesseract { class ValidateIndic : public Validator { public: ValidateIndic(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateIndic() override {} + ~ValidateIndic() override = default; protected: // Returns whether codes matches the pattern for an Indic Grapheme. diff --git a/src/training/unicharset/validate_javanese.h b/src/training/unicharset/validate_javanese.h index c2fcd53d..b8a046f1 100644 --- a/src/training/unicharset/validate_javanese.h +++ b/src/training/unicharset/validate_javanese.h @@ -28,7 +28,7 @@ namespace tesseract { class ValidateJavanese : public Validator { public: ValidateJavanese(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateJavanese() override {} + ~ValidateJavanese() override = default; protected: // Returns whether codes matches the pattern for an Javanese Grapheme. diff --git a/src/training/unicharset/validate_khmer.h b/src/training/unicharset/validate_khmer.h index e921be38..a0eb331a 100644 --- a/src/training/unicharset/validate_khmer.h +++ b/src/training/unicharset/validate_khmer.h @@ -9,7 +9,7 @@ namespace tesseract { class ValidateKhmer : public Validator { public: ValidateKhmer(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateKhmer() override {} + ~ValidateKhmer() override = default; protected: // Returns whether codes matches the pattern for an Khmer Grapheme. diff --git a/src/training/unicharset/validate_myanmar.h b/src/training/unicharset/validate_myanmar.h index 662728e4..48b169f4 100644 --- a/src/training/unicharset/validate_myanmar.h +++ b/src/training/unicharset/validate_myanmar.h @@ -9,7 +9,7 @@ namespace tesseract { class ValidateMyanmar : public Validator { public: ValidateMyanmar(ViramaScript script, bool report_errors) : Validator(script, report_errors) {} - ~ValidateMyanmar() override {} + ~ValidateMyanmar() override = default; protected: // Returns whether codes matches the pattern for a Myanmar Grapheme. diff --git a/src/viewer/svmnode.cpp b/src/viewer/svmnode.cpp index 5857469c..d8c7c55f 100644 --- a/src/viewer/svmnode.cpp +++ b/src/viewer/svmnode.cpp @@ -52,7 +52,7 @@ SVMenuNode::SVMenuNode() { is_check_box_entry_ = false; } -SVMenuNode::~SVMenuNode() {} +SVMenuNode::~SVMenuNode() = default; // Create a new sub menu node with just a caption. This is used to create // nodes which act as parent nodes to other nodes (e.g. submenus). diff --git a/src/wordrec/lm_pain_points.h b/src/wordrec/lm_pain_points.h index 4071eeca..b989c2f7 100644 --- a/src/wordrec/lm_pain_points.h +++ b/src/wordrec/lm_pain_points.h @@ -70,7 +70,7 @@ public: , fixed_pitch_(fp) , dict_(d) , debug_level_(deb) {} - ~LMPainPoints() {} + ~LMPainPoints() = default; // Returns true if the heap of pain points of pp_type is not empty(). inline bool HasPainPoints(LMPainPointsType pp_type) const { diff --git a/src/wordrec/lm_state.h b/src/wordrec/lm_state.h index dca05acd..6fc0bee8 100644 --- a/src/wordrec/lm_state.h +++ b/src/wordrec/lm_state.h @@ -202,8 +202,7 @@ struct LanguageModelState { : viterbi_state_entries_prunable_length(0) , viterbi_state_entries_prunable_max_cost(FLT_MAX) , viterbi_state_entries_length(0) {} - ~LanguageModelState() { - } + ~LanguageModelState() = default; /// Clears the viterbi search state back to its initial conditions. void Clear(); From 5384aa7b218b110fef1ded14387749f7c906178d Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 21 Mar 2021 20:44:22 +0100 Subject: [PATCH 9/9] Modernize code (clang-tidy -checks='-*,modernize-use-equals-delete') Signed-off-by: Stefan Weil --- src/ccstruct/blamer.h | 2 +- src/ccstruct/ocrrow.h | 2 +- src/ccutil/elst2.h | 2 +- src/textord/fpchop.h | 2 +- src/training/pango/ligature_table.h | 4 ++-- src/training/pango/pango_font_info.h | 4 ++-- src/training/pango/stringrenderer.h | 4 ++-- src/training/unicharset/icuerrorcode.h | 4 ++-- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ccstruct/blamer.h b/src/ccstruct/blamer.h index 28d1d47f..3dd9e7a1 100644 --- a/src/ccstruct/blamer.h +++ b/src/ccstruct/blamer.h @@ -289,7 +289,7 @@ struct BlamerBundle { private: // Copy assignment operator (currently unused, therefore private). - BlamerBundle &operator=(const BlamerBundle &other); + BlamerBundle &operator=(const BlamerBundle &other) = delete; void SetBlame(IncorrectResultReason irr, const std::string &msg, const WERD_CHOICE *choice, bool debug) { incorrect_result_reason_ = irr; diff --git a/src/ccstruct/ocrrow.h b/src/ccstruct/ocrrow.h index 8f60b77b..dff96398 100644 --- a/src/ccstruct/ocrrow.h +++ b/src/ccstruct/ocrrow.h @@ -147,7 +147,7 @@ public: private: // Copy constructor (currently unused, therefore private). - ROW(const ROW &source); + ROW(const ROW &source) = delete; int32_t kerning; // inter char gap int32_t spacing; // inter word gap diff --git a/src/ccutil/elst2.h b/src/ccutil/elst2.h index dc67d515..92702999 100644 --- a/src/ccutil/elst2.h +++ b/src/ccutil/elst2.h @@ -245,7 +245,7 @@ public: private: // Don't use the following constructor. - ELIST2_ITERATOR(); + ELIST2_ITERATOR() = delete; }; /*********************************************************************** diff --git a/src/textord/fpchop.h b/src/textord/fpchop.h index 97189f08..d66f1a5d 100644 --- a/src/textord/fpchop.h +++ b/src/textord/fpchop.h @@ -53,7 +53,7 @@ public: private: // Copy constructor (currently unused, therefore private). - C_OUTLINE_FRAG(const C_OUTLINE_FRAG &other); + C_OUTLINE_FRAG(const C_OUTLINE_FRAG &other) = delete; }; ELISTIZEH(C_OUTLINE_FRAG) diff --git a/src/training/pango/ligature_table.h b/src/training/pango/ligature_table.h index af806373..133ea98c 100644 --- a/src/training/pango/ligature_table.h +++ b/src/training/pango/ligature_table.h @@ -71,8 +71,8 @@ protected: int max_norm_length_; private: - LigatureTable(const LigatureTable &); - void operator=(const LigatureTable &); + LigatureTable(const LigatureTable &) = delete; + void operator=(const LigatureTable &) = delete; }; } // namespace tesseract diff --git a/src/training/pango/pango_font_info.h b/src/training/pango/pango_font_info.h index 6a5df5c9..a80e94cd 100644 --- a/src/training/pango/pango_font_info.h +++ b/src/training/pango/pango_font_info.h @@ -144,8 +144,8 @@ private: static std::string cache_dir_; private: - PangoFontInfo(const PangoFontInfo &); - void operator=(const PangoFontInfo &); + PangoFontInfo(const PangoFontInfo &) = delete; + void operator=(const PangoFontInfo &) = delete; }; // Static utility methods for querying font availability and font-selection diff --git a/src/training/pango/stringrenderer.h b/src/training/pango/stringrenderer.h index 1679f064..d30cb5a1 100644 --- a/src/training/pango/stringrenderer.h +++ b/src/training/pango/stringrenderer.h @@ -224,8 +224,8 @@ protected: int last_offset_; // Offset returned from last successful rendering private: - StringRenderer(const StringRenderer &); - void operator=(const StringRenderer &); + StringRenderer(const StringRenderer &) = delete; + void operator=(const StringRenderer &) = delete; }; } // namespace tesseract diff --git a/src/training/unicharset/icuerrorcode.h b/src/training/unicharset/icuerrorcode.h index bed3886d..b2173096 100644 --- a/src/training/unicharset/icuerrorcode.h +++ b/src/training/unicharset/icuerrorcode.h @@ -55,8 +55,8 @@ protected: private: // Disallow implicit copying of object. - IcuErrorCode(const IcuErrorCode &); - void operator=(const IcuErrorCode &); + IcuErrorCode(const IcuErrorCode &) = delete; + void operator=(const IcuErrorCode &) = delete; }; } // namespace tesseract