IBM Metal C

Examples

Example 6. Hash Table

A hash table (hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index, using the key value, into an array of slots, to find the associated value.

Ideally, the hash function will assign each key to a unique slot; in practice some keys usually hash to the same slot. This duplicated mapping is called a collision, and can be resolved by a number of well documentaed solutions. This implementation resolves the collision through double hashing and open addressing. The average cost for each hash table lookup is independent of the number of elements stored in the table. In many situations, hash tables are more efficient than search trees or any other table lookup structure.

Performance Analysis

With an ideal hash function, a table of size k with open addressing has no collisions and contains up to k elements, with a single comparison for successful lookup. A table of size k with chaining and n keys has the minimum of max(0, n-k) collisions and Ο(1 + n/k) comparisons for lookup. For the worst choice of hash function, every insertion causes a collision, and hash tables degenerate to a linear search, up to n comparisons for a successful lookup.

This hash table implmentation is based upon the Open Scatter Table Search with Collision Resolution by Open Addressing and Double Hashing, found in D.E. Knuth’s The Art of Computer Programming. There are two hash functions employed in this program, the Fowler/Noll/Vo (FNV) hash and MurmurHash3

Type Signature

extern int hcreate(int elements, int elementSize, int keyLength, int keyOffset, ENTRY *__item);
extern ENTRY* data hsearch(ENTRY *__item, ACTION __action);
extern hdestroy(ENTRY *__item);

Signature Details

hcreate – Create a new hash table.
elements – maximum number of hash table entries. Minimum 50 – maximum 1,000,000
elementSize – size in bytes of each table entry.
Key length – length in bytes of the table key.
Key offset – offset in bytes of the table key from the start of an entry.
The action argument is a member of an enumeration type ACTION indicating the disposition of the entry if it cannot be found in the table. ENTER indicates that the item should be inserted in the table at an appropriate point. FIND indicates that no entry should be made.
hsearch() – Add or find a hash table entry
returns a NULL pointer if either the action is FIND and the item could not be found or the action is ENTER and the table is full.
 
hdestroy – Free the storage associated with a hash table

C Source

References

  1. Hash function, Wikipedia, 1997
  2. The Art of Computer Programming, Volume 3: Sorting and Searching, Chapter 6.4., Knuth, D. Addison Wesley, 1973
  3. FNV Hash, Landon Curt Noll, 1994-2013
  4. SMHasher & MurmurHash, Google Code