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/basic_type.h"
7 #include "osl/bits/pieceStand.h"
8 #include "osl/simpleState.h"
9 #include <cstddef>
10 
11 namespace osl
12 {
13  namespace hash
14  {
15  struct BoardKey96 : public std::pair<uint64_t,uint32_t>
16  {
18  BoardKey96(const std::pair<uint64_t,uint32_t>& src)
19  : std::pair<uint64_t,uint32_t>(src)
20  {
21  }
22  uint32_t signature() const { return second; }
23  size_t size() const { return 2; }
24  uint64_t operator[](size_t i) const { return i ? first : second; }
25  };
26  class HashGenTable;
28  {
29  uint64_t board64;
30  uint32_t board32, piece_stand;
31  };
36  class HashKey128 : private HashKey128Layout
37  {
38  friend class HashGenTable;
39  public:
41  {
42  board64 = board32 = piece_stand = 0;
43  }
44  HashKey128(uint64_t h0, uint32_t h1, uint32_t s)
45  {
46  board64 = h0;
47  board32 = h1;
48  piece_stand = s;
49  }
51  {
52  }
53  const BoardKey96 boardKey() const {
54  return std::make_pair(board64, board32);
55  }
56  uint64_t boardKey64() const { return board64; }
57  uint64_t signature() const { return board32; }
59  uint64_t hash64() const { return board64 + pieceStand64(); }
60  uint64_t pieceStand64() const {
61  return Stand_Hash.toUint64(pieceStand());
62  }
63  const PieceStand pieceStand() const{ return PieceStand(piece_stand); }
64  const PieceStand blackStand() const { return PieceStand(piece_stand); }
65  void setPieceStand(const PieceStand& p) { piece_stand=p.getFlags(); }
66 
71  bool isSameBoard(const HashKey128& key) const
72  {
73  return boardKey() == key.boardKey();
74  }
76  {
77  board64 += r.board64;
78  board32 += r.board32;
79  PieceStand new_stand(piece_stand);
80  new_stand.addAtmostOnePiece(r.pieceStand());
81  piece_stand = new_stand.getFlags();
82  return *this;
83  }
85  {
86  board64 -= r.board64;
87  board32 -= r.board32;
88  PieceStand new_stand(piece_stand);
89  new_stand.subAtmostOnePiece(r.pieceStand());
90  piece_stand = new_stand.getFlags();
91  return *this;
92  }
93  void add(Move move) { board64 += move.intValue(); }
94  void changeTurn() { board64 ^= static_cast<uint64_t>(1); }
95  void setPlayer(Player p)
96  {
97  board64 &= ~static_cast<uint64_t>(1);
98  board64 |= playerToIndex(p);
99  }
100  bool playerBit() const { return board64 & 1; }
101  bool isPlayerOfTurn(Player p) const
102  {
103  return playerBit() == playerToIndex(p);
104  }
105  Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
110  void setRandom();
111  size_t size() const { return 2; }
112  uint64_t operator[](size_t i) const { return i ? board64 : board32; }
113  struct StandHash
114  {
117  StandHash();
118  uint64_t toUint64(PieceStand stand) const
119  {
120  int major_pawn = stand.get(PAWN)*9
121  + stand.get(ROOK)*3 + stand.get(BISHOP);
122  int pieces = stand.get(GOLD)*125 + stand.get(SILVER)*25
123  + stand.get(KNIGHT)*5 + stand.get(LANCE);
124  return HashMajorPawn[major_pawn] + HashPiece[pieces];
125  }
126  };
127  static const StandHash Stand_Hash;
128  };
129  inline bool operator==(const HashKey128& l, const HashKey128& r)
130  {
131  return l.boardKey() == r.boardKey() && l.pieceStand() == r.pieceStand();
132  }
133  inline bool operator!=(const HashKey128& l, const HashKey128& r)
134  {
135  return !(l==r);
136  }
141  inline bool operator<(const HashKey128& l, const HashKey128& r)
142  {
143  if (l.pieceStand() < r.pieceStand())
144  return true;
145  else if (r.pieceStand() < l.pieceStand())
146  return false;
147  return l.boardKey() < r.boardKey();
148  }
149 
152  class HashKey : public HashKeyBase
153  {
154  public:
155  HashKey() :HashKeyBase(){}
156  HashKey(const SimpleState&);
157  const HashKey newHashWithMove(Move move) const;
158  const HashKey newMakeMove(Move) const;
159  const HashKey newUnmakeMove(Move) const;
160 
161  void dumpContents(std::ostream& os) const;
162  void dumpContentsCerr() const;
163  static const HashKey readFromDump(const std::string&);
164  static const HashKey readFromDump(std::istream&);
165  };
166  std::ostream& operator<<(std::ostream& os,const HashKey& h);
167 
169  {
171  public:
172  static void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
173  assert(sq.isValid() && isValidPtypeO(ptypeo));
174  hk += HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
175  }
176  static void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
177  assert(sq.isValid() && isValidPtypeO(ptypeo));
178  hk -= HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
179  }
180  };
181 
182  } // namespace hash
183  using hash::HashKey;
184  using hash::HashGenTable;
185  using hash::BoardKey;
186 } // namespace osl
187 
188 namespace std
189 {
190  template <typename T> struct hash;
191  template <>
192  struct hash<osl::HashKey>{
193  unsigned long operator()(const osl::HashKey& h) const {
194  return h.signature();
195  }
196  };
197  template<>
198  struct hash<osl::BoardKey>
199  {
200  unsigned long operator()(const osl::BoardKey& h) const {
201  return h.signature();
202  }
203  };
204 } // namespace stl
205 
206 #endif /* OSL_HASH_KEY_H */
207 // ;;; Local Variables:
208 // ;;; mode:c++
209 // ;;; c-basic-offset:2
210 // ;;; End:
void setPieceStand(const PieceStand &p)
Definition: hashKey.h:65
void add(Move move)
Definition: hashKey.h:93
uint64_t operator[](size_t i) const
Definition: hashKey.h:112
unsigned long operator()(const osl::HashKey &h) const
Definition: hashKey.h:193
std::ostream & operator<<(std::ostream &os, const HashKey &h)
Definition: hashKey.cc:25
bool operator<(const HashKey128 &l, const HashKey128 &r)
set等で使うためのみの不等号.
Definition: hashKey.h:141
uint64_t pieceStand64() const
Definition: hashKey.h:60
const BoardKey96 boardKey() const
Definition: hashKey.h:53
CArray< uint64_t, 19 *3 *3 > HashMajorPawn
Definition: hashKey.h:115
bool operator==(const HashKey128 &l, const HashKey128 &r)
Definition: hashKey.h:129
uint64_t toUint64(PieceStand stand) const
Definition: hashKey.h:118
unsigned int get(Ptype type) const
static const CArray2d< HashKey128Layout, Square::SIZE, PTYPEO_SIZE > key
Definition: hashKey.h:170
unsigned long operator()(const osl::BoardKey &h) const
Definition: hashKey.h:200
const PieceStand blackStand() const
Definition: hashKey.h:64
bool isValidPtypeO(int ptypeO)
Definition: basic_type.cc:30
uint64_t operator[](size_t i) const
Definition: hashKey.h:24
uint32_t signature() const
Definition: hashKey.h:22
int intValue() const
Definition: basic_type.h:1065
const PieceStand pieceStand() const
Definition: hashKey.h:63
size_t size() const
Definition: hashKey.h:111
void subAtmostOnePiece(PieceStand const &ps)
圧縮していない moveの表現 .
Definition: basic_type.h:1051
uint64_t hash64() const
持駒も含んだ64bitのハッシュ
Definition: hashKey.h:59
BoardKey96(const std::pair< uint64_t, uint32_t > &src)
Definition: hashKey.h:18
HashKey128 & operator+=(const HashKey128 &r)
Definition: hashKey.h:75
void setPlayer(Player p)
Definition: hashKey.h:95
constexpr int playerToIndex(Player player)
Definition: basic_type.h:16
static const StandHash Stand_Hash
Definition: hashKey.h:127
unsigned int index() const
Definition: basic_type.h:572
unsigned int getFlags() const
static void addHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition: hashKey.h:172
CArray< uint64_t, 5 *5 *5 *5 > HashPiece
Definition: hashKey.h:116
bool isSameBoard(const HashKey128 &key) const
駒台の情報を除いて同じかどうか.
Definition: hashKey.h:71
uint64_t boardKey64() const
Definition: hashKey.h:56
BoardKey96 BoardKey
Definition: hashKey.h:151
手番を含んだ盤面の状態のハッシュ値を保持するためのクラス.
Definition: hashKey.h:36
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199
HashKey128 HashKeyBase
Definition: hashKey.h:150
HashKey128(uint64_t h0, uint32_t h1, uint32_t s)
Definition: hashKey.h:44
bool operator!=(const HashKey128 &l, const HashKey128 &r)
Definition: hashKey.h:133
HashKey128 & operator-=(const HashKey128 &r)
Definition: hashKey.h:84
Player turn() const
Definition: hashKey.h:105
size_t size() const
Definition: hashKey.h:23
Player
Definition: basic_type.h:8
uint64_t signature() const
Definition: hashKey.h:57
bool isPlayerOfTurn(Player p) const
Definition: hashKey.h:101
HashKey128(const HashKey128Layout &src)
Definition: hashKey.h:50
片方の手番の持駒の枚数を記録するクラス.
bool playerBit() const
Definition: hashKey.h:100
static void subHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition: hashKey.h:176
void addAtmostOnePiece(PieceStand const &ps)
pieceStand同士の加算,減算.
bool isValid() const
Definition: basic_type.cc:184