All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hashKey.cc
Go to the documentation of this file.
1 #include "osl/hash/hashKey.h"
2 #include <iomanip>
3 #include <cstdlib>
4 #include <iostream>
5 #include <sstream>
6 
7 namespace osl
8 {
9 #ifdef OSL_LONG_HASH_KEY
10  BOOST_STATIC_ASSERT(sizeof(HashKey) >= sizeof(int)*5);
11 #else
12  BOOST_STATIC_ASSERT(sizeof(HashKey) == sizeof(int)*4);
13 #endif
14 } // namespace osl
15 
16 #ifndef MINIMAL
17 std::ostream& osl::hash::operator<<(std::ostream& os,const osl::hash::HashKey& h)
18 {
19  os << h.pieceStand();
20  const BoardKey& board_key = h.boardKey();
21  for (size_t i=0; i<board_key.size(); ++i)
22  {
23  os << ':'
24  << std::setfill('0') << std::setbase(16) << std::setw(8)
25  << board_key[i];
26  }
27  return os << ':' << std::setbase(10);
28 }
29 
30 void osl::hash::HashKey::dumpContents(std::ostream& os) const
31 {
32  os << pieceStand().getFlags();
33  for (size_t i=0; i<size(); ++i) {
34  os << ' ' << operator[](i);
35  }
36 }
37 
39 {
40  dumpContents(std::cerr);
41 }
42 
43 # ifdef OSL_LONG_HASH_KEY
44 const osl::hash::HashKey osl::hash::HashKey::readFromDump(const std::string& str)
45 {
46  std::istringstream is(str);
47  return readFromDump(is);
48 }
49 
51 {
52  HashKey key;
53  int stand;
54  is >> stand;
55  key.piece_stand = PieceStand(stand);
56  for (size_t i=0; i<key.size(); ++i) {
57  is >> key[i];
58  }
59  return key;
60 }
61 # endif
62 #endif
63 
64 osl::hash::HashKey::HashKey(const SimpleState& state)
65 {
66  for(int num=0;num<40;num++){
67  Piece p=state.pieceOf(num);
68  if(state.usedMask().test(num))
69  Hash_Gen_Table.addHashKey(*this, p.square(),p.ptypeO());
70  }
71  setPlayer(state.turn());
72 }
73 
75 newHashWithMove(Move move) const
76 {
77  return newMakeMove(move);
78 }
79 
81 newMakeMove(Move move) const
82 {
83  HashKey ret(*this);
84  if (! move.isPass())
85  {
86  assert(move.isValid());
87  Square from=move.from();
88  Square to=move.to();
89  Ptype capturePtype=move.capturePtype();
90  PtypeO ptypeO=move.ptypeO();
91  PtypeO oldPtypeO=move.oldPtypeO();
92  if (capturePtype!=PTYPE_EMPTY)
93  {
94  PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
95  PtypeO capturedPtypeO=captured(capturePtypeO);
96 
97  Hash_Gen_Table.subHashKey(ret,to,capturePtypeO);
98  Hash_Gen_Table.addHashKey(ret,Square::STAND(),capturedPtypeO);
99  }
100  Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
101  Hash_Gen_Table.addHashKey(ret,to,ptypeO);
102  }
103  ret.changeTurn();
104  return ret;
105 }
106 
108 newUnmakeMove(Move move) const
109 {
110  HashKey ret(*this);
111  if (! move.isPass())
112  {
113  assert(move.isValid());
114  Square from=move.from();
115  Square to=move.to();
116  Ptype capturePtype=move.capturePtype();
117  PtypeO ptypeO=move.ptypeO();
118  PtypeO oldPtypeO=move.oldPtypeO();
119  if (capturePtype!=PTYPE_EMPTY)
120  {
121  PtypeO capturePtypeO=newPtypeO(alt(move.player()),capturePtype);
122  PtypeO capturedPtypeO=captured(capturePtypeO);
123 
124  Hash_Gen_Table.addHashKey(ret,to,capturePtypeO);
125  Hash_Gen_Table.subHashKey(ret,Square::STAND(),capturedPtypeO);
126  }
127  Hash_Gen_Table.addHashKey(ret,from,oldPtypeO);
128  Hash_Gen_Table.subHashKey(ret,to,ptypeO);
129  }
130  ret.changeTurn();
131  return ret;
132 }
133 
134 #ifndef OSL_LONG_HASH_KEY
135 namespace osl
136 {
137  const CArray2d<hash::HashKey128Layout,Square::SIZE,PTYPEO_SIZE>
139 #include "hash.txt"
140  }};
141 }
142 #endif
143 
145 {
146 #ifdef OSL_LONG_HASH_KEY
147  for(int j=0;j<PTYPEO_SIZE;j++)
148  {
149  const PtypeO pjo = (PtypeO)(j+PTYPEO_MIN);
150  for(int i=0;i<Square::SIZE;i++)
151  {
152  if (Square::nth(i) == Square::STAND())
153  {
154  const Ptype pj = getPtype(pjo);
155  if (isBasic(pj) && (getOwner(pjo) == BLACK))
156  {
157  PieceStand stand;
158  stand.add(pj);
159  key[i][j].setPieceStand(stand);
160  }
161  }
162  else
163  {
164  key[i][j].setRandom();
165  }
166  }
167  }
168 # ifdef OSL_DUMP_HASH_KEY
169  std::ofstream os("hash.txt");
170  for(int i=0;i<Square::SIZE;i++) {
171  os << "// " << i << "\n{\n";
172  for(int j=0;j<PTYPEO_SIZE;j++) {
173  os << "HashKey("
174  << key[i][j].board64 << "ull, " << key[i][j].board32 << "u, "
175  << key[i][j].piece_stand.getFlags() << "u),\n";
176  }
177  os << "},\n";
178  }
179 # endif
180 #endif
181 }
182 
183 
184 // ;;; Local Variables:
185 // ;;; mode:c++
186 // ;;; c-basic-offset:2
187 // ;;; End: