00001
00002
00003 #include "osl/category/analyzer/generateCategoryMoves.h"
00004 #include "osl/category/analyzer/probOfEachCategory.h"
00005 #include "osl/category/categoryListUtil.h"
00006 #include "osl/category/standardMoveGenerator.h"
00007 #include "osl/category/categoryUtil.h"
00008
00009 namespace osl
00010 {
00011 namespace category
00012 {
00013 namespace analyzer
00014 {
00015 template void GenerateCategoryMoves::
00016 generateAndSet<StandardMoveGenerator>(const CategoryEnv&, CategoryMoveVector&);
00017 template void GenerateCategoryMoves::
00018 generateAndSetDetailed<StandardMoveGenerator>(const CategoryEnv&, CategoryMoveVector&);
00019 }
00020 }
00021 }
00022
00023 struct osl::category::analyzer::GenerateCategoryMoves::Helper
00024 {
00025 CategoryMoveVector *out;
00026 template <bool is_king_escape>
00027 bool operator()(const char *category_name,
00028 const MoveLogProbVector& moves)
00029 {
00030 if (! moves.empty())
00031 {
00032 out->push_front(CategoryMoves(moves, category_name));
00033
00034 return ! is_king_escape;
00035 }
00036 return true;
00037 }
00038 };
00039
00040 struct osl::category::analyzer::GenerateCategoryMoves::DetailFinder
00041 : public osl::category::analyzer::ProbOfEachCategory::CallBack
00042 {
00043 const int expected;
00044 std::string match;
00045 explicit DetailFinder(int e) : expected(e)
00046 {
00047 }
00048 ~DetailFinder();
00049
00050 void found(const char *name, int prob)
00051 {
00052 if (prob == expected)
00053 match = name;
00054 }
00055 void notFound(const char *)
00056 {
00057 }
00058 };
00059
00060 osl::category::analyzer::GenerateCategoryMoves::
00061 DetailFinder::~DetailFinder()
00062 {
00063 }
00064
00065 template <class Categories>
00066 void osl::category::analyzer::
00067 GenerateCategoryMoves::generateAndSetDetailed(const CategoryEnv& env, CategoryMoveVector& out)
00068 {
00069
00070 CategoryMoveVector rough;
00071 generateAndSet<Categories>(env, rough);
00072 for (CategoryMoveVector::const_iterator p=rough.begin(); p!=rough.end(); ++p)
00073 {
00074 const std::string category = p->category;
00075 if (category == "Pass")
00076 {
00077 out.push_front(CategoryMoves(p->moves, category));
00078 continue;
00079 }
00080 for (MoveLogProbVector::const_iterator q=p->moves.begin();
00081 q!=p->moves.end(); ++q)
00082 {
00083 DetailFinder callback(q->getLogProb());
00084 ProbOfEachCategory analyzer(env, q->getMove(), &callback);
00085 analyzer.analyze(StandardMoveGenerator());
00086
00087 const std::string detailed = (category == callback.match)
00088 ? category : category + callback.match;
00089 CategoryMoveVector::iterator target = out.end();
00090 for (CategoryMoveVector::iterator r=out.begin(); r!=out.end(); ++r)
00091 {
00092 if (r->category == detailed)
00093 {
00094 target = r;
00095 break;
00096 }
00097 }
00098 if (target == out.end())
00099 {
00100 out.push_front(CategoryMoves(MoveLogProbVector(), detailed));
00101 target = out.begin();
00102 }
00103 target->moves.push_back(*q);
00104 }
00105 }
00106 out.reverse();
00107 }
00108
00109 template <class Categories>
00110 void osl::category::analyzer::
00111 GenerateCategoryMoves::generateAndSet(const CategoryEnv& env, CategoryMoveVector& vector)
00112 {
00113 vector.clear();
00114 Helper out = { &vector };
00115 CategoryListUtil::forEachCategory(Categories(), env, out);
00116 vector.reverse();
00117 }
00118
00119
00120
00121
00122
00123