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 GeneralBoardKey(const GeneralBoardKey& src)
00029 {
00030 for (size_t i=0; i<SIZE; ++i)
00031 elements[i] = src[i];
00032 }
00033 typedef Integer int_t;
00034 size_t size() const{ return SIZE; }
00035 int_t operator[](size_t i) const{
00036 return elements[i];
00037 }
00038 int_t& operator[](size_t i){
00039 return elements[i];
00040 }
00041 unsigned int signature() const { return elements[1]; }
00045 GeneralBoardKey& operator+=(const GeneralBoardKey& r)
00046 {
00047 assert(r.playerBit() == 0);
00048 for(size_t i=0;i<SIZE;i++)
00049 elements[i]+=r.elements[i];
00050 return *this;
00051 }
00055 GeneralBoardKey& operator-=(const GeneralBoardKey& r)
00056 {
00057 assert(r.playerBit() == 0);
00058 for(size_t i=0;i<SIZE;i++)
00059 elements[i]-=r.elements[i];
00060 return *this;
00061 }
00062 void changeTurn()
00063 {
00064 elements[0]^=static_cast<int_t>(1);
00065 }
00069 void setPlayer(Player p)
00070 {
00071 elements[0]=(elements[0]& ~static_cast<int_t>(1))|playerToIndex(p);
00072 }
00073 bool playerBit() const
00074 {
00075 return (static_cast<int>(elements[0]) & 1);
00076 }
00077 bool isPlayerOfTurn(Player p) const
00078 {
00079 return playerBit() == playerToIndex(p);
00080 }
00081 Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
00085 void setRandom();
00086 };
00087
00088 template<typename Integer,size_t SIZE>
00089 inline bool operator==(const GeneralBoardKey<Integer,SIZE>& l,
00090 const GeneralBoardKey<Integer,SIZE>& r)
00091 {
00092 for(size_t i=0;i<SIZE;i++)
00093 if (l[i]!=r[i]) return false;
00094 return true;
00095 }
00096 template<typename Integer,size_t SIZE>
00097 inline bool operator!=(const GeneralBoardKey<Integer,SIZE>& l,
00098 const GeneralBoardKey<Integer,SIZE>& r)
00099 {
00100 return !(l==r);
00101 }
00106 template<typename Integer,size_t SIZE>
00107 inline bool operator<(const GeneralBoardKey<Integer,SIZE>& l,
00108 const GeneralBoardKey<Integer,SIZE>& r)
00109 {
00110 for(size_t i=0;i<SIZE-1;i++)
00111 if (l[i]!=r[i]) return l[i]<r[i];
00112 return l[SIZE-1]<r[SIZE-1];
00113 }
00114
00118 template<typename BoardKeyBase>
00119 struct GeneralHashKey
00120 {
00121 typedef BoardKeyBase base_t;
00122 BoardKeyBase board_key;
00123 PieceStand piece_stand;
00124
00125 typedef typename BoardKeyBase::int_t int_t;
00126
00127 GeneralHashKey()
00128 {
00129 }
00130 const base_t& boardKey() const{
00131 return board_key;
00132 }
00133 unsigned int signature() const
00134 {
00135 return board_key.signature();
00136 }
00137 const PieceStand& pieceStand() const{
00138 return piece_stand;
00139 }
00140 void setPieceStand(const PieceStand& p){
00141 piece_stand=p;
00142 }
00143 size_t size() const{
00144 return board_key.size();
00145 }
00146 int_t operator[](size_t i) const{
00147 return board_key[i];
00148 }
00149 int_t& operator[](size_t i){
00150 return board_key[i];
00151 }
00156 bool isSameBoard(const GeneralHashKey& key) const
00157 {
00158 return board_key == key.boardKey();
00159 }
00160 GeneralHashKey& operator+=(const GeneralHashKey& r)
00161 {
00162 board_key+=r.board_key;
00163 piece_stand.addAtmostOnePiece(r.piece_stand);
00164 return *this;
00165 }
00166 GeneralHashKey& operator-=(const GeneralHashKey& r)
00167 {
00168 board_key-=r.board_key;
00169 piece_stand.subAtmostOnePiece(r.piece_stand);
00170 return *this;
00171 }
00172 const PieceStand blackStand() const
00173 {
00174 return piece_stand;
00175 }
00176 void changeTurn()
00177 {
00178 board_key.changeTurn();
00179 }
00180 void setPlayer(Player p)
00181 {
00182 board_key.setPlayer(p);
00183 }
00184 bool isPlayerOfTurn(Player p) const
00185 {
00186 return board_key.isPlayerOfTurn(p);
00187 }
00188 Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
00192 void setRandom();
00193
00194 };
00195 template<typename T>
00196 inline bool operator==(const GeneralHashKey<T>& l,
00197 const GeneralHashKey<T>& r)
00198 {
00202 return l.board_key == r.board_key &&
00203 l.piece_stand == r.piece_stand;
00204 }
00205 template<typename T>
00206 inline bool operator!=(const GeneralHashKey<T>& l,
00207 const GeneralHashKey<T>& r)
00208 {
00209 return !(l==r);
00210 }
00215 template<typename T>
00216 inline bool operator<(const GeneralHashKey<T>& l,
00217 const GeneralHashKey<T>& r)
00218 {
00219 if (l.pieceStand < r.pieceStand) return true;
00220 else if (r.pieceStand < l.pieceStand) return false;
00221 return l.board_key < r.board_key;
00222 }
00223
00224 typedef GeneralBoardKey<unsigned int,4> BoardKey32;
00225 typedef GeneralBoardKey<unsigned long long,2> BoardKey64;
00226 typedef GeneralHashKey<BoardKey32> HashKey32;
00227 typedef GeneralHashKey<BoardKey64> HashKey64;
00228 }
00229 }
00230
00231 #endif
00232
00233
00234
00235
00236