c說話完成的hashtable分享。本站提示廣大學習愛好者:(c說話完成的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 算法