1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Rearrange WDLEntry API

To avoid using temporaries like keys[]
This commit is contained in:
Marco Costalba 2016-05-26 09:51:12 +02:00
parent 107750279d
commit 32c1f1da26

View file

@ -137,7 +137,7 @@ struct Atomic {
}; };
struct WDLEntry : public Atomic { struct WDLEntry : public Atomic {
WDLEntry(const Position& pos, Key keys[]); WDLEntry(const std::string& code);
~WDLEntry(); ~WDLEntry();
void* baseAddress; void* baseAddress;
@ -393,14 +393,16 @@ public:
} }
}; };
WDLEntry::WDLEntry(const Position& pos, Key keys[]) WDLEntry::WDLEntry(const std::string& code) {
{
StateInfo st;
Position pos;
memset(this, 0, sizeof(WDLEntry)); memset(this, 0, sizeof(WDLEntry));
ready = false; ready = false;
key = keys[WHITE]; key = pos.set(code, WHITE, &st).material_key();
key2 = keys[BLACK];
pieceCount = popcount(pos.pieces()); pieceCount = popcount(pos.pieces());
hasPawns = pos.pieces(PAWN); hasPawns = pos.pieces(PAWN);
@ -419,10 +421,12 @@ WDLEntry::WDLEntry(const Position& pos, Key keys[])
pawn.pawnCount[0] = pos.count<PAWN>(c ? WHITE : BLACK); pawn.pawnCount[0] = pos.count<PAWN>(c ? WHITE : BLACK);
pawn.pawnCount[1] = pos.count<PAWN>(c ? BLACK : WHITE); pawn.pawnCount[1] = pos.count<PAWN>(c ? BLACK : WHITE);
} }
key2 = pos.set(code, BLACK, &st).material_key();
} }
WDLEntry::~WDLEntry() WDLEntry::~WDLEntry() {
{
if (baseAddress) if (baseAddress)
TBFile::unmap(baseAddress, mapping); TBFile::unmap(baseAddress, mapping);
@ -434,8 +438,8 @@ WDLEntry::~WDLEntry()
delete piece[i].precomp; delete piece[i].precomp;
} }
DTZEntry::DTZEntry(const WDLEntry& wdl) DTZEntry::DTZEntry(const WDLEntry& wdl) {
{
memset(this, 0, sizeof(DTZEntry)); memset(this, 0, sizeof(DTZEntry));
key = wdl.key; key = wdl.key;
@ -450,8 +454,8 @@ DTZEntry::DTZEntry(const WDLEntry& wdl)
} }
} }
DTZEntry::~DTZEntry() DTZEntry::~DTZEntry() {
{
if (baseAddress) if (baseAddress)
TBFile::unmap(baseAddress, mapping); TBFile::unmap(baseAddress, mapping);
@ -464,8 +468,8 @@ DTZEntry::~DTZEntry()
// Given a position return a string of the form KQPvKRP, where KQP represents // Given a position return a string of the form KQPvKRP, where KQP represents
// the white pieces if mirror == false and the black pieces if mirror == true. // the white pieces if mirror == false and the black pieces if mirror == true.
std::string pos_code(const Position& pos, bool mirror = false) std::string pos_code(const Position& pos, bool mirror = false) {
{
std::string w, b; std::string w, b;
for (PieceType pt = KING; pt >= PAWN; --pt) { for (PieceType pt = KING; pt >= PAWN; --pt) {
@ -476,16 +480,14 @@ std::string pos_code(const Position& pos, bool mirror = false)
return mirror ? b + 'v' + w : w + 'v' + b; return mirror ? b + 'v' + w : w + 'v' + b;
} }
void HashTable::insert(const std::vector<PieceType>& pieces) void HashTable::insert(const std::vector<PieceType>& pieces) {
{
StateInfo st;
Position pos;
std::string code; std::string code;
for (PieceType pt : pieces) for (PieceType pt : pieces)
code += PieceToChar[pt]; code += PieceToChar[pt];
TBFile file(pos_code(pos.set(code, WHITE, &st)) + ".rtbw"); TBFile file(code.insert(code.find('K', 1), "v") + ".rtbw"); // KRK -> KRvK
if (!file.is_open()) if (!file.is_open())
return; return;
@ -494,13 +496,10 @@ void HashTable::insert(const std::vector<PieceType>& pieces)
MaxCardinality = std::max(pieces.size(), MaxCardinality); MaxCardinality = std::max(pieces.size(), MaxCardinality);
Key keys[] = { pos.set(code, WHITE, &st).material_key(), WDLTable.push_back(WDLEntry(code));
pos.set(code, BLACK, &st).material_key() };
WDLTable.push_back(WDLEntry(pos.set(code, WHITE, &st), keys)); insert(WDLTable.back().key , &WDLTable.back());
insert(WDLTable.back().key2, &WDLTable.back());
insert(keys[WHITE], &WDLTable.back());
insert(keys[BLACK], &WDLTable.back());
} }
// TB are compressed with canonical Huffman code. The compressed data is divided into // TB are compressed with canonical Huffman code. The compressed data is divided into