@@ -25,23 +25,38 @@ public struct HashEntry
2525 }
2626
2727 public static short PERSISTENT = 9999 ;
28-
28+ public const int DEFAULT_SIZE_MB = 50 ;
2929 const int ENTRY_SIZE = 16 ; //BYTES
30- const int HASH_MEMORY = 256 ; //Megabytes
31- const int TT_SIZE = ( HASH_MEMORY * 1024 * 1024 ) / ENTRY_SIZE ;
30+ static HashEntry [ ] _table ;
31+
32+ static int Index ( in ulong hash ) => ( int ) ( hash % ( ulong ) _table . Length ) ;
33+
34+ static Transpositions ( )
35+ {
36+ Resize ( DEFAULT_SIZE_MB ) ;
37+ }
3238
33- static HashEntry [ ] _table = new HashEntry [ TT_SIZE ] ;
39+ public static void Resize ( int hashSizeMBytes )
40+ {
41+ int length = ( hashSizeMBytes * 1024 * 1024 ) / ENTRY_SIZE ;
42+ _table = new HashEntry [ length ] ;
43+ }
44+
45+ public static void Clear ( )
46+ {
47+ Array . Clear ( _table , 0 , _table . Length ) ;
48+ }
3449
3550 public static void Store ( ulong zobristHash , int depth , SearchWindow window , int score , Move bestMove )
3651 {
37- int slot = ( int ) ( zobristHash % TT_SIZE ) ;
38- ref HashEntry entry = ref _table [ slot ] ;
52+ int index = Index ( zobristHash ) ;
53+ ref HashEntry entry = ref _table [ index ] ;
3954 if ( entry . Depth == PERSISTENT )
4055 return ;
4156
4257 //don't overwrite a bestmove unless it's a new position OR the new bestMove is explored to a greater depth
4358 if ( entry . Hash != zobristHash || ( depth >= entry . Depth && bestMove != default ) )
44- _table [ slot ] . BestMove = bestMove ;
59+ _table [ index ] . BestMove = bestMove ;
4560
4661 entry . Hash = zobristHash ;
4762 entry . Depth = ( short ) Math . Max ( 0 , depth ) ;
@@ -66,18 +81,18 @@ public static void Store(ulong zobristHash, int depth, SearchWindow window, int
6681 internal static Move GetBestMove ( Board position )
6782 {
6883 ulong zobristHash = position . ZobristHash ;
69- int slot = ( int ) ( zobristHash % TT_SIZE ) ;
70- if ( _table [ slot ] . Hash == zobristHash )
71- return _table [ slot ] . BestMove ;
84+ int index = Index ( zobristHash ) ;
85+ if ( _table [ index ] . Hash == zobristHash )
86+ return _table [ index ] . BestMove ;
7287 else
7388 return default ;
7489 }
7590
7691 public static bool GetScore ( Board position , int depth , SearchWindow window , out int score )
7792 {
7893 ulong zobristHash = position . ZobristHash ;
79- int slot = ( int ) ( zobristHash % TT_SIZE ) ;
80- ref HashEntry entry = ref _table [ slot ] ;
94+ int index = Index ( zobristHash ) ;
95+ ref HashEntry entry = ref _table [ index ] ;
8196
8297 score = entry . Score ;
8398 if ( entry . Hash != zobristHash || entry . Depth < depth )
@@ -95,10 +110,5 @@ public static bool GetScore(Board position, int depth, SearchWindow window, out
95110
96111 return false ;
97112 }
98-
99- public static void Clear ( )
100- {
101- Array . Clear ( _table , 0 , TT_SIZE ) ;
102- }
103113 }
104114}
0 commit comments