How to hash a buffer with std::hash?

2.2k Views Asked by At

I study std::hash's references, and find it can't hash a serialized data, like char*. Is it correct or normal ? How can I hash a serialized buffer?

1

There are 1 best solutions below

2
On BEST ANSWER

The idea with std::hash is to provide a general hashing algorithm for fixed-size data that is good enough for most uses, so users don't need to roll their own every time. The problem with variable length inputs is that hashing them is a much more complex problem, often depending on characteristics of the data itself, to require the standard library to include such an algorithm, and thus the implementation is punted to the developer. For example, a hash algorithm that works great for ASCII strings might not work so great for data containing mostly zeros, and a good algorithm for the latter might give too many collisions for strings. (There are also speed tradeoffs; some hashing algorithms might work great for everything but be too slow.)

IIRC, an old, old hashing algorithm for ASCII strings is to simply multiply every character's ASCII value together. Needless to say, this is really fast and only works because there are no zeros.

So instead of using std::hash, you're supposed to write your own hashing class with the same API (i.e. it must define size_t operator()(Key)) and pass that class as the Hash template parameter to hash-using templates like std::unordered_set.