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 <algorithm>
#include <numeric>
#include <vector>
#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 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();
}

Wyświetl plik

@ -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<std::filesystem::path>& 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;
}
}

Wyświetl plik

@ -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);
};