Skip to content

Commit

Permalink
all: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Mar 9, 2024
1 parent fa28c8c commit e65d273
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# libds

Data structures implemented in C.

## Map

A hash map with support for dynamic resizing.

```c
map* m = map_new();

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
2 changes: 1 addition & 1 deletion map.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stddef.h>

/*
* A hash map that supports dynamic resizing.
* A hash map with support for dynamic resizing.
* The keys are strings, the values must not be NULL.
* Must be created with map_new() and freed with map_free().
*
Expand Down

0 comments on commit e65d273

Please sign in to comment.