+
+
+/// EndgameFunctions member definitions. This class is used to store the maps
+/// of end game and scaling functions that MaterialInfoTable will query for
+/// each key. The maps are constant and are populated only at construction,
+/// but are per-thread instead of globals to avoid expensive locks.
+
+EndgameFunctions::EndgameFunctions() {
+
+ KNNKMaterialKey = buildKey("KNNK");
+ KKNNMaterialKey = buildKey("KKNN");
+
+ add("KPK", &EvaluateKPK);
+ add("KKP", &EvaluateKKP);
+ add("KBNK", &EvaluateKBNK);
+ add("KKBN", &EvaluateKKBN);
+ add("KRKP", &EvaluateKRKP);
+ add("KPKR", &EvaluateKPKR);
+ add("KRKB", &EvaluateKRKB);
+ add("KBKR", &EvaluateKBKR);
+ add("KRKN", &EvaluateKRKN);
+ add("KNKR", &EvaluateKNKR);
+ add("KQKR", &EvaluateKQKR);
+ add("KRKQ", &EvaluateKRKQ);
+ add("KBBKN", &EvaluateKBBKN);
+ add("KNKBB", &EvaluateKNKBB);
+
+ add("KNPK", WHITE, &ScaleKNPK);
+ add("KKNP", BLACK, &ScaleKKNP);
+ add("KRPKR", WHITE, &ScaleKRPKR);
+ add("KRKRP", BLACK, &ScaleKRKRP);
+ add("KBPKB", WHITE, &ScaleKBPKB);
+ add("KBKBP", BLACK, &ScaleKBKBP);
+ add("KBPPKB", WHITE, &ScaleKBPPKB);
+ add("KBKBPP", BLACK, &ScaleKBKBPP);
+ add("KBPKN", WHITE, &ScaleKBPKN);
+ add("KNKBP", BLACK, &ScaleKNKBP);
+ add("KRPPKRP", WHITE, &ScaleKRPPKRP);
+ add("KRPKRPP", BLACK, &ScaleKRPKRPP);
+ add("KRPPKRP", WHITE, &ScaleKRPPKRP);
+ add("KRPKRPP", BLACK, &ScaleKRPKRPP);
+}
+
+Key EndgameFunctions::buildKey(const std::string& keyCode) {
+
+ assert(keyCode.length() > 0 && keyCode[0] == 'K');
+ assert(keyCode.length() < 8);
+
+ std::stringstream s;
+ bool upcase = false;
+
+ // Build up a fen substring with the given pieces, note
+ // that the fen string could be of an illegal position.
+ for (size_t i = 0; i < keyCode.length(); i++)
+ {
+ if (keyCode[i] == 'K')
+ upcase = !upcase;
+
+ s << char(upcase? toupper(keyCode[i]) : tolower(keyCode[i]));
+ }
+ s << 8 - keyCode.length() << "/8/8/8/8/8/8/8 w -";
+ return Position(s.str()).get_material_key();
+}
+
+void EndgameFunctions::add(const std::string& keyCode, EndgameEvaluationFunctionBase* f) {
+
+ EEFmap.insert(std::pair<Key, EndgameEvaluationFunctionBase*>(buildKey(keyCode), f));
+}
+
+void EndgameFunctions::add(const std::string& keyCode, Color c, EndgameScalingFunctionBase* f) {
+
+ ScalingInfo s = {c, f};
+ ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s));
+}
+
+EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const {
+
+ std::map<Key, EndgameEvaluationFunctionBase*>::const_iterator it(EEFmap.find(key));
+ return (it != EEFmap.end() ? it->second : NULL);
+}
+
+EndgameScalingFunctionBase* EndgameFunctions::getESF(Key key, Color* c) const {
+
+ std::map<Key, ScalingInfo>::const_iterator it(ESFmap.find(key));
+ if (it == ESFmap.end())
+ return NULL;
+
+ *c = it->second.col;
+ return it->second.fun;
+}