Debian Codegraph @

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

raw
1 /* dominanceTable.cc
2 */
3 #include "dominanceTable.h"
4 #include "checkHashRecord.h"
5 #include "sameBoardList.h"
6 #include "osl/stat/histogram.h"
7 #include "osl/hash/hashKey.h"
8 #include "osl/stl/hash_map.h"
9 #include <algorithm>
10 #include <iostream>
11
12 struct osl::checkmate::DominanceTable::Table : public hash_map<BoardKey,checkmate::SameBoardList>
13 {
14 #ifdef OSL_SMP
15 static const int initial_capacity = 100;
16 #else
17 static const int initial_capacity = 1000000;
18 #endif
19 typedef hash_map<BoardKey,checkmate::SameBoardList> hash_map_t;
20
21 explicit Table(size_t capacity) : hash_map_t(capacity)
22 {
23 }
24 };
25
26 osl::checkmate::
27 DominanceTable::DominanceTable(Player a)
28 : table(new Table(Table::initial_capacity)), attacker(a),
29 rootNode(new CheckHashRecord)
30 {
31 }
32 osl::checkmate::
33 DominanceTable::~DominanceTable()
34 {
35 #ifdef CHECKMATE_DEBUG
36 confirmNoVisitedRecords();
37 #endif
38 #ifdef DOMINANCETABLE_SHOW_STAT
39 if (! table->empty())
40 {
41 stat::Histogram h(1,100);
42 for (Table::iterator p=table->begin(); p!=table->end(); ++p)
43 {
44 h.add(p->second.size());
45 }
46 h.show(std::cerr);
47 }
48 #endif
49 }
50
51 osl::checkmate::CheckHashRecord * osl::checkmate::
52 DominanceTable::find(const HashKey& key)
53 {
54 Table::iterator p=table->find(key.boardKey());
55 if (p==table->end())
56 return 0;
57 return p->second.find(key.blackStand());
58 }
59
60 osl::checkmate::CheckHashRecord * osl::checkmate::
61 DominanceTable::allocate(const HashKey& key, const PieceStand& white_stand,
62 const PathEncoding& path)
63 {
64 size_t count = 0;
65 SameBoardList& l = (*table)[key.boardKey()];
66 if (attacker == BLACK)
67 return l.allocate<BLACK>(key.blackStand(), white_stand, path, count);
68 else
69 return l.allocate<WHITE>(key.blackStand(), white_stand, path, count);
70 }
71
72 void osl::checkmate::
73 DominanceTable::clear()
74 {
75 TwinTableHolder::clear();
76 table->clear();
77 }
78
79 const osl::checkmate::CheckHashRecord * osl::checkmate::
80 DominanceTable::find(const HashKey& key) const
81 {
82 Table::const_iterator p=table->find(key.boardKey());
83 if (p==table->end())
84 return 0;
85 return p->second.find(key.blackStand());
86 }
87 size_t osl::checkmate::
88 DominanceTable::size() const
89 {
90 return table->size();
91 }
92
93 void osl::checkmate::
94 DominanceTable::confirmNoVisitedRecords() const
95 {
96 int visited = 0;
97 for (Table::const_iterator p=table->begin(); p!=table->end(); ++p)
98 {
99 const int curVisited = p->second.confirmNoVisitedRecords();
100 #ifdef CHECKMATE_EXTRA_DEBUG
101 if (curVisited)
102 std::cerr << p->first.getSignature()
103 << " " << p->first.getBoardKey()[0]
104 << "\n";
105 #endif
106 visited += curVisited;
107 }
108 if (visited != countVisited())
109 {
110 // rev. 1705 以降,王手の連続の棋譜を渡した場合は
111 // 詰将棋の外からisVisitedを書き込んだ局面が残っている.
112 std::cerr << "DominanceTable::confirmNoVisitedRecords "
113 << visited << " visited nodes found\n";
114 }
115 }
116
117 // ;;; Local Variables:
118 // ;;; mode:c++
119 // ;;; c-basic-offset:2
120 // ;;; End: