Skip to content

Commit

Permalink
region management / remove redundant computation
Browse files Browse the repository at this point in the history
throw if Region ctor fails
#220 remove vestigial std::string** update;
Region::allregions-> list
TravelMapping#523 redundant active/preview mileage
TravelMapping#522 redundant active/preview system counts
  • Loading branch information
yakra committed Apr 23, 2022
1 parent cc9da66 commit e36c526
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 106 deletions.
7 changes: 6 additions & 1 deletion siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

std::list<HighwaySystem*> HighwaySystem::syslist;
std::list<HighwaySystem*>::iterator HighwaySystem::it;
unsigned int HighwaySystem::num_active = 0;
unsigned int HighwaySystem::num_preview = 0;

HighwaySystem::HighwaySystem(std::string &line, ErrorList &el, std::vector<std::pair<std::string,std::string>> &countries)
{ std::ifstream file;
Expand Down Expand Up @@ -54,7 +56,10 @@ HighwaySystem::HighwaySystem(std::string &line, ErrorList &el, std::vector<std::
level = level_str[0];
if (level_str != "active" && level_str != "preview" && level_str != "devel")
el.add_error("Unrecognized level in " + Args::systemsfile + " line: " + line);

switch(level)
{ case 'p': num_preview++; break;
case 'a': num_active++;
}
std::cout << systemname << '.' << std::flush;

// read chopped routes CSV
Expand Down
2 changes: 2 additions & 0 deletions siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class HighwaySystem

static std::list<HighwaySystem*> syslist;
static std::list<HighwaySystem*>::iterator it;
static unsigned int num_active;
static unsigned int num_preview;

HighwaySystem(std::string &, ErrorList &, std::vector<std::pair<std::string,std::string>> &);

Expand Down
8 changes: 2 additions & 6 deletions siteupdate/cplusplus/classes/Region/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ std::pair<std::string, std::string> *country_or_continent_by_code(std::string co
return 0;
}

std::vector<Region*> Region::allregions;
std::list<Region> Region::allregions;
std::unordered_map<std::string, Region*> Region::code_hash;

Region::Region (const std::string &line,
Expand All @@ -26,12 +26,8 @@ Region::Region (const std::string &line,
split(line, fields, NumFields, ';');
if (NumFields != 5)
{ el.add_error("Could not parse regions.csv line: [" + line + "], expected 5 fields, found " + std::to_string(NumFields));
continent = country_or_continent_by_code("error", continents);
country = country_or_continent_by_code("error", countries);
is_valid = 0;
return;
throw 1;
}
is_valid = 1;
// code
if (code.size() > DBFieldLength::regionCode)
el.add_error("Region code > " + std::to_string(DBFieldLength::regionCode)
Expand Down
4 changes: 2 additions & 2 deletions siteupdate/cplusplus/classes/Region/Region.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ErrorList;
class HGVertex;
class Waypoint;
#include <list>
#include <mutex>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -46,9 +47,8 @@ class Region
double overall_mileage;
std::mutex mtx;
std::vector<std::pair<HGVertex*,Waypoint*>> vertices;
bool is_valid;

static std::vector<Region*> allregions;
static std::list<Region> allregions;
static std::unordered_map<std::string, Region*> code_hash;

Region (const std::string&,
Expand Down
18 changes: 5 additions & 13 deletions siteupdate/cplusplus/classes/TravelerList/userlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,9 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
<< format_clinched_mi(active_preview_mileage_by_region.at(region), region->active_preview_mileage) << '\n';
}

unsigned int active_systems = 0;
unsigned int preview_systems = 0;

// present stats by system here, also generate entries for
// DB table clinchedSystemMileageByRegion as we compute and
// have the data handy
for (HighwaySystem *h : HighwaySystem::syslist)
if (h->active_or_preview())
{ if (h->active()) active_systems++;
else preview_systems++;
double t_system_overall = 0;
{ double t_system_overall = 0;
if (system_region_mileages.count(h))
t_system_overall = system_region_miles(h);
if (t_system_overall)
Expand Down Expand Up @@ -110,13 +102,13 @@ void TravelerList::userlog(const double total_active_only_miles, const double to

// grand summary, active only
sprintf(fstr,"\nTraveled %i of %i (%.1f%%), Clinched %i of %i (%.1f%%) active systems",
active_systems_traveled, active_systems, 100*(double)active_systems_traveled/active_systems,
active_systems_clinched, active_systems, 100*(double)active_systems_clinched/active_systems);
active_systems_traveled, HighwaySystem::num_active, 100*(double)active_systems_traveled/HighwaySystem::num_active,
active_systems_clinched, HighwaySystem::num_active, 100*(double)active_systems_clinched/HighwaySystem::num_active);
log << fstr << '\n';
// grand summary, active+preview
sprintf(fstr,"Traveled %i of %i (%.1f%%), Clinched %i of %i (%.1f%%) preview systems",
preview_systems_traveled, preview_systems, 100*(double)preview_systems_traveled/preview_systems,
preview_systems_clinched, preview_systems, 100*(double)preview_systems_clinched/preview_systems);
preview_systems_traveled, HighwaySystem::num_preview, 100*(double)preview_systems_traveled/HighwaySystem::num_preview,
preview_systems_clinched, HighwaySystem::num_preview, 100*(double)preview_systems_clinched/HighwaySystem::num_preview);
log << fstr << '\n';

// updated routes, sorted by date
Expand Down
14 changes: 5 additions & 9 deletions siteupdate/cplusplus/functions/allbyregionactiveonly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
#include "../threads/threads.h"
#include <fstream>

void allbyregionactiveonly(std::mutex* mtx)
{ double total_mi;
char fstr[112];
void allbyregionactiveonly(std::mutex* mtx, double total_mi)
{ char fstr[112];
std::ofstream allfile(Args::csvstatfilepath + "/allbyregionactiveonly.csv");
allfile << "Traveler,Total";
std::list<Region*> regions;
total_mi = 0;
for (Region* r : Region::allregions)
if (r->active_only_mileage)
{ regions.push_back(r);
total_mi += r->active_only_mileage;
}
for (Region& r : Region::allregions)
if (r.active_only_mileage)
regions.push_back(&r);
regions.sort(sort_regions_by_code);
for (Region *region : regions)
allfile << ',' << region->code;
Expand Down
14 changes: 5 additions & 9 deletions siteupdate/cplusplus/functions/allbyregionactivepreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
#include "../threads/threads.h"
#include <fstream>

void allbyregionactivepreview(std::mutex* mtx)
{ double total_mi;
char fstr[112];
void allbyregionactivepreview(std::mutex* mtx, double total_mi)
{ char fstr[112];
std::ofstream allfile(Args::csvstatfilepath + "/allbyregionactivepreview.csv");
allfile << "Traveler,Total";
std::list<Region*> regions;
total_mi = 0;
for (Region* r : Region::allregions)
if (r->active_preview_mileage)
{ regions.push_back(r);
total_mi += r->active_preview_mileage;
}
for (Region& r : Region::allregions)
if (r.active_preview_mileage)
regions.push_back(&r);
regions.sort(sort_regions_by_code);
for (Region *region : regions)
allfile << ',' << region->code;
Expand Down
16 changes: 8 additions & 8 deletions siteupdate/cplusplus/functions/sql_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ void sqlfile1
sqlfile << "PRIMARY KEY(code), FOREIGN KEY (country) REFERENCES countries(code), FOREIGN KEY (continent) REFERENCES continents(code));\n";
sqlfile << "INSERT INTO regions VALUES\n";
first = 1;
for (size_t r = 0; r < Region::allregions.size()-1; r++)
for (auto r = Region::allregions.begin(); &*r != &*Region::allregions.rbegin(); r++)
{ if (!first) sqlfile << ',';
first = 0;
sqlfile << "('" << Region::allregions[r]->code << "','" << double_quotes(Region::allregions[r]->name)
<< "','" << Region::allregions[r]->country_code() << "','" << Region::allregions[r]->continent_code()
<< "','" << Region::allregions[r]->type << "')\n";
sqlfile << "('" << r->code << "','" << double_quotes(r->name)
<< "','" << r->country_code() << "','" << r->continent_code()
<< "','" << r->type << "')\n";
}
sqlfile << ";\n";

Expand Down Expand Up @@ -271,12 +271,12 @@ void sqlfile1
<< "), activeMileage DOUBLE, activePreviewMileage DOUBLE);\n";
sqlfile << "INSERT INTO overallMileageByRegion VALUES\n";
first = 1;
for (Region* region : Region::allregions)
{ if (region->active_only_mileage+region->active_preview_mileage == 0) continue;
for (Region& region : Region::allregions)
{ if (region.active_only_mileage+region.active_preview_mileage == 0) continue;
if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "','%.17g','%.17g')\n", region->active_only_mileage, region->active_preview_mileage);
sqlfile << "('" << region->code << fstr;
sprintf(fstr, "','%.17g','%.17g')\n", region.active_only_mileage, region.active_preview_mileage);
sqlfile << "('" << region.code << fstr;
}
sqlfile << ";\n";

Expand Down
45 changes: 21 additions & 24 deletions siteupdate/cplusplus/siteupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ This module defines classes to represent the contents of a
#include <thread>
#include "threads/threads.h"
#endif
void allbyregionactiveonly(std::mutex*);
void allbyregionactivepreview(std::mutex*);
void allbyregionactiveonly(std::mutex*, double);
void allbyregionactivepreview(std::mutex*, double);
using namespace std;

int main(int argc, char *argv[])
Expand Down Expand Up @@ -161,18 +161,15 @@ int main(int argc, char *argv[])
while(getline(file, line))
{ if (line.back() == 0x0D) line.erase(line.end()-1); // trim DOS newlines
if (line.empty()) continue;
Region* r = new Region(line, countries, continents, el);
// deleted on termination of program
if (r->is_valid)
{ Region::allregions.push_back(r);
Region::code_hash[r->code] = r;
} else delete r;
try { Region::allregions.emplace_back(line, countries, continents, el);
Region::code_hash[Region::allregions.back().code] = &Region::allregions.back();
} catch (int&err) {}
}
}
file.close();
// create a dummy region to catch unrecognized region codes in .csv files
Region::allregions.push_back(new Region("error;unrecognized region code;error;error;unrecognized region code", countries, continents, el));
Region::code_hash[Region::allregions.back()->code] = Region::allregions.back();
Region::allregions.emplace_back("error;unrecognized region code;error;error;unrecognized region code", countries, continents, el);
Region::code_hash[Region::allregions.back().code] = &Region::allregions.back();

// Create a list of HighwaySystem objects, one per system in systems.csv file
cout << et.et() << "Reading systems list in " << Args::highwaydatapath << "/" << Args::systemsfile << "." << endl;
Expand Down Expand Up @@ -503,10 +500,10 @@ int main(int argc, char *argv[])
double active_only_miles = 0;
double active_preview_miles = 0;
double overall_miles = 0;
for (Region* r : Region::allregions)
{ active_only_miles += r->active_only_mileage;
active_preview_miles += r->active_preview_mileage;
overall_miles += r->overall_mileage;
for (Region& r : Region::allregions)
{ active_only_miles += r.active_only_mileage;
active_preview_miles += r.active_preview_mileage;
overall_miles += r.overall_mileage;
}
sprintf(fstr, "Active routes (active): %.2f mi\n", active_only_miles);
hdstatsfile << fstr;
Expand All @@ -519,11 +516,11 @@ int main(int argc, char *argv[])
// a nice enhancement later here might break down by continent, then country,
// then region
list<string> region_entries;
for (Region* region : Region::allregions)
if (region->overall_mileage)
for (Region& region : Region::allregions)
if (region.overall_mileage)
{ sprintf(fstr, ": %.2f (active), %.2f (active, preview) %.2f (active, preview, devel)\n",
region->active_only_mileage, region->active_preview_mileage, region->overall_mileage);
region_entries.push_back(region->code + fstr);
region.active_only_mileage, region.active_preview_mileage, region.overall_mileage);
region_entries.push_back(region.code + fstr);
}
region_entries.sort();
for (string& e : region_entries) hdstatsfile << e;
Expand Down Expand Up @@ -579,22 +576,22 @@ int main(int argc, char *argv[])
{ case 1:
#endif
cout << et.et() << "Writing allbyregionactiveonly.csv." << endl;
allbyregionactiveonly(0);
allbyregionactiveonly(0, active_only_miles);
cout << et.et() << "Writing allbyregionactivepreview.csv." << endl;
allbyregionactivepreview(0);
allbyregionactivepreview(0, active_preview_miles);
cout << et.et() << "Writing per-system stats csv files." << endl;
for (HighwaySystem* h : HighwaySystem::syslist) h->stats_csv();
#ifdef threading_enabled
break;
case 2:
thr[0] = thread(allbyregionactiveonly, &list_mtx);
thr[1] = thread(allbyregionactivepreview, &list_mtx);
thr[0] = thread(allbyregionactiveonly, &list_mtx, active_only_miles);
thr[1] = thread(allbyregionactivepreview, &list_mtx, active_preview_miles);
thr[0].join();
thr[1].join();
break;
default:
thr[0] = thread(allbyregionactiveonly, (std::mutex*)0);
thr[1] = thread(allbyregionactivepreview, (std::mutex*)0);
thr[0] = thread(allbyregionactiveonly, nullptr, active_only_miles);
thr[1] = thread(allbyregionactivepreview, nullptr, active_preview_miles);
thr[2] = thread(StatsCsvThread, 2, &list_mtx);
thr[0].join();
thr[1].join();
Expand Down
6 changes: 3 additions & 3 deletions siteupdate/cplusplus/tasks/subgraphs/continent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ cout << et.et() << "Creating continent graphs." << endl;
for (size_t c = 0; c < continents.size()-1; c++)
{ regions = new list<Region*>;
// deleted @ end of HighwayGraph::write_subgraphs_tmg
for (Region* r : Region::allregions)
for (Region& r : Region::allregions)
// does it match this continent and have routes?
if (&continents[c] == r->continent && r->active_preview_mileage)
regions->push_back(r);
if (&continents[c] == r.continent && r.active_preview_mileage)
regions->push_back(&r);
// generate for any continent with at least 1 region with mileage
if (regions->size() < 1) delete regions;
else { GraphListEntry::add_group(
Expand Down
6 changes: 3 additions & 3 deletions siteupdate/cplusplus/tasks/subgraphs/country.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ cout << et.et() << "Creating country graphs." << endl;
for (size_t c = 0; c < countries.size()-1; c++)
{ regions = new list<Region*>;
// deleted @ end of HighwayGraph::write_subgraphs_tmg
for (Region* r : Region::allregions)
for (Region& r : Region::allregions)
// does it match this country and have routes?
if (&countries[c] == r->country && r->active_preview_mileage)
regions->push_back(r);
if (&countries[c] == r.country && r.active_preview_mileage)
regions->push_back(&r);
// does it have at least two? if none, no data,
// if 1 we already generated a graph for that one region
if (regions->size() < 2) delete regions;
Expand Down
6 changes: 3 additions & 3 deletions siteupdate/cplusplus/tasks/subgraphs/multiregion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ while (getline(file, line))
regions = new list<Region*>;
// deleted @ end of HighwayGraph::write_subgraphs_tmg
for(char* rg = strtok(fields[2], ","); rg; rg = strtok(0, ","))
for (Region* r : Region::allregions)
if (rg == r->code)
{ regions->push_back(r);
for (Region& r : Region::allregions)
if (rg == r.code)
{ regions->push_back(&r);
break;
}
GraphListEntry::add_group(fields[1], fields[0], 'R', regions, nullptr, nullptr);
Expand Down
10 changes: 5 additions & 5 deletions siteupdate/cplusplus/tasks/subgraphs/region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ cout << et.et() << "Creating regional data graphs." << endl;
// any active or preview systems

// add entries to graph vector
for (Region* region : Region::allregions)
{ if (region->active_preview_mileage == 0) continue;
regions = new list<Region*>(1, region);
for (Region& region : Region::allregions)
{ if (region.active_preview_mileage == 0) continue;
regions = new list<Region*>(1, &region);
// deleted @ end of HighwayGraph::write_subgraphs_tmg
GraphListEntry::add_group(
region->code + "-region",
region->name + " (" + region->type + ")",
region.code + "-region",
region.name + " (" + region.type + ")",
'r', regions, nullptr, nullptr);
}
#ifndef threading_enabled
Expand Down
1 change: 0 additions & 1 deletion siteupdate/cplusplus/threads/ReadListThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void ReadListThread(unsigned int id, std::mutex* tl_mtx, ErrorList* el)
//printf("ReadListThread %02i (*it)++\n", id); fflush(stdout);
std::cout << tl << ' ' << std::flush;
tl_mtx->unlock();
std::string** update;
TravelerList *t = new TravelerList(tl, el);
// deleted on termination of program
TravelerList::mtx.lock();
Expand Down
Loading

0 comments on commit e36c526

Please sign in to comment.