Data Structures and Algorithms/Data Structures
59 / 67

05/2021Data Structures and Algorithms

Data Structures

Four reusable C modules — a bag, a counter set, a set, and a hashtable — where the hashtable is an array of sets that chains collisions for expected constant-time lookup.

╌╌╌╌

Four reusable container modules in C, each an opaque type behind a small allocate / insert / find / iterate / free interface.

A bag is an unordered collection of void* items, implemented as a singly linked list used as a stack: bag_insert pushes onto the head and bag_extract pops any item back off. A counter set trades items for tallies, so counters_add, keyed on an int, either starts a new count at one or increments an existing one over the same linked-list backing.

A set stores (char* key, void* item) pairs in a linked list, copying each key string and rejecting duplicates, with set_find returning an item by key. A hashtable presents that same interface but scales it, holding an array of num_slots sets and routing each key through Bob Jenkins' one-at-a-time hash to a slot, so hashtable_insert and hashtable_find delegate to a short per-slot set. Chaining collisions inside those sets turns the set's linear scan into an expected lookup.

References

  1. Project repository
  2. Reference notes: Hash Tables

╌╌ END ╌╌