All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hashKey.h
Go to the documentation of this file.
1 /* hashKey.h
2  */
3 #ifndef OSL_HASH_KEY_H
4 #define OSL_HASH_KEY_H
5 
6 #include "osl/config.h"
7 #ifdef OSL_LONG_HASH_KEY
8 # include "osl/hash/boardKey.h"
9 #else
10 # include "osl/hash/boardKey128.h"
11 #endif
12 #include "osl/piece.h"
13 #include "osl/move.h"
14 #include "osl/pieceStand.h"
15 #include "osl/state/simpleState.h"
16 #include "osl/misc/carray.h"
17 
18 namespace osl
19 {
20  namespace hash
21  {
22 #ifdef OSL_LONG_HASH_KEY
23 # if OSL_WORDSIZE == 64
24  typedef HashKey64 HashKeyBase;
25  typedef BoardKey64 BoardKey;
26 # elif OSL_WORDSIZE == 32
27  typedef HashKey32 HashKeyBase;
28  typedef BoardKey32 BoardKey;
29 # endif
30 #else
33 #endif
34  class HashKey : public HashKeyBase
35  {
36  public:
38  HashKey(const SimpleState&);
39 #ifndef OSL_LONG_HASH_KEY
40  HashKey(uint64_t h0, uint32_t h1, uint32_t s)
41  : HashKeyBase(h0, h1, s)
42  {
43  }
44 #endif
45  const HashKey newHashWithMove(Move move) const;
46  const HashKey newMakeMove(Move) const;
47  const HashKey newUnmakeMove(Move) const;
48 
49  void dumpContents(std::ostream& os) const;
50  void dumpContentsCerr() const;
51  static const HashKey readFromDump(const std::string&);
52  static const HashKey readFromDump(std::istream&);
53  };
54  std::ostream& operator<<(std::ostream& os,const HashKey& h);
55 
57  {
58 #ifdef OSL_LONG_HASH_KEY
59  CArray2d<HashKey,Square::SIZE,PTYPEO_SIZE> key;
60 #else
61  static const CArray2d<HashKey128Layout,Square::SIZE,PTYPEO_SIZE> key;
62 #endif
63  public:
64  HashGenTable();
65 #ifdef OSL_LONG_HASH_KEY
66  void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) const{
67  assert(sq.isValid() && isValidPtypeO(ptypeo));
68  hk+=key[sq.index()][ptypeo-PTYPEO_MIN];
69  }
70  void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) const{
71  assert(sq.isValid() && isValidPtypeO(ptypeo));
72  hk-=key[sq.index()][ptypeo-PTYPEO_MIN];
73 #else
74  static void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
75  assert(sq.isValid() && isValidPtypeO(ptypeo));
76  hk += HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
77  }
78  static void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
79  assert(sq.isValid() && isValidPtypeO(ptypeo));
80  hk -= HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
81 #endif
82  }
83  };
84  extern const HashGenTable Hash_Gen_Table;
85 
86  } // namespace hash
87  using hash::HashKey;
88  using hash::BoardKey;
89 
90  namespace stl
91  {
92  template <typename T> struct hash;
93  template <>
94  struct hash<osl::hash::HashKey>{
95  unsigned long operator()(const HashKey& h) const{
96  return h.signature();
97  }
98  };
99  template<>
100  struct hash<osl::hash::BoardKey>
101  {
102  unsigned long operator()(const BoardKey& h) const
103  {
104  return h.signature();
105  }
106  };
107  } // namespace stl
108 #ifdef USE_TBB_HASH
109  struct TBBHashCompare
110  {
111  size_t hash(HashKey const& key) const {
112  return (size_t)(key.signature());
113  }
114  bool equal(HashKey const& key1, HashKey const& key2) const {
115  return key1==key2;
116  }
117  };
118  struct TBBSignatureCompare
119  {
120  size_t hash(hash::BoardKey const& key) const {
121  return (size_t)key.signature();
122  }
123  bool equal(hash::BoardKey const& key1, hash::BoardKey const& key2) const {
124  return key1==key2;
125  }
126  };
127 #endif
128 } // namespace osl
129 
130 #endif /* OSL_HASH_KEY_H */
131 // ;;; Local Variables:
132 // ;;; mode:c++
133 // ;;; c-basic-offset:2
134 // ;;; End: