From 8eb9465efeb8813c93bfab2a8f385549f45bd827 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 12 Dec 2014 00:01:31 +0100 Subject: [PATCH] Parse timestamps from the binary format. Needs some rework, though. --- binloader.cpp | 15 +++++++++++++-- binlookup.cpp | 4 ++-- binmerger.cpp | 8 +++++++- count.h | 2 ++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/binloader.cpp b/binloader.cpp index 98b2945..e472364 100644 --- a/binloader.cpp +++ b/binloader.cpp @@ -20,6 +20,7 @@ struct Element { string bpfen_and_move; Result result; int opening_num, white_elo, black_elo; + time_t timestamp; bool operator< (const Element& other) const { return bpfen_and_move < other.bpfen_and_move; @@ -62,6 +63,7 @@ int main(int argc, char **argv) } int opening_num, white_elo, black_elo; + time_t timestamp; if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) { perror("fread()"); //exit(1); @@ -77,9 +79,14 @@ int main(int argc, char **argv) //exit(1); break; } + if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) { + perror("fread()"); + //exit(1); + break; + } int bucket = hash_key_to_bucket(bpfen_and_move.data(), bpfen_and_move.size(), num_buckets); - elems[bucket].emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo}); + elems[bucket].emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo, timestamp}); ++num_elems; } fclose(fp); @@ -110,12 +117,16 @@ int main(int argc, char **argv) } else if (e.result == BLACK) { ++c.black; } - c.opening_num = e.opening_num; if (e.white_elo >= 100 && e.black_elo >= 100) { c.sum_white_elo += e.white_elo; c.sum_black_elo += e.black_elo; ++c.num_elo; } + if (c.first_timestamp == DUMMY_TIMESTAMP || + e.timestamp < c.first_timestamp) { + c.first_timestamp = e.timestamp; + c.opening_num = e.opening_num; + } if (j == elems[i].size() - 1 || e.bpfen_and_move != elems[i][j + 1].bpfen_and_move) { mtbl_writer_add(mtbl, (const uint8_t *)e.bpfen_and_move.data(), e.bpfen_and_move.size(), diff --git a/binlookup.cpp b/binlookup.cpp index 083941d..b3a0488 100644 --- a/binlookup.cpp +++ b/binlookup.cpp @@ -41,11 +41,11 @@ int main(int argc, char **argv) while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) { string move((char *)(key + prefix_len), len_key - prefix_len); const Count* c = (Count *)val; - printf("%s %d %d %d %u %f %f %d\n", move.c_str(), + printf("%s %d %d %d %u %f %f %d %ld\n", move.c_str(), c->white, c->draw, c->black, c->opening_num, float(c->sum_white_elo) / c->num_elo, float(c->sum_black_elo) / c->num_elo, - c->num_elo); + c->num_elo, c->first_timestamp); } } } diff --git a/binmerger.cpp b/binmerger.cpp index 612a19b..620ef0a 100644 --- a/binmerger.cpp +++ b/binmerger.cpp @@ -25,10 +25,16 @@ void merge_count(void* userdata, c->white = c0->white + c1->white; c->draw = c0->draw + c1->draw; c->black = c0->black + c1->black; - c->opening_num = c0->opening_num; // Arbitrary choice. c->sum_white_elo = c0->sum_white_elo + c1->sum_white_elo; c->sum_black_elo = c0->sum_black_elo + c1->sum_black_elo; c->num_elo = c0->num_elo + c1->num_elo; + if (c0->first_timestamp <= c1->first_timestamp) { + c->opening_num = c0->opening_num; + c->first_timestamp = c0->first_timestamp; + } else { + c->opening_num = c1->opening_num; + c->first_timestamp = c1->first_timestamp; + } *merged_val = reinterpret_cast(c.release()); *len_merged_val = sizeof(Count); diff --git a/count.h b/count.h index 8d843c7..9cb6a18 100644 --- a/count.h +++ b/count.h @@ -1,3 +1,4 @@ +#define DUMMY_TIMESTAMP 32503680000 // 3000-01-01 00:00:00 UTC. struct Count { int white = 0; @@ -7,4 +8,5 @@ struct Count { unsigned long long sum_white_elo = 0; unsigned long long sum_black_elo = 0; int num_elo = 0; + time_t first_timestamp = DUMMY_TIMESTAMP; }; -- 2.39.2