File: hashKey.cc

package info (click to toggle)
libosl 0.6.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 148,792 kB
  • ctags: 151,985
  • sloc: cpp: 131,133; ansic: 7,228; ruby: 1,290; makefile: 569; perl: 309; sh: 35
file content (187 lines) | stat: -rw-r--r-- 4,302 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "osl/hash/hashKey.h"
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <sstream>

namespace osl
{
#ifdef OSL_LONG_HASH_KEY
  BOOST_STATIC_ASSERT(sizeof(HashKey) >= sizeof(int)*5);
#else
  BOOST_STATIC_ASSERT(sizeof(HashKey) == sizeof(int)*4);
#endif
} // namespace osl

#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);
}

#  ifdef OSL_LONG_HASH_KEY
const osl::hash::HashKey osl::hash::HashKey::readFromDump(const std::string& str)
{
  std::istringstream is(str);
  return readFromDump(is);
}

const osl::hash::HashKey osl::hash::HashKey::readFromDump(std::istream& is)
{
  HashKey key;
  int stand;
  is >> stand;
  key.piece_stand = PieceStand(stand);
  for (size_t i=0; i<key.size(); ++i) {
    is >> key[i];
  }
  return key;
}
#  endif
#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))
      Hash_Gen_Table.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);

      Hash_Gen_Table.subHashKey(ret,to,capturePtypeO);
      Hash_Gen_Table.addHashKey(ret,Square::STAND(),capturedPtypeO);
    }
    Hash_Gen_Table.subHashKey(ret,from,oldPtypeO);
    Hash_Gen_Table.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);

      Hash_Gen_Table.addHashKey(ret,to,capturePtypeO);
      Hash_Gen_Table.subHashKey(ret,Square::STAND(),capturedPtypeO);
    }
    Hash_Gen_Table.addHashKey(ret,from,oldPtypeO);
    Hash_Gen_Table.subHashKey(ret,to,ptypeO);
  }
  ret.changeTurn();
  return ret;
}

#ifndef OSL_LONG_HASH_KEY
namespace osl
{
  const CArray2d<hash::HashKey128Layout,Square::SIZE,PTYPEO_SIZE>
  hash::HashGenTable::key = {{
#include "hash.txt"
    }};
}
#endif
  
osl::hash::HashGenTable::HashGenTable()
{
#ifdef OSL_LONG_HASH_KEY
  for(int j=0;j<PTYPEO_SIZE;j++)
  {
    const PtypeO pjo = (PtypeO)(j+PTYPEO_MIN);
    for(int i=0;i<Square::SIZE;i++)
    {
      if (Square::nth(i) == Square::STAND())
      {
	const Ptype pj = getPtype(pjo);
	if (isBasic(pj) && (getOwner(pjo) == BLACK))
	{
	  PieceStand stand;
	  stand.add(pj);
	  key[i][j].setPieceStand(stand);
	}
      }
      else
      {
	key[i][j].setRandom();
      }
    }
  }
#  ifdef OSL_DUMP_HASH_KEY
  std::ofstream os("hash.txt");
  for(int i=0;i<Square::SIZE;i++) {
    os << "// " << i << "\n{\n";
    for(int j=0;j<PTYPEO_SIZE;j++) {
      os << "HashKey("
	 << key[i][j].board64 << "ull, " << key[i][j].board32 << "u, "
	 << key[i][j].piece_stand.getFlags() << "u),\n";
    }
    os << "},\n";
  }
#  endif
#endif
}


// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: