boardKey.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boardKey.h
Go to the documentation of this file.
1 /* boardKey.h
2 */
3 #ifndef OSL_BOARD_KEY_H
4 #define OSL_BOARD_KEY_H
5 #include "osl/misc/carray.h"
6 #include "osl/pieceStand.h"
7 #include <cstddef>
8
9 namespace osl
10 {
11 namespace hash
12 {
22 template<typename Integer,size_t SIZE>
23 class GeneralBoardKey
24 {
25 CArray<Integer,SIZE> elements;
26 public:
27 GeneralBoardKey();
28 GeneralBoardKey(const GeneralBoardKey& src)
29 {
30 for (size_t i=0; i<SIZE; ++i)
31 elements[i] = src[i]; // better than memmove
32 }
33 typedef Integer int_t;
34 size_t size() const{ return SIZE; }
35 int_t operator[](size_t i) const{
36 return elements[i];
37 }
38 int_t& operator[](size_t i){
39 return elements[i];
40 }
41 unsigned int signature() const { return elements[1]; }
45 GeneralBoardKey& operator+=(const GeneralBoardKey& r)
46 {
47 assert(r.playerBit() == 0);
48 for(size_t i=0;i<SIZE;i++)
49 elements[i]+=r.elements[i];
50 return *this;
51 }
55 GeneralBoardKey& operator-=(const GeneralBoardKey& r)
56 {
57 assert(r.playerBit() == 0);
58 for(size_t i=0;i<SIZE;i++)
59 elements[i]-=r.elements[i];
60 return *this;
61 }
62 void changeTurn()
63 {
64 elements[0]^=static_cast<int_t>(1);
65 }
69 void setPlayer(Player p)
70 {
71 elements[0]=(elements[0]& ~static_cast<int_t>(1))|playerToIndex(p);
72 }
73 bool playerBit() const
74 {
75 return (static_cast<int>(elements[0]) & 1);
76 }
77 bool isPlayerOfTurn(Player p) const
78 {
79 return playerBit() == playerToIndex(p);
80 }
81 Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
85 void setRandom();
86 };
87
88 template<typename Integer,size_t SIZE>
89 inline bool operator==(const GeneralBoardKey<Integer,SIZE>& l,
90 const GeneralBoardKey<Integer,SIZE>& r)
91 {
92 for(size_t i=0;i<SIZE;i++)
93 if (l[i]!=r[i]) return false;
94 return true;
95 }
96 template<typename Integer,size_t SIZE>
97 inline bool operator!=(const GeneralBoardKey<Integer,SIZE>& l,
98 const GeneralBoardKey<Integer,SIZE>& r)
99 {
100 return !(l==r);
101 }
106 template<typename Integer,size_t SIZE>
107 inline bool operator<(const GeneralBoardKey<Integer,SIZE>& l,
108 const GeneralBoardKey<Integer,SIZE>& r)
109 {
110 for(size_t i=0;i<SIZE-1;i++)
111 if (l[i]!=r[i]) return l[i]<r[i];
112 return l[SIZE-1]<r[SIZE-1];
113 }
114
118 template<typename BoardKeyBase>
119 struct GeneralHashKey
120 {
121 typedef BoardKeyBase base_t;
122 BoardKeyBase board_key;
123 PieceStand piece_stand;
124
125 typedef typename BoardKeyBase::int_t int_t;
126
127 GeneralHashKey()
128 {
129 }
130 const base_t& boardKey() const{
131 return board_key;
132 }
133 unsigned int signature() const
134 {
135 return board_key.signature();
136 }
137 const PieceStand& pieceStand() const{
138 return piece_stand;
139 }
140 void setPieceStand(const PieceStand& p){
141 piece_stand=p;
142 }
143 size_t size() const{
144 return board_key.size();
145 }
146 int_t operator[](size_t i) const{
147 return board_key[i];
148 }
149 int_t& operator[](size_t i){
150 return board_key[i];
151 }
156 bool isSameBoard(const GeneralHashKey& key) const
157 {
158 return board_key == key.boardKey();
159 }
160 GeneralHashKey& operator+=(const GeneralHashKey& r)
161 {
162 board_key+=r.board_key;
163 piece_stand.addAtmostOnePiece(r.piece_stand);
164 return *this;
165 }
166 GeneralHashKey& operator-=(const GeneralHashKey& r)
167 {
168 board_key-=r.board_key;
169 piece_stand.subAtmostOnePiece(r.piece_stand);
170 return *this;
171 }
172 const PieceStand blackStand() const
173 {
174 return piece_stand;
175 }
176 void changeTurn()
177 {
178 board_key.changeTurn();
179 }
180 void setPlayer(Player p)
181 {
182 board_key.setPlayer(p);
183 }
184 bool isPlayerOfTurn(Player p) const
185 {
186 return board_key.isPlayerOfTurn(p);
187 }
188 Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
192 void setRandom();
193
194 };
195 template<typename T>
196 inline bool operator==(const GeneralHashKey<T>& l,
197 const GeneralHashKey<T>& r)
198 {
202 return l.board_key == r.board_key &&
203 l.piece_stand == r.piece_stand;
204 }
205 template<typename T>
206 inline bool operator!=(const GeneralHashKey<T>& l,
207 const GeneralHashKey<T>& r)
208 {
209 return !(l==r);
210 }
215 template<typename T>
216 inline bool operator<(const GeneralHashKey<T>& l,
217 const GeneralHashKey<T>& r)
218 {
219 if (l.piece_stand < r.piece_stand) return true;
220 else if (r.piece_stand < l.piece_stand) return false;
221 return l.board_key < r.board_key;
222 }
223
224 typedef GeneralBoardKey<unsigned int,4> BoardKey32;
225 typedef GeneralBoardKey<unsigned long long,2> BoardKey64;
226 typedef GeneralHashKey<BoardKey32> HashKey32;
227 typedef GeneralHashKey<BoardKey64> HashKey64;
228 } // namespace hash
229 } // namespace osl
230
231 #endif /* OSL_BOARD_KEY_H */
232 // ;;; Local Variables:
233 // ;;; mode:c++
234 // ;;; c-basic-offset:2
235 // ;;; End:
236