]> git.sesse.net Git - remoteglot-book/blobdiff - binloader.cpp
Store and merge the file number information. Still unused in the UI.
[remoteglot-book] / binloader.cpp
index fdad082502a21c10223ae50748551d9fb3678896..8660e8e299bd93f70f19767d671af0e6d8288f80 100644 (file)
@@ -9,6 +9,7 @@
 #include <utility>
 #include <memory>
 #include <string>
+#include <unordered_set>
 #include <string.h>
 #include "count.pb.h"
 #include "hash.h"
@@ -19,13 +20,16 @@ using namespace std;
 
 enum Result { WHITE = 0, DRAW, BLACK };
 struct Element {
-       string bpfen_and_move;
+       string bpfen;
+       string move;
        Result result;
        int opening_num, white_elo, black_elo;
        time_t timestamp;
+       int file_num;
+       long start_position;
 
        bool operator< (const Element& other) const {
-               return bpfen_and_move < other.bpfen_and_move;
+               return bpfen < other.bpfen;
        }
 };
 
@@ -49,9 +53,9 @@ int main(int argc, char **argv)
                                break;
                        }
                
-                       string bpfen_and_move;
-                       bpfen_and_move.resize(l);
-                       if (fread(&bpfen_and_move[0], l, 1, fp) != 1) {
+                       string bpfen;
+                       bpfen.resize(l);
+                       if (fread(&bpfen[0], l, 1, fp) != 1) {
                                perror("fread()");
                //              exit(1);
                                break;
@@ -64,8 +68,9 @@ int main(int argc, char **argv)
                                break;
                        }
 
-                       int opening_num, white_elo, black_elo;
+                       int opening_num, white_elo, black_elo, file_num;
                        time_t timestamp;
+                       long start_position;
                        if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
                                perror("fread()");
                                //exit(1);
@@ -86,9 +91,32 @@ int main(int argc, char **argv)
                                //exit(1);
                                break;
                        }
+                       if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
+                               perror("fread()");
+                               //exit(1);
+                               break;
+                       }
+                       if (fread(&start_position, sizeof(start_position), 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, timestamp});
+                       l = getc(fp);
+                       if (l == -1) {
+                               break;
+                       }
+                       string move;
+                       move.resize(l);
+                       if (fread(&move[0], l, 1, fp) != 1) {
+                               perror("fread()");
+               //              exit(1);
+                               break;
+                       }
+
+                       int bucket = hash_key_to_bucket(bpfen.data(), bpfen.size(), num_buckets);
+                       elems[bucket].emplace_back(Element {std::move(bpfen), std::move(move), Result(r), opening_num, white_elo, black_elo, timestamp, file_num, start_position});
                        ++num_elems;
                }
                fclose(fp);
@@ -111,6 +139,7 @@ int main(int argc, char **argv)
                mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
                mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
                Count c;
+               unordered_set<string> moves;
                for (size_t j = 0; j < elems[i].size(); ++j) {
                        const Element &e = elems[i][j];
                        if (e.result == WHITE) {
@@ -130,13 +159,20 @@ int main(int argc, char **argv)
                                        c.set_first_timestamp(e.timestamp);
                                }
                                c.set_opening_num(e.opening_num);
+                               c.set_pgn_file_num(e.file_num);
+                               c.set_pgn_start_position(e.start_position);
+                       }
+                       if (!moves.count(e.move)) {
+                               moves.insert(e.move);
+                               c.add_move(e.move);
                        }
-                       if (j == elems[i].size() - 1 || e.bpfen_and_move != elems[i][j + 1].bpfen_and_move) {
+                       if (j == elems[i].size() - 1 || e.bpfen != elems[i][j + 1].bpfen) {
                                c.SerializeToString(&buf);
                                mtbl_writer_add(mtbl,
-                                       (const uint8_t *)e.bpfen_and_move.data(), e.bpfen_and_move.size(),
+                                       (const uint8_t *)e.bpfen.data(), e.bpfen.size(),
                                        (const uint8_t *)buf.data(), buf.size());
                                c = Count();
+                               moves.clear();
                        }
                }
                mtbl_writer_destroy(&mtbl);