11 #include "merge_count.h"
16 string read_hex_line(FILE *fp)
19 if (fgets(hex, sizeof(hex), fp) == NULL) {
22 char *ptr = strchr(hex, '\n');
28 ret.resize(strlen(hex) / 2);
30 for (size_t i = 0; i < ret.size(); ++i) {
32 x[0] = hex[i * 2 + 0];
33 x[1] = hex[i * 2 + 1];
36 sscanf(x, "%02x", &k);
42 int main(int argc, char **argv)
44 int num_buckets = atoi(argv[2]);
45 mtbl_reader** mtbls = new mtbl_reader*[num_buckets];
46 const mtbl_source** srcs = new const mtbl_source*[num_buckets];
47 for (int i = 0; i < num_buckets; ++i) {
52 while (!feof(stdin)) {
53 string bpfen = read_hex_line(stdin);
58 int bucket = hash_key_to_bucket(bpfen.data(), bpfen.size(), num_buckets);
59 if (mtbls[bucket] == NULL) {
61 snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
63 mtbls[bucket] = mtbl_reader_init(filename, NULL);
64 srcs[bucket] = mtbl_reader_source(mtbls[bucket]);
67 // Give back the hash of the position to be kind to the user
68 uint16_t board_hash = util::Hash32(bpfen.data(), bpfen.size());
69 printf("%d\n", board_hash);
71 mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)bpfen.data(), bpfen.size());
73 const uint8_t *key, *val;
74 size_t len_key, len_val;
78 while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
81 tmpc.ParseFromArray(val, len_val);
82 c = merge_count(c, tmpc);
84 c.ParseFromArray(val, len_val);
90 printf("%d %d %d %u %ld %ld %d %ld %d %ld",
91 c.white(), c.draw(), c.black(), c.opening_num(),
94 c.num_elo(), c.first_timestamp(),
96 c.pgn_start_position());
97 for (int j = 0; j < c.move_size(); ++j) {
98 printf(" %s", c.move(j).c_str());
105 mtbl_iter_destroy(&it);
108 for (int i = 0; i < num_buckets; ++i) {
109 if (mtbls[i] != NULL) {
110 mtbl_reader_destroy(&mtbls[i]);