Skip to content

Commit f50764d

Browse files
committed
range for loop cleanup
* Retrieve elements by value rather than reference. Same principle as in #270, just without the big speed increase. Best practice nonetheless. * Convert traditional for loops to range for loops where possible, for cleaner more concise code.
1 parent 8bcc764 commit f50764d

File tree

9 files changed

+52
-56
lines changed

9 files changed

+52
-56
lines changed

siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class HighwayGraph
9696
// compress edges adjacent to hidden vertices
9797
counter = 0;
9898
std::cout << et.et() + "Compressing collapsed edges" << std::flush;
99-
for (std::pair<const Waypoint*, HGVertex*> wv : vertices)
99+
for (std::pair<Waypoint* const, HGVertex*>& wv : vertices)
100100
{ if (counter % 10000 == 0) std::cout << '.' << std::flush;
101101
counter++;
102102
if (!wv.second->visibility)
@@ -146,7 +146,7 @@ class HighwayGraph
146146
} // end ctor
147147

148148
void clear()
149-
{ for (std::pair<const Waypoint*, HGVertex*> wv : vertices) delete wv.second;
149+
{ for (std::pair<Waypoint* const, HGVertex*>& wv : vertices) delete wv.second;
150150
vertex_names.clear();
151151
waypoint_naming_log.clear();
152152
vertices.clear();

siteupdate/cplusplus/classes/HighwaySegment/HighwaySegment.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,15 @@ unsigned int HighwaySegment::index()
4949
/*std::string HighwaySegment::concurrent_travelers_sanity_check()
5050
{ if (route->system->devel()) return "";
5151
if (concurrent)
52-
for (HighwaySegment *conc : *concurrent)
53-
{ if (clinched_by.size() != conc->clinched_by.size())
54-
{ if (conc->route->system->devel()) continue;
52+
for (HighwaySegment *other : *concurrent)
53+
{ if (clinched_by.size() != other->clinched_by.size())
54+
{ if (other->route->system->devel()) continue;
5555
return "[" + str() + "] clinched by " + std::to_string(clinched_by.size()) + " travelers; [" \
56-
+ conc->str() + "] clinched by " + std::to_string(conc->clinched_by.size()) + '\n';
56+
+ other->str() + "] clinched by " + std::to_string(other->clinched_by.size()) + '\n';
5757
}
5858
else for (TravelerList *t : clinched_by)
59-
{ std::list<TravelerList*>::iterator ct;
60-
for (ct = conc->clinched_by.begin(); ct != conc->clinched_by.end(); ct++)
61-
if (*ct == t) break;
62-
if (ct == conc->clinched_by.end())
63-
return t->traveler_name + " has clinched [" + str() + "], but not [" + conc->str() + "]\n";
64-
}
59+
if (other->clinched_by.find(t) == other->clinched_by.end())
60+
return t->traveler_name + " has clinched [" + str() + "], but not [" + other->str() + "]\n";
6561
}
6662
return "";
6763
}//*/

siteupdate/cplusplus/classes/HighwaySystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class HighwaySystem
128128
/* Return total system mileage across all regions */
129129
double total_mileage()
130130
{ double mi = 0;
131-
for (std::pair<Region*, double> rm : mileage_by_region) mi += rm.second;
131+
for (std::pair<Region* const, double>& rm : mileage_by_region) mi += rm.second;
132132
return mi;
133133
}
134134

siteupdate/cplusplus/classes/Region.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
std::pair<std::string, std::string> *country_or_continent_by_code(std::string code, std::vector<std::pair<std::string, std::string>> &pair_vector)
2-
{ for (std::vector<std::pair<std::string, std::string>>::iterator c = pair_vector.begin(); c != pair_vector.end(); c++)
3-
if (c->first == code) return &*c;
2+
{ for (std::pair<std::string, std::string>& c : pair_vector)
3+
if (c.first == code) return &c;
44
return 0;
55
}
66

siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ class TravelerList
165165
}
166166
if (point_indices.size() != 2)
167167
{ bool invalid_char = 0;
168-
for (size_t c = 0; c < trim_line.size(); c++)
169-
if (iscntrl(trim_line[c]))
170-
{ trim_line[c] = '?';
168+
for (char& c : trim_line)
169+
if (iscntrl(c))
170+
{ c = '?';
171171
invalid_char = 1;
172172
}
173173
log << "Waypoint label(s) not found in line: " << trim_line;
@@ -189,9 +189,9 @@ class TravelerList
189189
}
190190
catch (const std::out_of_range& oor)
191191
{ bool invalid_char = 0;
192-
for (size_t c = 0; c < trim_line.size(); c++)
193-
if (iscntrl(trim_line[c]))
194-
{ trim_line[c] = '?';
192+
for (char& c : trim_line)
193+
if (iscntrl(c))
194+
{ c = '?';
195195
invalid_char = 1;
196196
}
197197
log << "Unknown region/highway combo in line: " << trim_line;
@@ -209,21 +209,21 @@ class TravelerList
209209
/* Return active mileage across all regions */
210210
double active_only_miles()
211211
{ double mi = 0;
212-
for (std::pair<Region*, double> rm : active_only_mileage_by_region) mi += rm.second;
212+
for (std::pair<Region* const, double>& rm : active_only_mileage_by_region) mi += rm.second;
213213
return mi;
214214
}
215215

216216
/* Return active+preview mileage across all regions */
217217
double active_preview_miles()
218218
{ double mi = 0;
219-
for (std::pair<Region*, double> rm : active_preview_mileage_by_region) mi += rm.second;
219+
for (std::pair<Region* const, double>& rm : active_preview_mileage_by_region) mi += rm.second;
220220
return mi;
221221
}
222222

223223
/* Return mileage across all regions for a specified system */
224224
double system_region_miles(HighwaySystem *h)
225225
{ double mi = 0;
226-
for (std::pair<Region*, double> rm : system_region_mileages.at(h)) mi += rm.second;
226+
for (std::pair<Region* const, double>& rm : system_region_mileages.at(h)) mi += rm.second;
227227
return mi;
228228
}
229229

siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void WaypointQuadtree::write_qt_tmg(std::string filename)
259259
std::ofstream tmgfile(filename);
260260
tmgfile << "TMG 1.0 simple\n";
261261
tmgfile << vertices.size() << ' ' << edges.size() << '\n';
262-
for (std::string v : vertices) tmgfile << v << '\n';
263-
for (std::string e : edges) tmgfile << e << '\n';
262+
for (std::string& v : vertices) tmgfile << v << '\n';
263+
for (std::string& e : edges) tmgfile << e << '\n';
264264
tmgfile.close();
265265
}

siteupdate/cplusplus/functions/graph_generation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ HighwayGraph graph_data(all_waypoints, highway_systems, datacheckerrors, args.nu
55

66
cout << et.et() << "Writing graph waypoint simplification log." << endl;
77
ofstream wslogfile(args.logfilepath + "/waypointsimplification.log");
8-
for (string line : graph_data.waypoint_naming_log)
8+
for (string& line : graph_data.waypoint_naming_log)
99
wslogfile << line << '\n';
1010
wslogfile.close();
1111
graph_data.waypoint_naming_log.clear();
@@ -34,7 +34,8 @@ else { list<Region*> *regions;
3434
graph_vector[0].edges = 0; graph_vector[0].vertices = graph_data.vertices.size();
3535
graph_vector[1].edges = 0; graph_vector[1].vertices = 0;
3636
graph_vector[2].edges = 0; graph_vector[2].vertices = 0;
37-
for (std::pair<const Waypoint*, HGVertex*> wv : graph_data.vertices)
37+
// get master graph vertex & edge counts for terminal output before writing files
38+
for (std::pair<Waypoint* const, HGVertex*>& wv : graph_data.vertices)
3839
{ graph_vector[0].edges += wv.second->incident_s_edges.size();
3940
if (wv.second->visibility >= 1)
4041
{ graph_vector[2].vertices++;

siteupdate/cplusplus/functions/sql_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void sqlfile1
273273
first = 1;
274274
for (HighwaySystem *h : *highway_systems)
275275
if (h->active_or_preview())
276-
for (std::pair<Region*,double> rm : h->mileage_by_region)
276+
for (std::pair<Region* const,double>& rm : h->mileage_by_region)
277277
{ if (!first) sqlfile << ',';
278278
first = 0;
279279
char fstr[35];
@@ -293,7 +293,7 @@ void sqlfile1
293293
sqlfile << "INSERT INTO clinchedOverallMileageByRegion VALUES\n";
294294
first = 1;
295295
for (TravelerList *t : *traveler_lists)
296-
for (std::pair<Region*,double> rm : t->active_preview_mileage_by_region)
296+
for (std::pair<Region* const,double>& rm : t->active_preview_mileage_by_region)
297297
{ if (!first) sqlfile << ',';
298298
first = 0;
299299
double active_miles = 0;

siteupdate/cplusplus/siteupdate.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int main(int argc, char *argv[])
248248
}
249249
cout << endl;
250250
// at the end, print the lines ignored
251-
for (string l : ignoring) cout << l << endl;
251+
for (string& l : ignoring) cout << l << endl;
252252
ignoring.clear();
253253
}
254254
file.close();
@@ -285,9 +285,8 @@ int main(int argc, char *argv[])
285285
#else
286286
for (HighwaySystem* h : highway_systems)
287287
{ std::cout << h->systemname << std::flush;
288-
for (std::list<Route>::iterator r = h->route_list.begin(); r != h->route_list.end(); r++)
289-
{ r->read_wpt(&all_waypoints, &el, args.highwaydatapath+"/hwy_data", datacheckerrors, &all_wpt_files);
290-
}
288+
for (Route& r : h->route_list)
289+
r.read_wpt(&all_waypoints, &el, args.highwaydatapath+"/hwy_data", datacheckerrors, &all_wpt_files);
291290
std::cout << "!" << std::endl;
292291
}
293292
#endif
@@ -529,7 +528,7 @@ int main(int argc, char *argv[])
529528
{ inusefile << r.root << '(' << r.point_list.size() << "):";
530529
list<string> liu_list(r.labels_in_use.begin(), r.labels_in_use.end());
531530
liu_list.sort();
532-
for (string label : liu_list) inusefile << ' ' << label;
531+
for (string& label : liu_list) inusefile << ' ' << label;
533532
inusefile << '\n';
534533
r.labels_in_use.clear();
535534
}
@@ -546,7 +545,7 @@ int main(int argc, char *argv[])
546545
string ual_entry = r.root + '(' + to_string(r.unused_alt_labels.size()) + "):";
547546
list<string> ual_list(r.unused_alt_labels.begin(), r.unused_alt_labels.end());
548547
ual_list.sort();
549-
for (string label : ual_list) ual_entry += ' ' + label;
548+
for (string& label : ual_list) ual_entry += ' ' + label;
550549
r.unused_alt_labels.clear();
551550
unused_alt_labels.push_back(ual_entry);
552551
}
@@ -656,15 +655,15 @@ int main(int argc, char *argv[])
656655
region_entries.push_back(region->code + fstr);
657656
}
658657
region_entries.sort();
659-
for (string e : region_entries) hdstatsfile << e;
658+
for (string& e : region_entries) hdstatsfile << e;
660659

661660
for (HighwaySystem *h : highway_systems)
662661
{ sprintf(fstr, ") total: %.2f mi\n", h->total_mileage());
663662
hdstatsfile << "System " << h->systemname << " (" << h->level_name() << fstr;
664663
if (h->mileage_by_region.size() > 1)
665664
{ hdstatsfile << "System " << h->systemname << " by region:\n";
666665
list<Region*> regions_in_system;
667-
for (pair<Region*, double> rm : h->mileage_by_region)
666+
for (pair<Region* const, double>& rm : h->mileage_by_region)
668667
regions_in_system.push_back(rm.first);
669668
regions_in_system.sort(sort_regions_by_code);
670669
for (Region *r : regions_in_system)
@@ -673,19 +672,19 @@ int main(int argc, char *argv[])
673672
}
674673
}
675674
hdstatsfile << "System " << h->systemname << " by route:\n";
676-
for (list<ConnectedRoute>::iterator cr = h->con_route_list.begin(); cr != h->con_route_list.end(); cr++)
675+
for (ConnectedRoute& cr : h->con_route_list)
677676
{ double con_total_miles = 0;
678677
string to_write = "";
679-
for (Route *r : cr->roots)
678+
for (Route *r : cr.roots)
680679
{ sprintf(fstr, ": %.2f mi\n", r->mileage);
681680
to_write += " " + r->readable_name() + fstr;
682681
con_total_miles += r->mileage;
683682
}
684-
cr->mileage = con_total_miles; //FIXME?
683+
cr.mileage = con_total_miles; //FIXME?
685684
sprintf(fstr, ": %.2f mi", con_total_miles);
686-
hdstatsfile << cr->readable_name() << fstr;
687-
if (cr->roots.size() == 1)
688-
hdstatsfile << " (" << cr->roots[0]->readable_name() << " only)\n";
685+
hdstatsfile << cr.readable_name() << fstr;
686+
if (cr.roots.size() == 1)
687+
hdstatsfile << " (" << cr.roots[0]->readable_name() << " only)\n";
689688
else hdstatsfile << '\n' << to_write;
690689
}
691690
}
@@ -733,7 +732,7 @@ int main(int argc, char *argv[])
733732
allfile << '\n';
734733
for (TravelerList *t : traveler_lists)
735734
{ double t_total_mi = 0;
736-
for (std::pair<Region*, double> rm : t->active_only_mileage_by_region)
735+
for (std::pair<Region* const, double>& rm : t->active_only_mileage_by_region)
737736
t_total_mi += rm.second;
738737
sprintf(fstr, "%.2f", t_total_mi);
739738
allfile << t->traveler_name << ',' << fstr;
@@ -771,7 +770,7 @@ int main(int argc, char *argv[])
771770
allfile << '\n';
772771
for (TravelerList *t : traveler_lists)
773772
{ double t_total_mi = 0;
774-
for (std::pair<Region*, double> rm : t->active_preview_mileage_by_region)
773+
for (std::pair<Region* const, double>& rm : t->active_preview_mileage_by_region)
775774
t_total_mi += rm.second;
776775
sprintf(fstr, "%.2f", t_total_mi);
777776
allfile << t->traveler_name << ',' << fstr;
@@ -799,7 +798,7 @@ int main(int argc, char *argv[])
799798
sysfile << "Traveler,Total";
800799
regions.clear();
801800
total_mi = 0;
802-
for (std::pair<Region*, double> rm : h->mileage_by_region)
801+
for (std::pair<Region* const, double>& rm : h->mileage_by_region)
803802
{ regions.push_back(rm.first);
804803
total_mi += rm.second; //TODO is this right?
805804
}
@@ -835,7 +834,7 @@ int main(int argc, char *argv[])
835834
cout << et.et() << "Reading datacheckfps.csv." << endl;
836835
file.open(args.highwaydatapath+"/datacheckfps.csv");
837836
getline(file, line); // ignore header line
838-
list<array<string, 6>> datacheckfps; //FIXME try implementing as an unordered_multiset; see if speed increases
837+
list<array<string, 6>> datacheckfps;
839838
unordered_set<string> datacheck_always_error
840839
({ "BAD_ANGLE", "DUPLICATE_LABEL", "HIDDEN_TERMINUS",
841840
"INVALID_FINAL_CHAR", "INVALID_FIRST_CHAR",
@@ -881,22 +880,22 @@ int main(int argc, char *argv[])
881880
fpfile << "Log file created at: " << ctime(&timestamp);
882881
unsigned int counter = 0;
883882
unsigned int fpcount = 0;
884-
for (list<DatacheckEntry>::iterator d = datacheckerrors->entries.begin(); d != datacheckerrors->entries.end(); d++)
883+
for (DatacheckEntry& d : datacheckerrors->entries)
885884
{ //cout << "Checking: " << d->str() << endl;
886885
counter++;
887886
if (counter % 1000 == 0) cout << '.' << flush;
888887
for (list<array<string, 6>>::iterator fp = datacheckfps.begin(); fp != datacheckfps.end(); fp++)
889-
if (d->match_except_info(*fp))
890-
if (d->info == (*fp)[5])
888+
if (d.match_except_info(*fp))
889+
if (d.info == (*fp)[5])
891890
{ //cout << "Match!" << endl;
892-
d->fp = 1;
891+
d.fp = 1;
893892
fpcount++;
894893
datacheckfps.erase(fp);
895894
break;
896895
}
897896
else
898897
{ fpfile << "FP_ENTRY: " << (*fp)[0] << ';' << (*fp)[1] << ';' << (*fp)[2] << ';' << (*fp)[3] << ';' << (*fp)[4] << ';' << (*fp)[5] << '\n';
899-
fpfile << "CHANGETO: " << (*fp)[0] << ';' << (*fp)[1] << ';' << (*fp)[2] << ';' << (*fp)[3] << ';' << (*fp)[4] << ';' << d->info << '\n';
898+
fpfile << "CHANGETO: " << (*fp)[0] << ';' << (*fp)[1] << ';' << (*fp)[2] << ';' << (*fp)[3] << ';' << (*fp)[4] << ';' << d.info << '\n';
900899
}
901900
}
902901
fpfile.close();
@@ -910,8 +909,8 @@ int main(int argc, char *argv[])
910909
timestamp = time(0);
911910
fpfile << "Log file created at: " << ctime(&timestamp);
912911
if (datacheckfps.empty()) fpfile << "No unmatched FP entries.\n";
913-
else for (array<string, 6> entry : datacheckfps)
914-
fpfile << entry[0] << ';' << entry[1] << ';' << entry[2] << ';' << entry[3] << ';' << entry[4] << ';' << entry[5] << '\n';
912+
else for (array<string, 6>& entry : datacheckfps)
913+
fpfile << entry[0] << ';' << entry[1] << ';' << entry[2] << ';' << entry[3] << ';' << entry[4] << ';' << entry[5] << '\n';
915914
fpfile.close();
916915

917916
// datacheck.log file
@@ -999,7 +998,7 @@ int main(int argc, char *argv[])
999998
unsigned other_count = 0;
1000999
unsigned int total_rtes = 0;
10011000
for (HighwaySystem *h : highway_systems)
1002-
for (Route r : h->route_list)
1001+
for (Route& r : h->route_list)
10031002
{ total_rtes++;
10041003
if (h->devel()) d_count++;
10051004
else { if (h->active()) a_count++;

0 commit comments

Comments
 (0)