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

Introduce SimpleHash class

And use it for pawns and material infos.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-01-07 10:34:16 +01:00
parent 803c8e0be3
commit 0ddf84870a
5 changed files with 47 additions and 71 deletions

View file

@ -132,28 +132,8 @@ template<> const SFMap& EndgameFunctions::get<SF>() const { return maps.second;
//// Functions
////
/// MaterialInfoTable c'tor and d'tor, called once by each thread
MaterialInfoTable::MaterialInfoTable() {
entries = new MaterialInfo[MaterialTableSize];
funcs = new EndgameFunctions();
if (!entries || !funcs)
{
cerr << "Failed to allocate " << MaterialTableSize * sizeof(MaterialInfo)
<< " bytes for material hash table." << endl;
exit(EXIT_FAILURE);
}
memset(entries, 0, MaterialTableSize * sizeof(MaterialInfo));
}
MaterialInfoTable::~MaterialInfoTable() {
delete funcs;
delete [] entries;
}
MaterialInfoTable::MaterialInfoTable() { funcs = new EndgameFunctions(); }
MaterialInfoTable::~MaterialInfoTable() { delete funcs; }
/// MaterialInfoTable::game_phase() calculates the phase given the current
/// position. Because the phase is strictly a function of the material, it
@ -181,8 +161,7 @@ Phase MaterialInfoTable::game_phase(const Position& pos) {
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
Key key = pos.get_material_key();
unsigned index = unsigned(key & (MaterialTableSize - 1));
MaterialInfo* mi = entries + index;
MaterialInfo* mi = find(key);
// If mi->key matches the position's material hash key, it means that we
// have analysed this material configuration before, and we can simply

View file

@ -27,6 +27,7 @@
#include "endgame.h"
#include "position.h"
#include "tt.h"
////
@ -67,26 +68,17 @@ private:
Phase gamePhase;
};
/// The MaterialInfoTable class represents a pawn hash table. It is basically
/// just an array of MaterialInfo objects and a few methods for accessing these
/// objects. The most important method is get_material_info, which looks up a
/// position in the table and returns a pointer to a MaterialInfo object.
/// The MaterialInfoTable class represents a pawn hash table. The most important
/// method is get_material_info, which returns a pointer to a MaterialInfo object.
class EndgameFunctions;
class MaterialInfoTable {
MaterialInfoTable(const MaterialInfoTable&);
MaterialInfoTable& operator=(const MaterialInfoTable&);
class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
public:
MaterialInfoTable();
~MaterialInfoTable();
MaterialInfo* get_material_info(const Position& pos);
static Phase game_phase(const Position& pos);
private:
MaterialInfo* entries;
EndgameFunctions* funcs;
};

View file

@ -81,27 +81,6 @@ namespace {
//// Functions
////
/// PawnInfoTable c'tor and d'tor instantiated one each thread
PawnInfoTable::PawnInfoTable() {
entries = new PawnInfo[PawnTableSize];
if (!entries)
{
std::cerr << "Failed to allocate " << (PawnTableSize * sizeof(PawnInfo))
<< " bytes for pawn hash table." << std::endl;
exit(EXIT_FAILURE);
}
memset(entries, 0, PawnTableSize * sizeof(PawnInfo));
}
PawnInfoTable::~PawnInfoTable() {
delete [] entries;
}
/// PawnInfoTable::get_pawn_info() takes a position object as input, computes
/// a PawnInfo object, and returns a pointer to it. The result is also stored
@ -113,8 +92,7 @@ PawnInfo* PawnInfoTable::get_pawn_info(const Position& pos) const {
assert(pos.is_ok());
Key key = pos.get_pawn_key();
unsigned index = unsigned(key & (PawnTableSize - 1));
PawnInfo* pi = entries + index;
PawnInfo* pi = find(key);
// If pi->key matches the position's pawn hash key, it means that we
// have analysed this pawn structure before, and we can simply return

View file

@ -27,8 +27,10 @@
#include "bitboard.h"
#include "position.h"
#include "tt.h"
#include "value.h"
////
//// Types
////
@ -69,29 +71,21 @@ private:
Score kingShelters[2];
};
/// The PawnInfoTable class represents a pawn hash table. It is basically
/// just an array of PawnInfo objects and a few methods for accessing these
/// objects. The most important method is get_pawn_info, which looks up a
/// position in the table and returns a pointer to a PawnInfo object.
class PawnInfoTable {
/// The PawnInfoTable class represents a pawn hash table. The most important
/// method is get_pawn_info, which returns a pointer to a PawnInfo object.
class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
enum SideType { KingSide, QueenSide };
PawnInfoTable(const PawnInfoTable&);
PawnInfoTable& operator=(const PawnInfoTable&);
public:
PawnInfoTable();
~PawnInfoTable();
PawnInfo* get_pawn_info(const Position& pos) const;
void prefetch(Key key) const;
private:
template<Color Us>
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
PawnInfo* entries;
};

View file

@ -34,6 +34,39 @@
//// Types
////
/// A simple fixed size hash table used to store pawns and material
/// configurations. It is basically just an array of Entry objects.
/// Without cluster concept or overwrite policy.
template<class Entry, int HashSize>
class SimpleHash {
SimpleHash(const SimpleHash&);
SimpleHash& operator=(const SimpleHash&);
public:
SimpleHash() {
entries = new Entry[HashSize];
if (!entries)
{
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
<< " bytes for material hash table." << std::endl;
exit(EXIT_FAILURE);
}
memset(entries, 0, HashSize * sizeof(Entry));
}
~SimpleHash() { delete [] entries; }
Entry* find(Key key) const { return entries + unsigned(key & (HashSize - 1)); }
protected:
Entry* entries;
};
/// The TTEntry class is the class of transposition table entries
///
/// A TTEntry needs 128 bits to be stored