From a44bf41f14004e9f235f1cd5db916285cd547cb3 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 26 Mar 2019 08:38:21 +0100 Subject: [PATCH] Modernize C++ loops The modifications were done using this command: run-clang-tidy-8.py -header-filter='.*' -checks='-*,modernize-loop-convert' -fix Then the resulting code was cleaned manually. Signed-off-by: Stefan Weil --- src/ccmain/osdetect.cpp | 18 ++++++++---------- src/ccmain/paramsd.cpp | 7 ++----- src/ccstruct/ratngs.cpp | 5 +---- src/ccutil/unicharcompress.cpp | 11 +++++------ src/classify/adaptmatch.cpp | 4 ++-- src/classify/cluster.cpp | 10 +++++----- src/classify/errorcounter.cpp | 4 ++-- 7 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/ccmain/osdetect.cpp b/src/ccmain/osdetect.cpp index 3abe642149..5ac0c58d6b 100644 --- a/src/ccmain/osdetect.cpp +++ b/src/ccmain/osdetect.cpp @@ -416,20 +416,20 @@ bool OrientationDetector::detect_blob(BLOB_CHOICE_LIST* scores) { // picking an arbitrary probability for it and way better than -inf. float worst_score = 0.0f; int num_good_scores = 0; - for (int i = 0; i < 4; ++i) { - if (blob_o_score[i] > 0.0f) { + for (float f : blob_o_score) { + if (f > 0.0f) { ++num_good_scores; - if (worst_score == 0.0f || blob_o_score[i] < worst_score) - worst_score = blob_o_score[i]; + if (worst_score == 0.0f || f < worst_score) + worst_score = f; } } if (num_good_scores == 1) { // Lower worst if there is only one. worst_score /= 2.0f; } - for (int i = 0; i < 4; ++i) { - if (blob_o_score[i] == 0.0f) { - blob_o_score[i] = worst_score; + for (float& f : blob_o_score) { + if (f == 0.0f) { + f = worst_score; total_blob_o_score += worst_score; } } @@ -469,10 +469,8 @@ ScriptDetector::ScriptDetector(const GenericVector* allowed_scripts, // Score the given blob and return true if it is now sure of the script after // adding this blob. void ScriptDetector::detect_blob(BLOB_CHOICE_LIST* scores) { - bool done[kMaxNumberOfScripts]; for (int i = 0; i < 4; ++i) { - for (int j = 0; j < kMaxNumberOfScripts; ++j) - done[j] = false; + bool done[kMaxNumberOfScripts] = { false }; BLOB_CHOICE_IT choice_it; choice_it.set_to_list(scores + i); diff --git a/src/ccmain/paramsd.cpp b/src/ccmain/paramsd.cpp index 92f4604338..c4e5c8b38e 100644 --- a/src/ccmain/paramsd.cpp +++ b/src/ccmain/paramsd.cpp @@ -340,11 +340,8 @@ void ParamsEditor::WriteParams(char *filename, filename); return; } - - for (auto iter = vcMap.begin(); - iter != vcMap.end(); - ++iter) { - ParamContent* cur = iter->second; + for (auto& iter : vcMap) { + ParamContent* cur = iter.second; if (!changes_only || cur->HasChanged()) { fprintf(fp, "%-25s %-12s # %s\n", cur->GetName(), cur->GetValue().string(), cur->GetDescription()); diff --git a/src/ccstruct/ratngs.cpp b/src/ccstruct/ratngs.cpp index 78f59ea423..d492300b3f 100644 --- a/src/ccstruct/ratngs.cpp +++ b/src/ccstruct/ratngs.cpp @@ -555,10 +555,7 @@ void WERD_CHOICE::SetScriptPositions(bool small_caps, TWERD* word, int debug) { return; } - int position_counts[4]; - for (int i = 0; i < 4; i++) { - position_counts[i] = 0; - } + int position_counts[4] = { 0, 0, 0, 0 }; int chunk_index = 0; for (int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) { diff --git a/src/ccutil/unicharcompress.cpp b/src/ccutil/unicharcompress.cpp index 0060eb5cf9..9354594733 100644 --- a/src/ccutil/unicharcompress.cpp +++ b/src/ccutil/unicharcompress.cpp @@ -168,14 +168,13 @@ bool UnicharCompress::ComputeEncoding(const UNICHARSET& unicharset, int null_id, } else { // Add the direct_set unichar-ids of the unicodes in sequence to the // code. - for (size_t i = 0; i < unicodes.size(); ++i) { + for (int uni : unicodes) { int position = code.length(); if (position >= RecodedCharID::kMaxCodeLen) { tprintf("Unichar %d=%s is too long to encode!!\n", u, unicharset.id_to_unichar(u)); return false; } - int uni = unicodes[i]; UNICHAR unichar(uni); char* utf8 = unichar.utf8_str(); if (!direct_set.contains_unichar(utf8)) @@ -412,11 +411,11 @@ void UnicharCompress::SetupDecoder() { void UnicharCompress::Cleanup() { decoder_.clear(); is_valid_start_.clear(); - for (auto it = next_codes_.begin(); it != next_codes_.end(); ++it) { - delete it->second; + for (auto& next_code : next_codes_) { + delete next_code.second; } - for (auto it = final_codes_.begin(); it != final_codes_.end(); ++it) { - delete it->second; + for (auto& final_code : final_codes_) { + delete final_code.second; } next_codes_.clear(); final_codes_.clear(); diff --git a/src/classify/adaptmatch.cpp b/src/classify/adaptmatch.cpp index 7ae3819fe1..49f72d5ad0 100644 --- a/src/classify/adaptmatch.cpp +++ b/src/classify/adaptmatch.cpp @@ -565,8 +565,8 @@ void Classify::InitAdaptiveClassifier(TessdataManager* mgr) { set_all_bits(AllConfigsOn, WordsInVectorOfSize(MAX_NUM_CONFIGS)); zero_all_bits(AllConfigsOff, WordsInVectorOfSize(MAX_NUM_CONFIGS)); - for (int i = 0; i < MAX_NUM_CLASSES; i++) { - BaselineCutoffs[i] = 0; + for (uint16_t& BaselineCutoff : BaselineCutoffs) { + BaselineCutoff = 0; } if (classify_use_pre_adapted_templates) { diff --git a/src/classify/cluster.cpp b/src/classify/cluster.cpp index 556b1f9e7f..d2504438b4 100644 --- a/src/classify/cluster.cpp +++ b/src/classify/cluster.cpp @@ -405,9 +405,9 @@ MakeClusterer (int16_t SampleSize, const PARAM_DESC ParamDesc[]) { Clusterer->KDTree = MakeKDTree (SampleSize, ParamDesc); // Initialize cache of histogram buckets to minimize recomputing them. - for (int d = 0; d < DISTRIBUTION_COUNT; ++d) { + for (auto & d : Clusterer->bucket_cache) { for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c) - Clusterer->bucket_cache[d][c] = nullptr; + d[c] = nullptr; } return Clusterer; @@ -520,10 +520,10 @@ void FreeClusterer(CLUSTERER *Clusterer) { if (Clusterer->Root != nullptr) FreeCluster (Clusterer->Root); // Free up all used buckets structures. - for (int d = 0; d < DISTRIBUTION_COUNT; ++d) { + for (auto & d : Clusterer->bucket_cache) { for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c) - if (Clusterer->bucket_cache[d][c] != nullptr) - FreeBuckets(Clusterer->bucket_cache[d][c]); + if (d[c] != nullptr) + FreeBuckets(d[c]); } free(Clusterer); diff --git a/src/classify/errorcounter.cpp b/src/classify/errorcounter.cpp index 9e07964994..a50d865cbf 100644 --- a/src/classify/errorcounter.cpp +++ b/src/classify/errorcounter.cpp @@ -465,8 +465,8 @@ bool ErrorCounter::ReportString(bool even_if_empty, const Counts& counts, delete [] formatted_str; // Now append each field of counts with a tab in front so the result can // be loaded into a spreadsheet. - for (int ct = 0; ct < CT_SIZE; ++ct) - report->add_str_int("\t", counts.n[ct]); + for (int ct : counts.n) + report->add_str_int("\t", ct); return true; }