-/// MaterialInfo::init() is called during program initialization. It
-/// precomputes material hash keys for a few basic endgames, in order
-/// to make it easy to recognize such endgames when they occur.
-
-void MaterialInfo::init() {
- KPKMaterialKey = Position::zobMaterial[WHITE][PAWN][1];
- KKPMaterialKey = Position::zobMaterial[BLACK][PAWN][1];
- KBNKMaterialKey =
- Position::zobMaterial[WHITE][BISHOP][1] ^
- Position::zobMaterial[WHITE][KNIGHT][1];
- KKBNMaterialKey =
- Position::zobMaterial[BLACK][BISHOP][1] ^
- Position::zobMaterial[BLACK][KNIGHT][1];
- KRKPMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KPKRMaterialKey =
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][ROOK][1];
- KRKBMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[BLACK][BISHOP][1];
- KBKRMaterialKey =
- Position::zobMaterial[WHITE][BISHOP][1] ^
- Position::zobMaterial[BLACK][ROOK][1];
- KRKNMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[BLACK][KNIGHT][1];
- KNKRMaterialKey =
- Position::zobMaterial[WHITE][KNIGHT][1] ^
- Position::zobMaterial[BLACK][ROOK][1];
- KQKRMaterialKey =
- Position::zobMaterial[WHITE][QUEEN][1] ^
- Position::zobMaterial[BLACK][ROOK][1];
- KRKQMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[BLACK][QUEEN][1];
- KRPKRMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][ROOK][1];
- KRKRPMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[BLACK][ROOK][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KRPPKRPMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[WHITE][PAWN][2] ^
- Position::zobMaterial[BLACK][ROOK][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KRPKRPPMaterialKey =
- Position::zobMaterial[WHITE][ROOK][1] ^
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][ROOK][1] ^
- Position::zobMaterial[BLACK][PAWN][1] ^
- Position::zobMaterial[BLACK][PAWN][2];
- KNNKMaterialKey =
- Position::zobMaterial[WHITE][KNIGHT][1] ^
- Position::zobMaterial[WHITE][KNIGHT][2];
- KKNNMaterialKey =
- Position::zobMaterial[BLACK][KNIGHT][1] ^
- Position::zobMaterial[BLACK][KNIGHT][2];
- KBPKBMaterialKey =
- Position::zobMaterial[WHITE][BISHOP][1] ^
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][BISHOP][1];
- KBKBPMaterialKey =
- Position::zobMaterial[WHITE][BISHOP][1] ^
- Position::zobMaterial[BLACK][BISHOP][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KBPKNMaterialKey =
- Position::zobMaterial[WHITE][BISHOP][1] ^
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][KNIGHT][1];
- KNKBPMaterialKey =
- Position::zobMaterial[WHITE][KNIGHT][1] ^
- Position::zobMaterial[BLACK][BISHOP][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KNPKMaterialKey =
- Position::zobMaterial[WHITE][KNIGHT][1] ^
- Position::zobMaterial[WHITE][PAWN][1];
- KKNPMaterialKey =
- Position::zobMaterial[BLACK][KNIGHT][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
- KPKPMaterialKey =
- Position::zobMaterial[WHITE][PAWN][1] ^
- Position::zobMaterial[BLACK][PAWN][1];
-}
+/// 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;