Debian Codegraph @

old-sample/legacy-checkmate/lib/analyzer/checkTableAnalyzer.cc

raw
1 /* checkTableAnalyzer.cc
2 */
3 #include "analyzer/checkTableAnalyzer.h"
4 #include "analyzer/treeWriter.h"
5 #include "analyzer/proofTreeTraverser.h"
6 #include "analyzer/disproofTreeTraverser.h"
7 #include "analyzer/recordSet.h"
8 #include "analyzer/showAllTree.h"
9 #include "checkHashRecord.h"
10 #include "checkMoveList.h"
11 #include "osl/checkmate/proofDisproof.h"
12 #include "corruptCheckTable.h"
13 #include <boost/scoped_ptr.hpp>
14 #include <iostream>
15 #include <fstream>
16
17 namespace osl
18 {
19 namespace checkmate
20 {
21 using namespace analyzer;
22 static void traverse(const CheckHashRecord *record, RecordSet& out)
23 {
24 if (! record)
25 return;
26 if (! out.insert(record).second)
27 return;
28
29 for (CheckMoveList::const_iterator p=record->moves.begin();
30 p!=record->moves.end(); ++p)
31 {
32 traverse(p->record, out);
33 }
34 }
35
36
37 static void examineProofTree(const CheckHashRecord *record,
38 const TwinTable& table,
39 const HashKey& key,
40 const PathEncoding& path,
41 bool orNode, TreeWriter& writer,
42 size_t& tree_size, size_t& leaf_size)
43 {
44 tree_size = 1;
45 leaf_size = 1;
46 assert(record);
47 assert(record->proofDisproof().isCheckmateSuccess());
48 ProofTreeTraverser traverser(writer, table);
49 if (orNode) // 手番側が詰めた
50 traverser.traverseOrNode(Move::INVALID(), record, key, path);
51 else // 手番側が詰まされた
52 traverser.traverseAndNode(Move::INVALID(), record, key, path);
53 tree_size = traverser.getVisited().size();
54 leaf_size = traverser.getLeaves();
55 }
56
57 static void examineDisproofTree(const CheckHashRecord *record,
58 const TwinTable& table,
59 const HashKey& key,
60 const PathEncoding& path,
61 bool orNode, bool isPartialStack,
62 TreeWriter& writer,
63 size_t& tree_size, size_t& leaf_size)
64 {
65 tree_size = 1;
66 leaf_size = 1;
67 assert(record);
68 if (! record->proofDisproof().isCheckmateFail())
69 {
70 if (! record->findLoop(path, table))
71 {
72 assert(isPartialStack);
73 assert(! record->twins.empty());
74 return;
75 }
76 }
77 DisproofTreeTraverser traverser(writer, table, isPartialStack);
78 if (orNode) // 手番側が詰を逃れた
79 traverser.traverseOrNode(Move::INVALID(), record, key, path);
80 else // 手番側が逃げられた
81 traverser.traverseAndNode(Move::INVALID(), record, key, path);
82 tree_size = traverser.getVisited().size();
83 leaf_size = traverser.getLeaves();
84 }
85
86 static void examineTreeGuess(const CheckHashRecord *record,
87 const TwinTable& table,
88 const HashKey& key,
89 const PathEncoding& path,
90 bool orNode, bool isPartialStack,
91 TreeWriter& writer,
92 size_t& tree_size, size_t& leaf_size)
93 {
94 if (! record)
95 {
96 tree_size = 0;
97 leaf_size = 0;
98 return;
99 }
100 if ((! record->proofDisproof().isFinal())
101 && (! record->findLoop(path, table)))
102 {
103 tree_size = 1;
104 leaf_size = 1;
105 return;
106 }
107 if (record->proofDisproof().isCheckmateFail()
108 || record->findLoop(path, table))
109 {
110 examineDisproofTree(record, table, key, path, orNode, isPartialStack,
111 writer, tree_size, leaf_size);
112 }
113 else
114 {
115 examineProofTree(record, table, key, path, orNode,
116 writer, tree_size, leaf_size);
117 }
118 }
119 } // namespace checkmate
120 } // namespace osl
121
122 osl::checkmate::analyzer::
123 CheckTableAnalyzer::CheckTableAnalyzer(const TwinTable& t, bool outline)
124 : table(t), useOutlineFormat(outline)
125 {
126 }
127
128 osl::checkmate::analyzer::
129 CheckTableAnalyzer::~CheckTableAnalyzer()
130 {
131 }
132
133 void osl::checkmate::analyzer::CheckTableAnalyzer::
134 showTree(const CheckHashRecord *record, std::ostream& os,
135 int maxDepth, bool expandFinalState, bool showTerminalMoves,
136 size_t threshold) const
137 {
138 if (! record)
139 return;
140 ShowAllTree traverser(os, maxDepth, expandFinalState, showTerminalMoves);
141 if (useOutlineFormat)
142 traverser.showOutline(record);
143 else
144 traverser.showDot(record, threshold);
145 }
146
147 size_t osl::checkmate::analyzer::
148 CheckTableAnalyzer::treeSize(const CheckHashRecord *record) const
149 {
150 RecordSet nodes;
151 traverse(record, nodes);
152 return nodes.size();
153 }
154
155 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
156 proofTreeSize(const CheckHashRecord *record, const HashKey& key,
157 const PathEncoding& path, bool orNode) const
158 {
159 size_t leaf_size;
160 return proofTreeSize(record, key, path, orNode, leaf_size);
161 }
162 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
163 proofTreeSize(const CheckHashRecord *record,
164 const HashKey& key, const PathEncoding& path,
165 bool orNode, size_t& leaf_size) const
166 {
167 try
168 {
169 TreeWriter writer;
170 size_t tree_size;
171 examineProofTree(record, table, key, path, orNode,
172 writer, tree_size, leaf_size);
173 return tree_size;
174 }
175 catch (CorruptCheckTable& t)
176 {
177 std::cerr << t.what() << ", corrupt tree was " << t.record << "\n";
178 t.record->dump();
179 throw;
180 }
181 }
182
183 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
184 disproofTreeSize(const CheckHashRecord *record,
185 const HashKey& key, const PathEncoding& path,
186 bool orNode, bool isPartialStack) const
187 {
188 size_t leaf_size;
189 return disproofTreeSize(record, key, path, orNode, leaf_size,
190 isPartialStack);
191 }
192
193 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
194 disproofTreeSize(const CheckHashRecord *record,
195 const HashKey& key, const PathEncoding& path,
196 bool orNode, size_t& leaf_size, bool isPartialStack) const
197 {
198 try
199 {
200 TreeWriter writer;
201 size_t tree_size;
202 examineDisproofTree(record, table, key, path, orNode, isPartialStack,
203 writer, tree_size, leaf_size);
204 return tree_size;
205 }
206 catch (CorruptCheckTable& t)
207 {
208 std::cerr << t.what() << ", corrupt tree was " << t.record << "\n";
209 t.record->dump();
210 throw;
211 }
212 }
213
214 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
215 proofOrDisproofTreeSize(const CheckHashRecord *record,
216 const HashKey& key,
217 const PathEncoding& path,
218 bool orNode, bool isPartialStack) const
219 {
220 size_t leaf_size;
221 return proofOrDisproofTreeSize(record, key, path, orNode,
222 leaf_size, isPartialStack);
223
224 }
225
226 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
227 proofOrDisproofTreeSize(const CheckHashRecord *record,
228 const HashKey& key, const PathEncoding& path,
229 bool orNode, size_t& leaf_size,
230 bool isPartialStack) const
231 {
232 try
233 {
234 TreeWriter writer;
235 size_t tree_size;
236 examineTreeGuess(record, table, key, path, orNode, isPartialStack, writer,
237 tree_size, leaf_size);
238 return tree_size;
239 }
240 catch (CorruptCheckTable& t)
241 {
242 std::cerr << t.what() << ", corrupt tree was " << t.record << "\n";
243 t.record->dump();
244 const char *filename = "checkmate-oops.log";
245 std::ofstream os(filename);
246 std::cerr << "entire tree is being written in " << filename << "\n";
247 CheckTableAnalyzer a(table);
248 a.showTree(record, os, 100, true, true);
249 throw;
250 }
251 }
252
253 size_t osl::checkmate::analyzer::CheckTableAnalyzer::
254 showProofTree(const CheckHashRecord *record,
255 const HashKey& key, const PathEncoding& path,
256 bool orNode, std::ostream& os, bool isPartialStack) const
257 {
258 std::unique_ptr<TreeWriter> writer;
259 if (useOutlineFormat)
260 writer.reset(new TreeStreamWriter(&os, true));
261 else
262 writer.reset(new DotWriter(os));
263 size_t tree_size, leaf_size;
264 examineTreeGuess(record, table, key, path, orNode, isPartialStack, *writer,
265 tree_size, leaf_size);
266 return tree_size;
267 }
268
269 /* ------------------------------------------------------------------------- */
270 // ;;; Local Variables:
271 // ;;; mode:c++
272 // ;;; c-basic-offset:2
273 // ;;; End: