]> git.sesse.net Git - remoteglot-book/blobdiff - binmerger.cpp
Build with the new transposition data from pgn-extract. A lot of UI is still missing...
[remoteglot-book] / binmerger.cpp
index 9bd969ead0d3795ed0a495047ad5e2ae77232a0c..7e25a8f5cdabfa63aaede31f23d6a83c9354fbc3 100644 (file)
@@ -5,51 +5,22 @@
 #include <unordered_set>
 #include <string.h>
 #include <assert.h>
+#include "merge_count.h"
 #include "count.pb.h"
 
 using namespace std;
 
-void merge_count(void* userdata,
-                 const uint8_t *key, size_t len_key,
-                const uint8_t *val0, size_t len_val0,
-                const uint8_t *val1, size_t len_val1,
-                uint8_t **merged_val, size_t *len_merged_val)
+void merge_serialized_count(void* userdata,
+                            const uint8_t *key, size_t len_key,
+                            const uint8_t *val0, size_t len_val0,
+                            const uint8_t *val1, size_t len_val1,
+                            uint8_t **merged_val, size_t *len_merged_val)
 {
        Count c0, c1;
        c0.ParseFromArray(val0, len_val0);
        c1.ParseFromArray(val1, len_val1);
 
-       Count c;
-
-       c.set_white(c0.white() + c1.white());
-       c.set_draw(c0.draw() + c1.draw());
-       c.set_black(c0.black() + c1.black());
-       c.set_sum_white_elo(c0.sum_white_elo() + c1.sum_white_elo());
-       c.set_sum_black_elo(c0.sum_black_elo() + c1.sum_black_elo());
-       c.set_num_elo(c0.num_elo() + c1.num_elo());
-       if (c0.first_timestamp() <= c1.first_timestamp()) {
-               c.set_opening_num(c0.opening_num());
-               if (c0.has_first_timestamp()) {
-                       c.set_first_timestamp(c0.first_timestamp());
-               }
-       } else {
-               c.set_opening_num(c1.opening_num());
-               if (c1.has_first_timestamp()) {
-                       c.set_first_timestamp(c1.first_timestamp());
-               }
-       }
-
-       // Merge the moves, with deduplication.
-       unordered_set<string> moves;
-       for (int i = 0; i < c0.move_size(); ++i) {
-               moves.insert(c0.move(i));
-               c.add_move(c0.move(i));
-       }
-       for (int i = 0; i < c1.move_size(); ++i) {
-               if (!moves.count(c1.move(i))) {
-                       c.add_move(c1.move(i));
-               }
-       }
+       Count c = merge_count(c0, c1);
 
        static string buf;  // Keep allocated.
        c.SerializeToString(&buf);
@@ -62,7 +33,7 @@ void merge_count(void* userdata,
 int main(int argc, char **argv)
 {
        mtbl_merger_options* mopt = mtbl_merger_options_init();
-       mtbl_merger_options_set_merge_func(mopt, merge_count, NULL);
+       mtbl_merger_options_set_merge_func(mopt, merge_serialized_count, NULL);
        mtbl_merger* merger = mtbl_merger_init(mopt);
 
        for (int i = 1; i < argc - 1; ++i) {