[php]代码库
int hash_lookup(HashTable *ht, char *key, void **result)
{
int index = HASH_INDEX(ht, key);
Bucket *bucket = ht->buckets[index];
if(bucket == NULL) return FAILED;
// 查找这个链表以便找到正确的元素,通常这个链表应该是只有一个元素的,也就不用多次
// 循环。要保证这一点需要有一个合适的哈希算法,见前面相关哈希函数的链接。
while(bucket)
{
if(strcmp(bucket->key, key) == 0)
{
LOG_MSG("HashTable found key in index: %i with key: %s value: %p\n",
index, key, bucket->value);
*result = bucket->value;
return SUCCESS;
}
bucket = bucket->next;
}
LOG_MSG("HashTable lookup missed the key: %s\n", key);
return FAILED;
}