hashKey.cc
Go to the documentation of this file.
00001 #include "osl/hash/hashKey.h"
00002 #include <iomanip>
00003 #include <cstdlib>
00004 #include <iostream>
00005 #include <sstream>
00006 
00007 namespace osl
00008 {
00009 #ifdef OSL_LONG_HASH_KEY
00010   BOOST_STATIC_ASSERT(sizeof(HashKey) >= sizeof(int)*5);
00011 #else
00012   BOOST_STATIC_ASSERT(sizeof(HashKey) == sizeof(int)*4);
00013 #endif
00014 } // namespace osl
00015 
00016 #ifndef MINIMAL
00017 std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
00018 {
00019   os << h.pieceStand();
00020   const BoardKey& board_key = h.boardKey();
00021   for (size_t i=0; i<board_key.size(); ++i)
00022   {
00023     os << ':' 
00024        << std::setfill('0') << std::setbase(16) << std::setw(8)
00025        << board_key[i];
00026   }
00027   return os << ':' << std::setbase(10);
00028 }
00029 
00030 void osl::hash::HashKey::dumpContents(std::ostream& os) const
00031 {
00032   os << pieceStand().getFlags();
00033   for (size_t i=0; i<size(); ++i) {
00034     os << ' ' << operator[](i);
00035   }
00036 }
00037 
00038 void osl::hash::HashKey::dumpContentsCerr() const
00039 {
00040   dumpContents(std::cerr);
00041 }
00042 
00043 #  ifdef OSL_LONG_HASH_KEY
00044 const osl::hash::HashKey osl::hash::HashKey::readFromDump(const std::string& str)
00045 {
00046   std::istringstream is(str);
00047   return readFromDump(is);
00048 }
00049 
00050 const osl::hash::HashKey osl::hash::HashKey::readFromDump(std::istream& is)
00051 {
00052   HashKey key;
00053   int stand;
00054   is >> stand;
00055   key.piece_stand = PieceStand(stand);
00056   for (size_t i=0; i<key.size(); ++i) {
00057     is >> key[i];
00058   }
00059   return key;
00060 }
00061 #  endif
00062 #endif
00063 
00064 osl::hash::HashKey::HashKey(const SimpleState& state)
00065 {
00066   for(int num=0;num<40;num++){
00067     Piece p=state.pieceOf(num);
00068     if(state.usedMask().test(num))
00069       Hash_Gen_Table.addHashKey(*this, p.square(),p.ptypeO());
00070   }
00071   setPlayer(state.turn());
00072 }
00073 
00074 const osl::hash::HashKey osl::hash::HashKey::
00075 newHashWithMove(Move move) const
00076 {
00077   return newMakeMove(move);
00078 }
00079 
00080 const osl::hash::HashKey osl::hash::HashKey::
00081 newMakeMove(Move move) const
00082 {
00083   HashKey ret(*this);
00084   if (! move.isPass())
00085   {
00086     assert(move.isValid());
00087     Square from=move.from();
00088     Square to=move.to();
00089     Ptype capturePtype=move.capturePtype();
00090     PtypeO ptypeO=move.ptypeO();
00091     PtypeO oldPtypeO=move.oldPtypeO();
00092     if (capturePtype!=PTYPE_EMPTY)
00093     {
00094       PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00095       PtypeO capturedPtypeO=captured(capturePtypeO);
00096 
00097       Hash_Gen_Table.subHashKey(ret,to,capturePtypeO);
00098       Hash_Gen_Table.addHashKey(ret,Square::STAND(),capturedPtypeO);
00099     }
00100     Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
00101     Hash_Gen_Table.addHashKey(ret,to,ptypeO);
00102   }
00103   ret.changeTurn();
00104   return ret;
00105 }
00106 
00107 const osl::hash::HashKey osl::hash::HashKey::
00108 newUnmakeMove(Move move) const
00109 {
00110   HashKey ret(*this);
00111   if (! move.isPass())
00112   {
00113     assert(move.isValid());
00114     Square from=move.from();
00115     Square to=move.to();
00116     Ptype capturePtype=move.capturePtype();
00117     PtypeO ptypeO=move.ptypeO();
00118     PtypeO oldPtypeO=move.oldPtypeO();
00119     if (capturePtype!=PTYPE_EMPTY)
00120     {
00121       PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00122       PtypeO capturedPtypeO=captured(capturePtypeO);
00123 
00124       Hash_Gen_Table.addHashKey(ret,to,capturePtypeO);
00125       Hash_Gen_Table.subHashKey(ret,Square::STAND(),capturedPtypeO);
00126     }
00127     Hash_Gen_Table.addHashKey(ret,from,oldPtypeO);
00128     Hash_Gen_Table.subHashKey(ret,to,ptypeO);
00129   }
00130   ret.changeTurn();
00131   return ret;
00132 }
00133 
00134 #ifndef OSL_LONG_HASH_KEY
00135 namespace osl
00136 {
00137   const CArray2d<hash::HashKey128Layout,Square::SIZE,PTYPEO_SIZE>
00138   hash::HashGenTable::key = {{
00139 #include "hash.txt"
00140     }};
00141 }
00142 #endif
00143   
00144 osl::hash::HashGenTable::HashGenTable()
00145 {
00146 #ifdef OSL_LONG_HASH_KEY
00147   for(int j=0;j<PTYPEO_SIZE;j++)
00148   {
00149     const PtypeO pjo = (PtypeO)(j+PTYPEO_MIN);
00150     for(int i=0;i<Square::SIZE;i++)
00151     {
00152       if (Square::nth(i) == Square::STAND())
00153       {
00154         const Ptype pj = getPtype(pjo);
00155         if (isBasic(pj) && (getOwner(pjo) == BLACK))
00156         {
00157           PieceStand stand;
00158           stand.add(pj);
00159           key[i][j].setPieceStand(stand);
00160         }
00161       }
00162       else
00163       {
00164         key[i][j].setRandom();
00165       }
00166     }
00167   }
00168 #  ifdef OSL_DUMP_HASH_KEY
00169   std::ofstream os("hash.txt");
00170   for(int i=0;i<Square::SIZE;i++) {
00171     os << "// " << i << "\n{\n";
00172     for(int j=0;j<PTYPEO_SIZE;j++) {
00173       os << "HashKey("
00174          << key[i][j].board64 << "ull, " << key[i][j].board32 << "u, "
00175          << key[i][j].piece_stand.getFlags() << "u),\n";
00176     }
00177     os << "},\n";
00178   }
00179 #  endif
00180 #endif
00181 }
00182 
00183 
00184 // ;;; Local Variables:
00185 // ;;; mode:c++
00186 // ;;; c-basic-offset:2
00187 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines