説明を見る。00001 #include "osl/hashKey.h"
00002 #include "osl/random.h"
00003 #include <iomanip>
00004 #include <cstdlib>
00005 #include <iostream>
00006 #include <sstream>
00007
00008 static_assert(sizeof(osl::HashKey) == sizeof(int)*4, "hash key size");
00009
00010 void osl::hash::HashKey128::setRandom()
00011 {
00012 board64 = misc::Random<unsigned long long>::newValue() & ~static_cast<uint64_t>(1);
00013 board32 = misc::Random<unsigned int>::newValue();
00014 }
00015
00016 osl::hash::HashKey128::StandHash::StandHash()
00017 {
00018 for (uint64_t& value: HashMajorPawn)
00019 value = misc::Random<unsigned long long>::newValue() & ~1ull;
00020 for (uint64_t& value: HashPiece)
00021 value = misc::Random<unsigned long long>::newValue() & ~1ull;
00022 }
00023
00024 #ifndef MINIMAL
00025 std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
00026 {
00027 os << h.pieceStand();
00028 const BoardKey& board_key = h.boardKey();
00029 for (size_t i=0; i<board_key.size(); ++i)
00030 {
00031 os << ':'
00032 << std::setfill('0') << std::setbase(16) << std::setw(8)
00033 << board_key[i];
00034 }
00035 return os << ':' << std::setbase(10);
00036 }
00037
00038 void osl::hash::HashKey::dumpContents(std::ostream& os) const
00039 {
00040 os << pieceStand().getFlags();
00041 for (size_t i=0; i<size(); ++i) {
00042 os << ' ' << operator[](i);
00043 }
00044 }
00045
00046 void osl::hash::HashKey::dumpContentsCerr() const
00047 {
00048 dumpContents(std::cerr);
00049 }
00050 #endif
00051
00052 osl::hash::HashKey::HashKey(const SimpleState& state)
00053 {
00054 for(int num=0;num<40;num++){
00055 Piece p=state.pieceOf(num);
00056 if(state.usedMask().test(num))
00057 HashGenTable::addHashKey(*this, p.square(),p.ptypeO());
00058 }
00059 setPlayer(state.turn());
00060 }
00061
00062 const osl::hash::HashKey osl::hash::HashKey::
00063 newHashWithMove(Move move) const
00064 {
00065 return newMakeMove(move);
00066 }
00067
00068 const osl::hash::HashKey osl::hash::HashKey::
00069 newMakeMove(Move move) const
00070 {
00071 HashKey ret(*this);
00072 if (! move.isPass())
00073 {
00074 assert(move.isValid());
00075 Square from=move.from();
00076 Square to=move.to();
00077 Ptype capturePtype=move.capturePtype();
00078 PtypeO ptypeO=move.ptypeO();
00079 PtypeO oldPtypeO=move.oldPtypeO();
00080 if (capturePtype!=PTYPE_EMPTY)
00081 {
00082 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00083 PtypeO capturedPtypeO=captured(capturePtypeO);
00084
00085 HashGenTable::subHashKey(ret,to,capturePtypeO);
00086 HashGenTable::addHashKey(ret,Square::STAND(),capturedPtypeO);
00087 }
00088 HashGenTable::subHashKey(ret,from,oldPtypeO);
00089 HashGenTable::addHashKey(ret,to,ptypeO);
00090 }
00091 ret.changeTurn();
00092 return ret;
00093 }
00094
00095 const osl::hash::HashKey osl::hash::HashKey::
00096 newUnmakeMove(Move move) const
00097 {
00098 HashKey ret(*this);
00099 if (! move.isPass())
00100 {
00101 assert(move.isValid());
00102 Square from=move.from();
00103 Square to=move.to();
00104 Ptype capturePtype=move.capturePtype();
00105 PtypeO ptypeO=move.ptypeO();
00106 PtypeO oldPtypeO=move.oldPtypeO();
00107 if (capturePtype!=PTYPE_EMPTY)
00108 {
00109 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
00110 PtypeO capturedPtypeO=captured(capturePtypeO);
00111
00112 HashGenTable::addHashKey(ret,to,capturePtypeO);
00113 HashGenTable::subHashKey(ret,Square::STAND(),capturedPtypeO);
00114 }
00115 HashGenTable::addHashKey(ret,from,oldPtypeO);
00116 HashGenTable::subHashKey(ret,to,ptypeO);
00117 }
00118 ret.changeTurn();
00119 return ret;
00120 }
00121
00122 namespace osl
00123 {
00124 const CArray2d<hash::HashKey128Layout,Square::SIZE,PTYPEO_SIZE>
00125 hash::HashGenTable::key = {
00126 #include "bits/hash.txt"
00127 };
00128 }
00129
00130
00131
00132
00133