+
+
+/// 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 needed
+/// because std::map is not guaranteed to be thread-safe even if accessed
+/// only for a lookup.
+
+EndgameFunctions::EndgameFunctions() {
+
+ KNNKMaterialKey = buildKey("KNNK");
+ KKNNMaterialKey = buildKey("KKNN");
+
+ add_ef<KPK>("KPK");
+ add_ef<KBNK>("KBNK");
+ add_ef<KRKP>("KRKP");
+ add_ef<KRKB>("KRKB");
+ add_ef<KRKN>("KRKN");
+ add_ef<KQKR>("KQKR");
+ add_ef<KBBKN>("KBBKN");
+
+ add_sf<KNPK>("KNPK");
+ add_sf<KRPKR>("KRPKR");
+ add_sf<KBPKB>("KBPKB");
+ add_sf<KBPPKB>("KBPPKB");
+ add_sf<KBPKN>("KBPKN");
+ add_sf<KRPPKRP>("KRPPKRP");
+ add_sf<KRPPKRP>("KRPPKRP");
+}
+
+EndgameFunctions::~EndgameFunctions() {
+
+ for (std::map<Key, EF*>::iterator it = EEFmap.begin(); it != EEFmap.end(); ++it)
+ delete (*it).second;
+
+ for (std::map<Key, ScalingInfo>::iterator it = ESFmap.begin(); it != ESFmap.end(); ++it)
+ delete (*it).second.fun;
+}
+
+Key EndgameFunctions::buildKey(const 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();
+}
+
+const string EndgameFunctions::swapColors(const string& keyCode) {
+
+ // Build corresponding key for the opposite color: "KBPKN" -> "KNKBP"
+ size_t idx = keyCode.find("K", 1);
+ return keyCode.substr(idx) + keyCode.substr(0, idx);
+}
+
+template<EndgameType et>
+void EndgameFunctions::add_ef(const string& keyCode) {
+
+ EEFmap.insert(std::pair<Key, EF*>(buildKey(keyCode), new EvaluationFunction<et>(WHITE)));
+ EEFmap.insert(std::pair<Key, EF*>(buildKey(swapColors(keyCode)), new EvaluationFunction<et>(BLACK)));
+}
+
+template<EndgameType et>
+void EndgameFunctions::add_sf(const string& keyCode) {
+
+ ScalingInfo s1 = {WHITE, new ScalingFunction<et>(WHITE)};
+ ScalingInfo s2 = {BLACK, new ScalingFunction<et>(BLACK)};
+
+ ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(keyCode), s1));
+ ESFmap.insert(std::pair<Key, ScalingInfo>(buildKey(swapColors(keyCode)), s2));
+}
+
+EndgameEvaluationFunctionBase* EndgameFunctions::getEEF(Key key) const {
+
+ std::map<Key, EF*>::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;
+}