C语言实现一个简易的Hash table(6)

上一章中,我们实现了Hash表中的插入搜索删除接口,我们在初始化hash表时固定了大小为53,为了方便扩展,本章将介绍如何修改hash表的大小。

设置Hash表大小

现在,我们的hash表是固定大小(53)的,当插入越来越多数据时,我们的hash表就会被插满,这个问题有两个原因:

  1. 哈希表的性能随着高冲突率而降低
  2. 我们的'hash表'只能存储固定数量的记录,如果我们存储更多,将无法插入数据

为了减少hash表被插满的情况发生,当插入很多数据时,我们可以增大hash表的大小,hash表中的count属性代表已经插入的数据条数,在每次插入和删除时,我们计算表的“负载”,或插入的数量和总的大小的比率,如果它高于或低于某些值,我们会减小或扩大hash表的大小。

我们定义如下规则:

  1. 如果负载>0.7,就扩大
  2. 如果负载<0.1,就缩小

要调整大小,我们创建一个大约是当前大小的一半或两倍的新哈希表,并将所有未删除的项插入其中。

我们的新hash表大小应该是大约是当前大小的两倍或一半的素数,找到新的hash表大小并非易事。为了确定hash表的大小,我们现设置一个最基本的大小,然后将实际大小定义为大于基本大小的第一个素数。扩大时,我们先将基本大小加倍,找到第一个更大的素数,然后作为hash表的大小,缩小时,我们将大小减半并找到下一个更大的素数。

我们先从基本大小50开始,我们使用最简单粗暴的方法通过检查每个连续数是否为素数来查找下一个素数。这个简单粗暴的方法看起来不是很理想,但是我们实际需要检查的值很少,并且花费的时间超过了重新散列表中每个项目所花费的时间。

首先,我们先定义一个函数用来找到下一个素数,prime.hprime.c的内容如下:

// prime.h
int is_prime(const int x);
int next_prime(int x);
// prime.c
#include <math.h>
#include "prime.h"

/*
 * Return whether x is prime or not
 *
 * Returns:
 *   1  - prime
 *   0  - not prime
 *   -1 - undefined (i.e. x < 2)
 */
int is_prime(const int x) {
    if (x < 2) { return -1; }
    if (x < 4) { return 1; }
    if ((x % 2) == 0) { return 0; }
    for (int i = 3; i <= floor(sqrt((double) x)); i += 2) {
        if ((x % i) == 0) {
            return 0;
        }
    }
    return 1;
}

/*
 * Return the next prime after x, or x if x is prime
 */
int next_prime(int x) {
    while (is_prime(x) != 1) {
        x++;
    }
    return x;
}

下一步,我们需要修改ht_new函数,使之可以在创建hash表时指定大小,为此我们要创建一个新的函数ht_new_sized,在ht_new中我们调用ht_new_sized并给我们的hash表一个默认大小:

// hash_table.c
static ht_hash_table* ht_new_sized(const int base_size) {
    ht_hash_table* ht = xmalloc(sizeof(ht_hash_table));
    ht->base_size = base_size;

    ht->size = next_prime(ht->base_size);

    ht->count = 0;
    ht->items = xcalloc((size_t)ht->size, sizeof(ht_item*));
    return ht;
}


ht_hash_table* ht_new() {
    return ht_new_sized(HT_INITIAL_BASE_SIZE);
}

现在一切准备就绪。在我们的设置hash表大小函数中,我们需要检查以确保我们没有将哈希表的大小减小到最小值以下,然后,我们初始化一个所需大小的新hash表,原表中所有非NULL或者未被删除的都会插入到新hash表中,然后我们在删除旧的hash表之前将属性赋值给新的hash表

// hash_table.c
static void ht_resize(ht_hash_table* ht, const int base_size) {
    if (base_size < HT_INITIAL_BASE_SIZE) {
        return;
    }
    ht_hash_table* new_ht = ht_new_sized(base_size);
    for (int i = 0; i < ht->size; i++) {
        ht_item* item = ht->items[I];
        if (item != NULL && item != &HT_DELETED_ITEM) {
            ht_insert(new_ht, item->key, item->value);
        }
    }

    ht->base_size = new_ht->base_size;
    ht->count = new_ht->count;

    // To delete new_ht, we give it ht's size and items 
    const int tmp_size = ht->size;
    ht->size = new_ht->size;
    new_ht->size = tmp_size;

    ht_item** tmp_items = ht->items;
    ht->items = new_ht->items;
    new_ht->items = tmp_items;

    ht_del_hash_table(new_ht);
}

为了简化设置大小,我们定义了两个函数:

// hash_table.c
static void ht_resize_up(ht_hash_table* ht) {
    const int new_size = ht->base_size * 2;
    ht_resize(ht, new_size);
}


static void ht_resize_down(ht_hash_table* ht) {
    const int new_size = ht->base_size / 2;
    ht_resize(ht, new_size);
}

要执行调整大小,我们先检查插入和删除时hash表上的负载。 如果它高于或低于0.7和0.1的预定义限制,我们分别调高或调低。

为了避免进行浮点运算,我们将计数乘以100,并检查它是高于还是低于7010

// hash_table.c
void ht_insert(ht_hash_table* ht, const char* key, const char* value) {
    const int load = ht->count * 100 / ht->size;
    if (load > 70) {
        ht_resize_up(ht);
    }
    // ...
}


void ht_delete(ht_hash_table* ht, const char* key) {
    const int load = ht->count * 100 / ht->size;
    if (load < 10) {
        ht_resize_down(ht);
    }
    // ...
}

上一章:实现接口
下一章:附录:其他碰撞处理的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C语言实现hash表: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 100 typedef struct { char* key; char* value; } HashNode; typedef struct { HashNode** nodes; } HashTable; unsigned int hash(const char* key) { unsigned int hash = 0; for (int i = 0; i < strlen(key); i++) { hash = hash * 31 + key[i]; } return hash; } HashTable* createHashTable() { HashTable* table = malloc(sizeof(HashTable)); table->nodes = calloc(TABLE_SIZE, sizeof(HashNode*)); return table; } void set(HashTable* table, const char* key, const char* value) { unsigned int index = hash(key) % TABLE_SIZE; HashNode* node = table->nodes[index]; while (node != NULL && strcmp(node->key, key) != 0) { node = node->next; } if (node == NULL) { node = malloc(sizeof(HashNode)); node->key = strdup(key); node->next = table->nodes[index]; table->nodes[index] = node; } node->value = strdup(value); } char* get(HashTable* table, const char* key) { unsigned int index = hash(key) % TABLE_SIZE; HashNode* node = table->nodes[index]; while (node != NULL && strcmp(node->key, key) != 0) { node = node->next; } if (node == NULL) { return NULL; } else { return node->value; } } int main() { HashTable* table = createHashTable(); set(table, "apple", "red"); set(table, "banana", "yellow"); set(table, "grape", "purple"); printf("apple is %s\n", get(table, "apple")); printf("banana is %s\n", get(table, "banana")); printf("grape is %s\n", get(table, "grape")); return 0; } ``` 这个hash表使用了链表来解决hash冲突的问题,每个节点包含一个键和一个值。使用`set`函数可以向hash表中添加一个键值对,使用`get`函数可以根据键获取对应的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值