| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <mos/allocator.hpp> |
| 6 | #include <mos/lib/sync/spinlock.hpp> |
| 7 | #include <mos/moslib_global.hpp> |
| 8 | #include <mos/types.hpp> |
| 9 | #include <mos_stdlib.hpp> |
| 10 | |
| 11 | /** |
| 12 | * @defgroup libs_hashmap libs.HashMap |
| 13 | * @ingroup libs |
| 14 | * @brief A simple hashmap. |
| 15 | * @{ |
| 16 | */ |
| 17 | |
| 18 | typedef hash_t (*hashmap_hash_t)(const uintn key); /// A hashmap hash function prototype. |
| 19 | typedef int (*hashmap_key_compare_t)(const uintn key1, const uintn key2); /// A hashmap key comparison function prototype. |
| 20 | typedef bool (*hashmap_foreach_func_t)(const uintn key, void *value, void *data); /// A hashmap foreach callback function prototype. |
| 21 | |
| 22 | struct hashmap_entry_t; |
| 23 | |
| 24 | typedef struct _hashmap |
| 25 | { |
| 26 | s32 magic; |
| 27 | hashmap_entry_t **entries; |
| 28 | size_t capacity; |
| 29 | size_t size; |
| 30 | hashmap_hash_t hash_func; |
| 31 | hashmap_key_compare_t key_compare_func; |
| 32 | spinlock_t lock; |
| 33 | } hashmap_t; |
| 34 | |
| 35 | MOSAPI void hashmap_init(hashmap_t *map, size_t capacity, hashmap_hash_t hash_func, hashmap_key_compare_t compare_func); |
| 36 | MOSAPI void hashmap_deinit(hashmap_t *map); |
| 37 | |
| 38 | MOSAPI void *hashmap_put(hashmap_t *map, uintn key, void *value); |
| 39 | MOSAPI void *hashmap_get(hashmap_t *map, uintn key); |
| 40 | MOSAPI void *hashmap_remove(hashmap_t *map, uintn key); |
| 41 | |
| 42 | MOSAPI void hashmap_foreach(hashmap_t *map, hashmap_foreach_func_t func, void *data); |
| 43 | |
| 44 | /** @} */ |
| 45 | |