简介
哈希表(HashTable)又称为“散列表“,由包含集合中元素的哈希桶(Bucket)组成。
哈希函数(hash func)为根据索引键来返回数值哈希程序代码的算法。索引键(Key)是被存储对象的某些属性值(Value)。当对象加入至hashtable时,它存储在与对象哈希程序代码相符的哈希程序代码相关的Buket中。当在HashTable内搜寻值时,哈希程序代码会为该值产生,并且会搜寻与该哈希程序代码相关的Bucket。
哈希表的优势体现在于空间换时间上,在设计哈希表时需要注意以下情况:
1)Hash函数的选择,一个好的哈希函数可以均匀地将数据样本散列到表中;
2)冲突的解决方法,常用的冲突处理就是拉链法,即出现冲突时以链表的形式扩展;
分析
Hash表的定义:
struct hash_table {
unsigned hash_count; //hash 桶的个数
hash_reference referencer;//hash对象申请引用函数指针
hash_dereference dereferencer;//hash对象去引用函数指针
hash_comparator_t cmp;//hash key比较函数指针
unsigned (*do_hash)(const void *, unsigned, unsigned);// 由hash key计算出hash num
/* This must remain the last entry in this table. */
struct hash_bucket *buckets [1]; //hash桶结构体数组
};
Hash桶的定义
struct hash_bucket {
struct hash_bucket *next;//hash桶指针解决冲突的链地址法
const unsigned char *name; //name为hash key
unsigned len;//hash key的长度
hashed_object_t *value; //保存对象数据指针
};
new_hash_table函数为hash表分配初始内存:
rval = dmalloc(sizeof(struct hash_table) +
(extra * sizeof(struct hash_bucket *)), file, line);
struct hash_bucket *new_hash_bucket (file, line)函数为hash桶分配内存:
add hash元素的函数
void add_hash (table, key, len, pointer, file, line)
struct hash_table *table;
unsigned len;
const void *key;
hashed_object_t *pointer;
const char *file;
int line;
hash查找函数
int hash_lookup (vp, table, key, len, file, line);
hashno = (*table->do_hash)(key, len, table->hash_count); //由key得到hash number
for (bp = table -> buckets [hashno]; bp; bp = bp -> next) { //由hashno定位到hash桶//这里循环的意思是一个hashno对于多个hash桶了
if (len == bp -> len key的长度比较和值比较
&& !(*table->cmp)(bp->name, key, len)) {
if (table -> referencer) 增加vp的引用计数
(*table -> referencer) (vp, bp -> value,
file, line);
else
*vp = bp -> value;
return 1;
}
}
Hash 元素删除函数
void delete_hash_entry (table, key, len, file, line)
struct hash_table *table;
unsigned len;
const void *key;
const char *file;
int line;