#ifndef __SMCC_HASHTABLE_H__ #define __SMCC_HASHTABLE_H__ #include #include #include "vector.h" // 哈希表条目状态标记 typedef enum hash_table_entry_state { ENTRY_EMPTY, ENTRY_ACTIVE, ENTRY_TOMBSTONE } ht_entry_state_t; // 哈希表条目结构(不管理key/value内存) typedef struct hash_entry { const void* key; // 由调用者管理 void* value; // 由调用者管理 u32_t hash; // 预计算哈希值 ht_entry_state_t state; // 条目状态 } hash_entry_t; // 哈希表主体结构 typedef struct hash_table { vector_header(entries, hash_entry_t); // 使用vector管理条目 u32_t count; // 有效条目数(不含墓碑) u32_t tombstone_count; // 墓碑数量 u32_t (*hash_func)(const void* key); int(*key_cmp)(const void* key1, const void* key2); } hash_table_t; // WARN you need set hash_func and key_cmp before use void init_hashtable(hash_table_t* ht) ; void* hashtable_set(hash_table_t* ht, const void* key, void* value); void* hashtable_get(hash_table_t* ht, const void* key); void* hashtable_del(hash_table_t* ht, const void* key); void hashtable_destory(hash_table_t* ht); typedef int (*hash_table_iter_func)(const void* key, void* value, void* context); void hashtable_foreach(hash_table_t* ht, hash_table_iter_func iter_func, void* context); #endif // __SMCC_HASHTABLE_H__