- left = mid + 1;
- }
-
- assert(left == right);
-
- read_entry(entry, left);
- return entry.key == key ? left : bookSize;
-}
-
-
-/// Book::read_entry() takes a BookEntry reference and an integer index as
-/// input, and looks up the opening book entry at the given index in the book
-/// file. The book entry is copied to the first input parameter.
-
-void Book::read_entry(BookEntry& entry, int idx) {
-
- assert(idx >= 0 && idx < bookSize);
- assert(is_open());
-
- seekg(idx * EntrySize, ios_base::beg);
-
- *this >> entry;
-
- if (!good())
- {
- cerr << "Failed to read book entry at index " << idx << endl;
- exit(EXIT_FAILURE);
- }
-}
-
-
-/// Book::read_integer() reads size chars from the file stream
-/// and converts them in an integer number.
-
-uint64_t Book::read_integer(int size) {
-
- char buf[8];
- uint64_t n = 0;
-
- read(buf, size);
-
- // Numbers are stored on disk as a binary byte stream
- for (int i = 0; i < size; i++)
- n = (n << 8) + (unsigned char)buf[i];
-
- return n;
-}
-
-
-////
-//// Local definitions
-////
-
-namespace {
-
- uint64_t book_key(const Position& pos) {
-
- uint64_t result = 0;
- Bitboard b = pos.occupied_squares();
-
- while (b)
- {
- Square s = pop_1st_bit(&b);
- result ^= book_piece_key(pos.piece_on(s), s);
- }
-
- result ^= book_castle_key(pos);
- result ^= book_ep_key(pos);
- result ^= book_color_key(pos);
- return result;
- }
-
-
- uint64_t book_piece_key(Piece p, Square s) {
-
- // Convert pieces to the range 0..11
- static const int PieceTo12[] = { 0, 0, 2, 4, 6, 8, 10, 0, 0, 1, 3, 5, 7, 9, 11 };
-
- return Random64[PieceIdx + (PieceTo12[int(p)]^1) * 64 + int(s)];