11 #include "merge_count.h"
16 int main(int argc, char **argv)
18 int num_buckets = atoi(argv[2]);
19 mtbl_reader** mtbls = new mtbl_reader*[num_buckets];
20 const mtbl_source** srcs = new const mtbl_source*[num_buckets];
21 for (int i = 0; i < num_buckets; ++i) {
26 while (!feof(stdin)) {
28 if (fgets(hex_bpfen, sizeof(hex_bpfen), stdin) == NULL) {
31 char *ptr = strchr(hex_bpfen, '\n');
36 const int bpfen_len = strlen(hex_bpfen) / 2;
37 uint8_t *bpfen = new uint8_t[bpfen_len];
39 for (int i = 0; i < bpfen_len; ++i) {
41 x[0] = hex_bpfen[i * 2 + 0];
42 x[1] = hex_bpfen[i * 2 + 1];
45 sscanf(x, "%02x", &k);
49 int bucket = hash_key_to_bucket((const char *)bpfen, bpfen_len, num_buckets);
50 if (mtbls[bucket] == NULL) {
52 snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
54 mtbls[bucket] = mtbl_reader_init(filename, NULL);
55 srcs[bucket] = mtbl_reader_source(mtbls[bucket]);
58 // Give back the hash of the position to be kind to the user
59 uint16_t board_hash = util::Hash32((const char *)bpfen, bpfen_len);
60 printf("%d\n", board_hash);
62 mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], bpfen, bpfen_len);
64 const uint8_t *key, *val;
65 size_t len_key, len_val;
69 while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
72 tmpc.ParseFromArray(val, len_val);
73 c = merge_count(c, tmpc);
75 c.ParseFromArray(val, len_val);
81 printf("%d %d %d %u %ld %ld %d %ld %d %ld",
82 c.white(), c.draw(), c.black(), c.opening_num(),
85 c.num_elo(), c.first_timestamp(),
87 c.pgn_start_position());
88 for (int j = 0; j < c.move_size(); ++j) {
89 printf(" %s", c.move(j).c_str());
96 mtbl_iter_destroy(&it);
99 for (int i = 0; i < num_buckets; ++i) {
100 if (mtbls[i] != NULL) {
101 mtbl_reader_destroy(&mtbls[i]);