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 
8 static_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
25 std::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 
38 void 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))
57  HashGenTable::addHashKey(*this, p.square(),p.ptypeO());
58  }
59  setPlayer(state.turn());
60 }
61 
63 newHashWithMove(Move move) const
64 {
65  return newMakeMove(move);
66 }
67 
69 newMakeMove(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 
96 newUnmakeMove(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 
122 namespace osl
123 {
126 #include "bits/hash.txt"
127  };
128 }
129 
130 // ;;; Local Variables:
131 // ;;; mode:c++
132 // ;;; c-basic-offset:2
133 // ;;; End:
osl::Square
Definition: basic_type.h:532
osl::hash::HashKey::newHashWithMove
const HashKey newHashWithMove(Move move) const
Definition: hashKey.cc:63
osl::hash::BoardKey96::size
size_t size() const
Definition: hashKey.h:23
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
osl::hash::HashGenTable::key
static const CArray2d< HashKey128Layout, Square::SIZE, PTYPEO_SIZE > key
Definition: hashKey.h:170
osl::Move::isValid
bool isValid() const
Definition: basic_type.cc:246
osl::hash::HashKey128::setRandom
void setRandom()
乱数で初期化.
Definition: hashKey.cc:10
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::Piece::ptypeO
PtypeO ptypeO() const
Definition: basic_type.h:824
osl::hash::HashKey128::changeTurn
void changeTurn()
Definition: hashKey.h:94
osl::SimpleState
Definition: simpleState.h:35
osl::newPtypeO
PtypeO newPtypeO(Player player, Ptype ptype)
Definition: basic_type.h:211
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::hash::HashKey::HashKey
HashKey()
Definition: hashKey.h:155
osl::hash::HashKey128::StandHash::StandHash
StandHash()
Definition: hashKey.cc:16
osl::hash::HashKey128Layout::board64
uint64_t board64
Definition: hashKey.h:29
osl::Piece
駒.
Definition: basic_type.h:788
random.h
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition: simpleState.h:76
osl::hash::HashKey
Definition: hashKey.h:153
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::HashKey::dumpContents
void dumpContents(std::ostream &os) const
Definition: hashKey.cc:38
osl::Move::oldPtypeO
PtypeO oldPtypeO() const
移動前のPtypeO, i.e., 成る手だった場合成る前
Definition: basic_type.h:1168
osl::hash::BoardKey96
Definition: hashKey.h:16
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::hash::HashKey::newUnmakeMove
const HashKey newUnmakeMove(Move) const
Definition: hashKey.cc:96
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
osl::PieceMask::test
bool test(int num) const
Definition: pieceMask.h:45
osl::hash::operator<<
std::ostream & operator<<(std::ostream &os, const HashKey &h)
Definition: hashKey.cc:25
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::hash::HashKey::newMakeMove
const HashKey newMakeMove(Move) const
Definition: hashKey.cc:69
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
hashKey.h
osl::Piece::square
const Square square() const
Definition: basic_type.h:832
osl::Move::isPass
bool isPass() const
Definition: basic_type.h:1092
osl::hash::HashKey::dumpContentsCerr
void dumpContentsCerr() const
Definition: hashKey.cc:46
osl::SimpleState::usedMask
const PieceMask & usedMask() const
Definition: simpleState.h:131
osl::misc::Random
Definition: random.h:10
osl::hash::HashKey128Layout::board32
uint32_t board32
Definition: hashKey.h:30
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition: basic_type.h:85
osl::SimpleState::turn
Player turn() const
Definition: simpleState.h:220
osl::hash::HashKey128::boardKey
const BoardKey96 boardKey() const
Definition: hashKey.h:53
osl::Square::STAND
static const Square STAND()
Definition: basic_type.h:548
osl::CArray
Definition: container.h:20
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::hash::HashKey128::pieceStand
const PieceStand pieceStand() const
Definition: hashKey.h:63
osl::Move::player
Player player() const
Definition: basic_type.h:1195
osl
Definition: additionalEffect.h:6