From: Marco Costalba Date: Fri, 17 Jul 2009 12:32:27 +0000 (+0200) Subject: Remove even more redundancy in endgame functions handling X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=0d69ac33ff1b0258c9b18dca48f7b8b40fab5713;hp=342c8c883c2a3c58d7670600e27baf39f5309438 Remove even more redundancy in endgame functions handling Push on the templatization even more to chip out some code and take the opportunity to show some neat template trick ;-) Ok. I would say we can stop here now....it is quickly becoming a style exercise but we are not boost developers so give it a stop. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/endgame.h b/src/endgame.h index 271490b1..4426ef18 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -68,6 +68,7 @@ public: EndgameFunctionBase(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {} virtual ~EndgameFunctionBase() {} virtual T apply(const Position&) = 0; + Color color() const { return strongerSide; } protected: Color strongerSide, weakerSide; @@ -81,12 +82,14 @@ typedef EndgameFunctionBase EndgameScalingFunctionBase; template struct EvaluationFunction : public EndgameEvaluationFunctionBase { + typedef EndgameEvaluationFunctionBase Base; explicit EvaluationFunction(Color c): EndgameEvaluationFunctionBase(c) {} Value apply(const Position&); }; template struct ScalingFunction : public EndgameScalingFunctionBase { + typedef EndgameScalingFunctionBase Base; explicit ScalingFunction(Color c) : EndgameScalingFunctionBase(c) {} ScaleFactor apply(const Position&); }; diff --git a/src/material.cpp b/src/material.cpp index c9dcaba1..57cf05ca 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -69,23 +69,22 @@ class EndgameFunctions { public: EndgameFunctions(); ~EndgameFunctions(); - EF* getEEF(Key key) const; - SF* getESF(Key key, Color* c) const; + template T* get(Key key) const; private: - Key buildKey(const string& keyCode); - const string swapColors(const string& keyCode); - template void add_ef(const string& keyCode); - template void add_sf(const string& keyCode); + template void add(const string& keyCode); - struct ScalingInfo - { - Color col; - SF* fun; - }; + static Key buildKey(const string& keyCode); + static const string swapColors(const string& keyCode); std::map EEFmap; - std::map ESFmap; + std::map ESFmap; + + // Maps accessing functions for const and non-const references + template const std::map& map() const { return EEFmap; } + template<> const std::map& map() const { return ESFmap; } + template std::map& map() { return EEFmap; } + template<> std::map& map() { return ESFmap; } }; @@ -152,7 +151,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) { // Let's look if we have a specialized evaluation function for this // particular material configuration. First we look for a fixed // configuration one, then a generic one if previous search failed. - if ((mi->evaluationFunction = funcs->getEEF(key)) != NULL) + if ((mi->evaluationFunction = funcs->get(key)) != NULL) return mi; else if ( pos.non_pawn_material(BLACK) == Value(0) @@ -193,12 +192,11 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) { // if we decide to add more special cases. We face problems when there // are several conflicting applicable scaling functions and we need to // decide which one to use. - Color c; EndgameScalingFunctionBase* sf; - if ((sf = funcs->getESF(key, &c)) != NULL) + if ((sf = funcs->get(key)) != NULL) { - mi->scalingFunction[c] = sf; + mi->scalingFunction[sf->color()] = sf; return mi; } @@ -259,6 +257,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) { // Evaluate the material balance + Color c; int sign; Value egValue = Value(0); Value mgValue = Value(0); @@ -328,21 +327,21 @@ EndgameFunctions::EndgameFunctions() { KNNKMaterialKey = buildKey("KNNK"); KKNNMaterialKey = buildKey("KKNN"); - add_ef("KPK"); - add_ef("KBNK"); - add_ef("KRKP"); - add_ef("KRKB"); - add_ef("KRKN"); - add_ef("KQKR"); - add_ef("KBBKN"); - - add_sf("KNPK"); - add_sf("KRPKR"); - add_sf("KBPKB"); - add_sf("KBPPKB"); - add_sf("KBPKN"); - add_sf("KRPPKRP"); - add_sf("KRPPKRP"); + add >("KPK"); + add >("KBNK"); + add >("KRKP"); + add >("KRKB"); + add >("KRKN"); + add >("KQKR"); + add >("KBBKN"); + + add >("KNPK"); + add >("KRPKR"); + add >("KBPKB"); + add >("KBPPKB"); + add >("KBPKN"); + add >("KRPPKRP"); + add >("KRPPKRP"); } EndgameFunctions::~EndgameFunctions() { @@ -350,8 +349,8 @@ EndgameFunctions::~EndgameFunctions() { for (std::map::iterator it = EEFmap.begin(); it != EEFmap.end(); ++it) delete (*it).second; - for (std::map::iterator it = ESFmap.begin(); it != ESFmap.end(); ++it) - delete (*it).second.fun; + for (std::map::iterator it = ESFmap.begin(); it != ESFmap.end(); ++it) + delete (*it).second; } Key EndgameFunctions::buildKey(const string& keyCode) { @@ -382,35 +381,18 @@ const string EndgameFunctions::swapColors(const string& keyCode) { return keyCode.substr(idx) + keyCode.substr(0, idx); } -template -void EndgameFunctions::add_ef(const string& keyCode) { - - EEFmap.insert(std::pair(buildKey(keyCode), new EvaluationFunction(WHITE))); - EEFmap.insert(std::pair(buildKey(swapColors(keyCode)), new EvaluationFunction(BLACK))); -} - -template -void EndgameFunctions::add_sf(const string& keyCode) { - - ScalingInfo s1 = {WHITE, new ScalingFunction(WHITE)}; - ScalingInfo s2 = {BLACK, new ScalingFunction(BLACK)}; +template +void EndgameFunctions::add(const string& keyCode) { - ESFmap.insert(std::pair(buildKey(keyCode), s1)); - ESFmap.insert(std::pair(buildKey(swapColors(keyCode)), s2)); -} - -EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const { + typedef typename T::Base F; - std::map::const_iterator it(EEFmap.find(key)); - return (it != EEFmap.end() ? it->second : NULL); + map().insert(std::pair(buildKey(keyCode), new T(WHITE))); + map().insert(std::pair(buildKey(swapColors(keyCode)), new T(BLACK))); } -EndgameScalingFunctionBase* EndgameFunctions::getESF(Key key, Color* c) const { - - std::map::const_iterator it(ESFmap.find(key)); - if (it == ESFmap.end()) - return NULL; +template +T* EndgameFunctions::get(Key key) const { - *c = it->second.col; - return it->second.fun; + std::map::const_iterator it(map().find(key)); + return (it != map().end() ? it->second : NULL); }