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