mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 09:39:36 +00:00
Rename class Book to PolyglotBook
And move struct BookEntry out of the header where it is not needed. No functional change.
This commit is contained in:
parent
4b7dbb3922
commit
9ce7469846
3 changed files with 31 additions and 33 deletions
42
src/book.cpp
42
src/book.cpp
|
@ -35,6 +35,16 @@ using namespace std;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// A Polyglot book is a series of "entries" of 16 bytes. All integers are
|
||||||
|
// stored in big-endian format, with highest byte first (regardless of size).
|
||||||
|
// The entries are ordered according to the key in ascending order.
|
||||||
|
struct BookEntry {
|
||||||
|
uint64_t key;
|
||||||
|
uint16_t move;
|
||||||
|
uint16_t count;
|
||||||
|
uint32_t learn;
|
||||||
|
};
|
||||||
|
|
||||||
// Random numbers from PolyGlot, used to compute book hash keys
|
// Random numbers from PolyGlot, used to compute book hash keys
|
||||||
const Key PolyGlotRandoms[781] = {
|
const Key PolyGlotRandoms[781] = {
|
||||||
0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
|
0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
|
||||||
|
@ -338,20 +348,20 @@ namespace {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Book::Book() {
|
PolyglotBook::PolyglotBook() {
|
||||||
|
|
||||||
for (int i = Time::now() % 10000; i > 0; i--)
|
for (int i = Time::now() % 10000; i > 0; i--)
|
||||||
RKiss.rand<unsigned>(); // Make random number generation less deterministic
|
RKiss.rand<unsigned>(); // Make random number generation less deterministic
|
||||||
}
|
}
|
||||||
|
|
||||||
Book::~Book() { if (is_open()) close(); }
|
PolyglotBook::~PolyglotBook() { if (is_open()) close(); }
|
||||||
|
|
||||||
|
|
||||||
/// Book::operator>>() reads sizeof(T) chars from the file's binary byte stream
|
/// operator>>() reads sizeof(T) chars from the file's binary byte stream and
|
||||||
/// and converts them in a number of type T. A Polyglot book stores numbers in
|
/// converts them in a number of type T. A Polyglot book stores numbers in
|
||||||
/// big-endian format.
|
/// big-endian format.
|
||||||
|
|
||||||
template<typename T> Book& Book::operator>>(T& n) {
|
template<typename T> PolyglotBook& PolyglotBook::operator>>(T& n) {
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
for (size_t i = 0; i < sizeof(T); i++)
|
for (size_t i = 0; i < sizeof(T); i++)
|
||||||
|
@ -360,15 +370,15 @@ template<typename T> Book& Book::operator>>(T& n) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> Book& Book::operator>>(BookEntry& e) {
|
template<> PolyglotBook& PolyglotBook::operator>>(BookEntry& e) {
|
||||||
return *this >> e.key >> e.move >> e.count >> e.learn;
|
return *this >> e.key >> e.move >> e.count >> e.learn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Book::open() tries to open a book file with the given name after closing
|
/// open() tries to open a book file with the given name after closing any
|
||||||
/// any exsisting one.
|
/// exsisting one.
|
||||||
|
|
||||||
bool Book::open(const char* fName) {
|
bool PolyglotBook::open(const char* fName) {
|
||||||
|
|
||||||
if (is_open()) // Cannot close an already closed file
|
if (is_open()) // Cannot close an already closed file
|
||||||
close();
|
close();
|
||||||
|
@ -381,11 +391,11 @@ bool Book::open(const char* fName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Book::probe() tries to find a book move for the given position. If no move
|
/// probe() tries to find a book move for the given position. If no move is
|
||||||
/// is found returns MOVE_NONE. If pickBest is true returns always the highest
|
/// found returns MOVE_NONE. If pickBest is true returns always the highest
|
||||||
/// rated move, otherwise randomly chooses one, based on the move score.
|
/// rated move, otherwise randomly chooses one, based on the move score.
|
||||||
|
|
||||||
Move Book::probe(const Position& pos, const string& fName, bool pickBest) {
|
Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest) {
|
||||||
|
|
||||||
if (fileName != fName && !open(fName.c_str()))
|
if (fileName != fName && !open(fName.c_str()))
|
||||||
return MOVE_NONE;
|
return MOVE_NONE;
|
||||||
|
@ -437,11 +447,11 @@ Move Book::probe(const Position& pos, const string& fName, bool pickBest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Book::find_first() takes a book key as input, and does a binary search
|
/// find_first() takes a book key as input, and does a binary search through
|
||||||
/// through the book file for the given key. Returns the index of the leftmost
|
/// the book file for the given key. Returns the index of the leftmost book
|
||||||
/// book entry with the same key as the input.
|
/// entry with the same key as the input.
|
||||||
|
|
||||||
size_t Book::find_first(uint64_t key) {
|
size_t PolyglotBook::find_first(uint64_t key) {
|
||||||
|
|
||||||
seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
|
seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
|
||||||
|
|
||||||
|
|
20
src/book.h
20
src/book.h
|
@ -26,26 +26,14 @@
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "rkiss.h"
|
#include "rkiss.h"
|
||||||
|
|
||||||
|
class PolyglotBook : private std::ifstream {
|
||||||
/// A Polyglot book is a series of "entries" of 16 bytes. All integers are
|
|
||||||
/// stored highest byte first (regardless of size). The entries are ordered
|
|
||||||
/// according to key. Lowest key first.
|
|
||||||
struct BookEntry {
|
|
||||||
uint64_t key;
|
|
||||||
uint16_t move;
|
|
||||||
uint16_t count;
|
|
||||||
uint32_t learn;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class Book : private std::ifstream {
|
|
||||||
public:
|
public:
|
||||||
Book();
|
PolyglotBook();
|
||||||
~Book();
|
~PolyglotBook();
|
||||||
Move probe(const Position& pos, const std::string& fName, bool pickBest);
|
Move probe(const Position& pos, const std::string& fName, bool pickBest);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<typename T> Book& operator>>(T& n);
|
template<typename T> PolyglotBook& operator>>(T& n);
|
||||||
|
|
||||||
bool open(const char* fName);
|
bool open(const char* fName);
|
||||||
size_t find_first(uint64_t key);
|
size_t find_first(uint64_t key);
|
||||||
|
|
|
@ -224,7 +224,7 @@ size_t Search::perft(Position& pos, Depth depth) {
|
||||||
|
|
||||||
void Search::think() {
|
void Search::think() {
|
||||||
|
|
||||||
static Book book; // Defined static to initialize the PRNG only once
|
static PolyglotBook book; // Defined static to initialize the PRNG only once
|
||||||
|
|
||||||
Position& pos = RootPosition;
|
Position& pos = RootPosition;
|
||||||
Chess960 = pos.is_chess960();
|
Chess960 = pos.is_chess960();
|
||||||
|
|
Loading…
Add table
Reference in a new issue