Considerare il seguente codice. Che cos'è una buona funzione di hashing per l'array in Key da utilizzare in una unordered_map?Come hash un array bidimensionale tristato?
#include <unordered_map>
using namespace std;
enum TriState {
S0 = -1,
S1 = 0,
S2 = +1
};
struct K { // Key for the map
TriState a[8][8];
bool operator==(const K& k1) const {
for (int i = 0; i < 64; i++)
if (k1.a[0][i] != a[0][i])
return false;
return true;
}
};
struct Hash {
size_t operator()(const K& k) const {
size_t s;
// s = what is a good hash value?
return s;
}
};
unordered_map<K, int, Hash> m;
Normalmente nei giochi da tavolo è usato l'hashing di Zobristo. – wildplasser
L'ottimizzatore in me si riduce all'operatore == metodo. 64 int legge che potrebbe diventare un singolo 16 byte letto con un po 'di giocherellando. –
@MichaelDorgan: errore di ottimizzazione. Basta usare 'memcmp' e lasciare che il compilatore lo semplifichi :). – kennytm