kopia lustrzana https://gitlab.com/tomaszg/geostat
Simplify streak calculations
rodzic
c97c017ecc
commit
3d33de5a23
37
common.cpp
37
common.cpp
|
@ -1,4 +1,5 @@
|
|||
#include "common.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
@ -178,49 +179,43 @@ void show_nested_histogram(const Caches& cc, std::string Cache::*ptr, std::strin
|
|||
}
|
||||
}
|
||||
|
||||
static bool same_day(const std::tm& a, const std::tm& b) {
|
||||
return (a.tm_year == b.tm_year && a.tm_mon == b.tm_mon && a.tm_mday == b.tm_mday);
|
||||
}
|
||||
|
||||
static void next_day(std::tm& a) {
|
||||
a.tm_mday++;
|
||||
mktime(&a);
|
||||
}
|
||||
|
||||
int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::tm& start) {
|
||||
int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::time_t& start) {
|
||||
int max_str = 0;
|
||||
int cur_str = 0;
|
||||
|
||||
std::tm cur_tm;
|
||||
cur_tm.tm_year = 0;
|
||||
std::tm cur_start_tm = cur_tm;
|
||||
start = cur_tm;
|
||||
int cur = 0;
|
||||
int cur_start = cur;
|
||||
int max_start = cur;
|
||||
|
||||
for (auto i : cc) {
|
||||
if (same_day(cur_tm, *std::localtime(&i.first)))
|
||||
if (cur == i.first / 86400)
|
||||
continue;
|
||||
next_day(cur_tm);
|
||||
if (same_day(cur_tm, *std::localtime(&i.first))) {
|
||||
if (i.first / 86400 - cur == 1) {
|
||||
cur_str++;
|
||||
cur = i.first / 86400;
|
||||
continue;
|
||||
}
|
||||
|
||||
// streak ended
|
||||
|
||||
if (max_str < cur_str) {
|
||||
max_str = cur_str;
|
||||
start = cur_start_tm;
|
||||
max_start = cur_start;
|
||||
}
|
||||
cur_str = 1;
|
||||
cur_tm = cur_start_tm = *std::localtime(&i.first);
|
||||
cur = cur_start = i.first / 86400;
|
||||
if (max_str == 0) {
|
||||
max_str = 1;
|
||||
start = cur_tm;
|
||||
max_start = cur;
|
||||
}
|
||||
}
|
||||
|
||||
if (max_str < cur_str) {
|
||||
max_str = cur_str;
|
||||
start = cur_start_tm;
|
||||
max_start = cur_start;
|
||||
}
|
||||
|
||||
start = max_start * 86400; // convert back from month count to time_t
|
||||
return max_str;
|
||||
}
|
||||
|
||||
|
|
2
common.h
2
common.h
|
@ -8,7 +8,7 @@ void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string
|
|||
void show_histogram(const std::map<std::string, int>& data, const std::string& caption, bool html = 0, bool sort_by_val = 1);
|
||||
void show_nested_histogram(const Caches& cc, std::string Cache::*ptr, std::string Cache::*ptr2, const std::string& caption, bool html = 0, bool sort_by_val = 1);
|
||||
|
||||
int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::tm& start);
|
||||
int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::time_t& start);
|
||||
|
||||
long int get_num(char c, char* opt);
|
||||
|
||||
|
|
|
@ -377,10 +377,11 @@ int main(int argc, char** argv) {
|
|||
std::cout << i.first << ", found " << i.second << " caches<br>\n";
|
||||
std::cout << " </p>\n</details>\n";
|
||||
|
||||
std::tm streak;
|
||||
std::time_t streak;
|
||||
int longest_str = find_streak(sorted_caches, streak);
|
||||
char str_tmp[20];
|
||||
std::strftime(str_tmp, 20, "%F", &streak);
|
||||
std::tm* str_tm = std::localtime(&streak);
|
||||
std::strftime(str_tmp, 20, "%F", str_tm);
|
||||
std::cout << "Longest caching streak: <span class=\"value\">" << longest_str << "</span> starting on <span class=\"value\">" << str_tmp << "</span><br>\n";
|
||||
|
||||
float tot_dist = 0;
|
||||
|
|
Ładowanie…
Reference in New Issue