boardKey128.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boardKey128.h
Go to the documentation of this file.
1 /* boardKey128.h
2 */
3 #ifndef OSL_BOARDKEY128_H
4 #define OSL_BOARDKEY128_H
5
6 #include "osl/pieceStand.h"
7 #include "osl/move.h"
8 #include "osl/misc/carray.h"
9 #include <cstddef>
10
11 namespace osl
12 {
13 namespace hash
14 {
15 struct BoardKey96 : public std::pair<uint64_t,uint32_t>
16 {
17 BoardKey96() {}
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;
27 struct HashKey128Layout
28 {
29 uint64_t board64;
30 uint32_t board32, piece_stand;
31 };
36 class HashKey128 : private HashKey128Layout
37 {
38 friend class HashGenTable;
39 public:
40 HashKey128()
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 }
50 HashKey128(const HashKey128Layout& src) : HashKey128Layout(src)
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 }
75 HashKey128& operator+=(const HashKey128& r)
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 }
84 HashKey128& operator-=(const HashKey128& r)
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 {
115 CArray<uint64_t, 19*3*3> HashMajorPawn;
116 CArray<uint64_t, 5*5*5*5> HashPiece;
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 } // namespace hash
150 } // namespace osl
151
152
153 #endif /* OSL_BOARDKEY128_H */
154 // ;;; Local Variables:
155 // ;;; mode:c++
156 // ;;; c-basic-offset:2
157 // ;;; End:
158