需要帮助理解和调整此算法的哈希表

时间:2014-07-16 12:30:12

标签: c algorithm hashtable

我正在实现哈希表作为练习,并且需要创建一些函数,当负载超过1.5时,或者当哈希表中的条目数/桶数超过1.5时,将扩大哈希映射

我找到了哈希表的其他实现。但是,不同之处在于我使用指向单元格(在运行时动态创建)的存储桶,这些存储块全部链接到彼此作为链接列表。该实现使用具有预先分配的结构的哈希表。

所以它看起来有点像[斗] - > [记忆细胞] - > [记忆细胞] - > [记忆细胞]

以下是找到的实现:

static void expandIfNecessary(Hashmap* map) {
// If the load factor exceeds 0.75...
if (map->size > (map->bucketCount * 3 / 4)) {
    // Start off with a 0.33 load factor.
    size_t newBucketCount = map->bucketCount << 1;
    Entry** newBuckets = calloc(newBucketCount, sizeof(Entry*));
    if (newBuckets == NULL) {
        // Abort expansion.
        return;
    }

    // Move over existing entries.
    size_t i;
    for (i = 0; i < map->bucketCount; i++) {
        Entry* entry = map->buckets[i];
        while (entry != NULL) {
            Entry* next = entry->next;
            size_t index = calculateIndex(newBucketCount, entry->hash);
            entry->next = newBuckets[index];
            newBuckets[index] = entry;
            entry = next;
        }
    }

    // Copy over internals.
    free(map->buckets);
    map->buckets = newBuckets;
    map->bucketCount = newBucketCount;
}
}

大部分内容适用,但本部分除外:

        while (entry != NULL) {
            Entry* next = entry->next;
            size_t index = calculateIndex(newBucketCount, entry->hash);
            entry->next = newBuckets[index];
            newBuckets[index] = entry;
            entry = next;
        }

据推测,我想确保链接列表分开,以便条目仍然正确连接。在理解这里发生的事情时,我也有点模糊。

我的尝试看起来像是:

         while (entry != NULL) { 
                    // use the pointer of this cell to get to the next cell, store this in a pointer called next
                    // hash the key of the current entry
                    ...

这是我开始不了解实施的部分,以及我不确定如何处理链接列表的部分。我该如何申请?你能解释一下如何适应它吗?

1 个答案:

答案 0 :(得分:0)

当散列散列时,散列表项会重新定位到新的存储桶/容器:

old_index = item->hash % old_bucket_count    
new_index = item->hash % new_bucket_count

为了提高性能,此实现将每个项目的哈希值存储在entry->hash中。 正在做的是取出一个项目,找到它的新索引(桶)并将其放在该桶的链接列表的前面。

相关问题