分享
 
 
 

摘自PHP的HASH算法实现

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*

* 头文件

* 说明:此实现不保存对象,只保存对象的引用

*/

#ifndef _HASH_H

#define _HASH_H

#ifdef __cplusplus

extern "C" {

#endif

typedef struct tagHASHBUCKET HASHBUCKET, *LPHASHBUCKET;

typedef struct tagHASHTABLE HASHTABLE, *LPHASHTABLE;

struct tagHASHBUCKET

{

unsigned long h;

unsigned int key_length;

void *data;

LPHASHBUCKET next;

LPHASHBUCKET previous;

LPHASHBUCKET conflict_next;

LPHASHBUCKET conflict_previous;

char key[1];

};

typedef unsigned long (*hash_func_t)(char *, unsigned int);

struct tagHASHTABLE

{

unsigned int table_size;

unsigned int size_index;

unsigned int elements;

hash_func_t hash;

LPHASHBUCKET p;

LPHASHBUCKET head;

LPHASHBUCKET tail;

LPHASHBUCKET *buckets;

};

extern int hash_create(LPHASHTABLE, unsigned int, hash_func_t);

extern int hash_entry(LPHASHTABLE, char *, unsigned int, void *);

extern int hash_find(LPHASHTABLE, char *, unsigned int, void **);

extern int hash_update(LPHASHTABLE, char *, unsigned int, void *);

extern int hash_remove(LPHASHTABLE, char *, unsigned int);

extern int hash_destroy(LPHASHTABLE);

#ifdef __cplusplus

};

#endif

#endif

/*

* HASH实现

*/

#include <stdlib.h>

#include <memory.h>

#include "main.h"

static unsigned int size_table[] =

{5, 11, 19, 53, 107, 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987, 262237, 524521, 1048793, 2097397, 41941

03, 8388857, 16777447, 33554201, 67108961, 134217487, 268435697, 536870683, 1073741621, 2147483399};

#define COUNTS_OF_SIZE_TABLE (sizeof(size_table) / sizeof(size_table[0]))

static unsigned long

hashpjw(char *key, unsigned int key_length)

{

unsigned long h, g;

char *p;

h = 0;

p = key + key_length;

while (key < p)

{

h = (h << 4) + *key++;

if ((g = (h & 0xF0000000)))

{

h = h ^ (g >> 24);

h = h ^ g;

}

}

return h;

}

static int

hash_do_rehash(LPHASHTABLE pht)

{

LPHASHBUCKET p;

unsigned int index;

memset(pht->buckets, 0, sizeof(LPHASHBUCKET) * size_table[pht->size_index]);

for (p = pht->head; p; p = p->next)

{

index = p->h % pht->table_size;

p->conflict_next = 0;

p->conflict_previous = pht->buckets[index];

if (p->conflict_previous)

{

p->conflict_previous->conflict_next = p;

}

pht->buckets[index] = p;

}

return 0;

}

static int

hash_do_resize(LPHASHTABLE pht)

{

LPHASHBUCKET *pp;

if (pht->size_index < (unsigned int)COUNTS_OF_SIZE_TABLE - 1)

{

pp = (LPHASHBUCKET *)realloc(pht->buckets, size_table[pht->size_index + 1] * sizeof(LPHASHBUCKET));

if (pp)

{

pht->buckets = pp;

pht->size_index++;

pht->table_size = size_table[pht->size_index];

hash_do_rehash(pht);

return 0;

}

return -1;

}

return 0;

}

int

hash_create(LPHASHTABLE pht, unsigned int size, hash_func_t hash)

{

int i;

for (i = 0; i < COUNTS_OF_SIZE_TABLE; ++i)

{

if (size <= size_table[i])

{

size = size_table[i];

pht->size_index = i;

break;

}

}

if (i == COUNTS_OF_SIZE_TABLE)

{

size = size_table[COUNTS_OF_SIZE_TABLE - 1];

pht->size_index = COUNTS_OF_SIZE_TABLE - 1;

}

pht->buckets = (LPHASHBUCKET *)calloc(size, sizeof(LPHASHBUCKET));

if (!pht->buckets)

{

return -1;

}

pht->hash = hash? hash: hashpjw;

pht->elements = 0;

pht->head = 0;

pht->p = 0;

pht->tail = 0;

pht->table_size = size;

return 0;

}

int

hash_entry(LPHASHTABLE pht, char *key, unsigned int key_length, void *data)

{

unsigned long h;

unsigned int index;

LPHASHBUCKET p;

h = pht->hash(key, key_length);

index = h % pht->table_size;

for (p = pht->buckets[index]; p; p = p->conflict_previous)

{

if (p->h == h && p->key_length == key_length)

{

if (!memcmp(p->key, key, key_length))

{

return -1;

}

}

}

p = (LPHASHBUCKET)malloc(sizeof(HASHBUCKET) - 1 + key_length);

if (!p)

{

return -1;

}

memcpy(p->key, key, key_length);

p->key_length = key_length;

p->h = h;

p->data = data;

p->conflict_next = 0;

p->conflict_previous = pht->buckets[index];

if (p->conflict_previous)

{

p->conflict_previous->conflict_next = p;

}

p->previous = pht->tail;

p->next = 0;

pht->tail = p;

if (p->previous)

{

p->previous->next = p;

}

if (!pht->head)

{

pht->head = p;

}

pht->buckets[index] = p;

++pht->elements;

if (pht->elements > pht->table_size)

{

hash_do_resize(pht);

}

return 0;

}

int

hash_find(LPHASHTABLE pht, char *key, unsigned int key_length, void **data)

{

unsigned long h;

unsigned int index;

LPHASHBUCKET p;

h = pht->hash(key, key_length);

index = h % pht->table_size;

for (p = pht->buckets[index]; p; p = p->conflict_previous)

{

if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))

{

*data = p->data;

return 0;

}

}

return -1;

}

int

hash_remove(LPHASHTABLE pht, char *key, unsigned int key_length)

{

unsigned long h;

unsigned int index;

LPHASHBUCKET p;

h = pht->hash(key, key_length);

index = h % pht->table_size;

for (p = pht->buckets[index]; p; p = p->conflict_previous)

{

if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))

{

if (p->conflict_previous)

{

p->conflict_previous->conflict_next = p->conflict_next;

}

if (p->conflict_next)

{

p->conflict_next->conflict_previous = p->conflict_previous;

}

if (p->previous)

{

p->previous->next = p->next;

}

if (p->next)

{

p->next->previous = p->previous;

}

if (pht->buckets[index] == p)

{

pht->buckets[index] = p->conflict_previous;

}

if (pht->head == p)

{

pht->head = p->next;

}

if (pht->tail == p)

{

pht->tail = p->previous;

}

--pht->elements;

free(p);

return 0;

}

}

return -1;

}

int

hash_update(LPHASHTABLE pht, char *key, unsigned int key_length, void *data)

{

unsigned long h;

unsigned int index;

LPHASHBUCKET p;

h = pht->hash(key, key_length);

index = h % pht->table_size;

for (p = pht->buckets[index]; p; p = p->conflict_previous)

{

if (p->h == h && p->key_length == key_length && !memcmp(p->key, key, key_length))

{

p->data = data;

return 0;

}

}

return -1;

}

int

hash_destroy(LPHASHTABLE pht)

{

LPHASHBUCKET p, q;

p = pht->head;

while (p)

{

q = p;

p = p->next;

free(q);

}

free(pht->buckets);

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有