All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bookInMemory.cc
Go to the documentation of this file.
1 /* bookInMemory.cc
2  */
6 #include "osl/oslConfig.h"
7 #include <boost/foreach.hpp>
8 #include <boost/scoped_ptr.hpp>
9 
11 BookInMemory::BookInMemory(const std::string& filename)
12 {
13  readAll(filename);
14 }
15 
18 {
19 }
20 
22 BookInMemory::readRecursive(const HashKey& key, int index, WeightedBook& book, int depth, int /*parent_visit*/)
23 {
24  const int depth_threshold = 60, visit_threshold = 4, scale = 16;
25  const int visit = book.getBlackWinCount(index)+book.getWhiteWinCount(index);
26  if (depth > depth_threshold || table.find(key) != table.end()
27  || visit < visit_threshold)
28  return visit;
29  const vector<record::opening::WMove>& moves = book.getMoves(index);
30  FixedCapacityVector<std::pair<int, Move>, 40> children;
31  BOOST_FOREACH(WMove move, moves)
32  {
33  const HashKey child = key.newMakeMove(move.getMove());
34  const int cv = readRecursive(child, move.getStateIndex(), book, depth+1, visit);
35  if (cv < visit_threshold || cv*scale < visit || move.getWeight() == 0)
36  continue;
37  children.push_back(std::make_pair(cv, move.getMove()));
38  if (children.size() == children.capacity())
39  break;
40  }
41  std::sort(children.begin(), children.end());
42  std::reverse(children.begin(), children.end());
43  if (! children.empty()) {
44  moves_t& store = table[key];
45  store.fill(Move());
46  for (size_t i=0; i<children.size(); ++i) {
47  store[i] = children[i].second;
48  if (i+1 == store.size())
49  break;
50  }
51  }
52  return visit;
53 }
54 
56 BookInMemory::readAll(const std::string& filename)
57 {
58  WeightedBook book(OslConfig::openingBook(filename));
59  int index = book.getStartState();
60  const NumEffectState state;
61  readRecursive(HashKey(state), index, book, 0, 0);
62 }
63 
65 BookInMemory::find(const HashKey& key, MoveVector& out) const
66 {
67  table_t::const_iterator p = table.find(key);
68  if (p == table.end())
69  return;
70  BOOST_FOREACH(Move move, p->second)
71  if (move.isNormal())
72  out.push_back(move);
73 }
74 
77 BookInMemory::instance(const std::string& filename)
78 {
79  static std::map<std::string,boost::shared_ptr<BookInMemory> > table;
80  boost::shared_ptr<BookInMemory> &book = table[filename];
81  if (! book)
82  book.reset(new BookInMemory(filename));
83  return *book;
84 }
85 
86 // ;;; Local Variables:
87 // ;;; mode:c++
88 // ;;; c-basic-offset:2
89 // ;;; End: