【发布时间】:2018-05-27 14:26:28
【问题描述】:
我正在实现一个 Hashtable 来处理与 robin hood 散列的冲突。但是,以前我使用的是链接,插入近 100 万个密钥的过程几乎是瞬间完成的。罗宾汉散列不会发生同样的情况,我觉得这很奇怪,因为我觉得它更快。所以我想问的是我的插入功能是否正确实现。代码如下:
typedef struct hashNode{
char *word;
int freq; //not utilized in the insertion
int probe;// distance from the calculated index to the actual index it was inserted.
struct hashNode* next; //not utilized in the insertion
struct hashNode* base; //not utilized in the insertion
}hashNode;
typedef hashNode* hash_ptr;
hash_ptr hashTable[NUM_WORDS] = {NULL}; // NUM_WORDS = 1000000
// Number of actual entries = 994707
hash_ptr swap(hash_ptr node, int index){
hash_ptr temp = hashTable[index];
hashTable[index] = node;
return temp;
}
static void insertion(hash_ptr node,int index){
while(hashTable[index]){
if(node->probe > hashTable[index]->probe){
node = swap(node,index);
}
node->probe++;
index++;
if(index > NUM_WORDS) index = 0;
}
hashTable[index] = node;
}
将所有内容上下文化:
- 节点参数是新条目。
- index 参数是新条目所在的位置,如果它没有被占用的话。
【问题讨论】:
-
您在这两种情况下看到的时间是什么?你能把你正在使用的测试和测量代码也放上去吗?
-
@HoriaComan 我没有使用任何实际测量值。我所拥有的是,我知道两个哈希表何时完成。在链接中大约需要 1 秒。在罗宾汉中,它持续了 20 分钟,但仍未完成
-
对我来说听起来像是一个无限循环。使用调试器。打印出一些东西,就像每次你成功地向哈希表中添加一些东西一样。如果您有一段时间没有看到打印,请闯入并单步执行代码。
-
@so 我照你说的做了,我似乎没有陷入困境。它只是非常缓慢地添加条目。我知道这可能取决于很多因素,但是,robin hood 散列应该比链接更快吗?或者可能是代码中的问题?谢谢
标签: c algorithm hashtable probing