Lambdafy some comparison functions

sql-rework
Tomasz Goliński 2019-11-07 18:00:05 +01:00
rodzic 53dc1f1c57
commit 4c9995dccf
3 zmienionych plików z 9 dodań i 25 usunięć

Wyświetl plik

@ -27,15 +27,3 @@ float Cache::distance() const {
std::string Cache::date() const {
return std::to_string(date_found.tm_year+1900) + '-' + std::to_string(date_found.tm_mon) + "-" + std::to_string(date_found.tm_mday);
}
bool CacheCmpNS(const Cache& lhs, const Cache& rhs) {
return lhs.pos.lat < rhs.pos.lat;
}
bool CacheCmpEW(const Cache& lhs, const Cache& rhs) {
return lhs.pos.lon < rhs.pos.lon;
}
bool CacheCmpDist(const Cache& lhs, const Cache& rhs) {
return lhs.distance() < rhs.distance();
}

Wyświetl plik

@ -72,8 +72,4 @@ public:
}
};
bool CacheCmpNS(const Cache& lhs, const Cache& rhs);
bool CacheCmpEW(const Cache& lhs, const Cache& rhs);
bool CacheCmpDist(const Cache& lhs, const Cache& rhs);
typedef std::set<Cache, CacheCmp> Caches;

Wyświetl plik

@ -47,16 +47,16 @@ void show_histogram(Caches* cc, std::string Cache::*ptr, std::string caption, bo
for (auto el : histogram)
pairs.push_back(el);
if (sort_by_val)
sort(pairs.begin(), pairs.end(), [=](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second > b.second; });
sort(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second > b.second; });
else
sort(pairs.begin(), pairs.end(), [=](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.first < b.first; });
sort(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.first < b.first; });
if (html) {
int max;
if (sort_by_val)
max = pairs[0].second;
else
max = std::max_element(pairs.begin(), pairs.end(), [=](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second < b.second; })->second;
max = std::max_element(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second < b.second; })->second;
int i = 0;
std::cout << "<h2>" << caption << "</h2>\n";
std::cout << "<dl class=\"histogram\">\n";
@ -256,10 +256,10 @@ int main(int argc, char** argv) {
}
if ((show_minmax || show_html) && !get_not_found) {
auto N = std::max_element(cc.begin(), cc.end(), CacheCmpNS);
auto S = std::min_element(cc.begin(), cc.end(), CacheCmpNS);
auto E = std::max_element(cc.begin(), cc.end(), CacheCmpEW);
auto W = std::min_element(cc.begin(), cc.end(), CacheCmpEW);
auto N = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lat < b.pos.lat; });
auto S = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lat < b.pos.lat; });
auto E = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lon < b.pos.lon; });
auto W = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lon < b.pos.lon; });
if (show_minmax) {
std::cout << "Most N:\n";
@ -287,8 +287,8 @@ int main(int argc, char** argv) {
}
if (show_dist && !get_not_found) {
auto far = std::max_element(cc.begin(), cc.end(), CacheCmpDist);
auto near = std::min_element(cc.begin(), cc.end(), CacheCmpDist);
auto far = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.distance() < b.distance(); });
auto near = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.distance() < b.distance(); });
std::cout << "Nearest cache: " << near->distance() << " km\n";
near->show();