00001
00002
00003 #ifndef CATEGORY_PROBOFEACHCATEGORY_H
00004 #define CATEGORY_PROBOFEACHCATEGORY_H
00005
00006 #include "osl/category/categoryEnv.h"
00007 #include "osl/category/categoryList.h"
00008 #include "osl/category/categoryTraits.h"
00009 #include "osl/container/moveLogProbVector.h"
00010 #include "osl/misc/loki.h"
00011 #include <iosfwd>
00012 namespace osl
00013 {
00014 namespace category
00015 {
00016 template <class T> class Classify;
00017 template <class T> class CategoryUtil;
00018 namespace analyzer
00019 {
00023 class ProbOfEachCategory
00024 {
00025 public:
00026 struct CallBack
00027 {
00028 virtual ~CallBack() {}
00029 virtual void found(const char *, int ){}
00030 virtual void notFound(const char *) {}
00031 };
00032 private:
00033 const CategoryEnv& env;
00034 Move target;
00035 CallBack *function;
00036 int classify_piece_value;
00037 public:
00038 ProbOfEachCategory(const CategoryEnv& e, Move t, CallBack *f);
00039 template <typename Head, typename Tail>
00040 void analyze(CategoryList<Head, Tail>);
00041 private:
00045 template <typename Category>
00046 bool analyzeOne(Category, Int2Type<false>);
00047 bool analyze(NullCategory);
00048 template <class Category>
00049 bool analyzeOne(Category, Int2Type<true>)
00050 {
00051 return analyzeClassifyRoot(Category());
00052 }
00053 template <typename SubCategories>
00054 bool analyzeClassifyRoot(Classify<SubCategories>)
00055 {
00056 if (MinProb<Classify<SubCategories> >::minProb() > env.limit)
00057 return false;
00058 analyzeClassify(SubCategories());
00059 return true;
00060 }
00061 template <typename Head, typename Tail>
00062 void analyzeClassify(CategoryList<Head,Tail>);
00063 void analyzeClassify(NullCategory);
00064 public:
00065 struct Reporter : public ProbOfEachCategory::CallBack
00066 {
00067 std::ostream& os;
00068 bool verbose;
00069 explicit Reporter(std::ostream&, bool verbose=false);
00070 void found(const char *name, int prob);
00071 void notFound(const char *name);
00072 };
00073 };
00074 }
00075 }
00076 }
00077
00078 template <typename Head, typename Tail>
00079 void osl::category::analyzer::
00080 ProbOfEachCategory::analyze(CategoryList<Head, Tail>)
00081 {
00082 const bool match = analyzeOne(Head(), Int2Type<Head::IsClassifier>());
00083 if (Head::IsKingEscape && match)
00084 return;
00085 analyze(Tail());
00086 }
00087
00088 template <typename Category>
00089 bool osl::category::analyzer::
00090 ProbOfEachCategory::analyzeOne(Category, Int2Type<false>)
00091 {
00092 if (MinProb<Category>::minProb() > env.limit)
00093 return false;
00094 MoveLogProbVector moves;
00095 CategoryUtil<Category>::generate(env,moves);
00096 for (size_t i=0; i<moves.size(); ++i)
00097 {
00098 if (moves[i].getMove() == target)
00099 {
00100 function->found(Category::getName(), moves[i].getLogProb());
00101 return true;
00102 }
00103 }
00104 function->notFound(Category::getName());
00105 return false;
00106 }
00107
00108 template <typename Head, typename Tail>
00109 void osl::category::analyzer::
00110 ProbOfEachCategory::analyzeClassify(CategoryList<Head,Tail>)
00111 {
00112 const std::pair<bool,int> r =
00113 CategoryUtil<Head>::getProbIf(env, target, classify_piece_value);
00114 if (r.first)
00115 function->found(Head::getName(), r.second);
00116 else
00117 function->notFound(Head::getName());
00118 return analyzeClassify(Tail());
00119 }
00120
00121 #endif
00122
00123
00124
00125