1 | /*
|
---|
2 | * SPDX-FileCopyrightText: 2006 Jakub Jermar
|
---|
3 | * SPDX-FileCopyrightText: 2012 Adam Hraska
|
---|
4 | *
|
---|
5 | * SPDX-License-Identifier: BSD-3-Clause
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** @addtogroup kernel_generic
|
---|
9 | * @{
|
---|
10 | */
|
---|
11 | /** @file
|
---|
12 | */
|
---|
13 |
|
---|
14 | #ifndef KERNEL_HASH_TABLE_H_
|
---|
15 | #define KERNEL_HASH_TABLE_H_
|
---|
16 |
|
---|
17 | #include <adt/list.h>
|
---|
18 | #include <stdbool.h>
|
---|
19 | #include <macros.h>
|
---|
20 | #include <member.h>
|
---|
21 |
|
---|
22 | /** Opaque hash table link type. */
|
---|
23 | typedef struct ht_link {
|
---|
24 | link_t link;
|
---|
25 | } ht_link_t;
|
---|
26 |
|
---|
27 | /** Set of operations for hash table. */
|
---|
28 | typedef struct {
|
---|
29 | /** Returns the hash of the key stored in the item (ie its lookup key). */
|
---|
30 | size_t (*hash)(const ht_link_t *item);
|
---|
31 |
|
---|
32 | /** Returns the hash of the key. */
|
---|
33 | size_t (*key_hash)(const void *key);
|
---|
34 |
|
---|
35 | /** True if the items are equal (have the same lookup keys). */
|
---|
36 | bool (*equal)(const ht_link_t *item1, const ht_link_t *item2);
|
---|
37 |
|
---|
38 | /** Returns true if the key is equal to the item's lookup key. */
|
---|
39 | bool (*key_equal)(const void *key, const ht_link_t *item);
|
---|
40 |
|
---|
41 | /** Hash table item removal callback.
|
---|
42 | *
|
---|
43 | * Must not invoke any mutating functions of the hash table.
|
---|
44 | *
|
---|
45 | * @param item Item that was removed from the hash table.
|
---|
46 | */
|
---|
47 | void (*remove_callback)(ht_link_t *item);
|
---|
48 | } hash_table_ops_t;
|
---|
49 |
|
---|
50 | /** Hash table structure. */
|
---|
51 | typedef struct {
|
---|
52 | hash_table_ops_t *op;
|
---|
53 | list_t *bucket;
|
---|
54 | size_t bucket_cnt;
|
---|
55 | size_t full_item_cnt;
|
---|
56 | size_t item_cnt;
|
---|
57 | size_t max_load;
|
---|
58 | bool apply_ongoing;
|
---|
59 | } hash_table_t;
|
---|
60 |
|
---|
61 | #define hash_table_get_inst(item, type, member) \
|
---|
62 | member_to_inst((item), type, member)
|
---|
63 |
|
---|
64 | extern bool hash_table_create(hash_table_t *, size_t, size_t,
|
---|
65 | hash_table_ops_t *);
|
---|
66 | extern void hash_table_destroy(hash_table_t *);
|
---|
67 |
|
---|
68 | extern bool hash_table_empty(hash_table_t *);
|
---|
69 | extern size_t hash_table_size(hash_table_t *);
|
---|
70 |
|
---|
71 | extern void hash_table_clear(hash_table_t *);
|
---|
72 | extern void hash_table_insert(hash_table_t *, ht_link_t *);
|
---|
73 | extern bool hash_table_insert_unique(hash_table_t *, ht_link_t *);
|
---|
74 | extern ht_link_t *hash_table_find(const hash_table_t *, const void *);
|
---|
75 | extern ht_link_t *hash_table_find_next(const hash_table_t *, ht_link_t *,
|
---|
76 | ht_link_t *);
|
---|
77 | extern size_t hash_table_remove(hash_table_t *, const void *);
|
---|
78 | extern void hash_table_remove_item(hash_table_t *, ht_link_t *);
|
---|
79 | extern void hash_table_apply(hash_table_t *, bool (*)(ht_link_t *, void *),
|
---|
80 | void *);
|
---|
81 |
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /** @}
|
---|
85 | */
|
---|