]> git.sesse.net Git - stockfish/commitdiff
Introduce SimpleHash class
authorMarco Costalba <mcostalba@gmail.com>
Fri, 7 Jan 2011 09:34:16 +0000 (10:34 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 7 Jan 2011 10:20:49 +0000 (11:20 +0100)
And use it for pawns and material infos.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/material.cpp
src/material.h
src/pawns.cpp
src/pawns.h
src/tt.h

index 0098b39c949cc30fa6ddc1ef21196e209d7983c2..cc90c2f9f620dbdc35d1571d1711e7f204cd42a5 100644 (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
index 535c216c0742053ecaa852923be264f4d9068e19..d039dbbf4888eea7ce5ae097fcf10d75ee46e4d3 100644 (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;
 };
 
index e3201081635be80e9aaa5a3520b13ff90cf3d21e..0846344223137f93a2372c737b6a553609e8c5a4 100644 (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
index 86708488cb276f6c13ce40d768d7bda7991d3485..e41044091566050567d413cb2d5888dba6585538 100644 (file)
 
 #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.
 
-  enum SideType { KingSide, QueenSide };
+class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
 
-  PawnInfoTable(const PawnInfoTable&);
-  PawnInfoTable& operator=(const PawnInfoTable&);
+  enum SideType { KingSide, QueenSide };
 
 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;
 };
 
 
index 778e5e81bf86dd37ad5725823967ea9ef87b4f0e..f2928fb9e84b4b754ae9c1ecfb9a1c01b7778a56 100644 (file)
--- a/src/tt.h
+++ b/src/tt.h
 //// 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