Looks like a decent hash to me. But depending how big MAX_STRING_LENGTH is it might not be very cache efficient.
With a small change:
Code:
#define MAX_ZOB_SIZE ???
unsigned int keys[256][MAX_ZOB_SIZE];
unsigned int hash_string(std::string s) {
unsigned int hash = 0;
for (size_t i = 0; i < s.length(); ++i) {
hash ^= zobrist_keys[s[i]][i % MAX_ZOB_SIZE];
}
return hash;
}
You are not limited to a maximum string length, and you can control how big each Zobrist bucket is. You might find that smaller MAX_ZOB_SIZE gives better performance without hurting the hash all that much.
EDIT: Other notes... You're passing s by value, which is gonna hurt. And the compiler might optimize the comparison with s.length(), but maybe not..