00001
00002
00003 #ifndef OSL_HISTORYTABLE_H
00004 #define OSL_HISTORYTABLE_H
00005
00006 #include "osl/move.h"
00007 #include "osl/misc/carray.h"
00008 #include "osl/misc/carray2d.h"
00009 #include "osl/stl/vector.h"
00010 #ifdef OSL_SMP
00011 # include "osl/misc/lightMutex.h"
00012 #endif
00013 #include <iosfwd>
00014 namespace osl
00015 {
00016 namespace search
00017 {
00018 class HistoryTable
00019 {
00020 public:
00021 struct Entry
00022 {
00023 uint64_t value;
00024 #ifdef OSL_SMP
00025 mutable LightMutex mutex;
00026 #endif
00027 Entry() : value(0)
00028 {
00029 }
00030 };
00031 private:
00032 CArray<CArray2d<Entry,Position::SIZE, Position::SIZE>,2> table;
00033 public:
00034 uint64_t value(Move move) const
00035 {
00036 if (! move.isNormal())
00037 return 0;
00038 const int from_index = move.isDrop() ? (int)move.ptype() : (int)move.from().uintValue();
00039 const Entry& e = table[move.player()][from_index][move.to().uintValue()];
00040 return e.value;
00041 }
00042 void add(Move move, int inc)
00043 {
00044 if (! move.isNormal())
00045 return;
00046 const int from_index = move.isDrop() ? (int)move.ptype() : (int)move.from().uintValue();
00047 Entry& e = table[move.player()][from_index][move.to().uintValue()];
00048 #ifdef OSL_SMP
00049 SCOPED_LOCK(lk, e.mutex);
00050 #endif
00051 e.value += inc;
00052 }
00053 void clear(Move move)
00054 {
00055 if (! move.isNormal())
00056 return;
00057 const int from_index = move.isDrop() ? (int)move.ptype() : (int)move.from().uintValue();
00058 Entry& e = table[move.player()][from_index][move.to().uintValue()];
00059 e.value = 0;
00060 }
00061 struct OutputEntry
00062 {
00063 int from_or_ptype;
00064 Position to;
00065 uint64_t value;
00066 explicit OutputEntry(int i=0, int j=0, uint64_t v=0)
00067 : from_or_ptype(i), to(Position::makeDirect(j)), value(v)
00068 {
00069 }
00070 bool operator>(const OutputEntry& r) const
00071 {
00072 if (value != r.value)
00073 return value > r.value;
00074 if (from_or_ptype != r.from_or_ptype)
00075 return from_or_ptype > r.from_or_ptype;
00076 return to > r.to;
00077 }
00078 };
00079 void extractTopN(Player p, vector<OutputEntry>& out, size_t limit) const;
00080 };
00081 std::ostream& operator<<(std::ostream&, const HistoryTable::OutputEntry&);
00082 }
00083 };
00084
00085 #endif
00086
00087
00088
00089