diff --git a/common.cpp b/common.cpp index d7a9196..941f8ea 100644 --- a/common.cpp +++ b/common.cpp @@ -1,6 +1,7 @@ #include "common.h" #include +#include #include #include @@ -251,15 +252,9 @@ void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption) } float average(const Caches& cc, float Cache::*ptr) { - float count = 0; - for (auto el : cc) - count += el.*ptr; - return count / cc.size(); + return std::accumulate(cc.begin(), cc.end(), 0., [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size(); } float average(const Caches& cc, int Cache::*ptr) { - int count = 0; - for (auto el : cc) - count += el.*ptr; - return (1.0 * count) / cc.size(); + return std::accumulate(cc.begin(), cc.end(), 0, [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size(); } diff --git a/region.cpp b/region.cpp index b74e8e6..fb32eeb 100644 --- a/region.cpp +++ b/region.cpp @@ -17,11 +17,11 @@ BoundingBox::BoundingBox(Position p1, Position p2) { b.lon = std::max(p1.lon, p2.lon); } -bool BoundingBox::within(Position p) { +bool BoundingBox::within(Position p) const { return (p.lat >= a.lat && p.lat <= b.lat && p.lon >= a.lon && p.lon <= b.lon); } -bool ContourData::within(Position p) { +bool ContourData::within(Position p) const { return bbox.within(p) && bg::within(point_t{p.lon, p.lat}, contour); } @@ -69,8 +69,10 @@ Region::Region(const std::filesystem::path& geojson, const std::filesystem::path name = reg.first; contour = reg.second; - for (auto& el : parse_geojson(geojson_sub)) - subregions.emplace_back(new Region(el.second, el.first)); + auto geodata = parse_geojson(geojson_sub); + std::transform(geodata.begin(), geodata.end(), std::back_inserter(subregions), [&](auto a) { return new Region(a.second, a.first); }); +// for (auto& el : parse_geojson(geojson_sub)) +// subregions.emplace_back(new Region(el.second, el.first)); } Region::~Region() { @@ -100,13 +102,10 @@ Country::Country(const std::vector& jsons) { } void Country::locate(Cache& c) { - Position p = c.pos; - for (auto& el : regions) { - if (el->within(p)) { - c.region = el->get_name(); - c.subregion = el->find_subregion(p); - Debug(3) << "Found subregion " << c.subregion; - return; - } + auto el = std::find_if(regions.begin(), regions.end(), [&](auto a) { return a->within(c.pos); }); + if (el != regions.end()) { + c.region = (*el)->get_name(); + c.subregion = (*el)->find_subregion(c.pos); + Debug(3) << "Found subregion " << c.subregion; } } diff --git a/region.h b/region.h index 10f0f09..85d2517 100644 --- a/region.h +++ b/region.h @@ -17,7 +17,7 @@ private: Position a; Position b; public: - inline bool within(Position p); + inline bool within(Position p) const; BoundingBox(Position p1, Position p2); }; @@ -28,7 +28,7 @@ private: BoundingBox bbox; public: explicit ContourData(BoundingBox bbox); - bool within(Position p); + bool within(Position p) const; void add_point(Position p); }; @@ -44,8 +44,8 @@ public: Region(const std::filesystem::path& geojson, const std::filesystem::path& geojson_sub); ~Region(); - bool within(Position p) { return contour->within(p); } - std::string get_name() { return name; } + bool within(Position p) const { return contour->within(p); } + std::string get_name() const { return name; } std::string find_subregion(Position p); };