Not sure whether it is sensible reopen my earlier thread on Hashing URL.
Nonetheless, I am still curious know how this work undercover.
Assumption: We have a hashtable with n (where n < Infinity) element where asymptotic time complexity is o(1); we (CLR) have achieved this while applying some hashing function ( Hn-1 hash function where n>1).
Question: Can someone explain me how CLR map Key to the hash code when we seek (retrieve) any element (if different hashing functions are used)? How CLR track (if it) the hashing function of any live object (hash table)?
Thanks in advance.
Conceptually, there are two hash functions. The first hash function, as you probably have guessed, is the key object’s
GetHashCodemethod. The second hash function is a hash of the key returned by the first hash function.So, imagine a hash table that has a capacity of 1,024 items, and you’re going to insert two keys:
K1andK2.K1.GetHashCode()returns 1,023.K2.GetHashCode()returns 65,535The code then divides the returned key by the hash table size and takes the remainder. So both of the keys map to position 1,023 in the hash table.
K1is added to the table. When it comes time to addK2, there is a collision. So the code resorts to the second hash function. That second hash function is probably a “bit mixer” (often the last stage in calculating a hash code) of some sort that randomizes the bits in the returned key. Conceptually, the code would look something like this:The point here is that the code doesn’t have to keep track of multiple hash functions for the different keys. It knows that it can call
Key.GetHashCode()to get the object’s hash code. From there, it can call its own bit mixer function or functions to generate additional hash codes.