hashKey.h
Go to the documentation of this file.
00001 /* hashKey.h
00002  */
00003 #ifndef OSL_HASH_KEY_H
00004 #define OSL_HASH_KEY_H
00005 
00006 #include "osl/config.h"
00007 #ifdef OSL_LONG_HASH_KEY
00008 #  include "osl/hash/boardKey.h"
00009 #else
00010 #  include "osl/hash/boardKey128.h"
00011 #endif
00012 #include "osl/piece.h"
00013 #include "osl/move.h"
00014 #include "osl/pieceStand.h"
00015 #include "osl/state/simpleState.h"
00016 #include "osl/misc/carray.h"
00017 
00018 namespace osl
00019 {
00020   namespace hash
00021   {
00022 #ifdef OSL_LONG_HASH_KEY
00023 #  if OSL_WORDSIZE == 64
00024     typedef HashKey64 HashKeyBase;
00025     typedef BoardKey64 BoardKey;
00026 #  elif OSL_WORDSIZE == 32
00027     typedef HashKey32 HashKeyBase;
00028     typedef BoardKey32 BoardKey;
00029 #  endif
00030 #else
00031     typedef HashKey128 HashKeyBase;
00032     typedef BoardKey96 BoardKey;
00033 #endif
00034     class HashKey : public HashKeyBase
00035     {
00036     public:
00037       HashKey() :HashKeyBase(){}
00038       HashKey(const SimpleState&);
00039 #ifndef OSL_LONG_HASH_KEY
00040       HashKey(uint64_t h0, uint32_t h1, uint32_t s)
00041         : HashKeyBase(h0, h1, s)
00042       {
00043       }
00044 #endif
00045       const HashKey newHashWithMove(Move move) const;
00046       const HashKey newMakeMove(Move) const;
00047       const HashKey newUnmakeMove(Move) const;
00048 
00049       void dumpContents(std::ostream& os) const;
00050       void dumpContentsCerr() const;
00051       static const HashKey readFromDump(const std::string&);
00052       static const HashKey readFromDump(std::istream&);
00053     };
00054     std::ostream& operator<<(std::ostream& os,const HashKey& h);
00055 
00056     class HashGenTable
00057     {
00058 #ifdef OSL_LONG_HASH_KEY
00059       CArray2d<HashKey,Square::SIZE,PTYPEO_SIZE> key;
00060 #else
00061       static const CArray2d<HashKey128Layout,Square::SIZE,PTYPEO_SIZE> key;
00062 #endif
00063     public:
00064       HashGenTable();
00065 #ifdef OSL_LONG_HASH_KEY
00066       void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) const{
00067         assert(sq.isValid() && isValidPtypeO(ptypeo));
00068         hk+=key[sq.index()][ptypeo-PTYPEO_MIN];
00069       }
00070       void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) const{
00071         assert(sq.isValid() && isValidPtypeO(ptypeo));
00072         hk-=key[sq.index()][ptypeo-PTYPEO_MIN];
00073 #else
00074       static void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
00075         assert(sq.isValid() && isValidPtypeO(ptypeo));
00076         hk += HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
00077       }
00078       static void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
00079         assert(sq.isValid() && isValidPtypeO(ptypeo));
00080         hk -= HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
00081 #endif
00082       }
00083     };
00084     extern const HashGenTable Hash_Gen_Table;
00085 
00086   } // namespace hash
00087   using hash::HashKey;
00088   using hash::BoardKey;
00089 
00090   namespace stl
00091   {
00092     template <typename T> struct hash;
00093     template <>
00094     struct hash<osl::hash::HashKey>{
00095       unsigned long operator()(const HashKey& h) const{
00096         return h.signature();
00097       }
00098     };
00099     template<>
00100     struct hash<osl::hash::BoardKey>
00101     {
00102       unsigned long operator()(const BoardKey& h) const
00103       {
00104         return h.signature();
00105       }
00106     };
00107   } // namespace stl
00108 #ifdef USE_TBB_HASH
00109   struct TBBHashCompare
00110   {
00111     size_t hash(HashKey const& key) const {
00112       return (size_t)(key.signature());
00113     }
00114     bool equal(HashKey const& key1, HashKey const& key2) const {
00115       return key1==key2;
00116     }
00117   };
00118   struct TBBSignatureCompare
00119   {
00120     size_t hash(hash::BoardKey const& key) const {
00121       return (size_t)key.signature();
00122     }
00123     bool equal(hash::BoardKey const& key1, hash::BoardKey const& key2) const {
00124       return key1==key2;
00125     }
00126   };
00127 #endif
00128 } // namespace osl
00129 
00130 #endif /* OSL_HASH_KEY_H */
00131 // ;;; Local Variables:
00132 // ;;; mode:c++
00133 // ;;; c-basic-offset:2
00134 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines