Templates for common functions like average or sum

master
Tomasz Golinski 2022-07-13 18:56:48 +02:00
rodzic d673bdd7b9
commit 6de198b9c6
2 zmienionych plików z 27 dodań i 55 usunięć

Wyświetl plik

@ -264,63 +264,43 @@ uint get_num(char c, char* opt) {
} }
} }
void average_html(const Caches& cc, float Cache::*ptr, const std::string& caption) { template void average_html(const Caches& cc, float Cache::*ptr, const std::string& caption);
template void average_html(const Caches& cc, uint Cache::*ptr, const std::string& caption);
template void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption);
template <typename T>
void average_html(const Caches& cc, T Cache::*ptr, const std::string& caption) {
std::cout << "<div class=\"basic_stats\">\n"; std::cout << "<div class=\"basic_stats\">\n";
std::cout << "Average " << caption << ": <span class=\"value\">" << average(cc, ptr) << "</span><br>\n"; std::cout << "Average " << caption << ": <span class=\"value\">" << average(cc, ptr) << "</span><br>\n";
std::cout << "</div>\n"; std::cout << "</div>\n";
} }
void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption) { template float average(const Caches& cc, float Cache::*ptr);
std::cout << "<div class=\"basic_stats\">\n"; template float average(const Caches& cc, int Cache::*ptr);
std::cout << "Average " << caption << ": <span class=\"value\">" << average(cc, ptr) << "</span><br>\n"; template float average(const Caches& cc, uint Cache::*ptr);
std::cout << "</div>\n";
template <typename T>
float average(const Caches& cc, T Cache::*ptr) {
return 1.0 * std::accumulate(cc.begin(), cc.end(), 0., [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size();
} }
void average_html(const Caches& cc, uint Cache::*ptr, const std::string& caption) { template void sum_html(const Caches& cc, float Cache::*ptr, const std::string& caption);
std::cout << "<div class=\"basic_stats\">\n"; template void sum_html(const Caches& cc, int Cache::*ptr, const std::string& caption);
std::cout << "Average " << caption << ": <span class=\"value\">" << average(cc, ptr) << "</span><br>\n"; template void sum_html(const Caches& cc, uint Cache::*ptr, const std::string& caption);
std::cout << "</div>\n";
}
float average(const Caches& cc, float Cache::*ptr) { template <typename T>
return std::accumulate(cc.begin(), cc.end(), 0., [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size(); void sum_html(const Caches& cc, T Cache::*ptr, const std::string& caption) {
}
float average(const Caches& cc, int Cache::*ptr) {
return 1.0 * 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, uint Cache::*ptr) {
return 1.0 * std::accumulate(cc.begin(), cc.end(), 0, [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; }) / cc.size();
}
void sum_html(const Caches& cc, float Cache::*ptr, const std::string& caption) {
std::cout << "<div class=\"basic_stats\">\n"; std::cout << "<div class=\"basic_stats\">\n";
std::cout << "Total " << caption << ": <span class=\"value\">" << sum(cc, ptr) << "</span><br>\n"; std::cout << "Total " << caption << ": <span class=\"value\">" << sum(cc, ptr) << "</span><br>\n";
std::cout << "</div>\n"; std::cout << "</div>\n";
} }
void sum_html(const Caches& cc, int Cache::*ptr, const std::string& caption) { template float sum(const Caches& cc, float Cache::*ptr);
std::cout << "<div class=\"basic_stats\">\n"; template int sum(const Caches& cc, int Cache::*ptr);
std::cout << "Total " << caption << ": <span class=\"value\">" << sum(cc, ptr) << "</span><br>\n"; template uint sum(const Caches& cc, uint Cache::*ptr);
std::cout << "</div>\n";
}
void sum_html(const Caches& cc, uint Cache::*ptr, const std::string& caption) { template <typename T>
std::cout << "<div class=\"basic_stats\">\n"; T sum(const Caches& cc, T Cache::*ptr) {
std::cout << "Total " << caption << ": <span class=\"value\">" << sum(cc, ptr) << "</span><br>\n";
std::cout << "</div>\n";
}
float sum(const Caches& cc, float Cache::*ptr) {
return std::accumulate(cc.begin(), cc.end(), 0., [&](const float& a, const Cache& b) { return std::move(a) + b.*ptr; });
}
int sum(const Caches& cc, int Cache::*ptr) {
return std::accumulate(cc.begin(), cc.end(), 0, [&](const int& a, const Cache& b) { return std::move(a) + b.*ptr; });
}
int sum(const Caches& cc, uint Cache::*ptr) {
return std::accumulate(cc.begin(), cc.end(), 0, [&](const int& a, const Cache& b) { return std::move(a) + b.*ptr; }); return std::accumulate(cc.begin(), cc.end(), 0, [&](const int& a, const Cache& b) { return std::move(a) + b.*ptr; });
} }

Wyświetl plik

@ -17,19 +17,11 @@ uint find_streak(const Date_Caches& cc, std::time_t& start);
uint get_num(char c, char* opt); uint get_num(char c, char* opt);
void average_html(const Caches& cc, float Cache::*ptr, const std::string& caption); template <typename T> void average_html(const Caches& cc, T Cache::*ptr, const std::string& caption);
void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption); template <typename T> float average(const Caches& cc, T Cache::*ptr);
void average_html(const Caches& cc, uint Cache::*ptr, const std::string& caption);
float average(const Caches& cc, float Cache::*ptr);
float average(const Caches& cc, int Cache::*ptr);
float average(const Caches& cc, uint Cache::*ptr);
void sum_html(const Caches& cc, float Cache::*ptr, const std::string& caption); template <typename T> void sum_html(const Caches& cc, T Cache::*ptr, const std::string& caption);
void sum_html(const Caches& cc, int Cache::*ptr, const std::string& caption); template <typename T> T sum(const Caches& cc, T Cache::*ptr);
void sum_html(const Caches& cc, uint Cache::*ptr, const std::string& caption);
float sum(const Caches& cc, float Cache::*ptr);
int sum(const Caches& cc, int Cache::*ptr);
int sum(const Caches& cc, uint Cache::*ptr);
int count_caches(const Caches& cc, std::string& codes, std::function<bool(const Cache& c)> test); int count_caches(const Caches& cc, std::string& codes, std::function<bool(const Cache& c)> test);