gnugo-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnugo-devel] hash stuff cleanup


From: Arend Bayer
Subject: [gnugo-devel] hash stuff cleanup
Date: Mon, 13 Sep 2004 17:57:08 +0200 (CEST)


- hash value cleanups

Since komaster and kom_pos have become private variables of board.c, it
is now more natural to include that ko hash value directly in the hash value
of the board state, computed by the board library.

Other than that, this patch uses Gunnar's INIT_ARRAY also in hash.c, and
adds a new set of hash variables to compute goal hashes (replacing an
ugly hack by me).

Arend

Index: engine/board.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/board.c,v
retrieving revision 1.100
diff -u -p -r1.100 board.c
--- engine/board.c      5 Sep 2004 15:21:58 -0000       1.100
+++ engine/board.c      13 Sep 2004 12:16:12 -0000
@@ -1132,6 +1132,25 @@ is_illegal_ko_capture(int pos, int color
              || (board[EAST(pos)] == OTHER_COLOR(color))));
 }
 
+/* Necessary work to set the new komaster state. */
+static void
+set_new_komaster(int new_komaster)
+{
+  PUSH_VALUE(komaster);
+  hashdata_invert_komaster(&board_hash, komaster);
+  komaster = new_komaster;
+  hashdata_invert_komaster(&board_hash, komaster);
+}
+
+/* Necessary work to set the new komaster position. */
+static void
+set_new_kom_pos(int new_kom_pos)
+{
+  PUSH_VALUE(kom_pos);
+  hashdata_invert_kom_pos(&board_hash, kom_pos);
+  kom_pos = new_kom_pos;
+  hashdata_invert_kom_pos(&board_hash, kom_pos);
+}
 
 /* Variation of trymove()/tryko() where ko captures (both conditional
  * and unconditional) must follow a komaster scheme.
@@ -1202,10 +1221,8 @@ komaster_trymove(int pos, int color, con
          && (IS_STONE(board[kom_pos])
              || (!is_ko(kom_pos, WHITE, NULL)
                  && is_suicide(kom_pos, WHITE))))) {
-    PUSH_VALUE(komaster);
-    PUSH_VALUE(kom_pos);
-    komaster = EMPTY;
-    kom_pos = NO_MOVE;
+    set_new_komaster(EMPTY);
+    set_new_kom_pos(NO_MOVE);
   }
 
   *is_conditional_ko = 0;
@@ -1213,10 +1230,8 @@ komaster_trymove(int pos, int color, con
 
   if (!ko_move) {
     if (komaster == WEAK_KO) {
-      PUSH_VALUE(komaster);
-      PUSH_VALUE(kom_pos);
-      komaster = EMPTY;
-      kom_pos = NO_MOVE;
+      set_new_komaster(EMPTY);
+      set_new_kom_pos(NO_MOVE);
     }
   }
   else {
@@ -1252,10 +1267,8 @@ komaster_trymove(int pos, int color, con
 
     /* Conditional ko capture, set komaster parameters. */
     if (komaster == EMPTY || komaster == WEAK_KO) {
-      PUSH_VALUE(komaster);
-      PUSH_VALUE(kom_pos);
-      komaster = color;
-      kom_pos = kpos;
+      set_new_komaster(color);
+      set_new_kom_pos(kpos);
       return 1;
     }
   }
@@ -1263,26 +1276,23 @@ komaster_trymove(int pos, int color, con
   if (!ko_move)
     return 1;
 
-  PUSH_VALUE(komaster);
-  PUSH_VALUE(kom_pos);
-
   if (komaster == other) {
     if (color == WHITE)
-      komaster = GRAY_BLACK;
+      set_new_komaster(GRAY_BLACK);
     else
-      komaster = GRAY_WHITE;
+      set_new_komaster(GRAY_WHITE);
   }
   else if (komaster == color) {
     /* This is where we update kom_pos after a nested capture. */
-    kom_pos = kpos;
+    set_new_kom_pos(kpos);
   }
   else {
     /* We can reach here when komaster is EMPTY or WEAK_KO. If previous
      * move was also a ko capture, we now set komaster to WEAK_KO.
      */
     if (previous_board_ko_pos != NO_MOVE) {
-      komaster = WEAK_KO;
-      kom_pos = previous_board_ko_pos;
+      set_new_komaster(WEAK_KO);
+      set_new_kom_pos(previous_board_ko_pos);
     }
   }
   
Index: engine/cache.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/cache.c,v
retrieving revision 1.45
diff -u -p -r1.45 cache.c
--- engine/cache.c      7 Jun 2004 17:15:52 -0000       1.45
+++ engine/cache.c      13 Sep 2004 12:16:12 -0000
@@ -45,15 +45,10 @@ static void tt_clear(Transposition_table
 Transposition_table ttable;
 
 
-#define INIT_ARRAY(a) \
-  hash_init_zobrist_array(a, (int) (sizeof(a) / sizeof(a[0])))
-
 /* Arrays with random numbers for Zobrist hashing of input data (other
  * than the board position). If you add an array here, do not forget
  * to also initialize it in keyhash_init() below.
  */
-static Hash_data komaster_hash[NUM_KOMASTER_STATES];
-static Hash_data kom_pos_hash[BOARDMAX];
 static Hash_data target1_hash[BOARDMAX];
 static Hash_data target2_hash[BOARDMAX];
 static Hash_data routine_hash[NUM_CACHE_ROUTINES];
@@ -65,8 +60,6 @@ keyhash_init(void)
   
   if (!is_initialized) {
     
-    INIT_ARRAY(komaster_hash);
-    INIT_ARRAY(kom_pos_hash);
     INIT_ARRAY(target1_hash);
     INIT_ARRAY(target2_hash);
     INIT_ARRAY(routine_hash);
@@ -80,8 +73,6 @@ calculate_hashval_for_tt(Hash_data *hash
                         int target2, Hash_data *extra_hash)
 { 
   *hashdata = board_hash;                /* from globals.c */
-  hashdata_xor(*hashdata, komaster_hash[get_komaster()]);
-  hashdata_xor(*hashdata, kom_pos_hash[get_kom_pos()]);
   hashdata_xor(*hashdata, routine_hash[routine]);
   hashdata_xor(*hashdata, target1_hash[target1]);
   if (target2 != NO_MOVE)
Index: engine/hash.c
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/hash.c,v
retrieving revision 1.29
diff -u -p -r1.29 hash.c
--- engine/hash.c       8 Jul 2004 15:49:02 -0000       1.29
+++ engine/hash.c       13 Sep 2004 12:16:12 -0000
@@ -41,13 +41,15 @@
 /* ================================================================ */
 
 
-static int is_initialized = 0;
 
 
 /* Random values for the board hash function. For stones and ko position. */
 static Hash_data white_hash[BOARDMAX];
 static Hash_data black_hash[BOARDMAX];
 static Hash_data ko_hash[BOARDMAX];
+static Hash_data komaster_hash[NUM_KOMASTER_STATES];
+static Hash_data kom_pos_hash[BOARDMAX];
+static Hash_data goal_hash[BOARDMAX];
 
 
 /* Get a random Hashvalue, where all bits are used. */
@@ -80,13 +82,17 @@ hash_init_zobrist_array(Hash_data *array
 void
 hash_init(void)
 {
+  static int is_initialized = 0;
   if (is_initialized)
     return;
   
-  hash_init_zobrist_array(black_hash, BOARDMAX);
-  hash_init_zobrist_array(white_hash, BOARDMAX);
-  hash_init_zobrist_array(ko_hash, BOARDMAX);
-  
+  INIT_ARRAY(black_hash);
+  INIT_ARRAY(white_hash);
+  INIT_ARRAY(ko_hash);
+  INIT_ARRAY(komaster_hash);
+  INIT_ARRAY(kom_pos_hash);
+  INIT_ARRAY(goal_hash);
+
   is_initialized = 1;
 }
 
@@ -119,10 +125,7 @@ hashdata_recalc(Hash_data *target, Inter
 }
 
 
-/*
- * Set or remove ko in the hash value and hash position.
- */
-
+/* Set or remove ko in the hash value and hash position.  */
 void
 hashdata_invert_ko(Hash_data *hd, int pos)
 {
@@ -130,10 +133,7 @@ hashdata_invert_ko(Hash_data *hd, int po
 }
 
 
-/*
- * Set or remove a stone of COLOR at pos in a Hash_data.
- */
-
+/* Set or remove a stone of COLOR at pos in a Hash_data.  */
 void
 hashdata_invert_stone(Hash_data *hd, int pos, int color)
 {
@@ -144,11 +144,19 @@ hashdata_invert_stone(Hash_data *hd, int
 }
 
 
-/* Compute hash value to identify the goal area.
- *
- * FIXME: It would be cleaner to have a separate zobrist array for the
- *        goal and xor the values in goal as usual.
- */
+/* Set or remove the komaster value in the hash data. */
+void hashdata_invert_komaster(Hash_data *hd, int komaster)
+{
+  hashdata_xor(*hd, komaster_hash[komaster]);
+}
+
+/* Set or remove the komaster position in the hash data. */
+void hashdata_invert_kom_pos(Hash_data *hd, int kom_pos)
+{
+  hashdata_xor(*hd, kom_pos_hash[kom_pos]);
+}
+
+/* Compute hash value to identify the goal area. */
 Hash_data
 goal_to_hashvalue(const char *goal)
 {
@@ -161,8 +169,7 @@ goal_to_hashvalue(const char *goal)
   for (pos = BOARDMIN; pos < BOARDMAX; pos++)
     if (ON_BOARD(pos) && goal[pos])
       for (i = 0; i < NUM_HASHVALUES; i++) 
-       return_value.hashval[i] += (white_hash[pos].hashval[i]
-                                   + black_hash[pos].hashval[i]);
+       return_value.hashval[i] ^= goal_hash[pos].hashval[i];
   
   return return_value;
 }
Index: engine/hash.h
===================================================================
RCS file: /cvsroot/gnugo/gnugo/engine/hash.h,v
retrieving revision 1.30
diff -u -p -r1.30 hash.h
--- engine/hash.h       8 Jul 2004 15:49:02 -0000       1.30
+++ engine/hash.h       13 Sep 2004 12:16:12 -0000
@@ -84,10 +84,14 @@ Hash_data goal_to_hashvalue(const char *
 
 void hash_init_zobrist_array(Hash_data *array, int size);
 void hash_init(void);
+#define INIT_ARRAY(a) \
+  hash_init_zobrist_array(a, (int) (sizeof(a) / sizeof(a[0])))
 
 void hashdata_recalc(Hash_data *hd, Intersection *board, int ko_pos);
 void hashdata_invert_ko(Hash_data *hd, int pos);
 void hashdata_invert_stone(Hash_data *hd, int pos, int color);
+void hashdata_invert_komaster(Hash_data *hd, int komaster);
+void hashdata_invert_kom_pos(Hash_data *hd, int kom_pos);
 
 char *hashdata_to_string(Hash_data *hashdata);
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]