00001 /* hashKey.h 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 #elif OSL_WORDSIZE == 32 00022 typedef HashKey32 HashKeyBase; 00023 typedef BoardKey32 BoardKey; 00024 #endif 00025 class HashKey : public HashKeyBase 00026 { 00027 public: 00028 HashKey() :HashKeyBase(){} 00029 HashKey(const SimpleState&); 00030 const HashKey newHashWithMove(Move move) const; 00031 00032 const HashKey newMakeMove(Move) const; 00033 const HashKey newUnmakeMove(Move) const; 00034 00035 void dumpContents(std::ostream& os) const; 00036 void dumpContentsCerr() const; 00037 static const HashKey readFromDump(const std::string&); 00038 static const HashKey readFromDump(std::istream&); 00039 }; 00040 std::ostream& operator<<(std::ostream& os,const HashKey& h); 00041 00042 class HashGenTable 00043 { 00044 CArray2d<HashKey,Position::SIZE,PTYPEO_SIZE> hashKey; 00045 public: 00046 HashGenTable(); 00047 void addHashKey(HashKey& hk,Position pos,PtypeO ptypeo) const{ 00048 assert(pos.isValid() && isValidPtypeO(ptypeo)); 00049 hk+=hashKey[pos.index()][ptypeo-PTYPEO_MIN]; 00050 } 00051 void subHashKey(HashKey& hk,Position pos,PtypeO ptypeo) const{ 00052 assert(pos.isValid() && isValidPtypeO(ptypeo)); 00053 hk-=hashKey[pos.index()][ptypeo-PTYPEO_MIN]; 00054 } 00055 }; 00056 extern const HashGenTable Hash_Gen_Table; 00057 00058 } // namespace hash 00059 using hash::HashKey; 00060 using hash::BoardKey; 00061 00062 namespace stl 00063 { 00064 template <typename T> struct hash; 00065 template <> 00066 struct hash<osl::hash::HashKey>{ 00067 unsigned long operator()(const HashKey& h) const{ 00068 return h.signature(); 00069 } 00070 }; 00071 template<> 00072 struct hash<osl::hash::BoardKey> 00073 { 00074 unsigned long operator()(const BoardKey& h) const 00075 { 00076 return h.signature(); 00077 } 00078 }; 00079 } // namespace stl 00080 #ifdef USE_TBB_HASH 00081 struct TBBHashCompare 00082 { 00083 size_t hash(HashKey const& key) const { 00084 return (size_t)(key.signature()); 00085 } 00086 bool equal(HashKey const& key1, HashKey const& key2) const { 00087 return key1==key2; 00088 } 00089 }; 00090 struct TBBSignatureCompare 00091 { 00092 size_t hash(hash::BoardKey const& key) const { 00093 return (size_t)key.signature(); 00094 } 00095 bool equal(hash::BoardKey const& key1, hash::BoardKey const& key2) const { 00096 return key1==key2; 00097 } 00098 }; 00099 #endif 00100 } // namespace osl 00101 00102 #endif /* _HASH_KEY_H */ 00103 // ;;; Local Variables: 00104 // ;;; mode:c++ 00105 // ;;; c-basic-offset:2 00106 // ;;; End: