BDE 4.14.0 Production release
|
Provide a wrapper to improve "bad" hash algorithms.
This component provides a functor type, bslh::FibonacciBadHashWrapper
, that can be used to wrap a hash algorithm ameliorating the unwanted properties of a poor hash function.
For example, the identity function is often used to hash integer values (see bsl::hash<int>
), and although the identity function has the property that two different input values are unlikely to result in the same hash value, it does not have many of the other desirable properties of a hash function (common input values are not evenly distributed over the range of hash values, a small change the input does not change the hash value extensively, and it is easy to determine the input from a hash value).
The bslh::FibonacciBadHashWrapper
can be used on the output of a bad hash function (like the identity function) to address some (but not all) of those deficiencies. Specifically, the resulting hash value is more evenly distributed over the range, and a small change in the input will result in a larger change in the hash value.
The wrapped hash function object must be a function object, default constructible, copy constructible, and destructible. The result of operator()
must be convertable to size_t
. For an instance of a hash functor, operator()
must provide the same result for each argument value for the lifetime of the functor. For the bslh::FibonacciBadHashWrapper
to support the type KEY
in its operator()
, the wrapped hash function's operator()
must be invokable with the type KEY
.
This section illustrates intended usage of this component.
Suppose we are using a hash table that requires variation in the most-significant and least-significant bits to operate efficiently (e.g., Abseil's flat hash map). The keys we will be using are small integer values, and we would like to use the identity function as the hash functor since it is efficient. A simple and efficient method to obtain a hash functor with the necessary qualities is to wrap the identity functor with bslh::FibonaccaBadHashWrapper
.
First, we define our IdentityHash
class.
Then, we instantiate an instance of the identity functor and the wrapped functor.
Finally, we examine the range of values obtained from small integer values: