c语言实现的hashtable分享
更新时间:2020年4月25日 17:42 点击:2285
头文件 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语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
详解C语言中的rename()函数和remove()函数的使用方法
这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25- 这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
- 今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
- 很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
- 这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25
- 下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言之整数划分问题(递归法)实例代码的相关资料,需要的朋友可以参考下...2020-04-25
- 本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。...2020-04-25
C语言正则表达式详解 regcomp() regexec() regfree()用法详解
C语言处理正则表达式常用的函数有regcomp()、regexec()、regfree()和regerror(),这里就为大家介绍一下,需要的朋友可以参考一下啊...2020-04-25- 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对...2020-06-25