00001
00002
00003 #ifndef _HASH_KEY_H
00004 #define _HASH_KEY_H
00005
00006 #include "osl/config.h"
00007 #include "osl/hash/boardKey.h"
00008 #include "osl/piece.h"
00009 #include "osl/move.h"
00010 #include "osl/pieceStand.h"
00011 #include "osl/state/simpleState.h"
00012 #include "osl/misc/carray.h"
00013
00014 namespace osl
00015 {
00016 namespace hash
00017 {
00018 #if OSL_WORDSIZE == 64
00019 typedef HashKey64 HashKeyBase;
00020 typedef BoardKey64 BoardKey;
00021 typedef SignatureKey64 SignatureKey;
00022 #elif OSL_WORDSIZE == 32
00023 typedef HashKey32 HashKeyBase;
00024 typedef BoardKey32 BoardKey;
00025 typedef SignatureKey32 SignatureKey;
00026 #endif
00027 class HashKey : public HashKeyBase
00028 {
00029 public:
00030 HashKey() :HashKeyBase(){}
00031 const HashKey newHashWithMove(Move move) const;
00032
00033 PtypeO doSimpleMove(const SimpleState& state,
00034 Position from, Position to, int promote_mask);
00035 void doDropMove(const SimpleState&, Position to,Ptype ptype);
00036 void doCaptureMove(const SimpleState& state,
00037 Position from, Position to, Piece p1, int promote_mask);
00038 static HashKey calcHash(const SimpleState& state);
00039
00040 void dumpContents(std::ostream& os) const;
00041 static const HashKey readFromDump(const std::string&);
00042 static const HashKey readFromDump(std::istream&);
00043 };
00044 std::ostream& operator<<(std::ostream& os,const HashKey& h);
00045
00046 class HashGenTable
00047 {
00048 CArray2d<HashKey,Position::SIZE,PTYPEO_SIZE> hashKey;
00049 public:
00050 HashGenTable();
00051 void addHashKey(HashKey& hk,Position pos,PtypeO ptypeo) const{
00052 assert(pos.isValid() && isValidPtypeO(ptypeo));
00053 hk+=hashKey[pos.index()][ptypeo-PTYPEO_MIN];
00054 }
00055 void subHashKey(HashKey& hk,Position pos,PtypeO ptypeo) const{
00056 assert(pos.isValid() && isValidPtypeO(ptypeo));
00057 hk-=hashKey[pos.index()][ptypeo-PTYPEO_MIN];
00058 }
00059 };
00060 extern const HashGenTable Hash_Gen_Table;
00061
00062 inline
00063 PtypeO HashKey::doSimpleMove(const SimpleState& state,
00064 Position from, Position to, int promote_mask)
00065 {
00066 const PtypeO ptypeo=state.getPieceAt(from).ptypeO();
00067 const PtypeO newPtypeo=promoteWithMask(ptypeo,promote_mask);
00068 Hash_Gen_Table.subHashKey(*this,from,ptypeo);
00069 Hash_Gen_Table.addHashKey(*this,to,newPtypeo);
00070 return newPtypeo;
00071 }
00072 inline
00073 void HashKey::doDropMove(const SimpleState& state, Position to,Ptype ptype)
00074 {
00075 const Player p=state.getTurn();
00076 Hash_Gen_Table.subHashKey(*this,Position::STAND(),newPtypeO(p,ptype));
00077 Hash_Gen_Table.addHashKey(*this,to,newPtypeO(p,ptype));
00078 }
00079 inline
00080 void HashKey::doCaptureMove(const SimpleState& state, Position from,
00081 Position to, Piece p1, int promote_mask)
00082 {
00083 const Ptype capturePtype=p1.ptype();
00084 const Piece p0=state.getPieceAt(from);
00085 const Piece newPiece=p0.promoteWithMask(promote_mask);
00086 const Player p=state.getTurn();
00087
00088 Hash_Gen_Table.subHashKey(*this,to,newPtypeO(alt(p),capturePtype));
00089 Hash_Gen_Table.addHashKey(*this,Position::STAND(),newPtypeO(p,unpromote(capturePtype)));
00090
00091 Hash_Gen_Table.subHashKey(*this,from,p0.ptypeO());
00092 Hash_Gen_Table.addHashKey(*this,to,newPiece.ptypeO());
00093 }
00094
00095 }
00096 using hash::HashKey;
00097 using hash::SignatureKey;
00098
00099 namespace stl
00100 {
00101 template <typename T> struct hash;
00102 template <>
00103 struct hash<osl::hash::HashKey>{
00104 unsigned long operator()(const HashKey& h) const{
00105 return h.getSignature();
00106 }
00107 };
00108 template<>
00109 struct hash<osl::hash::SignatureKey>
00110 {
00111 unsigned long operator()(const SignatureKey& h) const
00112 {
00113 return h.getSignature();
00114 }
00115 };
00116 }
00117
00118 }
00119
00120 #endif
00121
00122
00123
00124