Skip to content

Commit

Permalink
Modernize C++ loops
Browse files Browse the repository at this point in the history
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 <sw@weilnetz.de>
  • Loading branch information
stweil committed Mar 26, 2019
1 parent ed01167 commit a44bf41
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 34 deletions.
18 changes: 8 additions & 10 deletions src/ccmain/osdetect.cpp
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -469,10 +469,8 @@ ScriptDetector::ScriptDetector(const GenericVector<int>* 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);
Expand Down
7 changes: 2 additions & 5 deletions src/ccmain/paramsd.cpp
Expand Up @@ -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());
Expand Down
5 changes: 1 addition & 4 deletions src/ccstruct/ratngs.cpp
Expand Up @@ -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) {
Expand Down
11 changes: 5 additions & 6 deletions src/ccutil/unicharcompress.cpp
Expand Up @@ -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))
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/classify/adaptmatch.cpp
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/classify/cluster.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/classify/errorcounter.cpp
Expand Up @@ -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;
}

Expand Down

0 comments on commit a44bf41

Please sign in to comment.