tidwall/hashmap.c logo

tidwall/hashmap.c

Free

Hash map implementation in C.

FreeFree tier
Type
Open Source

About tidwall/hashmap.c

An efficient hash map library in C that uses open addressing with Robin Hood hashing. Provides a generic interface supporting variable-sized items and multiple built-in hash algorithms including SipHash, MurmurHash3, and xxHash. Compatible with C99 and above, and allows custom allocators. The library offers a simple API for creating, inserting, retrieving, deleting, and iterating over key-value pairs, with an example demonstrating usage with struct data.

Key Features

Open addressing using Robin Hood hashing
Generic interface with support for variable-sized items
Built-in hash algorithms: SipHash, MurmurHash3, xxHash
Supports C99 and up
Supports custom allocators
High performance

Pros & Cons

Pros
  • Robin Hood hashing improves cache locality and reduces collision chaining
  • Generic interface works with any data type
  • Multiple built-in hash functions provide flexibility
  • Custom allocator support allows integration with specific memory management schemes
  • C99 compatible, suitable for embedded and legacy systems
Cons
  • No built-in thread safety (external synchronization required)
  • Requires manual memory management typical of C programming

Best For

Storing and retrieving key-value data in C applicationsImplementing dictionaries or associative arraysBuilding caches and lookup tablesData serialization and deserialization structures

FAQ

What hash functions are built into hashmap.c?
The library includes SipHash, MurmurHash3, and xxHash, and allows users to supply alternative hashing algorithms.
How do I create a new hash map?
Use the hashmap_new function with the size of the item type, initial capacity, optional seeds, a hash function, a compare function, and optional allocator handles.
How do I iterate over all items in the hash map?
You can use either hashmap_scan with a callback function or hashmap_iter in a loop to access each item sequentially.
Is there support for custom memory allocation?
Yes, the library allows custom allocators via callbacks passed to hashmap_new.