Assuming that we have a set of elements and want to store them in a hash map (for example std::unoredered_set
), and each element has a key of type uint64_t
which value can vary from 0 to its max possible value, is it the best choice to use trivial hash function, where a hash value of a key is a key itself? Does it depend on container in use (i.e. Google's sparse hash vs unordered map from STL)? The probability of appearance of key values is unknown.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What are the problems associated to Best First Sea
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
I would suggest a good 64-bit mixer of which there are many to choose from. The finalizer from MurmerHash3 is fairly quick and does a reasonable job in just five lines of code:
Numerical Recipes, 3rd Edition, recommends this:
If all you have to hash is a uint64_t of any possible value with unknown probabilities, and your output must be a uint64_t, then you don't gain any advantage by changing the value. Just use the key itself.
If you knew something about the distribution of your values or your values were restricted to a smaller range (which is really the same thing as knowing about the distribution), then it could be beneficial to apply a transformation to the key, but this depends on the implementation of the container. You would only benefit by reducing collisions when the table transforms a hash into a bucket index, but that depends both on the table's algorithm and the current/average state of the table (how often each bucket is used).
HashMaps are very useful in giving you fast access to data. The complexity of hash operations are
O(1)
which are very fast compared to other well-known structures such as double linked lists, linked lists or trees.To use
uint64_t
as a key for the hash, you can use hash containers such as GHASHLISHThe
GLIB
library is thread-safe and used by multiple open source projects. It does not supportuint64_t
as a key natively, so you have to provide your own hash_function.As an example, you can use FNV hash
Here's a quick example of how to hash
uint64
touint32
using FNV: