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