- // Named endgame evaluation and scaling functions, these
- // are accessed direcly and not through the function maps.
- EvaluationFunction<KmmKm> EvaluateKmmKm(WHITE);
- EvaluationFunction<KXK> EvaluateKXK(WHITE), EvaluateKKX(BLACK);
- ScalingFunction<KBPsK> ScaleKBPsK(WHITE), ScaleKKBPs(BLACK);
- ScalingFunction<KQKRPs> ScaleKQKRPs(WHITE), ScaleKRPsKQ(BLACK);
- ScalingFunction<KPsK> ScaleKPsK(WHITE), ScaleKKPs(BLACK);
- ScalingFunction<KPKP> ScaleKPKPw(WHITE), ScaleKPKPb(BLACK);
-
- typedef EndgameEvaluationFunctionBase EF;
- typedef EndgameScalingFunctionBase SF;
-}
-
-
-////
-//// Classes
-////
-
-/// EndgameFunctions class stores endgame evaluation and scaling functions
-/// in two std::map. Because STL library is not guaranteed to be thread
-/// safe even for read access, the maps, although with identical content,
-/// are replicated for each thread. This is faster then using locks.
-
-class EndgameFunctions {
-public:
- EndgameFunctions();
- ~EndgameFunctions();
- template<class T> T* get(Key key) const;
-
-private:
- template<class T> void add(const string& keyCode);
-
- static Key buildKey(const string& keyCode);
- static const string swapColors(const string& keyCode);
-
- // Here we store two maps, for evaluate and scaling functions
- pair<map<Key, EF*>, map<Key, SF*> > maps;
-
- // Maps accessing functions returning const and non-const references
- template<typename T> const map<Key, T*>& get() const { return maps.first; }
- template<typename T> map<Key, T*>& get() { return maps.first; }
-};
-
-// Explicit specializations of a member function shall be declared in
-// the namespace of which the class template is a member.
-template<> const map<Key, SF*>&
-EndgameFunctions::get<SF>() const { return maps.second; }
-
-template<> map<Key, SF*>&
-EndgameFunctions::get<SF>() { return maps.second; }
-
-
-////
-//// Functions
-////
-
-/// MaterialInfoTable c'tor and d'tor, called once by each thread
-
-MaterialInfoTable::MaterialInfoTable(unsigned int numOfEntries) {
-
- size = numOfEntries;
- entries = new MaterialInfo[size];
- funcs = new EndgameFunctions();
-
- if (!entries || !funcs)
- {
- cerr << "Failed to allocate " << numOfEntries * sizeof(MaterialInfo)
- << " bytes for material hash table." << endl;
- Application::exit_with_failure();
+ // Endgame evaluation and scaling functions accessed direcly and not through
+ // the function maps because correspond to more then one material hash key.
+ Endgame<Value, KmmKm> EvaluateKmmKm[] = { Endgame<Value, KmmKm>(WHITE), Endgame<Value, KmmKm>(BLACK) };
+ Endgame<Value, KXK> EvaluateKXK[] = { Endgame<Value, KXK>(WHITE), Endgame<Value, KXK>(BLACK) };
+
+ Endgame<ScaleFactor, KBPsK> ScaleKBPsK[] = { Endgame<ScaleFactor, KBPsK>(WHITE), Endgame<ScaleFactor, KBPsK>(BLACK) };
+ Endgame<ScaleFactor, KQKRPs> ScaleKQKRPs[] = { Endgame<ScaleFactor, KQKRPs>(WHITE), Endgame<ScaleFactor, KQKRPs>(BLACK) };
+ Endgame<ScaleFactor, KPsK> ScaleKPsK[] = { Endgame<ScaleFactor, KPsK>(WHITE), Endgame<ScaleFactor, KPsK>(BLACK) };
+ Endgame<ScaleFactor, KPKP> ScaleKPKP[] = { Endgame<ScaleFactor, KPKP>(WHITE), Endgame<ScaleFactor, KPKP>(BLACK) };
+
+ // Helper templates used to detect a given material distribution
+ template<Color Us> bool is_KXK(const Position& pos) {
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+ return pos.non_pawn_material(Them) == VALUE_ZERO
+ && pos.piece_count(Them, PAWN) == 0
+ && pos.non_pawn_material(Us) >= RookValueMidgame;