00001
00002
00003 #ifndef _BOARD_KEY_H
00004 #define _BOARD_KEY_H
00005 #include "osl/misc/carray.h"
00006 #include "osl/pieceStand.h"
00007 #include <cstddef>
00008
00009 namespace osl
00010 {
00011 namespace hash
00012 {
00022 template<typename Integer,size_t SIZE>
00023 class GeneralBoardKey
00024 {
00025 CArray<Integer,SIZE> elements;
00026 public:
00027 GeneralBoardKey();
00028 typedef Integer int_t;
00029 size_t size() const{
00030 return SIZE;
00031 }
00032 int_t operator[](size_t i) const{
00033 return elements[i];
00034 }
00035 int_t& operator[](size_t i){
00036 return elements[i];
00037 }
00041 GeneralBoardKey& operator+=(const GeneralBoardKey& r)
00042 {
00043 assert(r.playerBit() == 0);
00044 for(size_t i=0;i<SIZE;i++)
00045 elements[i]+=r.elements[i];
00046 return *this;
00047 }
00051 GeneralBoardKey& operator-=(const GeneralBoardKey& r)
00052 {
00053 assert(r.playerBit() == 0);
00054 for(size_t i=0;i<SIZE;i++)
00055 elements[i]-=r.elements[i];
00056 return *this;
00057 }
00058 void changeTurn()
00059 {
00060 elements[0]^=static_cast<int_t>(1);
00061 }
00065 void setPlayer(Player p)
00066 {
00067 elements[0]=(elements[0]& ~static_cast<int_t>(1))|playerToIndex(p);
00068 }
00069 bool playerBit() const
00070 {
00071 return (static_cast<int>(elements[0]) & 1);
00072 }
00073 bool isPlayerOfTurn(Player p) const
00074 {
00075 return playerBit() == playerToIndex(p);
00076 }
00080 void setRandom();
00081 };
00082
00083 template<typename Integer,size_t SIZE>
00084 inline bool operator==(const GeneralBoardKey<Integer,SIZE>& l,
00085 const GeneralBoardKey<Integer,SIZE>& r)
00086 {
00087 for(size_t i=0;i<SIZE;i++)
00088 if (l[i]!=r[i]) return false;
00089 return true;
00090 }
00091 template<typename Integer,size_t SIZE>
00092 inline bool operator!=(const GeneralBoardKey<Integer,SIZE>& l,
00093 const GeneralBoardKey<Integer,SIZE>& r)
00094 {
00095 return !(l==r);
00096 }
00101 template<typename Integer,size_t SIZE>
00102 inline bool operator<(const GeneralBoardKey<Integer,SIZE>& l,
00103 const GeneralBoardKey<Integer,SIZE>& r)
00104 {
00105 for(size_t i=0;i<SIZE-1;i++)
00106 if (l[i]!=r[i]) return l[i]<r[i];
00107 return l[SIZE-1]<r[SIZE-1];
00108 }
00109
00113 template<typename BoardKeyBase>
00114 class GeneralSignatureBoardKey
00115 {
00116 public:
00117 typedef BoardKeyBase base_t;
00118 typedef typename BoardKeyBase::int_t int_t;
00119 private:
00120 BoardKeyBase board_key;
00121 unsigned int signature;
00122 public:
00123 GeneralSignatureBoardKey() : signature(0)
00124 {
00125 }
00126 unsigned int getSignature() const
00127 {
00128 return signature;
00129 }
00130 void setSignature(int s)
00131 {
00132 signature=s;
00133 }
00134 const BoardKeyBase& getBoardKey() const{ return board_key; }
00135 size_t size() const{
00136 return board_key.size();
00137 }
00138 int_t operator[](size_t i) const{
00139 return board_key[i];
00140 }
00141 int_t& operator[](size_t i){
00142 return board_key[i];
00143 }
00144 GeneralSignatureBoardKey& operator+=(const GeneralSignatureBoardKey& r)
00145 {
00146 board_key+=r.board_key;
00147 signature+=r.signature;
00148 return *this;
00149 }
00150 GeneralSignatureBoardKey& operator-=(const GeneralSignatureBoardKey& r)
00151 {
00152 board_key-=r.board_key;
00153 signature-=r.signature;
00154 return *this;
00155 }
00156 void changeTurn()
00157 {
00158 board_key.changeTurn();
00159 }
00160 void setPlayer(Player p)
00161 {
00162 board_key.setPlayer(p);
00163 }
00164 bool isPlayerOfTurn(Player p) const
00165 {
00166 return board_key.isPlayerOfTurn(p);
00167 }
00168 void setRandom();
00169 };
00170
00171 template<typename T>
00172 inline bool operator==(const GeneralSignatureBoardKey<T>& l,
00173 const GeneralSignatureBoardKey<T>& r)
00174 {
00178 return l.getSignature() == r.getSignature() &&
00179 l.getBoardKey() == r.getBoardKey();
00180 }
00181 template<typename T>
00182 inline bool operator!=(const GeneralSignatureBoardKey<T>& l,
00183 const GeneralSignatureBoardKey<T>& r)
00184 {
00185 return !(l==r);
00186 }
00191 template<typename T>
00192 inline bool operator<(const GeneralSignatureBoardKey<T>& l,
00193 const GeneralSignatureBoardKey<T>& r)
00194 {
00195 if (l.getSignature() < r.getSignature()) return true;
00196 else if (r.getSignature() < l.getSignature()) return false;
00197 return l.getBoardKey() < r.getBoardKey();
00198 }
00199
00203 template<typename SignatureBoardKeyBase>
00204 struct GeneralHashKey
00205 {
00206 typedef SignatureBoardKeyBase base_t;
00207 SignatureBoardKeyBase signatureKey;
00208 PieceStand pieceStand;
00209
00210 typedef typename SignatureBoardKeyBase::int_t int_t;
00211
00212 GeneralHashKey()
00213 {
00214 }
00215 const base_t& getSignatureKey() const{
00216 return signatureKey;
00217 }
00218 unsigned int getSignature() const
00219 {
00220 return signatureKey.getSignature();
00221 }
00222 void setSignature(int s)
00223 {
00224 signatureKey.setSignature(s);
00225 }
00226 const PieceStand& getPieceStand() const{
00227 return pieceStand;
00228 }
00229 void setPieceStand(const PieceStand& p){
00230 pieceStand=p;
00231 }
00232 size_t size() const{
00233 return signatureKey.size();
00234 }
00235 int_t operator[](size_t i) const{
00236 return signatureKey[i];
00237 }
00238 int_t& operator[](size_t i){
00239 return signatureKey[i];
00240 }
00245 bool isSameBoard(const GeneralHashKey& key) const
00246 {
00247 return signatureKey == key.getSignatureKey();
00248 }
00249 GeneralHashKey& operator+=(const GeneralHashKey& r)
00250 {
00251 signatureKey+=r.signatureKey;
00252 pieceStand.addAtmostOnePiece(r.pieceStand);
00253 return *this;
00254 }
00255 GeneralHashKey& operator-=(const GeneralHashKey& r)
00256 {
00257 signatureKey-=r.signatureKey;
00258 pieceStand.subAtmostOnePiece(r.pieceStand);
00259 return *this;
00260 }
00261 const PieceStand blackStand() const
00262 {
00263 return pieceStand;
00264 }
00265 void changeTurn()
00266 {
00267 signatureKey.changeTurn();
00268 }
00269 void setPlayer(Player p)
00270 {
00271 signatureKey.setPlayer(p);
00272 }
00273 bool isPlayerOfTurn(Player p) const
00274 {
00275 return signatureKey.isPlayerOfTurn(p);
00276 }
00277 Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
00281 void setRandom();
00282
00283 };
00284 template<typename T>
00285 inline bool operator==(const GeneralHashKey<T>& l,
00286 const GeneralHashKey<T>& r)
00287 {
00291 return l.signatureKey == r.signatureKey &&
00292 l.pieceStand == r.pieceStand;
00293 }
00294 template<typename T>
00295 inline bool operator!=(const GeneralHashKey<T>& l,
00296 const GeneralHashKey<T>& r)
00297 {
00298 return !(l==r);
00299 }
00304 template<typename T>
00305 inline bool operator<(const GeneralHashKey<T>& l,
00306 const GeneralHashKey<T>& r)
00307 {
00308 if (l.pieceStand < r.pieceStand) return true;
00309 else if (r.pieceStand < l.pieceStand) return false;
00310 return l.signatureKey < r.signatureKey;
00311 }
00312
00313 typedef GeneralBoardKey<unsigned int,4> BoardKey32;
00314 typedef GeneralBoardKey<unsigned long long,2> BoardKey64;
00315 typedef GeneralSignatureBoardKey<BoardKey32> SignatureKey32;
00316 typedef GeneralSignatureBoardKey<BoardKey64> SignatureKey64;
00317 typedef GeneralHashKey<SignatureKey32> HashKey32;
00318 typedef GeneralHashKey<SignatureKey64> HashKey64;
00319 }
00320 }
00321
00322 #endif
00323
00324
00325
00326
00327