Using the OpenJDK's hashCode, I tried to implement a generic hashing routine in C:
U32 hashObject(void *object_generic, U32 object_length) {
if (object_generic == NULL) return 0;
U8 *object = (U8*)object_generic;
U32 hash = 1;
for (U32 i = 0; i < object_length; ++i) {
// hash = 31 * hash + object[i]; // Original prime used in OpenJDK
hash = 92821 * hash + object[i]; // Better constant found here: https://stackoverflow.com/questions/1835976/what-is-a-sensible-prime-for-hashcode-calculation
}
return hash;
}
The idea is that I can pass a pointer to any C object (primitive type, struct, array, etc.) and the object will be uniquely hashed. However, since this is the first time I am doing something like this, I'd like to ask- Is this the right approach? Are there any pitfalls that I need to be aware of?