X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmaterial.cpp;h=3fc05db8fc78e09c928f2464590d032988099b59;hb=620cfbb6760911a9f2ce188cd7244d86a76953c2;hp=c9dcaba1d42c8441a02bb03638ac80b75d3c3520;hpb=297c12e595ebc33e11be73ee4b188326418acb4f;p=stockfish diff --git a/src/material.cpp b/src/material.cpp index c9dcaba1..3fc05db8 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -57,37 +57,40 @@ namespace { //// Classes //// +typedef EndgameEvaluationFunctionBase EF; +typedef EndgameScalingFunctionBase SF; /// See header for a class description. It is declared here to avoid /// to include in the header file. class EndgameFunctions { - - typedef EndgameEvaluationFunctionBase EF; - typedef EndgameScalingFunctionBase SF; - 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 std::map& map() { return EEFmap; } }; +// Explicit specializations of a member function shall be declared in +// the namespace of which the class template is a member. +template<> const std::map& +EndgameFunctions::map() const { return ESFmap; } + +template<> std::map& +EndgameFunctions::map() { return ESFmap; } + //// //// Functions @@ -152,7 +155,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 +196,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; + SF* 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 +261,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 +331,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 +353,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 +385,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) { +template +void EndgameFunctions::add(const string& keyCode) { - ScalingInfo s1 = {WHITE, new ScalingFunction(WHITE)}; - ScalingInfo s2 = {BLACK, new ScalingFunction(BLACK)}; + typedef typename T::Base F; - ESFmap.insert(std::pair(buildKey(keyCode), s1)); - ESFmap.insert(std::pair(buildKey(swapColors(keyCode)), s2)); + map().insert(std::pair(buildKey(keyCode), new T(WHITE))); + map().insert(std::pair(buildKey(swapColors(keyCode)), new T(BLACK))); } -EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const { - - std::map::const_iterator it(EEFmap.find(key)); - return (it != EEFmap.end() ? it->second : NULL); -} - -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; + typename std::map::const_iterator it(map().find(key)); + return (it != map().end() ? it->second : NULL); }