Update hashmap.c

慕容承霖
pqg5afj7f 6 months ago
parent 381a991f88
commit 1fc6f455bf

@ -4,22 +4,25 @@
#include <stdbool.h> #include <stdbool.h>
#include "types.h" #include "types.h"
#define TABLE_SIZE 10007 // Use a prime number for better distribution #define TABLE_SIZE 10007 // Use a prime number for better distribution
// 使用一个质数作为哈希表的大小,有助于更好的分布
// 定义哈希节点结构体
typedef struct HashNode { typedef struct HashNode {
uint64_t key; uint64_t key; // 哈希节点的键值
struct HashNode *next; struct HashNode *next;// 指向下一个哈希节点的指针
} HashNode; } HashNode;
// 定义哈希表结构体
typedef struct HashMap { typedef struct HashMap {
HashNode **table; HashNode **table;// 哈希表的数组,存储指向哈希节点的指针
} HashMap; } HashMap;
static HashMap *_hashmap; static HashMap *_hashmap;// 静态变量,存储哈希表的实例
// 重置哈希表,如果未初始化则初始化,如果已初始化则清空
void hashmap_reset() { void hashmap_reset() {
if (unlikely(!_hashmap)) { if (unlikely(!_hashmap)) {
@ -48,13 +51,13 @@ void hashmap_reset() {
} }
} }
// 哈希函数,计算键值的哈希索引
static inline unsigned int hash(uint64_t key) { static inline unsigned int hash(uint64_t key) {
return key % TABLE_SIZE; return key % TABLE_SIZE;
} }
// 搜索并添加键值如果type大于等于8则返回false
// type must be below 8 // type must be below 8
bool hashmap_search_and_add(uint8_t type, uint64_t key) { bool hashmap_search_and_add(uint8_t type, uint64_t key) {
@ -78,7 +81,7 @@ bool hashmap_search_and_add(uint8_t type, uint64_t key) {
return false; return false;
} }
// 搜索并添加指针类型的键值如果type大于等于8则返回false
// type must be below 8 // type must be below 8
bool hashmap_search_and_add_ptr(uint8_t type, u8 *key) { bool hashmap_search_and_add_ptr(uint8_t type, u8 *key) {
@ -90,7 +93,7 @@ bool hashmap_search_and_add_ptr(uint8_t type, u8 *key) {
} }
/* below is not used */ /* below is not used */
/* 下面的函数未使用 */
void hashmap_insert(uint64_t key) { void hashmap_insert(uint64_t key) {
unsigned int index = hash(key); unsigned int index = hash(key);

Loading…
Cancel
Save