hashKey.cc
Go to the documentation of this file.
#include "osl/hashKey.h"
#include "osl/random.h"
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <sstream>
static_assert(sizeof(osl::HashKey) == sizeof(int)*4, "hash key size");
void osl::hash::HashKey128::setRandom()
{
board64 = misc::Random<unsigned long long>::newValue() & ~static_cast<uint64_t>(1);
board32 = misc::Random<unsigned int>::newValue();
}
osl::hash::HashKey128::StandHash::StandHash()
{
for (uint64_t& value: HashMajorPawn)
value = misc::Random<unsigned long long>::newValue() & ~1ull;
for (uint64_t& value: HashPiece)
value = misc::Random<unsigned long long>::newValue() & ~1ull;
}
#ifndef MINIMAL
std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
{
os << h.pieceStand();
const BoardKey& board_key = h.boardKey();
for (size_t i=0; i<board_key.size(); ++i)
{
os << ':'
<< std::setfill('0') << std::setbase(16) << std::setw(8)
<< board_key[i];
}
return os << ':' << std::setbase(10);
}
void osl::hash::HashKey::dumpContents(std::ostream& os) const
{
os << pieceStand().getFlags();
for (size_t i=0; i<size(); ++i) {
os << ' ' << operator[](i);
}
}
void osl::hash::HashKey::dumpContentsCerr() const
{
dumpContents(std::cerr);
}
#endif
osl::hash::HashKey::HashKey(const SimpleState& state)
{
for(int num=0;num<40;num++){
Piece p=state.pieceOf(num);
if(state.usedMask().test(num))
HashGenTable::addHashKey(*this, p.square(),p.ptypeO());
}
setPlayer(state.turn());
}
const osl::hash::HashKey osl::hash::HashKey::
newHashWithMove(Move move) const
{
return newMakeMove(move);
}
const osl::hash::HashKey osl::hash::HashKey::
newMakeMove(Move move) const
{
HashKey ret(*this);
if (! move.isPass())
{
assert(move.isValid());
Square from=move.from();
Square to=move.to();
Ptype capturePtype=move.capturePtype();
PtypeO ptypeO=move.ptypeO();
PtypeO oldPtypeO=move.oldPtypeO();
if (capturePtype!=PTYPE_EMPTY)
{
PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
PtypeO capturedPtypeO=captured(capturePtypeO);
HashGenTable::subHashKey(ret,to,capturePtypeO);
HashGenTable::addHashKey(ret,Square::STAND(),capturedPtypeO);
}
HashGenTable::subHashKey(ret,from,oldPtypeO);
HashGenTable::addHashKey(ret,to,ptypeO);
}
ret.changeTurn();
return ret;
}
const osl::hash::HashKey osl::hash::HashKey::
newUnmakeMove(Move move) const
{
HashKey ret(*this);
if (! move.isPass())
{
assert(move.isValid());
Square from=move.from();
Square to=move.to();
Ptype capturePtype=move.capturePtype();
PtypeO ptypeO=move.ptypeO();
PtypeO oldPtypeO=move.oldPtypeO();
if (capturePtype!=PTYPE_EMPTY)
{
PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
PtypeO capturedPtypeO=captured(capturePtypeO);
HashGenTable::addHashKey(ret,to,capturePtypeO);
HashGenTable::subHashKey(ret,Square::STAND(),capturedPtypeO);
}
HashGenTable::addHashKey(ret,from,oldPtypeO);
HashGenTable::subHashKey(ret,to,ptypeO);
}
ret.changeTurn();
return ret;
}
namespace osl
{
const CArray2d<hash::HashKey128Layout,Square::SIZE,PTYPEO_SIZE>
hash::HashGenTable::key = {
#include "bits/hash.txt"
};
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
osl::Move::isValid
bool isValid() const
Definition basic_type.cc:246
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::oldPtypeO
PtypeO oldPtypeO() const
移動前のPtypeO, i.e., 成る手だった場合成る前
Definition basic_type.h:1168
osl::Move::isPass
bool isPass() const
Definition basic_type.h:1092
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::PieceMask::test
bool test(int num) const
Definition pieceMask.h:45
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptypeO
PtypeO ptypeO() const
Definition basic_type.h:824
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::SimpleState
Definition simpleState.h:35
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::usedMask
const PieceMask & usedMask() const
Definition simpleState.h:131
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::Square
Definition basic_type.h:532
osl::Square::STAND
static const Square STAND()
Definition basic_type.h:548
osl::hash::HashGenTable::key
static const CArray2d< HashKey128Layout, Square::SIZE, PTYPEO_SIZE > key
Definition hashKey.h:170
osl::hash::HashGenTable::subHashKey
static void subHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition hashKey.h:176
osl::hash::HashGenTable::addHashKey
static void addHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition hashKey.h:172
osl::hash::HashKey128::setPlayer
void setPlayer(Player p)
Definition hashKey.h:95
osl::hash::HashKey128::changeTurn
void changeTurn()
Definition hashKey.h:94
osl::hash::HashKey128::setRandom
void setRandom()
乱数で初期化.
Definition hashKey.cc:10
osl::hash::HashKey128::pieceStand
const PieceStand pieceStand() const
Definition hashKey.h:63
osl::hash::HashKey128::size
size_t size() const
Definition hashKey.h:111
osl::hash::HashKey128::operator[]
uint64_t operator[](size_t i) const
Definition hashKey.h:112
osl::hash::HashKey128::boardKey
const BoardKey96 boardKey() const
Definition hashKey.h:53
osl::hash::HashKey
Definition hashKey.h:153
osl::hash::HashKey::newMakeMove
const HashKey newMakeMove(Move) const
Definition hashKey.cc:69
osl::hash::HashKey::newHashWithMove
const HashKey newHashWithMove(Move move) const
Definition hashKey.cc:63
osl::hash::HashKey::dumpContentsCerr
void dumpContentsCerr() const
Definition hashKey.cc:46
osl::hash::HashKey::dumpContents
void dumpContents(std::ostream &os) const
Definition hashKey.cc:38
osl::hash::HashKey::newUnmakeMove
const HashKey newUnmakeMove(Move) const
Definition hashKey.cc:96
osl::hash::HashKey::HashKey
HashKey()
Definition hashKey.h:155
hashKey.h
osl::hash::BoardKey
BoardKey96 BoardKey
Definition hashKey.h:151
osl::hash::operator<<
std::ostream & operator<<(std::ostream &os, const HashKey &h)
Definition hashKey.cc:25
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition basic_type.h:211
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に，ownerを反転する．
Definition basic_type.h:264
random.h
osl::hash::BoardKey96::size
size_t size() const
Definition hashKey.h:23
osl::hash::HashKey128Layout::board64
uint64_t board64
Definition hashKey.h:29
osl::hash::HashKey128Layout::board32
uint32_t board32
Definition hashKey.h:30
osl::hash::HashKey128::StandHash::HashMajorPawn
CArray< uint64_t, 19 *3 *3 > HashMajorPawn
Definition hashKey.h:115
osl::hash::HashKey128::StandHash::HashPiece
CArray< uint64_t, 5 *5 *5 *5 > HashPiece
Definition hashKey.h:116
osl::hash::HashKey128::StandHash::StandHash
StandHash()
Definition hashKey.cc:16
osl::misc::Random
Definition random.h:10