refactor(hashtable): 简化哈希表初始化接口并优化文件打开模式

- 将哈希表初始化从两步设置改为一步完成,直接传递哈希函数和比较函数
- 重构scc_hashtable_init函数签名,接受函数指针参数
- 更新内部字段名从key_cmp为cmp_func以保持一致性
- 添加类型定义scc_hashtable_hash_func_t和scc_hashtable_equal_func_t
- 修改PAL层文件操作接口,使用枚举模式替代固定只读模式
- 更新IR上下文、预处理器宏表等组件使用新的初始化方式
- 移除AST头文件中未使用的语义分析回调类型定义
- 修复预处理器中的空指针访问问题
This commit is contained in:
zzy
2026-03-04 16:47:28 +08:00
parent 2c4b803058
commit 4015acd866
10 changed files with 48 additions and 51 deletions

View File

@@ -33,6 +33,9 @@ typedef struct scc_hashtable_entry {
scc_hashtable_entry_state_t state; /**< 当前条目状态 */
} scc_hashtable_entry_t;
typedef u32 (*scc_hashtable_hash_func_t)(const void *key);
typedef int (*scc_hashtable_equal_func_t)(const void *key1, const void *key2);
/**
* @struct scc_hashtable_t
* @brief 哈希表主体结构
@@ -48,14 +51,14 @@ typedef struct scc_hashtable {
* @param key 键指针
* @return 32位无符号哈希值
*/
u32 (*hash_func)(const void *key);
scc_hashtable_hash_func_t hash_func;
/**
* @brief 键比较函数指针
* @param key1 第一个键指针
* @param key2 第二个键指针
* @return 相同返回0不同返回非0
*/
int (*key_cmp)(const void *key1, const void *key2);
scc_hashtable_equal_func_t cmp_func;
} scc_hashtable_t;
/**
@@ -64,7 +67,9 @@ typedef struct scc_hashtable {
*
* @warning 必须设置hash_func和key_cmp后才能使用
*/
void scc_hashtable_init(scc_hashtable_t *ht);
void scc_hashtable_init(scc_hashtable_t *ht,
scc_hashtable_hash_func_t hash_func,
scc_hashtable_equal_func_t cmp_func);
/**
* @brief 插入/更新键值对

View File

@@ -4,11 +4,16 @@
#define SCC_INIT_HASHMAP_SIZE (32)
#endif
void scc_hashtable_init(scc_hashtable_t *ht) {
void scc_hashtable_init(scc_hashtable_t *ht,
scc_hashtable_hash_func_t hash_func,
scc_hashtable_equal_func_t cmp_func) {
scc_vec_init(ht->entries);
ht->count = 0;
ht->tombstone_count = 0;
Assert(ht->key_cmp != NULL && ht->hash_func != NULL);
ht->cmp_func = cmp_func;
ht->hash_func = hash_func;
Assert(ht->cmp_func != null && ht->hash_func != null);
}
static int next_power_of_two(int n) {
@@ -40,7 +45,7 @@ static scc_hashtable_entry_t *find_entry(scc_hashtable_t *ht, const void *key,
if (entry->state == ENTRY_TOMBSTONE) {
if (!tombstone)
tombstone = entry;
} else if (entry->hash == hash && ht->key_cmp(entry->key, key) == 0) {
} else if (entry->hash == hash && ht->cmp_func(entry->key, key) == 0) {
return entry;
}

View File

@@ -1,9 +1,8 @@
#include <scc_strpool.h>
void scc_strpool_init(scc_strpool_t *pool) {
pool->ht.hash_func = (u32 (*)(const void *))scc_strhash32;
pool->ht.key_cmp = (int (*)(const void *, const void *))scc_strcmp;
scc_hashtable_init(&pool->ht);
scc_hashtable_init(&pool->ht, (scc_hashtable_hash_func_t)scc_strhash32,
(scc_hashtable_equal_func_t)scc_strcmp);
}
const char *scc_strpool_intern(scc_strpool_t *pool, const char *str) {