I’m using Boost for C++ at the moment, and am trying to implement the unordered map (aka hash table) using CRC32. To my knowledge it will take a string as an initial key, hash it, and apply another operation so that it will fit into the number of buckets.
Though in my situation, I would like to hash the string key beforehand (using a separate CRC function in Boost), then use that ID to index the table. The problem that I need help with is that a CRC32 hash has 2^32 potential values, and I doubt I’ll ever need a table with 2^32 elements. What should I do in this situation?
Thanks for any help here!
Use the modulus operator —
%in the C-based languages:But note that the sign of the result, in C++, is “implementation defined”, and can be negative if hashValue is negative. So one may want to turn off the sign bit in hashValue before doing the
%operation.Also note that, if
hashtableSizeis known to be a power of two, one can simply maskhashValueto get the index: