00001
00002
00003 #include "osl/game_playing/tableStack.h"
00004 #include "osl/search/simpleHashTable.h"
00005 #include "osl/hash/hashKey.h"
00006 #include "osl/record/csa.h"
00007 #include "osl/misc/nonBlockDelete.h"
00008 #include <boost/scoped_ptr.hpp>
00009 #include <list>
00010 #include <iostream>
00011
00012 struct osl::game_playing::TableStack::TableWithRoot
00013 {
00014 boost::scoped_ptr<SimpleHashTable> table;
00015 HashKey root;
00016 };
00017
00018 struct osl::game_playing::TableStack::Stack
00019 : public std::list<TableWithRoot*>
00020 {
00021 };
00022
00023 osl::game_playing::
00024 TableStack::TableStack()
00025 : stack(new Stack())
00026 {
00027 }
00028
00029 osl::game_playing::
00030 TableStack::TableStack(const TableStack& copy)
00031 : stack(new Stack())
00032 {
00033 for (Stack::const_iterator p=copy.stack->begin();
00034 p!=copy.stack->end(); ++p)
00035 {
00036 stack->push_back(new TableWithRoot());
00037 TableWithRoot& store = *(stack->back());
00038 store.table.reset(new SimpleHashTable(100000,0,false));
00039 store.root = (*p)->root;
00040 try
00041 {
00042 (*p)->table->migrate(store.root, migration_depth,
00043 migration_limit, *store.table);
00044 }
00045 catch(TableFull&)
00046 {
00047 }
00048 }
00049 }
00050
00051 osl::game_playing::
00052 TableStack::~TableStack()
00053 {
00054 clear();
00055 }
00056
00057 void osl::game_playing::
00058 TableStack::clear()
00059 {
00060 while (! stack->empty())
00061 {
00062 TableWithRoot *top = stack->front();
00063 stack->pop_front();
00064 NonBlockDelete::planDelete(top);
00065 }
00066 }
00067
00068 void osl::game_playing::
00069 TableStack::pushMove(Move selected, const HashKey& root,
00070 const SimpleHashTable& table)
00071 {
00072 stack->push_back(new TableWithRoot());
00073 TableWithRoot& store = *(stack->back());
00074 store.table.reset(new SimpleHashTable(100000,0,table.verboseLevel()));
00075 store.root = root.newHashWithMove(selected);
00076 try
00077 {
00078 table.migrate(store.root, migration_depth,
00079 migration_limit, *store.table);
00080 }
00081 catch(TableFull&)
00082 {
00083 }
00084 if (table.verboseLevel() > 1 && store.table->size())
00085 std::cerr << "preserved " << store.table->size() << " entries for "
00086 << record::csa::show(selected)
00087 << " stack " << stack->size() << "\n";
00088
00089 if (stack->size() > max_size)
00090 {
00091 TableWithRoot *top = stack->front();
00092 stack->pop_front();
00093 NonBlockDelete::planDelete(top);
00094 }
00095 assert(stack->size() <= max_size);
00096 assert(! stack->empty());
00097 }
00098
00099 void osl::game_playing::
00100 TableStack::pushMove(Move selected)
00101 {
00102 if (stack->empty())
00103 return;
00104 pushMove(selected, stack->back()->root, *stack->back()->table);
00105 }
00106
00107 void osl::game_playing::
00108 TableStack::popMove()
00109 {
00110 if (! stack->empty())
00111 stack->pop_back();
00112 }
00113
00114 void osl::game_playing::
00115 TableStack::migrate(SimpleHashTable& table) const
00116 {
00117 if (stack->empty())
00118 return;
00119 assert(stack->back()->table.get());
00120 try
00121 {
00122 stack->back()->table->migrate(stack->back()->root, migration_depth,
00123 migration_limit, table);
00124 }
00125 catch(TableFull&)
00126 {
00127 }
00128 #ifndef NDEBUG
00129 if (table.verboseLevel() > 1 && table.size())
00130 {
00131 char ctime_buf[64];
00132 time_t t = time(0);
00133 std::cerr << "consistency " << ctime_r(&t, ctime_buf) << std::flush;
00134 }
00135 assert(table.isConsistent());
00136 #endif
00137 if (table.verboseLevel() > 1 && table.size())
00138 std::cerr << "migrated " << table.size() << " entries\n";
00139 }
00140
00141
00142
00143
00144
00145