Hash tables are widely used in computer applications, communications, and logic circuits to implement associative arrays, i.e., data structures that map keys to values. For example, hash tables are used in network switching elements, such as bridges and routers, to map keys that are derived from specified header fields of packets received by the switching element to instructions that are to be applied in forwarding the packets by the switching element. Many other applications of hash tables are known in the art.
A hash table uses a hash function to compute, for each key, an index to a “slot” in an array where the desired value is held. Such hash functions are chosen so as to provide a uniform distribution of hash results, and thus minimize the number of collisions between different keys, i.e., occurrences in which the hash function maps two (or more) keys to the same slot. Most hash table designs assume that collisions will occur and provide techniques for accommodating collisions and minimizing their effect on lookup performance. Arash Partow gives a useful survey of hashing techniques and definitions of available hash functions on the “hash functions” page of the “programming” section in the partow.net Web site.
One useful approach to avoidance of collisions is “cuckoo hashing,” described by Pagh and Rodler in an article entitled “Cuckoo Hashing,” Journal of Algorithms 51 (2004), pages 122-144. As described by the authors, cuckoo hashing uses two hash tables, T1 and T2, each accessed by a different, respective hash function, h1 or h2. When inserting a new element with key x into the tables, both h1(x) and h2(x) are computed, and if one of the corresponding slots is vacant, the value for x is inserted there. (Either slot may be chosen if both are vacant.) When both slots are occupied, however, the current occupant of one of the slots, for example, an existing element with key y, such that h1(y)=h1(x), is ejected from its slot and moved to the slot with index h2(y). If this latter slot is also occupied (for example, by an element with key z such that h2(z)=h2(y)), this process of ejection and replacement will continue until a vacant slot is found or until a predefined timeout period has expired. In the latter case, the tables will be recomputed using new hash functions.