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