diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-19 15:15:39 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-19 15:15:39 +1000 |
commit | 6fccac3372170153b59e829d11c6c0b0a5c2bc77 (patch) | |
tree | 92290b3ed628cd2706847fcc3f9bdddeda521d03 | |
parent | 259da84075c62c2ab96c7cb922df2000ebefb735 (diff) |
start fleshing out API for hashmap / hashset
-rw-r--r-- | src/std/containers/container_utils.h | 18 | ||||
-rw-r--r-- | src/std/containers/hashmap.h | 28 | ||||
-rw-r--r-- | src/std/containers/hashset.h | 21 | ||||
-rw-r--r-- | src/std/containers/hashtable.h | 10 | ||||
-rw-r--r-- | src/std/mem.h | 8 |
5 files changed, 74 insertions, 11 deletions
diff --git a/src/std/containers/container_utils.h b/src/std/containers/container_utils.h new file mode 100644 index 0000000..39906c3 --- /dev/null +++ b/src/std/containers/container_utils.h @@ -0,0 +1,18 @@ +/** + * @file container_utils.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-06-19 + * + * @copyright Copyright (c) 2024 + * + */ + +#pragma once + +typedef struct generic_iterator { + +} generic_iterator; + +typedef void* (*iterator_next_item)(void* iterator);
\ No newline at end of file diff --git a/src/std/containers/hashmap.h b/src/std/containers/hashmap.h new file mode 100644 index 0000000..64a5bf7 --- /dev/null +++ b/src/std/containers/hashmap.h @@ -0,0 +1,28 @@ +/** + * @file hashmap.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-04-27 + * + * @copyright Copyright (c) 2024 + * + */ + + +typedef struct hashmap hashmap; + +/* +Example usage +------------- +init hashmap +insert (string, material) +get (string) -> material_opt or material* ? + +*/ + +void hashmap_init(hashmap* map); + +// ... + +void hashmap_free(hashmap* map);
\ No newline at end of file diff --git a/src/std/containers/hashset.h b/src/std/containers/hashset.h index d153fd2..978ec00 100644 --- a/src/std/containers/hashset.h +++ b/src/std/containers/hashset.h @@ -7,4 +7,23 @@ * * @copyright Copyright (c) 2024 * - */
\ No newline at end of file + */ + +#include "defines.h" + +typedef struct hashset hashset; + +/** @brief Describes a function that will take a pointer to a datatype (e.g. a u64 or a struct) + and return a hashed key. */ +typedef uint64_t (*hash_item)(void* item); + +void hashset_init(hashset* set, hash_item hash_func, size_t initial_capacity); +// TODO: void hashset_from_iterator(); +bool hashset_insert(hashset* set, void* item, uint64_t* out_key); +void hashset_batch_insert(hashset* set, void* items, u64 item_count); +bool hashset_contains(hashset* set, void* item); +bool hashset_remove_item(hashset* set, void* item); +bool hashset_remove_key(hashset* set, uint64_t key); +void hashset_merge(hashset* set_a, hashset* set_b); +hashset* hashset_merge_cloned(hashset* set_a, hashset* set_b); +void hashset_free(hashset* set);
\ No newline at end of file diff --git a/src/std/containers/hashtable.h b/src/std/containers/hashtable.h deleted file mode 100644 index f5d98e7..0000000 --- a/src/std/containers/hashtable.h +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @file hashtable.h - * @author your name (you@domain.com) - * @brief - * @version 0.1 - * @date 2024-04-27 - * - * @copyright Copyright (c) 2024 - * - */
\ No newline at end of file diff --git a/src/std/mem.h b/src/std/mem.h index 6e76763..8ab6f46 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -12,6 +12,14 @@ #include <stddef.h> #include "defines.h" +typedef void* (*alloc_fn)(size_t size); +typedef void (*free_fn)(void* ptr); + +typedef struct allocator_t { + alloc_fn alloc; + free_fn free; +} allocator_t; + // --- Arena // Inspired by https://nullprogram.com/blog/2023/09/27/ |