diff options
| author | Mosder <xmosder12@gmail.com> | 2026-08-18 19:12:39 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-08-18 19:12:39 +0200 |
| commit | 3d63835dc40edc6143538fb78bbc5f3cb888e6f5 (patch) | |
| tree | e1481a2275437f45ce59ff0717a864407ffdfa19 | |
| parent | 6af63e218750d58ad1a87643890cb49c2352c666 (diff) | |
feat: added basic templates for hashmap implementation
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/moslib/ds/hashmap.h | 66 | ||||
| -rw-r--r-- | src/ds/hashmap.c | 9 | ||||
| -rw-r--r-- | tests/ds/hashmap_tests.c | 8 | ||||
| -rw-r--r-- | tests/ds/hashmap_tests.h | 3 | ||||
| -rw-r--r-- | tests/tests.c | 2 |
6 files changed, 90 insertions, 0 deletions
@@ -25,11 +25,13 @@ memory: structures: $(CC) $(CFLAGS) -c src/ds/array.c + $(CC) $(CFLAGS) -c src/ds/hashmap.c test: $(CC) $(CFLAGS_TEST) -c tests/mem/alloc_tests.c $(CC) $(CFLAGS_TEST) -c tests/ds/array_tests.c $(CC) $(CFLAGS_TEST) -c tests/string_tests.c + $(CC) $(CFLAGS_TEST) -c tests/ds/hashmap_tests.c $(CC) $(CFLAGS_TEST) -lmoslib *.o tests/tests.c -o run_tests ./run_tests rm run_tests *.o diff --git a/include/moslib/ds/hashmap.h b/include/moslib/ds/hashmap.h new file mode 100644 index 0000000..b468977 --- /dev/null +++ b/include/moslib/ds/hashmap.h @@ -0,0 +1,66 @@ +// moslib/ds/hashmap.h +// Implementation of hashmap + +// ======================================== QUICK USAGE GUIDE ========================================= +// ---------------------------------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------------------------------- +// ===================================== END OF QUICK USAGE GUIDE ===================================== + +#include <stddef.h> +#include <stdint.h> + +#ifndef MOSLIB_DS_HASHMAP_H +#define MOSLIB_DS_HASHMAP_H + +// Initial capacity to use in the hashmap +#ifndef MOS_HM_INIT_CAP +#define MOS_HM_INIT_CAP 16 +#endif // MOS_HM_INIT_CAP + +#ifndef MOS_FORCE_PREFIXES + +#define Hashmap MosHashmap +#define hm_new mos_hm_new + +#endif // MOS_FORCE_PREFIXES + +typedef struct MosHashmap MosHashmap; + +typedef struct { + enum { DEFAULT, STR, SS } key; + uint32_t (*hash)(void *key); + int (*eq)(void *key1, void *key2); +} MosHmInitArgs; + +// Create a new hashmap +// +// Arguments: +// key_t +// type of key to use +// +// val_t +// type of value to use +// +// Optional arguments: +// .key +// key specification enum +// STR - C string hash and equals functions +// SS - StringSlice hash and equals functions +// +// .hash +// user defined hash function +// will override the one specified by .key +// +// .eq +// user defined equals function +// will override the one specified by .key +// +// Returns: +// pointer to the new hashmap +#define mos_hm_new(key_t, val_t, ...) mos_hm_new_fn(sizeof(key_t), sizeof(val_t), (MosHmInitArgs){.key = DEFAULT, __VA_ARGS__}) + +// Function prototypes for macros +extern MosHashmap *mos_hm_new_fn(size_t key_size, size_t val_size, MosHmInitArgs args); + +#endif // MOSLIB_DS_HASHMAP_H diff --git a/src/ds/hashmap.c b/src/ds/hashmap.c new file mode 100644 index 0000000..8cf6a2d --- /dev/null +++ b/src/ds/hashmap.c @@ -0,0 +1,9 @@ +#define MOS_FORCE_PREFIXES +#include "moslib/ds/hashmap.h" + +MosHashmap *mos_hm_new_fn(size_t key_size, size_t val_size, MosHmInitArgs args) { + (void)key_size; + (void)val_size; + (void)args; + return NULL; +} diff --git a/tests/ds/hashmap_tests.c b/tests/ds/hashmap_tests.c new file mode 100644 index 0000000..4e9acd7 --- /dev/null +++ b/tests/ds/hashmap_tests.c @@ -0,0 +1,8 @@ +#include "hashmap_tests.h" +#define MOS_HM_INIT_CAP 8 +#include "moslib/ds/hashmap.h" + +void ds_hashmap_tests(Tester *tester) { + TestGroup *group = add_test_group(tester, "moslib/ds/hashmap.h"); + (void)group; +} diff --git a/tests/ds/hashmap_tests.h b/tests/ds/hashmap_tests.h new file mode 100644 index 0000000..1efcbb7 --- /dev/null +++ b/tests/ds/hashmap_tests.h @@ -0,0 +1,3 @@ +#include <moslib/tester.h> + +extern void ds_hashmap_tests(Tester *tester); diff --git a/tests/tests.c b/tests/tests.c index 378daf6..d7ce15b 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -1,6 +1,7 @@ #include <moslib/tester.h> #include "ds/array_tests.h" +#include "ds/hashmap_tests.h" #include "mem/alloc_tests.h" #include "string_tests.h" @@ -10,6 +11,7 @@ int main(void) { mem_alloc_tests(tester); ds_array_tests(tester); string_tests(tester); + ds_hashmap_tests(tester); run_tests(tester); free_tester(tester); |
