Debian Codegraph @

old-sample/legacy-checkmate/lib/oraclePoolLastMove.cc

raw
1 /* oraclePoolLastMove.cc
2 */
3 #include "oraclePoolLastMove.h"
4 #include "checkHashRecord.h"
5 #include "osl/state/simpleState.h"
6 #include "osl/stl/hash_map.h"
7 #ifdef OSL_SMP
8 # include "osl/misc/lightMutex.h"
9 #endif
10 #include <algorithm>
11
12 struct osl::checkmate::OraclePoolLastMove::Table
13 {
14 #if (__GNUC__ >= 4 && __GNUC_MINOR__ >=3)
15 typedef osl::hash_map<int,const osl::checkmate::CheckHashRecord*,
16 std::tr1::hash<int> > hash_map_t;
17 #else
18 typedef osl::hash_map<int,const osl::checkmate::CheckHashRecord*,
19 __gnu_cxx::hash<int> > hash_map_t;
20 #endif
21
22 #ifdef OSL_SMP
23 typedef osl::misc::LightMutex Mutex;
24 mutable Mutex mutex;
25 #endif
26
27 hash_map_t table;
28 const Player defender;
29
30 explicit Table(Player attacker) : defender(alt(attacker))
31 {
32 }
33 /**
34 * from と old ptype だけ見る
35 */
36 int makeKey(Square king_position, Move last_move) const
37 {
38 assert(! last_move.isDrop());
39 assert(last_move.player() == defender);
40 int result = king_position.index() * Square::indexMax();
41 result += last_move.to().index();
42 result *= Square::indexMax();
43 result += last_move.from().index();
44 result *= PTYPE_SIZE;
45 result += last_move.oldPtype();
46 return result;
47 }
48 const CheckHashRecord *& get(Square king_position, Move last_move)
49 {
50 const int key = makeKey(king_position, last_move);
51 #ifdef OSL_SMP
52 SCOPED_LOCK(lk,mutex);
53 #endif
54 return table[key];
55 }
56 const CheckHashRecord * find(Square king_position, Move last_move) const
57 {
58 const int key = makeKey(king_position, last_move);
59 #ifdef OSL_SMP
60 SCOPED_LOCK(lk,mutex);
61 #endif
62 hash_map_t::const_iterator pos = table.find(key);
63 if (pos != table.end())
64 return pos->second;
65 return 0;
66 }
67 size_t size() const
68 {
69 return table.size();
70 }
71 };
72
73 osl::checkmate::
74 OraclePoolLastMove::OraclePoolLastMove(Player attacker)
75 : oracles(new Table(attacker))
76 {
77 }
78
79 osl::checkmate::
80 OraclePoolLastMove::~OraclePoolLastMove()
81 {
82 }
83
84 void osl::checkmate::
85 OraclePoolLastMove::addOracle(const SimpleState& state, Move last_move,
86 const CheckHashRecord* oracle)
87 {
88 assert(state.turn() == alt(oracles->defender));
89 if (last_move.isPass())
90 return;
91 const Square king_position = state.kingSquare(oracles->defender);
92 assert(oracle->hasProofPieces());
93 oracles->get(king_position,last_move) = oracle;
94 }
95
96 const osl::checkmate::CheckHashRecord *osl::checkmate::
97 OraclePoolLastMove::findOracle(const SimpleState& state,
98 Move last_move, PieceStand black_stand,
99 unsigned short& oracle_age) const
100 {
101 assert(state.turn() == alt(oracles->defender));
102 if (oracle_age)
103 return 0;
104 if (last_move.isPass())
105 return 0;
106
107 const Square king_position = state.kingSquare(oracles->defender);
108 const CheckHashRecord *record = oracles->find(king_position, last_move);
109 if (record)
110 {
111 ++oracle_age;
112 const PieceStand attack_stand
113 = (oracles->defender != BLACK) ? black_stand : PieceStand(WHITE, state);
114 if (attack_stand.hasMoreThan<BLACK>(record->proofPieces()))
115 return record;
116 }
117 return 0;
118 }
119
120 size_t osl::checkmate::
121 OraclePoolLastMove::size() const
122 {
123 return oracles->size();
124 }
125
126 /* ------------------------------------------------------------------------- */
127 // ;;; Local Variables:
128 // ;;; mode:c++
129 // ;;; c-basic-offset:2
130 // ;;; coding:utf-8
131 // ;;; End: