00001
00002
00003 #ifndef OSL_CHECKMATE_CHECKHISTORYTOTABLE_H
00004 #define OSL_CHECKMATE_CHECKHISTORYTOTABLE_H
00005
00006 #include "osl/checkmate/checkHashRecord.h"
00007 #include "osl/checkmate/checkTableUtil.h"
00008 #include "osl/repetitionCounter.h"
00009 #include "osl/container/moveStack.h"
00010
00011 namespace osl
00012 {
00013 namespace checkmate
00014 {
00015 struct CheckHistoryToTable
00016 {
00017 template <class Table>
00018 static void write(Table&, const RepetitionCounter&, const MoveStack&,
00019 const SimpleState& state, Player attack);
00020 template <class Table>
00021 static void undoWrite(Table&, const RepetitionCounter&, Player attack);
00022 };
00023 }
00024 }
00025
00026 template <class Table>
00027 void osl::checkmate::
00028 CheckHistoryToTable::write(Table& table,
00029 const RepetitionCounter& counter,
00030 const MoveStack& moves,
00031 const SimpleState& state, Player attack)
00032 {
00033 PieceStand white_stand(WHITE, state);
00034 CheckHashRecord *root = table.root();
00035 for (int i=0; i<counter.checkCount(attack); ++i)
00036 {
00037 const HashKey& key = counter.history().top(i);
00038 if (key != counter.history().top(0))
00039 {
00040 const PathEncoding path(key.turn());
00041
00042 CheckHashRecord *record = 0;
00043 CheckTableUtil::allocate(record, table, key, white_stand, path, root);
00044 if (! record->isVisited)
00045 {
00046 record->isVisited = true;
00047 table.incrementVisited();
00048 }
00049 if (record->findLoop(path, table.getTwinTable()) == 0)
00050 {
00051 record->setLoopDetection(path, record);
00052 }
00053 }
00054 assert(moves.hasLastMove(i+1));
00055 if (! moves.hasLastMove(i+1))
00056 break;
00057 const Move last_move = moves.lastMove(i+1);
00058 if (last_move.isNormal())
00059 white_stand = white_stand.previousStand(WHITE, last_move);
00060 }
00061 }
00062
00063 template <class Table>
00064 void osl::checkmate::
00065 CheckHistoryToTable::undoWrite(Table& table,
00066 const RepetitionCounter& counter,
00067 Player attack)
00068 {
00069 for (int i=0; i<counter.checkCount(attack); ++i)
00070 {
00071 const HashKey& key = counter.history().top(i);
00072 CheckHashRecord *record = table.find(key);
00073 assert(record);
00074 if (record)
00075 record->isVisited = false;
00076 }
00077 }
00078
00079 #endif
00080
00081
00082
00083