2 * @file hashtable.h Simple hash table implementation.
4 * https://elixir.bootlin.com/linux/v5.18.12/source/include/linux/hashtable.h
5 * @author Lunaixsky (zelong56@gmail.com)
10 * @copyright Copyright (c) 2022
13 #ifndef __LUNAIX_HASHTABLE_H
14 #define __LUNAIX_HASHTABLE_H
17 #include <lunaix/ds/llist.h>
21 struct hlist_node* head;
24 #define __hashkey(table, hash) ((hash) % (sizeof(table) / sizeof(table[0])))
26 #define DECLARE_HASHTABLE(name, bucket_num) struct hbucket name[(bucket_num)];
28 #define hashtable_bucket_foreach(bucket, pos, n, member) \
29 for (pos = list_entry((bucket)->head, typeof(*pos), member); \
31 n = list_entry(pos->member.next, typeof(*pos), member); \
36 #define hashtable_hash_foreach(table, hash, pos, n, member) \
37 hashtable_bucket_foreach(&table[__hashkey(table, hash)], pos, n, member)
39 #define hashtable_init(table) \
41 for (u32_t i = 0; i < (sizeof(table) / sizeof(table[0])); i++) { \
46 #define hashtable_hash_in(table, list_node, hash) \
47 hlist_add(&table[__hashkey(table, hash)].head, list_node)
49 #endif /* __LUNAIX_HASHTABLE_H */