]> git.sesse.net Git - remoteglot-book/commitdiff
Parse timestamps from the binary format. Needs some rework, though.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 23:01:31 +0000 (00:01 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 23:01:31 +0000 (00:01 +0100)
binloader.cpp
binlookup.cpp
binmerger.cpp
count.h

index 98b29457e8a807eccb7806d0ab5d230ac6953468..e472364e47b234e8a3a0ed02f03c47c8d47a7582 100644 (file)
@@ -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(&timestamp, 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(),
index 083941de3fdb663aea033ea823ff1aabf310e294..b3a0488b0672e240c93bff1fc1c3d450c9fbd6a9 100644 (file)
@@ -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);
                }
        }
 }
index 612a19b123dd9100056468776eb42436ca18a071..620ef0afe5a25d1a55967de95c484f0e35925565 100644 (file)
@@ -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<uint8_t *>(c.release());
        *len_merged_val = sizeof(Count);
diff --git a/count.h b/count.h
index 8d843c7b70f2c968e7bac5e7189c14299e9e7d26..9cb6a18f36f724df2ccc7ce22d7e6297db75309c 100644 (file)
--- 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;
 };