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);
57 string prev_pos_hash = read_hex_line(stdin);
59 string searchkey = bpfen + prev_pos_hash;
60 int bucket = hash_key_to_bucket(searchkey.data(), searchkey.size(), num_buckets);
61 if (mtbls[bucket] == NULL) {
63 snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
65 mtbls[bucket] = mtbl_reader_init(filename, NULL);
66 srcs[bucket] = mtbl_reader_source(mtbls[bucket]);
69 // Give back the hash of the position to be kind to the user
70 uint16_t board_hash = util::Hash32(bpfen.data(), bpfen.size());
71 printf("%d\n", board_hash);
73 mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)searchkey.data(), searchkey.size());
75 const uint8_t *key, *val;
76 size_t len_key, len_val;
80 while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
83 tmpc.ParseFromArray(val, len_val);
84 c = merge_count(c, tmpc);
86 c.ParseFromArray(val, len_val);
92 printf("%d %d %d %u %ld %ld %d %ld %d %ld",
93 c.white(), c.draw(), c.black(), c.opening_num(),
96 c.num_elo(), c.first_timestamp(),
98 c.pgn_start_position());
99 for (int j = 0; j < c.move_size(); ++j) {
100 printf(" %s", c.move(j).c_str());
107 mtbl_iter_destroy(&it);
110 for (int i = 0; i < num_buckets; ++i) {
111 if (mtbls[i] != NULL) {
112 mtbl_reader_destroy(&mtbls[i]);