https://github.com/tmzane/libds
🧱 Data structures implemented in C
https://github.com/tmzane/libds
c data-structures hashmap
Last synced: 9 months ago
JSON representation
🧱 Data structures implemented in C
- Host: GitHub
- URL: https://github.com/tmzane/libds
- Owner: tmzane
- License: mpl-2.0
- Created: 2024-01-12T16:12:50.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-10T19:39:38.000Z (about 2 years ago)
- Last Synced: 2025-04-11T17:52:59.210Z (12 months ago)
- Topics: c, data-structures, hashmap
- Language: C
- Homepage:
- Size: 50.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libds
Data structures implemented in C.
## Map
A hash map with support for dynamic resizing and custom allocators.
```c
map* m = map_new(NULL);
map_get(m, "foo"); // -> NULL
map_len(m); // -> 0
map_set(m, "foo", "bar"); // -> "bar"
map_get(m, "foo"); // -> "bar"
map_len(m); // -> 1
map_set(m, "foo", "baz"); // -> "bar"
map_get(m, "foo"); // -> "baz"
map_len(m); // -> 1
map_del(m, "foo"); // -> "baz"
map_len(m); // -> 0
for (struct map_iter it = map_iter_new(m); map_iter_next(&it);) {
// it.key
// it.value
}
map_free(m);
```
There are two implementations: [open addressing][1] ([map_oa.c](map_oa.c)) and [separate chaining][2] ([map_sc.c](map_sc.c)).
Both are based on information from Wikipedia and the book [Crafting interpreters][3].
The hash function used is [FNV-1a][4].
[1]: https://en.wikipedia.org/wiki/Hash_table#Open_addressing
[2]: https://en.wikipedia.org/wiki/Hash_table#Separate_chaining
[3]: https://craftinginterpreters.com/hash-tables.html
[4]: https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function#FNV-1a_hash