Cuckoo Hash和多级Hash的粗浅认识

 

通过对Cuckoo Hash、多级Hash和BloomFilter的粗浅了解,感觉它们三者存在类似之处,算是近亲(暂且把普通的Hash称作远亲)。

 

Cuckoo Hash的思想非常简单,冲突时,重Hash,也就是为Key重新找个新的位置。显然,极端情况下,需要反反复复找位置,效率低。为了减少这个过程,Cuckoo Hash的实现一般引入了两个数组,这样只有在其中一个数组中不存在,就不会重新找位置。

 

对于Cuckoo Hash的实现有一个小疑问:Google/Baidu出的介绍或实现,都是将已存在的踢出来,但感觉为新插入的找个位置,貌似也没有问题,除非考虑到新插入的可能是热点,暂没能想出更好的理由。

 

多级Hash弱化了这个问题,它引入了更多的数组,比如20个,第一个位置被占了,就试第二个位置,依次类推,级数够多,最终能找到存放位置的概率就很高。但是也带来了另一个问题:太多级数,也会导致效率下降,因为每次都需要遍历级数次。常规的实现中,一般不同级的桶数会设定不同,一般从1级往后递减。

 

BloomFilter的用途和Cuckoo Hash、多级Hash明显不同,但同样通过多个数组来降低冲突概率,所以说它们很亲。

 

总的来说,这些思想都非常简单,而且很实用。而GoogleSparseHash则是另一种思想,省内存效率又不错,可以看作是虚拟化的思想,但它不适合用于共享内存这样一次性分配内存的场景。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++实现Cuckoo Hash的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; const int TABLE_SIZE = 11; const int MAX_RETRIES = 500; class CuckooHash { private: vector<int> table1; vector<int> table2; public: CuckooHash() { table1.resize(TABLE_SIZE); table2.resize(TABLE_SIZE); } int hash1(int key) { return key % TABLE_SIZE; } int hash2(int key) { return (key / TABLE_SIZE) % TABLE_SIZE; } void insert(int key) { int count = 0; while (count < MAX_RETRIES) { int index1 = hash1(key); int index2 = hash2(key); if (table1[index1] == key || table2[index2] == key) { cout << key << " already exists." << endl; return; } if (table1[index1] == 0) { table1[index1] = key; return; } else if (table2[index2] == 0) { table2[index2] = key; return; } else { int evictedKey = table1[index1]; table1[index1] = key; key = table2[index2]; table2[index2] = evictedKey; count++; } } cout << "Insertion failed." << endl; } void remove(int key) { int index1 = hash1(key); int index2 = hash2(key); if (table1[index1] == key) { table1[index1] = 0; } else if (table2[index2] == key) { table2[index2] = 0; } else { cout << key << " does not exist." << endl; } } bool search(int key) { int index1 = hash1(key); int index2 = hash2(key); return table1[index1] == key || table2[index2] == key; } }; int main() { CuckooHash hash; hash.insert(10); hash.insert(22); hash.insert(37); hash.insert(40); hash.insert(50); hash.insert(60); hash.remove(22); hash.remove(40); hash.insert(70); hash.insert(80); cout << "Search for 10: " << (hash.search(10) ? "Found" : "Not Found") << endl; cout << "Search for 37: " << (hash.search(37) ? "Found" : "Not Found") << endl; cout << "Search for 50: " << (hash.search(50) ? "Found" : "Not Found") << endl; cout << "Search for 80: " << (hash.search(80) ? "Found" : "Not Found") << endl; cout << "Search for 90: " << (hash.search(90) ? "Found" : "Not Found") << endl; return 0; } ``` 代码中使用了两个哈希表作为Cuckoo Hash的基础数据结构,其中使用了两个不同的哈希函数来计算每个键的两个哈希值。在插入键时,如果一个键已经存在于两个哈希表中的任何一个中,则插入操作失败。如果两个哈希表中有一个空闲位置,则将键插入其中一个哈希表中。如果两个哈希表中都没有空闲位置,则使用Cuckoo Hash算法将其中一个哈希表中的键替换为新插入的键,同时将被替换的键移动到另一个哈希表中。在删除键时,如果一个键存在于哈希表中,则将其从哈希表中删除。在搜索键时,如果一个键存在于两个哈希表中的任何一个中,则返回true,否则返回false。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值