1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Small simplification of endgame functions API

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-08-14 11:52:27 +01:00
parent 13524bea9b
commit 48e39c5c8e
3 changed files with 36 additions and 34 deletions

View file

@ -101,57 +101,59 @@ namespace {
/// Endgames member definitions /// Endgames member definitions
template<> const Endgames::EFMap& Endgames::get<EF>() const { return maps.first; } template<> const Endgames::EFMap& Endgames::map<EF>() const { return maps.first; }
template<> const Endgames::SFMap& Endgames::get<SF>() const { return maps.second; } template<> const Endgames::SFMap& Endgames::map<SF>() const { return maps.second; }
Endgames::Endgames() { Endgames::Endgames() {
add<Endgame<Value, KNNK> >("KNNK"); add<Value, KPK>("KPK");
add<Endgame<Value, KPK> >("KPK"); add<Value, KNNK>("KNNK");
add<Endgame<Value, KBNK> >("KBNK"); add<Value, KBNK>("KBNK");
add<Endgame<Value, KRKP> >("KRKP"); add<Value, KRKP>("KRKP");
add<Endgame<Value, KRKB> >("KRKB"); add<Value, KRKB>("KRKB");
add<Endgame<Value, KRKN> >("KRKN"); add<Value, KRKN>("KRKN");
add<Endgame<Value, KQKR> >("KQKR"); add<Value, KQKR>("KQKR");
add<Endgame<Value, KBBKN> >("KBBKN"); add<Value, KBBKN>("KBBKN");
add<Endgame<ScaleFactor, KNPK> >("KNPK"); add<ScaleFactor, KNPK>("KNPK");
add<Endgame<ScaleFactor, KRPKR> >("KRPKR"); add<ScaleFactor, KRPKR>("KRPKR");
add<Endgame<ScaleFactor, KBPKB> >("KBPKB"); add<ScaleFactor, KBPKB>("KBPKB");
add<Endgame<ScaleFactor, KBPPKB> >("KBPPKB"); add<ScaleFactor, KBPKN>("KBPKN");
add<Endgame<ScaleFactor, KBPKN> >("KBPKN"); add<ScaleFactor, KBPPKB>("KBPPKB");
add<Endgame<ScaleFactor, KRPPKRP> >("KRPPKRP"); add<ScaleFactor, KRPPKRP>("KRPPKRP");
} }
Endgames::~Endgames() { Endgames::~Endgames() {
for (EFMap::const_iterator it = get<EF>().begin(); it != get<EF>().end(); ++it) for (EFMap::const_iterator it = map<EF>().begin(); it != map<EF>().end(); ++it)
delete it->second; delete it->second;
for (SFMap::const_iterator it = get<SF>().begin(); it != get<SF>().end(); ++it) for (SFMap::const_iterator it = map<SF>().begin(); it != map<SF>().end(); ++it)
delete it->second; delete it->second;
} }
template<class T> template<typename T, EndgameType E>
void Endgames::add(const string& keyCode) { void Endgames::add(const string& keyCode) {
typedef typename T::Base F; typedef Endgame<T, E> EG;
typedef std::map<Key, F*> M; typedef typename EG::Base B;
typedef std::map<Key, B*> M;
const_cast<M&>(get<F>()).insert(std::pair<Key, F*>(mat_key(keyCode), new T(WHITE))); const_cast<M&>(map<B>()).insert(std::pair<Key, B*>(mat_key(keyCode), new EG(WHITE)));
const_cast<M&>(get<F>()).insert(std::pair<Key, F*>(mat_key(swap_colors(keyCode)), new T(BLACK))); const_cast<M&>(map<B>()).insert(std::pair<Key, B*>(mat_key(swap_colors(keyCode)), new EG(BLACK)));
} }
template<class T> template<typename T>
T* Endgames::get(Key key) const { EndgameBase<T>* Endgames::get(Key key) const {
typename std::map<Key, T*>::const_iterator it = get<T>().find(key); typedef EndgameBase<T> E;
return it != get<T>().end() ? it->second : NULL; typename std::map<Key, E*>::const_iterator it = map<E>().find(key);
return it != map<E>().end() ? it->second : NULL;
} }
// Explicit template instantiations // Explicit template instantiations
template EF* Endgames::get<EF>(Key key) const; template EF* Endgames::get<Value>(Key key) const;
template SF* Endgames::get<SF>(Key key) const; template SF* Endgames::get<ScaleFactor>(Key key) const;
/// Mate with KX vs K. This function is used to evaluate positions with /// Mate with KX vs K. This function is used to evaluate positions with

View file

@ -97,16 +97,16 @@ class Endgames {
public: public:
Endgames(); Endgames();
~Endgames(); ~Endgames();
template<class T> T* get(Key key) const; template<typename T> EndgameBase<T>* get(Key key) const;
private: private:
template<class T> void add(const std::string& keyCode); template<typename T, EndgameType E> void add(const std::string& keyCode);
// Here we store two maps, for evaluate and scaling functions... // Here we store two maps, for evaluate and scaling functions...
std::pair<EFMap, SFMap> maps; std::pair<EFMap, SFMap> maps;
// ...and here is the accessing template function // ...and here is the accessing template function
template<typename T> const std::map<Key, T*>& get() const; template<typename T> const std::map<Key, T*>& map() const;
}; };
#endif // !defined(ENDGAME_H_INCLUDED) #endif // !defined(ENDGAME_H_INCLUDED)

View file

@ -117,7 +117,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) const {
// Let's look if we have a specialized evaluation function for this // Let's look if we have a specialized evaluation function for this
// particular material configuration. First we look for a fixed // particular material configuration. First we look for a fixed
// configuration one, then a generic one if previous search failed. // configuration one, then a generic one if previous search failed.
if ((mi->evaluationFunction = funcs->get<EndgameBase<Value> >(key)) != NULL) if ((mi->evaluationFunction = funcs->get<Value>(key)) != NULL)
return mi; return mi;
if (is_KXK<WHITE>(pos)) if (is_KXK<WHITE>(pos))
@ -154,7 +154,7 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) const {
// scaling functions and we need to decide which one to use. // scaling functions and we need to decide which one to use.
EndgameBase<ScaleFactor>* sf; EndgameBase<ScaleFactor>* sf;
if ((sf = funcs->get<EndgameBase<ScaleFactor> >(key)) != NULL) if ((sf = funcs->get<ScaleFactor>(key)) != NULL)
{ {
mi->scalingFunction[sf->color()] = sf; mi->scalingFunction[sf->color()] = sf;
return mi; return mi;