Cppcheck: use more STD algorithms instead of loops

sql-rework
Tomasz Golinski 2020-07-06 17:06:59 +02:00
rodzic f2b0be9e99
commit 67d05f0742
3 zmienionych plików z 18 dodań i 24 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
#include "common.h" #include "common.h"
#include <algorithm> #include <algorithm>
#include <numeric>
#include <vector> #include <vector>
#include <iterator> #include <iterator>
@ -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 average(const Caches& cc, float Cache::*ptr) {
float count = 0; return std::accumulate(cc.begin(), cc.end(), 0., [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size();
for (auto el : cc)
count += el.*ptr;
return count / cc.size();
} }
float average(const Caches& cc, int Cache::*ptr) { float average(const Caches& cc, int Cache::*ptr) {
int count = 0; return std::accumulate(cc.begin(), cc.end(), 0, [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size();
for (auto el : cc)
count += el.*ptr;
return (1.0 * count) / cc.size();
} }

Wyświetl plik

@ -17,11 +17,11 @@ BoundingBox::BoundingBox(Position p1, Position p2) {
b.lon = std::max(p1.lon, p2.lon); 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); 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); 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; name = reg.first;
contour = reg.second; contour = reg.second;
for (auto& el : parse_geojson(geojson_sub)) auto geodata = parse_geojson(geojson_sub);
subregions.emplace_back(new Region(el.second, el.first)); 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() { Region::~Region() {
@ -100,13 +102,10 @@ Country::Country(const std::vector<std::filesystem::path>& jsons) {
} }
void Country::locate(Cache& c) { void Country::locate(Cache& c) {
Position p = c.pos; auto el = std::find_if(regions.begin(), regions.end(), [&](auto a) { return a->within(c.pos); });
for (auto& el : regions) { if (el != regions.end()) {
if (el->within(p)) { c.region = (*el)->get_name();
c.region = el->get_name(); c.subregion = (*el)->find_subregion(c.pos);
c.subregion = el->find_subregion(p);
Debug(3) << "Found subregion " << c.subregion; Debug(3) << "Found subregion " << c.subregion;
return;
}
} }
} }

Wyświetl plik

@ -17,7 +17,7 @@ private:
Position a; Position a;
Position b; Position b;
public: public:
inline bool within(Position p); inline bool within(Position p) const;
BoundingBox(Position p1, Position p2); BoundingBox(Position p1, Position p2);
}; };
@ -28,7 +28,7 @@ private:
BoundingBox bbox; BoundingBox bbox;
public: public:
explicit ContourData(BoundingBox bbox); explicit ContourData(BoundingBox bbox);
bool within(Position p); bool within(Position p) const;
void add_point(Position p); void add_point(Position p);
}; };
@ -44,8 +44,8 @@ public:
Region(const std::filesystem::path& geojson, const std::filesystem::path& geojson_sub); Region(const std::filesystem::path& geojson, const std::filesystem::path& geojson_sub);
~Region(); ~Region();
bool within(Position p) { return contour->within(p); } bool within(Position p) const { return contour->within(p); }
std::string get_name() { return name; } std::string get_name() const { return name; }
std::string find_subregion(Position p); std::string find_subregion(Position p);
}; };