BDE 4.14.0 Production release
|
Provide hash
for std::filesystem::path
.
Provides a hash
specialization for std::filesystem::path
, delegating to std::filesystem::path::hash_value
for the implementation since it is already available and conforming.
This section illustrates intended usage of this component.
Suppose we already have an array of unique values of type TYPE
, for which operator==
is defined, and we want to be able to quickly look up whether an element is in the array, without exhaustively applying operator==
to all the elements in sequence. The array itself is guaranteed not to change for the duration of our interest in it.
The problem is much simpler than building a general-purpose hash table, because we know how many elements our cross reference will contain in advance, so we will never have to dynamically grow the number of buckets
. We do not need to copy the values into our own area, so we don't have to create storage for them, or require that a copy constructor or destructor be available. We only require that they have a transitive, symmetric equivalence operation bool operator==
and that a hash function be provided.
We will need a hash function – the hash function is a function that will take as input an object of the type stored in our array, and yield a size_t
value that will be very randomized. Ideally, the slightest change in the value of the TYPE
object will result in a large change in the value returned by the hash function. In a good hash function, typically half the bits of the return value will change for a 1-bit change in the hashed value. We then use the result of the hash function to index into our array of buckets
. Each bucket
is simply a pointer to a value in our original array of TYPE
objects. We will resolve hash collisions in our array through linear probing
, where we will search consecutive buckets following the bucket where the collision occurred, testing occupied buckets for equality with the value we are searching on, and concluding that the value is not in the table if we encounter an empty bucket before we encounter one referring to an equal element.
An important quality of the hash function is that if two values are equivalent, they must yield the same hash value.
First, we define our HashCrossReference
template class, with the two type parameters TYPE
(the type being referenced') and HASHER
, which defaults to bslh::Hash<TYPE>
. This component provides the specialization of bslh::Hash
for std::filesystem::path
:
Then, In main
, we will first use our cross-reference to cross-reference a collection of std::filesystem::path
values. Note that the /
separator is equally valid on Windows and Unix-derived systems when used programmatically. We define our array and take its length:
Now, we create our cross-reference hcri
and verify it constructed properly. Note that we don't specify the second template parameter HASHER
and let it default to bslh::Hash<std::filesystem::path>
, which is already defined by this component:
Finally, we use hcri
to verify numbers that were and were not in the collection: