hashKey.h
Go to the documentation of this file.
/* hashKey.h
*/
#ifndef OSL_HASH_KEY_H
#define OSL_HASH_KEY_H
#include "osl/basic_type.h"
#include "osl/bits/pieceStand.h"
#include "osl/simpleState.h"
#include <cstddef>
namespace osl
{
namespace hash
{
struct BoardKey96 : public std::pair<uint64_t,uint32_t>
{
BoardKey96() {}
BoardKey96(const std::pair<uint64_t,uint32_t>& src)
: std::pair<uint64_t,uint32_t>(src)
{
}
uint32_t signature() const { return second; }
size_t size() const { return 2; }
uint64_t operator[](size_t i) const { return i ? first : second; }
};
class HashGenTable;
struct HashKey128Layout
{
uint64_t board64;
uint32_t board32, piece_stand;
};
class HashKey128 : private HashKey128Layout
{
friend class HashGenTable;
public:
HashKey128()
{
board64 = board32 = piece_stand = 0;
}
HashKey128(uint64_t h0, uint32_t h1, uint32_t s)
{
board64 = h0;
board32 = h1;
piece_stand = s;
}
HashKey128(const HashKey128Layout& src) : HashKey128Layout(src)
{
}
const BoardKey96 boardKey() const {
return std::make_pair(board64, board32);
}
uint64_t boardKey64() const { return board64; }
uint64_t signature() const { return board32; }
uint64_t hash64() const { return board64 + pieceStand64(); }
uint64_t pieceStand64() const {
return Stand_Hash.toUint64(pieceStand());
}
const PieceStand pieceStand() const{ return PieceStand(piece_stand); }
const PieceStand blackStand() const { return PieceStand(piece_stand); }
void setPieceStand(const PieceStand& p) { piece_stand=p.getFlags(); }
bool isSameBoard(const HashKey128& key) const
{
return boardKey() == key.boardKey();
}
HashKey128& operator+=(const HashKey128& r)
{
board64 += r.board64;
board32 += r.board32;
PieceStand new_stand(piece_stand);
new_stand.addAtmostOnePiece(r.pieceStand());
piece_stand = new_stand.getFlags();
return *this;
}
HashKey128& operator-=(const HashKey128& r)
{
board64 -= r.board64;
board32 -= r.board32;
PieceStand new_stand(piece_stand);
new_stand.subAtmostOnePiece(r.pieceStand());
piece_stand = new_stand.getFlags();
return *this;
}
void add(Move move) { board64 += move.intValue(); }
void changeTurn() { board64 ^= static_cast<uint64_t>(1); }
void setPlayer(Player p)
{
board64 &= ~static_cast<uint64_t>(1);
board64 |= playerToIndex(p);
}
bool playerBit() const { return board64 & 1; }
bool isPlayerOfTurn(Player p) const
{
return playerBit() == playerToIndex(p);
}
Player turn() const { return isPlayerOfTurn(BLACK) ? BLACK : WHITE; }
void setRandom();
size_t size() const { return 2; }
uint64_t operator[](size_t i) const { return i ? board64 : board32; }
struct StandHash
{
CArray<uint64_t, 19*3*3> HashMajorPawn;
CArray<uint64_t, 5*5*5*5> HashPiece;
StandHash();
uint64_t toUint64(PieceStand stand) const
{
int major_pawn = stand.get(PAWN)*9
+ stand.get(ROOK)*3 + stand.get(BISHOP);
int pieces = stand.get(GOLD)*125 + stand.get(SILVER)*25
+ stand.get(KNIGHT)*5 + stand.get(LANCE);
return HashMajorPawn[major_pawn] + HashPiece[pieces];
}
};
static const StandHash Stand_Hash;
};
inline bool operator==(const HashKey128& l, const HashKey128& r)
{
return l.boardKey() == r.boardKey() && l.pieceStand() == r.pieceStand();
}
inline bool operator!=(const HashKey128& l, const HashKey128& r)
{
return !(l==r);
}
inline bool operator<(const HashKey128& l, const HashKey128& r)
{
if (l.pieceStand() < r.pieceStand())
return true;
else if (r.pieceStand() < l.pieceStand())
return false;
return l.boardKey() < r.boardKey();
}
typedef HashKey128 HashKeyBase;
typedef BoardKey96 BoardKey;
class HashKey : public HashKeyBase
{
public:
HashKey() :HashKeyBase(){}
HashKey(const SimpleState&);
const HashKey newHashWithMove(Move move) const;
const HashKey newMakeMove(Move) const;
const HashKey newUnmakeMove(Move) const;
void dumpContents(std::ostream& os) const;
void dumpContentsCerr() const;
static const HashKey readFromDump(const std::string&);
static const HashKey readFromDump(std::istream&);
};
std::ostream& operator<<(std::ostream& os,const HashKey& h);
class HashGenTable
{
static const CArray2d<HashKey128Layout,Square::SIZE,PTYPEO_SIZE> key;
public:
static void addHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
assert(sq.isValid() && isValidPtypeO(ptypeo));
hk += HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
}
static void subHashKey(HashKey& hk,Square sq,PtypeO ptypeo) {
assert(sq.isValid() && isValidPtypeO(ptypeo));
hk -= HashKey128(key[sq.index()][ptypeo-PTYPEO_MIN]);
}
};
} // namespace hash
using hash::HashKey;
using hash::HashGenTable;
using hash::BoardKey;
} // namespace osl
namespace std
{
template <typename T> struct hash;
template <>
struct hash<osl::HashKey>{
unsigned long operator()(const osl::HashKey& h) const {
return h.signature();
}
};
template<>
struct hash<osl::BoardKey>
{
unsigned long operator()(const osl::BoardKey& h) const {
return h.signature();
}
};
} // namespace stl
#endif /* OSL_HASH_KEY_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
pieceStand.h
osl::CArray
Definition container.h:20
osl::HashKey
Definition hashKey.h:153
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::intValue
int intValue() const
Definition basic_type.h:1065
osl::PieceStand
片方の手番の持駒の枚数を記録するクラス.
Definition bits/pieceStand.h:38
osl::PieceStand::subAtmostOnePiece
void subAtmostOnePiece(PieceStand const &ps)
Definition bits/pieceStand.h:118
osl::PieceStand::getFlags
unsigned int getFlags() const
Definition bits/pieceStand.h:165
osl::PieceStand::get
unsigned int get(Ptype type) const
Definition bits/pieceStand.h:131
osl::PieceStand::addAtmostOnePiece
void addAtmostOnePiece(PieceStand const &ps)
pieceStand同士の加算，減算.
Definition bits/pieceStand.h:108
osl::SimpleState
Definition simpleState.h:35
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::isValid
bool isValid() const
Definition basic_type.cc:184
osl::hash::HashGenTable
Definition hashKey.h:169
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
手番を含んだ盤面の状態のハッシュ値を保持するためのクラス.
Definition hashKey.h:37
osl::hash::HashKey128::boardKey64
uint64_t boardKey64() const
Definition hashKey.h:56
osl::hash::HashKey128::setPlayer
void setPlayer(Player p)
Definition hashKey.h:95
osl::hash::HashKey128::isSameBoard
bool isSameBoard(const HashKey128 &key) const
駒台の情報を除いて同じかどうか.
Definition hashKey.h:71
osl::hash::HashKey128::changeTurn
void changeTurn()
Definition hashKey.h:94
osl::hash::HashKey128::signature
uint64_t signature() const
Definition hashKey.h:57
osl::hash::HashKey128::setRandom
void setRandom()
乱数で初期化.
Definition hashKey.cc:10
osl::hash::HashKey128::operator-=
HashKey128 & operator-=(const HashKey128 &r)
Definition hashKey.h:84
osl::hash::HashKey128::add
void add(Move move)
Definition hashKey.h:93
osl::hash::HashKey128::blackStand
const PieceStand blackStand() const
Definition hashKey.h:64
osl::hash::HashKey128::hash64
uint64_t hash64() const
持駒も含んだ64bitのハッシュ
Definition hashKey.h:59
osl::hash::HashKey128::Stand_Hash
static const StandHash Stand_Hash
Definition hashKey.h:127
osl::hash::HashKey128::setPieceStand
void setPieceStand(const PieceStand &p)
Definition hashKey.h:65
osl::hash::HashKey128::HashKey128
HashKey128(const HashKey128Layout &src)
Definition hashKey.h:50
osl::hash::HashKey128::HashKey128
HashKey128(uint64_t h0, uint32_t h1, uint32_t s)
Definition hashKey.h:44
osl::hash::HashKey128::HashGenTable
friend class HashGenTable
Definition hashKey.h:38
osl::hash::HashKey128::isPlayerOfTurn
bool isPlayerOfTurn(Player p) const
Definition hashKey.h:101
osl::hash::HashKey128::playerBit
bool playerBit() const
Definition hashKey.h:100
osl::hash::HashKey128::pieceStand
const PieceStand pieceStand() const
Definition hashKey.h:63
osl::hash::HashKey128::pieceStand64
uint64_t pieceStand64() const
Definition hashKey.h:60
osl::hash::HashKey128::turn
Player turn() const
Definition hashKey.h:105
osl::hash::HashKey128::size
size_t size() const
Definition hashKey.h:111
osl::hash::HashKey128::HashKey128
HashKey128()
Definition hashKey.h:40
osl::hash::HashKey128::operator+=
HashKey128 & operator+=(const HashKey128 &r)
Definition hashKey.h:75
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::readFromDump
static const HashKey readFromDump(std::istream &)
osl::hash::HashKey::newMakeMove
const HashKey newMakeMove(Move) const
Definition hashKey.cc:69
osl::hash::HashKey::readFromDump
static const HashKey readFromDump(const std::string &)
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
osl::hash
Definition hashKey.h:14
osl::hash::operator==
bool operator==(const HashKey128 &l, const HashKey128 &r)
Definition hashKey.h:129
osl::hash::BoardKey
BoardKey96 BoardKey
Definition hashKey.h:151
osl::hash::operator<
bool operator<(const HashKey128 &l, const HashKey128 &r)
set等で使うためのみの不等号.
Definition hashKey.h:141
osl::hash::operator!=
bool operator!=(const HashKey128 &l, const HashKey128 &r)
Definition hashKey.h:133
osl::hash::operator<<
std::ostream & operator<<(std::ostream &os, const HashKey &h)
Definition hashKey.cc:25
osl::hash::HashKeyBase
HashKey128 HashKeyBase
Definition hashKey.h:150
osl
Definition additionalEffect.h:6
osl::ROOK
@ ROOK
Definition basic_type.h:100
osl::BISHOP
@ BISHOP
Definition basic_type.h:99
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::isValidPtypeO
bool isValidPtypeO(int ptypeO)
Definition basic_type.cc:30
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
osl::Player
Player
Definition basic_type.h:8
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::PTYPEO_MIN
@ PTYPEO_MIN
Definition basic_type.h:200
std
Definition basic_type.h:1353
simpleState.h
osl::hash::BoardKey96
Definition hashKey.h:16
osl::hash::BoardKey96::BoardKey96
BoardKey96()
Definition hashKey.h:17
osl::hash::BoardKey96::signature
uint32_t signature() const
Definition hashKey.h:22
osl::hash::BoardKey96::operator[]
uint64_t operator[](size_t i) const
Definition hashKey.h:24
osl::hash::BoardKey96::BoardKey96
BoardKey96(const std::pair< uint64_t, uint32_t > &src)
Definition hashKey.h:18
osl::hash::BoardKey96::size
size_t size() const
Definition hashKey.h:23
osl::hash::HashKey128Layout
Definition hashKey.h:28
osl::hash::HashKey128Layout::piece_stand
uint32_t piece_stand
Definition hashKey.h:30
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
Definition hashKey.h:114
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::hash::HashKey128::StandHash::toUint64
uint64_t toUint64(PieceStand stand) const
Definition hashKey.h:118
std::hash< osl::BoardKey >::operator()
unsigned long operator()(const osl::BoardKey &h) const
Definition hashKey.h:200
std::hash< osl::HashKey >::operator()
unsigned long operator()(const osl::HashKey &h) const
Definition hashKey.h:193
std::hash
Definition basic_type.h:1354