From d2cdcaab8fab8cc73561d7870186b800d1f7acb8 Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Sun, 6 Mar 2022 10:31:38 -0500 Subject: [PATCH 1/7] set/map/dict cleanup --- .../cplusplus/classes/Datacheck/Datacheck.cpp | 2 +- .../classes/GraphGeneration/HighwayGraph.cpp | 5 +- .../classes/HighwaySegment/HighwaySegment.cpp | 2 +- .../classes/HighwaySystem/HighwaySystem.cpp | 2 +- .../classes/HighwaySystem/route_integrity.cpp | 2 +- siteupdate/cplusplus/classes/Route/Route.cpp | 8 +-- .../mark_chopped_route_segments.cpp | 4 +- .../mark_connected_route_segments.cpp | 4 +- .../straightforward_intersection.cpp | 2 +- siteupdate/cplusplus/functions/sql_file.cpp | 2 +- .../cplusplus/tasks/concurrency_detection.cpp | 2 +- .../cplusplus/templates/set_intersection.cpp | 2 +- siteupdate/python-teresco/siteupdate.py | 66 +++++++++---------- 13 files changed, 50 insertions(+), 53 deletions(-) diff --git a/siteupdate/cplusplus/classes/Datacheck/Datacheck.cpp b/siteupdate/cplusplus/classes/Datacheck/Datacheck.cpp index bbeefeba..3a20559a 100644 --- a/siteupdate/cplusplus/classes/Datacheck/Datacheck.cpp +++ b/siteupdate/cplusplus/classes/Datacheck/Datacheck.cpp @@ -64,7 +64,7 @@ void Datacheck::read_fps(std::string& path, ErrorList &el) + "], expected 6 fields, found " + std::to_string(NumFields)); continue; } - if (always_error.find(fields[4]) != always_error.end()) + if (always_error.count(fields[4])) std::cout << "datacheckfps.csv line not allowed (always error): " << line << std::endl; else fps.push_back(fields); } diff --git a/siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp b/siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp index e91b8594..196b6df4 100644 --- a/siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp +++ b/siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp @@ -91,8 +91,7 @@ HighwayGraph::HighwayGraph(WaypointQuadtree &all_waypoints, ElapsedTime &et) w->vertex->visibility = 1; // next, compare clinched_by sets; look for any element in the 1st not in the 2nd else for (TravelerList *t : w->vertex->incident_t_edges.front()->segment->clinched_by) - if (w->vertex->incident_t_edges.back()->segment->clinched_by.find(t) - == w->vertex->incident_t_edges.back()->segment->clinched_by.end()) + if (!w->vertex->incident_t_edges.back()->segment->clinched_by.count(t)) { w->vertex->visibility = 1; break; } @@ -228,7 +227,7 @@ inline void HighwayGraph::matching_vertices_and_edges // Compute sets of edges for subgraphs, optionally // restricted by region or system or placeradius. // Keep a count of collapsed & traveled vertices as we go. - #define AREA (!g.placeradius || mvset.find(e->vertex1) != mvset.end() && mvset.find(e->vertex2) != mvset.end()) + #define AREA (!g.placeradius || mvset.count(e->vertex1) && mvset.count(e->vertex2)) #define REGION (!g.regions || contains(*g.regions, e->segment->route->region)) for (HGVertex *v : mvset) { for (HGEdge *e : v->incident_s_edges) diff --git a/siteupdate/cplusplus/classes/HighwaySegment/HighwaySegment.cpp b/siteupdate/cplusplus/classes/HighwaySegment/HighwaySegment.cpp index 90dfc743..5e19be61 100644 --- a/siteupdate/cplusplus/classes/HighwaySegment/HighwaySegment.cpp +++ b/siteupdate/cplusplus/classes/HighwaySegment/HighwaySegment.cpp @@ -59,7 +59,7 @@ std::string HighwaySegment::segment_name() + other->str() + "] clinched by " + std::to_string(other->clinched_by.size()) + '\n'; } else for (TravelerList *t : clinched_by) - if (other->clinched_by.find(t) == other->clinched_by.end()) + if (other->clinched_by.count(t)) return t->traveler_name + " has clinched [" + str() + "], but not [" + other->str() + "]\n"; } return ""; diff --git a/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp b/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp index 09561cb5..e2544f29 100644 --- a/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp +++ b/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp @@ -164,7 +164,7 @@ void HighwaySystem::stats_csv() sysfile << '\n'; for (TravelerList *t : TravelerList::allusers) // only include entries for travelers who have any mileage in system - if (t->system_region_mileages.find(this) != t->system_region_mileages.end()) + if (t->system_region_mileages.count(this)) { sprintf(fstr, ",%.2f", t->system_region_miles(this)); sysfile << t->traveler_name << fstr; for (Region *region : regions) diff --git a/siteupdate/cplusplus/classes/HighwaySystem/route_integrity.cpp b/siteupdate/cplusplus/classes/HighwaySystem/route_integrity.cpp index 03e0dafd..73d2b276 100644 --- a/siteupdate/cplusplus/classes/HighwaySystem/route_integrity.cpp +++ b/siteupdate/cplusplus/classes/HighwaySystem/route_integrity.cpp @@ -20,7 +20,7 @@ void HighwaySystem::route_integrity(ErrorList& el) std::string upper_label(lbegin); upper(upper_label.data()); // if primary label not duplicated, add to pri_label_hash - if (r->alt_label_hash.find(upper_label) != r->alt_label_hash.end()) + if (r->alt_label_hash.count(upper_label)) { Datacheck::add(r, r->point_list[index]->label, "", "", "DUPLICATE_LABEL", ""); r->duplicate_labels.insert(upper_label); } diff --git a/siteupdate/cplusplus/classes/Route/Route.cpp b/siteupdate/cplusplus/classes/Route/Route.cpp index fa91093d..85a8b360 100644 --- a/siteupdate/cplusplus/classes/Route/Route.cpp +++ b/siteupdate/cplusplus/classes/Route/Route.cpp @@ -90,7 +90,7 @@ Route::Route(std::string &line, HighwaySystem *sys, ErrorList &el) // insert list name into pri_list_hash, checking for duplicate .list names std::string list_name(readable_name()); upper(list_name.data()); - if (alt_list_hash.find(list_name) != alt_list_hash.end()) + if (alt_list_hash.count(list_name)) el.add_error("Duplicate main list name in " + root + ": '" + readable_name() + "' already points to " + alt_list_hash.at(list_name)->root); else if (!pri_list_hash.insert(std::pair(list_name, this)).second) @@ -100,7 +100,7 @@ Route::Route(std::string &line, HighwaySystem *sys, ErrorList &el) for (std::string& a : alt_route_names) { list_name = rg_str + ' ' + a; upper(list_name.data()); - if (pri_list_hash.find(list_name) != pri_list_hash.end()) + if (pri_list_hash.count(list_name)) el.add_error("Duplicate alt route name in " + root + ": '" + region->code + ' ' + a + "' already points to " + pri_list_hash.at(list_name)->root); else if (!alt_list_hash.insert(std::pair(list_name, this)).second) @@ -175,9 +175,7 @@ std::string Route::name_no_abbrev() double Route::clinched_by_traveler(TravelerList *t) { double miles = 0; for (HighwaySegment *s : segment_list) - { std::unordered_set::iterator t_found = s->clinched_by.find(t); - if (t_found != s->clinched_by.end()) miles += s->length; - } + if (s->clinched_by.count(t)) miles += s->length; return miles; } diff --git a/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp b/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp index 836a40f6..67af95dd 100644 --- a/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp @@ -66,11 +66,11 @@ if (lit1 == r->alt_label_hash.end() || lit2 == r->alt_label_hash.end()) } // are either of the labels used duplicates? char duplicate = 0; -if (r->duplicate_labels.find(fields[2]) != r->duplicate_labels.end()) +if (r->duplicate_labels.count(fields[2])) { log << r->region->code << ": duplicate label " << fields[2] << " in " << r->root << '\n'; duplicate = 1; } -if (r->duplicate_labels.find(fields[3]) != r->duplicate_labels.end()) +if (r->duplicate_labels.count(fields[3])) { log << r->region->code << ": duplicate label " << fields[3] << " in " << r->root << '\n'; duplicate = 1; } diff --git a/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp b/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp index df2e158a..7e24818b 100644 --- a/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp @@ -92,11 +92,11 @@ if (lit1 == r1->alt_label_hash.end() || lit2 == r2->alt_label_hash.end()) } // are either of the labels used duplicates? char duplicate = 0; -if (r1->duplicate_labels.find(fields[2]) != r1->duplicate_labels.end()) +if (r1->duplicate_labels.count(fields[2])) { log << r1->region->code << ": duplicate label " << fields[2] << " in " << r1->root << ".\n"; duplicate = 1; } -if (r2->duplicate_labels.find(fields[5]) != r2->duplicate_labels.end()) +if (r2->duplicate_labels.count(fields[5])) { log << r2->region->code << ": duplicate label " << fields[5] << " in " << r2->root << ".\n"; duplicate = 1; } diff --git a/siteupdate/cplusplus/classes/Waypoint/canonical_waypoint_name/straightforward_intersection.cpp b/siteupdate/cplusplus/classes/Waypoint/canonical_waypoint_name/straightforward_intersection.cpp index 8aba5e02..b0fa20d4 100644 --- a/siteupdate/cplusplus/classes/Waypoint/canonical_waypoint_name/straightforward_intersection.cpp +++ b/siteupdate/cplusplus/classes/Waypoint/canonical_waypoint_name/straightforward_intersection.cpp @@ -14,7 +14,7 @@ if (ap_coloc.size() == 2) // if this is taken or if name_no_abbrev()s match, attempt to add in abbrevs if there's point in doing so if (ap_coloc[0]->route->abbrev.size() || ap_coloc[1]->route->abbrev.size()) { g->set_mtx[newname.back()].lock(); - bool taken = g->vertex_names[newname.back()].find(newname) != g->vertex_names[newname.back()].end(); + bool taken = g->vertex_names[newname.back()].count(newname); g->set_mtx[newname.back()].unlock(); if (taken || ap_coloc[0]->route->name_no_abbrev() == ap_coloc[1]->route->name_no_abbrev()) { const char *u0 = strchr(ap_coloc[0]->label.data(), '_'); diff --git a/siteupdate/cplusplus/functions/sql_file.cpp b/siteupdate/cplusplus/functions/sql_file.cpp index a32abf83..3b0ad09e 100644 --- a/siteupdate/cplusplus/functions/sql_file.cpp +++ b/siteupdate/cplusplus/functions/sql_file.cpp @@ -311,7 +311,7 @@ void sqlfile1 { if (!first) sqlfile << ','; first = 0; double active_miles = 0; - if (t->active_only_mileage_by_region.find(rm.first) != t->active_only_mileage_by_region.end()) + if (t->active_only_mileage_by_region.count(rm.first)) active_miles = t->active_only_mileage_by_region.at(rm.first); char fstr[65]; sprintf(fstr, "','%.15g','%.15g')\n", active_miles, rm.second); diff --git a/siteupdate/cplusplus/tasks/concurrency_detection.cpp b/siteupdate/cplusplus/tasks/concurrency_detection.cpp index 3752f3d6..009a009d 100644 --- a/siteupdate/cplusplus/tasks/concurrency_detection.cpp +++ b/siteupdate/cplusplus/tasks/concurrency_detection.cpp @@ -37,7 +37,7 @@ cout << "!\n"; // When splitting a region, perform a sanity check on concurrencies in its systems if (Args::splitregionpath != "") { for (HighwaySystem *h : HighwaySystem::syslist) - { if (splitsystems.find(h->systemname) == splitsystems.end()) continue; + { if (!splitsystems.count(h->systemname)) continue; ofstream fralog(Args::splitregionpath + "/logs/" + h->systemname + "-concurrencies.log"); for (Route *r : h->route_list) { if (r->region->code.substr(0, Args::splitregion.size()) != Args::splitregion) continue; diff --git a/siteupdate/cplusplus/templates/set_intersection.cpp b/siteupdate/cplusplus/templates/set_intersection.cpp index 1fa8eda1..df63428b 100644 --- a/siteupdate/cplusplus/templates/set_intersection.cpp +++ b/siteupdate/cplusplus/templates/set_intersection.cpp @@ -3,7 +3,7 @@ template std::unordered_set operator & (const std::unordered_set &a, const std::unordered_set &b) { std::unordered_set s; for (const item &i : a) - if (b.find(i) != b.end()) + if (b.count(i)) s.insert(i); return s; } diff --git a/siteupdate/python-teresco/siteupdate.py b/siteupdate/python-teresco/siteupdate.py index 85222cce..fa62d2f0 100755 --- a/siteupdate/python-teresco/siteupdate.py +++ b/siteupdate/python-teresco/siteupdate.py @@ -3475,11 +3475,11 @@ def run(self): print(et.et() + "Writing highway data stats log file (highwaydatastats.log).",flush=True) hdstatsfile = open(args.logfilepath+"/highwaydatastats.log","wt",encoding='UTF-8') hdstatsfile.write("Travel Mapping highway mileage as of " + str(datetime.datetime.now()) + '\n') -active_only_miles = math.fsum(list(active_only_mileage_by_region.values())) +active_only_miles = math.fsum(active_only_mileage_by_region.values()) hdstatsfile.write("Active routes (active): " + "{0:.2f}".format(active_only_miles) + " mi\n") -active_preview_miles = math.fsum(list(active_preview_mileage_by_region.values())) +active_preview_miles = math.fsum(active_preview_mileage_by_region.values()) hdstatsfile.write("Clinchable routes (active, preview): " + "{0:.2f}".format(active_preview_miles) + " mi\n") -overall_miles = math.fsum(list(overall_mileage_by_region.values())) +overall_miles = math.fsum(overall_mileage_by_region.values()) hdstatsfile.write("All routes (active, preview, devel): " + "{0:.2f}".format(overall_miles) + " mi\n") hdstatsfile.write("Breakdown by region:\n") # let's sort alphabetically by region instead of using whatever order @@ -3487,13 +3487,13 @@ def run(self): # a nice enhancement later here might break down by continent, then country, # then region region_entries = [] -for region in list(overall_mileage_by_region.keys()): +for region in overall_mileage_by_region: # look up active+preview and active-only mileages if they exist - if region in list(active_preview_mileage_by_region.keys()): + if region in active_preview_mileage_by_region: region_active_preview_miles = active_preview_mileage_by_region[region] else: region_active_preview_miles = 0.0 - if region in list(active_only_mileage_by_region.keys()): + if region in active_only_mileage_by_region: region_active_only_miles = active_only_mileage_by_region[region] else: region_active_only_miles = 0.0 @@ -3508,11 +3508,11 @@ def run(self): for h in highway_systems: hdstatsfile.write("System " + h.systemname + " (" + h.level + ") total: " - + "{0:.2f}".format(math.fsum(list(h.mileage_by_region.values()))) \ + + "{0:.2f}".format(math.fsum(h.mileage_by_region.values())) \ + ' mi\n') if len(h.mileage_by_region) > 1: hdstatsfile.write("System " + h.systemname + " by region:\n") - for region in sorted(h.mileage_by_region.keys()): + for region in sorted(h.mileage_by_region): hdstatsfile.write(region + ": " + "{0:.2f}".format(h.mileage_by_region[region]) + " mi\n") hdstatsfile.write("System " + h.systemname + " by route:\n") for cr in h.con_route_list: @@ -3540,13 +3540,13 @@ def run(self): for t in traveler_lists: print(".",end="",flush=True) t.log_entries.append("Clinched Highway Statistics") - t_active_only_miles = math.fsum(list(t.active_only_mileage_by_region.values())) + t_active_only_miles = math.fsum(t.active_only_mileage_by_region.values()) t.log_entries.append("Overall in active systems: " + format_clinched_mi(t_active_only_miles,active_only_miles)) - t_active_preview_miles = math.fsum(list(t.active_preview_mileage_by_region.values())) + t_active_preview_miles = math.fsum(t.active_preview_mileage_by_region.values()) t.log_entries.append("Overall in active+preview systems: " + format_clinched_mi(t_active_preview_miles,active_preview_miles)) t.log_entries.append("Overall by region: (each line reports active only then active+preview)") - for region in sorted(t.active_preview_mileage_by_region.keys()): + for region in sorted(t.active_preview_mileage_by_region): try: total_active_miles = active_only_mileage_by_region[region] except KeyError: @@ -3579,13 +3579,13 @@ def run(self): preview_systems += 1 t_system_overall = 0.0 if h.systemname in t.system_region_mileages: - t_system_overall = math.fsum(list(t.system_region_mileages[h.systemname].values())) + t_system_overall = math.fsum(t.system_region_mileages[h.systemname].values()) if t_system_overall > 0.0: if h.active(): t.active_systems_traveled += 1 else: t.preview_systems_traveled += 1 - if t_system_overall == math.fsum(list(h.mileage_by_region.values())): + if t_system_overall == math.fsum(h.mileage_by_region.values()): if h.active(): t.active_systems_clinched += 1 else: @@ -3595,10 +3595,10 @@ def run(self): # the DB, but add to logs only if it's been traveled at # all and it covers multiple regions t.log_entries.append("System " + h.systemname + " (" + h.level + ") overall: " + - format_clinched_mi(t_system_overall, math.fsum(list(h.mileage_by_region.values())))) + format_clinched_mi(t_system_overall, math.fsum(h.mileage_by_region.values()))) if len(h.mileage_by_region) > 1: t.log_entries.append("System " + h.systemname + " by region:") - for region in sorted(h.mileage_by_region.keys()): + for region in sorted(h.mileage_by_region): system_region_mileage = 0.0 if h.systemname in t.system_region_mileages and region in t.system_region_mileages[h.systemname]: system_region_mileage = t.system_region_mileages[h.systemname][region] @@ -3692,19 +3692,19 @@ def run(self): # first, overall per traveler by region, both active only and active+preview allfile = open(args.csvstatfilepath + "/allbyregionactiveonly.csv","w",encoding='UTF-8') allfile.write("Traveler,Total") -regions = sorted(active_only_mileage_by_region.keys()) +regions = sorted(active_only_mileage_by_region) for region in regions: allfile.write(',' + region) allfile.write('\n') for t in traveler_lists: - allfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(list(t.active_only_mileage_by_region.values())))) + allfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(t.active_only_mileage_by_region.values()))) for region in regions: - if region in t.active_only_mileage_by_region.keys(): + if region in t.active_only_mileage_by_region: allfile.write(',{0:.2f}'.format(t.active_only_mileage_by_region[region])) else: allfile.write(',0') allfile.write('\n') -allfile.write('TOTAL,{0:.2f}'.format(math.fsum(list(active_only_mileage_by_region.values())))) +allfile.write('TOTAL,{0:.2f}'.format(math.fsum(active_only_mileage_by_region.values()))) for region in regions: allfile.write(',{0:.2f}'.format(active_only_mileage_by_region[region])) allfile.write('\n') @@ -3713,19 +3713,19 @@ def run(self): # active+preview allfile = open(args.csvstatfilepath + "/allbyregionactivepreview.csv","w",encoding='UTF-8') allfile.write("Traveler,Total") -regions = sorted(active_preview_mileage_by_region.keys()) +regions = sorted(active_preview_mileage_by_region) for region in regions: allfile.write(',' + region) allfile.write('\n') for t in traveler_lists: - allfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(list(t.active_preview_mileage_by_region.values())))) + allfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(t.active_preview_mileage_by_region.values()))) for region in regions: - if region in t.active_preview_mileage_by_region.keys(): + if region in t.active_preview_mileage_by_region: allfile.write(',{0:.2f}'.format(t.active_preview_mileage_by_region[region])) else: allfile.write(',0') allfile.write('\n') -allfile.write('TOTAL,{0:.2f}'.format(math.fsum(list(active_preview_mileage_by_region.values())))) +allfile.write('TOTAL,{0:.2f}'.format(math.fsum(active_preview_mileage_by_region.values()))) for region in regions: allfile.write(',{0:.2f}'.format(active_preview_mileage_by_region[region])) allfile.write('\n') @@ -3737,21 +3737,21 @@ def run(self): continue sysfile = open(args.csvstatfilepath + "/" + h.systemname + '-all.csv',"w",encoding='UTF-8') sysfile.write('Traveler,Total') - regions = sorted(h.mileage_by_region.keys()) + regions = sorted(h.mileage_by_region) for region in regions: sysfile.write(',' + region) sysfile.write('\n') for t in traveler_lists: # only include entries for travelers who have any mileage in system if h.systemname in t.system_region_mileages: - sysfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(list(t.system_region_mileages[h.systemname].values())))) + sysfile.write(t.traveler_name + ",{0:.2f}".format(math.fsum(t.system_region_mileages[h.systemname].values()))) for region in regions: if region in t.system_region_mileages[h.systemname]: sysfile.write(',{0:.2f}'.format(t.system_region_mileages[h.systemname][region])) else: sysfile.write(',0') sysfile.write('\n') - sysfile.write('TOTAL,{0:.2f}'.format(math.fsum(list(h.mileage_by_region.values())))) + sysfile.write('TOTAL,{0:.2f}'.format(math.fsum(h.mileage_by_region.values()))) for region in regions: sysfile.write(',{0:.2f}'.format(h.mileage_by_region[region])) sysfile.write('\n') @@ -4000,7 +4000,7 @@ def run(self): print(fields[1] + ' ', end="", flush=True) region_list = [] selected_regions = fields[2].split(",") - for r in all_regions.keys(): + for r in all_regions: if r in selected_regions and r in active_preview_mileage_by_region: region_list.append(r) graph_data.write_subgraphs_tmg(graph_list, args.graphfilepath + "/", fields[1], @@ -4517,15 +4517,15 @@ def run(self): '), activeMileage DOUBLE, activePreviewMileage DOUBLE);\n') sqlfile.write('INSERT INTO overallMileageByRegion VALUES\n') first = True - for region in list(active_preview_mileage_by_region.keys()): + for region in active_preview_mileage_by_region: if not first: sqlfile.write(",") first = False active_only_mileage = 0.0 active_preview_mileage = 0.0 - if region in list(active_only_mileage_by_region.keys()): + if region in active_only_mileage_by_region: active_only_mileage = active_only_mileage_by_region[region] - if region in list(active_preview_mileage_by_region.keys()): + if region in active_preview_mileage_by_region: active_preview_mileage = active_preview_mileage_by_region[region] sqlfile.write("('" + region + "','" + str(active_only_mileage) + "','" + @@ -4542,7 +4542,7 @@ def run(self): first = True for h in highway_systems: if h.active_or_preview(): - for region in list(h.mileage_by_region.keys()): + for region in h.mileage_by_region: if not first: sqlfile.write(",") first = False @@ -4558,12 +4558,12 @@ def run(self): sqlfile.write('INSERT INTO clinchedOverallMileageByRegion VALUES\n') first = True for t in traveler_lists: - for region in list(t.active_preview_mileage_by_region.keys()): + for region in t.active_preview_mileage_by_region: if not first: sqlfile.write(",") first = False active_miles = 0.0 - if region in list(t.active_only_mileage_by_region.keys()): + if region in t.active_only_mileage_by_region: active_miles = t.active_only_mileage_by_region[region] sqlfile.write("('" + region + "','" + t.traveler_name + "','" + str(active_miles) + "','" + From c8b3c9829367d40c00641ee0ad0fe8cbda47ac80 Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Mon, 14 Mar 2022 13:38:28 -0400 Subject: [PATCH 2/7] r2cs remove unnecessary membership test C++ from bc0f67a Py from 2e80536 --- siteupdate/cplusplus/classes/TravelerList/userlog.cpp | 2 +- siteupdate/python-teresco/siteupdate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/siteupdate/cplusplus/classes/TravelerList/userlog.cpp b/siteupdate/cplusplus/classes/TravelerList/userlog.cpp index b5bb811c..b25d583a 100644 --- a/siteupdate/cplusplus/classes/TravelerList/userlog.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/userlog.cpp @@ -65,7 +65,7 @@ void TravelerList::userlog(ClinchedDBValues *clin_db_val, const double total_act sysregions.sort(sort_regions_by_code); for (Region *region : sysregions) { double system_region_mileage = 0; - if (system_region_mileages.count(h) && system_region_mileages.at(h).count(region)) + if (system_region_mileages.at(h).count(region)) { system_region_mileage = system_region_mileages.at(h).at(region); sprintf(fstr, "%.15g", system_region_mileage); if (!strchr(fstr, '.')) strcat(fstr, ".0"); diff --git a/siteupdate/python-teresco/siteupdate.py b/siteupdate/python-teresco/siteupdate.py index fa62d2f0..1923cbf8 100755 --- a/siteupdate/python-teresco/siteupdate.py +++ b/siteupdate/python-teresco/siteupdate.py @@ -3600,7 +3600,7 @@ def run(self): t.log_entries.append("System " + h.systemname + " by region:") for region in sorted(h.mileage_by_region): system_region_mileage = 0.0 - if h.systemname in t.system_region_mileages and region in t.system_region_mileages[h.systemname]: + if region in t.system_region_mileages[h.systemname]: system_region_mileage = t.system_region_mileages[h.systemname][region] csmbr_values.append("('" + h.systemname + "','" + region + "','" + t.traveler_name + "','" + From 597682e8535f1a1f08b26e42e9b6a08c5b997182 Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Fri, 18 Mar 2022 14:23:42 -0400 Subject: [PATCH 3/7] pyfloat redux, Waypoint only interactive rebase b54179c4e74277197b9d4efbcd47a01094f39179 --- .../classes/ConnectedRoute/ConnectedRoute.cpp | 8 ---- .../classes/ConnectedRoute/ConnectedRoute.h | 1 - siteupdate/cplusplus/classes/Route/Route.cpp | 14 ------- siteupdate/cplusplus/classes/Route/Route.h | 1 - .../cplusplus/classes/Waypoint/Waypoint.cpp | 37 ++++++------------- siteupdate/cplusplus/functions/sql_file.cpp | 13 ++++--- 6 files changed, 19 insertions(+), 55 deletions(-) diff --git a/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.cpp b/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.cpp index 95cf30cd..26dd3cb8 100644 --- a/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.cpp +++ b/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.cpp @@ -76,14 +76,6 @@ std::string ConnectedRoute::connected_rtes_line() return line; } -std::string ConnectedRoute::csv_line() -{ /* return csv line to insert into a table */ - char fstr[32]; - sprintf(fstr, "','%.15g'", mileage); - return "'" + system->systemname + "','" + route + "','" + banner + "','" + double_quotes(groupname) - + "','" + (roots.size() ? roots[0]->root.data() : "ERROR_NO_ROOTS") + fstr; -} - std::string ConnectedRoute::readable_name() { /* return a string for a human-readable connected route name */ std::string ans = route + banner; diff --git a/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.h b/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.h index 0c3995e1..9a2a4f4b 100644 --- a/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.h +++ b/siteupdate/cplusplus/classes/ConnectedRoute/ConnectedRoute.h @@ -21,7 +21,6 @@ class ConnectedRoute ConnectedRoute(std::string &, HighwaySystem *, ErrorList &); std::string connected_rtes_line(); - std::string csv_line(); std::string readable_name(); //std::string list_lines(int, int, std::string, size_t); }; diff --git a/siteupdate/cplusplus/classes/Route/Route.cpp b/siteupdate/cplusplus/classes/Route/Route.cpp index 85a8b360..097e2c2c 100644 --- a/siteupdate/cplusplus/classes/Route/Route.cpp +++ b/siteupdate/cplusplus/classes/Route/Route.cpp @@ -140,20 +140,6 @@ std::string Route::chopped_rtes_line() return line; } -std::string Route::csv_line() -{ /* return csv line to insert into a table */ - // note: alt_route_names does not need to be in the db since - // list preprocessing uses alt or canonical and no longer cares - std::string line = "'" + system->systemname + "','" + region->code + "','" + route + "','" + banner - + "','" + abbrev + "','" + double_quotes(city) + "','" + root + "','"; - char mstr[51]; - sprintf(mstr, "%.17g", mileage); - if (!strchr(mstr, '.')) strcat(mstr, ".0"); // add single trailing zero to ints for compatibility with Python - line += mstr; - line += "','" + std::to_string(rootOrder) + "'"; - return line; -} - std::string Route::readable_name() { /* return a string for a human-readable route name */ return rg_str + " " + route + banner + abbrev; diff --git a/siteupdate/cplusplus/classes/Route/Route.h b/siteupdate/cplusplus/classes/Route/Route.h index 10b157cf..25b7c912 100644 --- a/siteupdate/cplusplus/classes/Route/Route.h +++ b/siteupdate/cplusplus/classes/Route/Route.h @@ -88,7 +88,6 @@ class Route void print_route(); HighwaySegment* find_segment_by_waypoints(Waypoint*, Waypoint*); std::string chopped_rtes_line(); - std::string csv_line(); std::string readable_name(); std::string list_entry_name(); std::string name_no_abbrev(); diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp index 1ba93ed6..f35b8cea 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp @@ -100,16 +100,10 @@ Waypoint::Waypoint(char *line, Route *rte) } std::string Waypoint::str() -{ std::string ans = route->root + " " + label; - char coordstr[51]; - sprintf(coordstr, "%.15g", lat); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - ans += " ("; - ans += coordstr; - ans += ','; - sprintf(coordstr, "%.15g", lng); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - ans += coordstr; +{ std::string ans = route->root + " " + label + " ("; + char s[51]; int + e=sprintf(s,"%.15g",lat); if (lat==int(lat)) strcpy(s+e,".0"); ans+=s; ans+=','; + e=sprintf(s,"%.15g",lng); if (lng==int(lng)) strcpy(s+e,".0"); ans+=s; return ans + ')'; } @@ -238,15 +232,11 @@ void Waypoint::nmplogs(std::unordered_set &nmpfps, std::ofstream &n // make sure we only plot once, since the NMP should be listed // both ways (other_w in w's list, w in other_w's list) if (sort_root_at_label(this, other_w)) - { char coordstr[51]; - - nmpnmp << root_at_label(); - sprintf(coordstr, " %.15g", lat); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - nmpnmp << coordstr; - sprintf(coordstr, " %.15g", lng); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - nmpnmp << coordstr; + { char s[51]; + #define PYTHON_STYLE_FLOAT(F) e=sprintf(s," %.15g",F); if (F==int(F)) strcpy(s+e,".0"); nmpnmp< &nmpfps, std::ofstream &n nmpnmp << '\n'; nmpnmp << other_w->root_at_label(); - sprintf(coordstr, " %.15g", other_w->lat); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - nmpnmp << coordstr; - sprintf(coordstr, " %.15g", other_w->lng); - if (!strchr(coordstr, '.')) strcat(coordstr, ".0"); // add single trailing zero to ints for compatibility with Python - nmpnmp << coordstr; + PYTHON_STYLE_FLOAT(other_w->lat) + PYTHON_STYLE_FLOAT(other_w->lng) if (fp || li) { nmpnmp << ' '; if (fp) nmpnmp << "FP"; if (li) nmpnmp << "LI"; } nmpnmp << '\n'; + #undef PYTHON_STYLE_FLOAT } } // indicate if this was in the FP list or if it's off by exact amt diff --git a/siteupdate/cplusplus/functions/sql_file.cpp b/siteupdate/cplusplus/functions/sql_file.cpp index 3b0ad09e..6bc8b3c7 100644 --- a/siteupdate/cplusplus/functions/sql_file.cpp +++ b/siteupdate/cplusplus/functions/sql_file.cpp @@ -23,7 +23,7 @@ void sqlfile1 std::list *updates, std::list *systemupdates, std::mutex* term_mtx - ){ + ){ char fstr[65]; // Once all data is read in and processed, create a .sql file that will // create all of the DB tables to be used by other parts of the project std::ofstream sqlfile(Args::databasename+".sql"); @@ -142,7 +142,9 @@ void sqlfile1 for (Route *r : h->route_list) { if (!first) sqlfile << ','; first = 0; - sqlfile << "(" << r->csv_line() << ",'" << csvOrder << "')\n"; + sprintf(fstr, "%.17g", r->mileage); + sqlfile << "('" << r->system->systemname << "','" << r->region->code << "','" << r->route << "','" << r->banner << "','" << r->abbrev + << "','" << double_quotes(r->city) << "','" << r->root << "','" << fstr << "','" << r->rootOrder << "','" << csvOrder << "')\n"; csvOrder += 1; } sqlfile << ";\n"; @@ -164,7 +166,9 @@ void sqlfile1 for (ConnectedRoute *cr : h->con_route_list) { if (!first) sqlfile << ','; first = 0; - sqlfile << "(" << cr->csv_line() << ",'" << csvOrder << "')\n"; + sprintf(fstr, "','%.15g'", cr->mileage); + sqlfile << "('" << cr->system->systemname << "','" << cr->route << "','" << cr->banner << "','" << double_quotes(cr->groupname) + << "','" << (cr->roots.size() ? cr->roots[0]->root.data() : "ERROR_NO_ROOTS") << fstr << ",'" << csvOrder << "')\n"; csvOrder += 1; } sqlfile << ";\n"; @@ -269,7 +273,6 @@ void sqlfile1 { if (region->active_only_mileage+region->active_preview_mileage == 0) continue; if (!first) sqlfile << ','; first = 0; - char fstr[65]; sprintf(fstr, "','%.15g','%.15g')\n", region->active_only_mileage, region->active_preview_mileage); sqlfile << "('" << region->code << fstr; } @@ -290,7 +293,6 @@ void sqlfile1 for (std::pair& rm : h->mileage_by_region) { if (!first) sqlfile << ','; first = 0; - char fstr[35]; sprintf(fstr, "','%.15f')\n", rm.second); sqlfile << "('" << h->systemname << "','" << rm.first->code << fstr; } @@ -313,7 +315,6 @@ void sqlfile1 double active_miles = 0; if (t->active_only_mileage_by_region.count(rm.first)) active_miles = t->active_only_mileage_by_region.at(rm.first); - char fstr[65]; sprintf(fstr, "','%.15g','%.15g')\n", active_miles, rm.second); sqlfile << "('" << rm.first->code << "','" << t->traveler_name << fstr; } From 6e0693fba6462f2ef4fca142ce87882a9171e1ad Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Fri, 18 Mar 2022 02:03:39 -0400 Subject: [PATCH 4/7] fix potential buffer overflow This was introduced after the switch to clang, but only g++ reports it. --- .../cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp index a834272d..17af1e35 100644 --- a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp +++ b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp @@ -62,7 +62,7 @@ void WaypointQuadtree::insert(Waypoint *w, bool init) // DUPLICATE_COORDS datacheck for (Waypoint* p : *other_w->colocated) if (p->route == w->route) - { char fstr[44]; + { char fstr[48]; sprintf(fstr, "(%.15g,%.15g)", w->lat, w->lng); Datacheck::add(w->route, p->label, w->label, "", "DUPLICATE_COORDS", fstr); } From a3b35e045d0e319e37bd8641614b6b14c54d3531 Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Fri, 18 Mar 2022 10:03:40 -0400 Subject: [PATCH 5/7] deletion comments --- .../cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp | 1 + siteupdate/cplusplus/siteupdate.cpp | 1 + siteupdate/cplusplus/tasks/subgraphs/area.cpp | 1 + siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp | 1 + siteupdate/cplusplus/tasks/subgraphs/multisystem.cpp | 1 + 5 files changed, 5 insertions(+) diff --git a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp index 17af1e35..2d3fa831 100644 --- a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp +++ b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp @@ -294,6 +294,7 @@ void WaypointQuadtree::terminal_nodes(std::forward_list* node void WaypointQuadtree::sort() { std::forward_list* nodes = new std::forward_list[Args::numthreads]; + // deleted @ end of this function size_t slot = 0; terminal_nodes(nodes, slot); diff --git a/siteupdate/cplusplus/siteupdate.cpp b/siteupdate/cplusplus/siteupdate.cpp index a0033a4c..25214e6e 100644 --- a/siteupdate/cplusplus/siteupdate.cpp +++ b/siteupdate/cplusplus/siteupdate.cpp @@ -477,6 +477,7 @@ int main(int argc, char *argv[]) cout << et.et() << "Augmenting travelers for detected concurrent segments." << flush; #ifdef threading_enabled list* augment_lists = new list[Args::numthreads]; + // deleted once written to concurrencies.log TravelerList::tl_it = TravelerList::allusers.begin(); THREADLOOP thr[t] = thread(ConcAugThread, t, &list_mtx, augment_lists+t); THREADLOOP thr[t].join(); diff --git a/siteupdate/cplusplus/tasks/subgraphs/area.cpp b/siteupdate/cplusplus/tasks/subgraphs/area.cpp index 707a8bbb..ff583585 100644 --- a/siteupdate/cplusplus/tasks/subgraphs/area.cpp +++ b/siteupdate/cplusplus/tasks/subgraphs/area.cpp @@ -12,6 +12,7 @@ while (getline(file, line)) { if (line.empty()) continue; vector fields; char *cline = new char[line.size()+1]; + // deleted @ end of this while loop after tokens are processed strcpy(cline, line.data()); for (char *token = strtok(cline, ";"); token; token = strtok(0, ";")) fields.push_back(token); if (fields.size() != 5) diff --git a/siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp b/siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp index 5ab091ae..b6687b3a 100644 --- a/siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp +++ b/siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp @@ -12,6 +12,7 @@ while (getline(file, line)) { if (line.empty()) continue; vector fields; char *cline = new char[line.size()+1]; + // deleted @ end of this while loop after tokens are processed strcpy(cline, line.data()); for (char *token = strtok(cline, ";"); token; token = strtok(0, ";")) fields.push_back(token); if (fields.size() != 3) diff --git a/siteupdate/cplusplus/tasks/subgraphs/multisystem.cpp b/siteupdate/cplusplus/tasks/subgraphs/multisystem.cpp index 524a92be..8ba22552 100644 --- a/siteupdate/cplusplus/tasks/subgraphs/multisystem.cpp +++ b/siteupdate/cplusplus/tasks/subgraphs/multisystem.cpp @@ -11,6 +11,7 @@ while (getline(file, line)) { if (line.empty()) continue; vector fields; char *cline = new char[line.size()+1]; + // deleted @ end of this while loop after tokens are processed strcpy(cline, line.data()); for (char *token = strtok(cline, ";"); token; token = strtok(0, ";")) fields.push_back(token); if (fields.size() != 3) From 3ae68f11245a867ec2677424232eabbea28b244d Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Fri, 18 Mar 2022 11:29:11 -0400 Subject: [PATCH 6/7] Don't store update in TravelerList object Doesn't need to be permanent. --- siteupdate/cplusplus/classes/Route/Route.cpp | 4 ++-- siteupdate/cplusplus/classes/Route/Route.h | 2 +- .../classes/TravelerList/TravelerList.cpp | 2 ++ .../cplusplus/classes/TravelerList/TravelerList.h | 1 - .../TravelerList/mark_chopped_route_segments.cpp | 2 +- .../TravelerList/mark_connected_route_segments.cpp | 14 +++++++------- siteupdate/cplusplus/siteupdate.cpp | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/siteupdate/cplusplus/classes/Route/Route.cpp b/siteupdate/cplusplus/classes/Route/Route.cpp index 097e2c2c..365090ed 100644 --- a/siteupdate/cplusplus/classes/Route/Route.cpp +++ b/siteupdate/cplusplus/classes/Route/Route.cpp @@ -214,14 +214,14 @@ void Route::write_nmp_merged() wptfile.close(); } -void Route::store_traveled_segments(TravelerList* t, std::ofstream& log, unsigned int beg, unsigned int end) +void Route::store_traveled_segments(TravelerList* t, std::ofstream& log, std::string* update, unsigned int beg, unsigned int end) { // store clinched segments with traveler and traveler with segments for (unsigned int pos = beg; pos < end; pos++) { HighwaySegment *hs = segment_list[pos]; hs->add_clinched_by(t); t->clinched_segments.insert(hs); } - if (last_update && t->updated_routes.insert(this).second && t->update && last_update[0] >= *t->update) + if (last_update && t->updated_routes.insert(this).second && update && last_update[0] >= *update) log << "Route updated " << last_update[0] << ": " << readable_name() << '\n'; } diff --git a/siteupdate/cplusplus/classes/Route/Route.h b/siteupdate/cplusplus/classes/Route/Route.h index 25b7c912..df1f823c 100644 --- a/siteupdate/cplusplus/classes/Route/Route.h +++ b/siteupdate/cplusplus/classes/Route/Route.h @@ -94,7 +94,7 @@ class Route double clinched_by_traveler(TravelerList *); //std::string list_line(int, int); void write_nmp_merged(); - void store_traveled_segments(TravelerList*, std::ofstream &, unsigned int, unsigned int); + void store_traveled_segments(TravelerList*, std::ofstream&, std::string*, unsigned int, unsigned int); void compute_stats_r(); void con_mismatch(); Waypoint* con_beg(); diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp index c03d7dd9..09cd15a6 100644 --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp @@ -28,6 +28,7 @@ TravelerList::TravelerList(std::string travname, std::string* updarr[], ErrorLis // variables used in construction unsigned int list_entries = 0; std::ofstream splist; + std::string* update; if (Args::splitregionpath != "") splist.open(Args::splitregionpath+"/list_files/"+travname); // init user log @@ -136,6 +137,7 @@ TravelerList::TravelerList(std::string travname, std::string* updarr[], ErrorLis #undef UPDATE_NOTE } delete[] listdata; + if (update) delete update; log << "Processed " << list_entries << " good lines marking " << clinched_segments.size() << " segments traveled.\n"; log.close(); splist.close(); diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.h b/siteupdate/cplusplus/classes/TravelerList/TravelerList.h index 990129e1..9dad22a2 100644 --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.h +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.h @@ -24,7 +24,6 @@ class TravelerList public: std::unordered_set clinched_segments; std::string traveler_name; - std::string *update; std::unordered_map active_preview_mileage_by_region; // total mileage per region, active+preview only std::unordered_map active_only_mileage_by_region; // total mileage per region, active only std::unordered_map> system_region_mileages; // mileage per region per system diff --git a/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp b/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp index 67af95dd..f7e7a400 100644 --- a/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/mark_chopped_route_segments.cpp @@ -112,7 +112,7 @@ else { r->system->lniu_mtx.lock(); index2 = lit1->second; reverse = 1; } - r->store_traveled_segments(this, log, index1, index2); + r->store_traveled_segments(this, log, update, index1, index2); // new .list lines for region split-ups if (Args::splitregion == r->region->code) { diff --git a/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp b/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp index 7e24818b..86af51fc 100644 --- a/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/mark_connected_route_segments.cpp @@ -119,8 +119,8 @@ if (r1 == r2) continue; } if (index1 <= index2) - r1->store_traveled_segments(this, log, index1, index2); - else r1->store_traveled_segments(this, log, index2, index1); + r1->store_traveled_segments(this, log, update, index1, index2); + else r1->store_traveled_segments(this, log, update, index2, index1); } else { // user log warning for DISCONNECTED_ROUTE errors if (r1->con_route->disconnected) @@ -145,15 +145,15 @@ else { // user log warning for DISCONNECTED_ROUTE errors } // mark the beginning chopped route from index1 to its end if (r1->is_reversed()) - r1->store_traveled_segments(this, log, 0, index1); - else r1->store_traveled_segments(this, log, index1, r1->segment_list.size()); + r1->store_traveled_segments(this, log, update, 0, index1); + else r1->store_traveled_segments(this, log, update, index1, r1->segment_list.size()); // mark the ending chopped route from its beginning to index2 if (r2->is_reversed()) - r2->store_traveled_segments(this, log, index2, r2->segment_list.size()); - else r2->store_traveled_segments(this, log, 0, index2); + r2->store_traveled_segments(this, log, update, index2, r2->segment_list.size()); + else r2->store_traveled_segments(this, log, update, 0, index2); // mark any intermediate chopped routes in their entirety. for (size_t r = r1->rootOrder+1; r < r2->rootOrder; r++) - r1->con_route->roots[r]->store_traveled_segments(this, log, 0, r1->con_route->roots[r]->segment_list.size()); + r1->con_route->roots[r]->store_traveled_segments(this, log, update, 0, r1->con_route->roots[r]->segment_list.size()); } // both labels are valid; mark in use & proceed r1->system->lniu_mtx.lock(); diff --git a/siteupdate/cplusplus/siteupdate.cpp b/siteupdate/cplusplus/siteupdate.cpp index 25214e6e..b494bc91 100644 --- a/siteupdate/cplusplus/siteupdate.cpp +++ b/siteupdate/cplusplus/siteupdate.cpp @@ -342,7 +342,7 @@ int main(int argc, char *argv[]) { size_t NumFields = 4; string** fields = new string*[4]; fields[0] = new string; // deleted upon construction of unordered_map element - fields[1] = new string; // stays in TravelerList object + fields[1] = new string; // deleted @ end of TravelerList ctor fields[2] = new string; // deleted once written to user log fields[3] = new string; // deleted once written to user log split(line, fields, NumFields, ' '); From d56e5a67555922d06730363ca779c0acdc224dac Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Sat, 19 Mar 2022 18:56:51 -0400 Subject: [PATCH 7/7] refactor update retrieval into TravelerList ctor cherrypick 1d29004ed4436f8b1d8db5b36f8e2587f3d37d48 --- .../cplusplus/classes/TravelerList/TravelerList.cpp | 10 ++++++---- .../cplusplus/classes/TravelerList/TravelerList.h | 2 +- siteupdate/cplusplus/siteupdate.cpp | 8 +------- siteupdate/cplusplus/threads/ReadListThread.cpp | 7 +------ 4 files changed, 9 insertions(+), 18 deletions(-) diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp index 09cd15a6..658690b9 100644 --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp @@ -11,7 +11,7 @@ #include "../../functions/upper.h" #include -TravelerList::TravelerList(std::string travname, std::string* updarr[], ErrorList* el) +TravelerList::TravelerList(std::string travname, ErrorList* el) { // initialize object variables active_systems_traveled = 0; active_systems_clinched = 0; @@ -39,13 +39,15 @@ TravelerList::TravelerList(std::string travname, std::string* updarr[], ErrorLis log << ctime(&StartTime); mtx.unlock(); // write last update date & time if known - if (updarr) - { log << travname << " last updated: " << *updarr[1] << ' ' << *updarr[2] << ' ' << *updarr[3] << '\n'; + try { std::string** updarr = TravelerList::listupdates.at(travname); + log << travname << " last updated: " << *updarr[1] << ' ' << *updarr[2] << ' ' << *updarr[3] << '\n'; update = updarr[1]; delete updarr[2]; delete updarr[3]; delete[] updarr; - } else update = 0; + } catch (const std::out_of_range& oor) + { update = 0; + } // read .list file into memory // we can't getline here because it only allows one delimiter, and we need two; '\r' and '\n'. diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.h b/siteupdate/cplusplus/classes/TravelerList/TravelerList.h index 9dad22a2..bc092b80 100644 --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.h +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.h @@ -43,7 +43,7 @@ class TravelerList static std::list::iterator tl_it; static std::unordered_map listupdates; - TravelerList(std::string, std::string*[], ErrorList*); + TravelerList(std::string, ErrorList*); double active_only_miles(); double active_preview_miles(); double system_region_miles(HighwaySystem *); diff --git a/siteupdate/cplusplus/siteupdate.cpp b/siteupdate/cplusplus/siteupdate.cpp index b494bc91..015ea3df 100644 --- a/siteupdate/cplusplus/siteupdate.cpp +++ b/siteupdate/cplusplus/siteupdate.cpp @@ -370,13 +370,7 @@ int main(int argc, char *argv[]) #else for (string &t : TravelerList::ids) { cout << t << ' ' << std::flush; - std::string** update; - try { update = TravelerList::listupdates.at(t); - } - catch (const std::out_of_range& oor) - { update = 0; - } - TravelerList::allusers.push_back(new TravelerList(t, update, &el)); + TravelerList::allusers.push_back(new TravelerList(t, &el)); } #endif TravelerList::ids.clear(); diff --git a/siteupdate/cplusplus/threads/ReadListThread.cpp b/siteupdate/cplusplus/threads/ReadListThread.cpp index 9eab9f61..d5a07daf 100644 --- a/siteupdate/cplusplus/threads/ReadListThread.cpp +++ b/siteupdate/cplusplus/threads/ReadListThread.cpp @@ -13,12 +13,7 @@ void ReadListThread(unsigned int id, std::mutex* tl_mtx, ErrorList* el) std::cout << tl << ' ' << std::flush; tl_mtx->unlock(); std::string** update; - try { update = TravelerList::listupdates.at(tl); - } - catch (const std::out_of_range& oor) - { update = 0; - } - TravelerList *t = new TravelerList(tl, update, el); + TravelerList *t = new TravelerList(tl, el); // deleted on termination of program TravelerList::mtx.lock(); TravelerList::allusers.push_back(t);