| 1 |
/* checkHistoryToTable.h |
| 2 |
*/ |
| 3 |
#ifndef OSL_CHECKMATE_CHECKHISTORYTOTABLE_H |
| 4 |
#define OSL_CHECKMATE_CHECKHISTORYTOTABLE_H |
| 5 |
|
| 6 |
#include "checkHashRecord.h" |
| 7 |
#include "checkTableUtil.h" |
| 8 |
#include "osl/repetitionCounter.h" |
| 9 |
#include "osl/container/moveStack.h" |
| 10 |
|
| 11 |
namespace osl |
| 12 |
{ |
| 13 |
namespace checkmate |
| 14 |
{ |
| 15 |
struct CheckHistoryToTable |
| 16 |
{ |
| 17 |
template <class Table> |
| 18 |
static void write(Table&, const RepetitionCounter&, const MoveStack&, |
| 19 |
const SimpleState& state, Player attack); |
| 20 |
template <class Table> |
| 21 |
static void undoWrite(Table&, const RepetitionCounter&, Player attack); |
| 22 |
}; |
| 23 |
} // namespace checkmate |
| 24 |
} // namespace osl |
| 25 |
|
| 26 |
template <class Table> |
| 27 |
void osl::checkmate:: |
| 28 |
CheckHistoryToTable::write(Table& table, |
| 29 |
const RepetitionCounter& counter, |
| 30 |
const MoveStack& moves, |
| 31 |
const SimpleState& state, Player attack) |
| 32 |
{ |
| 33 |
PieceStand white_stand(WHITE, state); |
| 34 |
CheckHashRecord *root = table.root(); |
| 35 |
for (int i=0; i<counter.checkCount(attack); ++i) |
| 36 |
{ |
| 37 |
const HashKey& key = counter.history().top(i); |
| 38 |
if (key != counter.history().top(0)) // ignore current state |
| 39 |
{ |
| 40 |
const PathEncoding path(key.turn()); |
| 41 |
|
| 42 |
CheckHashRecord *record = 0; |
| 43 |
CheckTableUtil::allocate(record, table, key, white_stand, path, root); |
| 44 |
if (! record->isVisited) |
| 45 |
{ |
| 46 |
record->isVisited = true; |
| 47 |
table.incrementVisited(); |
| 48 |
} |
| 49 |
if (record->findLoop(path, table.getTwinTable()) == 0) |
| 50 |
{ |
| 51 |
record->setLoopDetection(path, record); |
| 52 |
} |
| 53 |
} |
| 54 |
assert(moves.hasLastMove(i+1)); // oops, different index |
| 55 |
if (! moves.hasLastMove(i+1)) |
| 56 |
break; |
| 57 |
const Move last_move = moves.lastMove(i+1); |
| 58 |
if (last_move.isNormal()) |
| 59 |
white_stand = white_stand.previousStand(WHITE, last_move); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
template <class Table> |
| 64 |
void osl::checkmate:: |
| 65 |
CheckHistoryToTable::undoWrite(Table& table, |
| 66 |
const RepetitionCounter& counter, |
| 67 |
Player attack) |
| 68 |
{ |
| 69 |
for (int i=0; i<counter.checkCount(attack); ++i) |
| 70 |
{ |
| 71 |
const HashKey& key = counter.history().top(i); |
| 72 |
CheckHashRecord *record = table.find(key); |
| 73 |
assert(record); |
| 74 |
if (record) |
| 75 |
record->isVisited = false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#endif /* OSL_CHECKMATE_CHECKHISTORYTOTABLE_H */ |
| 80 |
// ;;; Local Variables: |
| 81 |
// ;;; mode:c++ |
| 82 |
// ;;; c-basic-offset:2 |
| 83 |
// ;;; End: |