My Project
hashKey.cc
Go to the documentation of this file.
1#include "osl/hashKey.h"
2#include "osl/random.h"
3#include <iomanip>
4#include <cstdlib>
5#include <iostream>
6#include <sstream>
7
8static_assert(sizeof(osl::HashKey) == sizeof(int)*4, "hash key size");
9
11{
12 board64 = misc::Random<unsigned long long>::newValue() & ~static_cast<uint64_t>(1);
14}
15
17{
18 for (uint64_t& value: HashMajorPawn)
20 for (uint64_t& value: HashPiece)
22}
23
24#ifndef MINIMAL
25std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
26{
27 os << h.pieceStand();
28 const BoardKey& board_key = h.boardKey();
29 for (size_t i=0; i<board_key.size(); ++i)
30 {
31 os << ':'
32 << std::setfill('0') << std::setbase(16) << std::setw(8)
33 << board_key[i];
34 }
35 return os << ':' << std::setbase(10);
36}
37
38void osl::hash::HashKey::dumpContents(std::ostream& os) const
39{
40 os << pieceStand().getFlags();
41 for (size_t i=0; i<size(); ++i) {
42 os << ' ' << operator[](i);
43 }
44}
45
47{
48 dumpContents(std::cerr);
49}
50#endif
51
53{
54 for(int num=0;num<40;num++){
55 Piece p=state.pieceOf(num);
56 if(state.usedMask().test(num))
58 }
59 setPlayer(state.turn());
60}
61
63newHashWithMove(Move move) const
64{
65 return newMakeMove(move);
66}
67
69newMakeMove(Move move) const
70{
71 HashKey ret(*this);
72 if (! move.isPass())
73 {
74 assert(move.isValid());
75 Square from=move.from();
76 Square to=move.to();
77 Ptype capturePtype=move.capturePtype();
78 PtypeO ptypeO=move.ptypeO();
79 PtypeO oldPtypeO=move.oldPtypeO();
80 if (capturePtype!=PTYPE_EMPTY)
81 {
82 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
83 PtypeO capturedPtypeO=captured(capturePtypeO);
84
85 HashGenTable::subHashKey(ret,to,capturePtypeO);
86 HashGenTable::addHashKey(ret,Square::STAND(),capturedPtypeO);
87 }
88 HashGenTable::subHashKey(ret,from,oldPtypeO);
89 HashGenTable::addHashKey(ret,to,ptypeO);
90 }
91 ret.changeTurn();
92 return ret;
93}
94
96newUnmakeMove(Move move) const
97{
98 HashKey ret(*this);
99 if (! move.isPass())
100 {
101 assert(move.isValid());
102 Square from=move.from();
103 Square to=move.to();
104 Ptype capturePtype=move.capturePtype();
105 PtypeO ptypeO=move.ptypeO();
106 PtypeO oldPtypeO=move.oldPtypeO();
107 if (capturePtype!=PTYPE_EMPTY)
108 {
109 PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
110 PtypeO capturedPtypeO=captured(capturePtypeO);
111
112 HashGenTable::addHashKey(ret,to,capturePtypeO);
113 HashGenTable::subHashKey(ret,Square::STAND(),capturedPtypeO);
114 }
115 HashGenTable::addHashKey(ret,from,oldPtypeO);
116 HashGenTable::subHashKey(ret,to,ptypeO);
117 }
118 ret.changeTurn();
119 return ret;
120}
121
122namespace osl
123{
126#include "bits/hash.txt"
127 };
128}
129
130// ;;; Local Variables:
131// ;;; mode:c++
132// ;;; c-basic-offset:2
133// ;;; End:
圧縮していない moveの表現 .
Definition: basic_type.h:1052
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
bool isValid() const
Definition: basic_type.cc:246
Player player() const
Definition: basic_type.h:1195
PtypeO oldPtypeO() const
移動前のPtypeO, i.e., 成る手だった場合成る前
Definition: basic_type.h:1168
bool isPass() const
Definition: basic_type.h:1092
Ptype capturePtype() const
Definition: basic_type.h:1180
const Square to() const
Definition: basic_type.h:1132
const Square from() const
Definition: basic_type.h:1125
bool test(int num) const
Definition: pieceMask.h:45
PtypeO ptypeO() const
Definition: basic_type.h:824
const Square square() const
Definition: basic_type.h:832
Player turn() const
Definition: simpleState.h:220
const PieceMask & usedMask() const
Definition: simpleState.h:131
const Piece pieceOf(int num) const
Definition: simpleState.h:76
static const Square STAND()
Definition: basic_type.h:548
static const CArray2d< HashKey128Layout, Square::SIZE, PTYPEO_SIZE > key
Definition: hashKey.h:170
static void subHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition: hashKey.h:176
static void addHashKey(HashKey &hk, Square sq, PtypeO ptypeo)
Definition: hashKey.h:172
void setRandom()
乱数で初期化.
Definition: hashKey.cc:10
const PieceStand pieceStand() const
Definition: hashKey.h:63
const BoardKey96 boardKey() const
Definition: hashKey.h:53
const HashKey newMakeMove(Move) const
Definition: hashKey.cc:69
const HashKey newHashWithMove(Move move) const
Definition: hashKey.cc:63
void dumpContentsCerr() const
Definition: hashKey.cc:46
void dumpContents(std::ostream &os) const
Definition: hashKey.cc:38
const HashKey newUnmakeMove(Move) const
Definition: hashKey.cc:96
std::ostream & operator<<(std::ostream &os, const HashKey &h)
Definition: hashKey.cc:25
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ PTYPE_EMPTY
Definition: basic_type.h:85
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199
constexpr Player alt(Player player)
Definition: basic_type.h:13
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
size_t size() const
Definition: hashKey.h:23