服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - c语言实现的hashtable分享

c语言实现的hashtable分享

2021-01-14 16:25C语言教程网 C/C++

哈希表效率高,众所周知。应用广泛,php中大部分存储使用的都是hashtable,包括变量,数组…如何使用c语言实现hashtable呢,现提供自己的思路,如有不妥之处,敬请赐教

头文件 hashtable.h

 

复制代码 代码如下:

typedef struct _Bucket
{
    char *key;
    void *value;
    struct _Bucket *next;
} Bucket;

typedef struct _HashTable
{
    int size;
    int total;
    struct _Bucket *buckets;
} HashTable;

int hash_init(HashTable **ht);
int hash_find(HashTable *ht, char *key, void **result);
int hash_insert(HashTable *ht, char *key, void *value);
int hash_remove(HashTable *ht, char *key);
int hash_loop(HashTable *ht, void **result);
//int hash_index(HashTable *ht, char *key);
static unsigned int ELFHash(char *str, unsigned int length);

 

hashtable.c

 

复制代码 代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
#include "mempool.h"
#include "log.h"

#define SUCCESS 1
#define FAILED 0
#define HASH_LEN 5

int hash_init(HashTable **ht) {
    (*ht) = (HashTable *)malloc(sizeof(HashTable));
    if (NULL == ht) {
        write_log("HashTable init error");
        exit(1);
    }
    (*ht)->size = 0;
    (*ht)->total = HASH_LEN;
    Bucket *bucket = (Bucket *)malloc(sizeof(Bucket) * HASH_LEN);
    memset(bucket, 0, sizeof(sizeof(Bucket) * HASH_LEN));
    (*ht)->buckets = bucket;
    return SUCCESS;
}

int hash_insert(HashTable *ht, char *key, void *value) {
    if (ht->size >= ht->total) {
        ht->buckets = (Bucket *)realloc(ht->buckets, sizeof(Bucket) * (ht->size + HASH_LEN));
        ht->total = ht->size + HASH_LEN;
    }
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    int _tmpindex;
    char _tmpindexstr[20];
    while (NULL != bucket->value) {

        while (NULL != bucket->next) {
            if (strcmp(key, bucket->key) == 0) {
                memset(bucket->value, 0, sizeof(bucket->value));
                memcpy(bucket->value, value, sizeof(value));
                return SUCCESS;
            }
            bucket = bucket->next;
        }

        do {
            _tmpindex = abs(rand() - index);
            sprintf(_tmpindexstr, "%d", _tmpindex);
            _tmpindex = hash_index(ht, _tmpindexstr);
        } while (_tmpindex == index || ht->buckets[_tmpindex].value != NULL);

        index = _tmpindex;
        bucket->next = &ht->buckets[index];
        bucket = bucket->next;
    }

    bucket->key = (char *)malloc(sizeof(key));
    bucket->value = (void *)malloc(sizeof(value));
    memcpy(bucket->key, key, sizeof(key));
    memcpy(bucket->value, value, sizeof(value));
    bucket->next = NULL;
    ht->size ++;

    return SUCCESS;
}

int hash_find(HashTable *ht, char *key, void **result) {
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    if (NULL == bucket->value) {
        return FAILED;
    }

    while (strcmp(key, bucket->key)) {
        if (NULL != bucket->next) {
            bucket = bucket->next;
        } else {
            break;
        }
    }
    if (NULL == bucket->value || strcmp(key, bucket->key)) {
        return FAILED;
    }

    *result = bucket->value;
    return SUCCESS;

}

int hash_delete(HashTable *ht, char *key) {
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    if (NULL == bucket->value) {
        return FAILED;
    }

    while (strcmp(key, bucket->key)) {
        if (NULL != bucket->next) {
            bucket = bucket->next;
        } else {
            break;
        }
    }

    if (NULL == bucket->value || strcmp(key, bucket->key)) {
        return FAILED;
    }

    memset(bucket, 0, sizeof(Bucket));
    ht->size --;
    return SUCCESS;
}

void hash_status(HashTable *ht) {
    printf("Total Size:\t\t%d\n", ht->total);
    printf("Current Size:\t\t%d\n", ht->size);
}

int hash_index(HashTable *ht, char *key) {
    return ELFHash(key, ht->total);
}

// ELF Hash Function
static unsigned int ELFHash(char *str, unsigned int length){
    unsigned int hash = 0;
    unsigned int x = 0;

    while (*str)
    {
        hash = (hash << 4) + (*str++);//hash左移4位,把当前字符ASCII存入hash低四位。
        if ((x = hash & 0xF0000000L) != 0)
        {
            //如果最高的四位不为0,则说明字符多余7个,现在正在存第8个字符,如果不处理,再加下一个字符时,第一个字符会被移出,因此要有如下处理。
            //该处理,如果对于字符串(a-z 或者A-Z)就会仅仅影响5-8位,否则会影响5-31位,因为C语言使用的算数移位
            //因为1-4位刚刚存储了新加入到字符,所以不能>>28
            hash ^= (x >> 24);
            //上面这行代码并不会对X有影响,本身X和hash的高4位相同,下面这行代码&~即对28-31(高4位)位清零。
            hash &= ~x;
        }
    }
    //返回一个符号位为0的数,即丢弃最高位,以免函数外产生影响。(我们可以考虑,如果只有字符,符号位不可能为负)
    return (hash & 0x7FFFFFFF) % length;
}

 

其中key的映射使用的是 ELFHash 算法

延伸 · 阅读

精彩推荐
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30