随便贴一个libhash中的hash函数,写的貌似不错,贴出来玩玩。
hash.h
/*
*AustralianPublicLicenceB(OZPLB)
*
*Version1-0
*
*Copyright(c)2004NationalICTAustralia
*
*Allrightsreserved.
*
*Developedby:Embedded,Real-timeandOperatingSystemsProgram(ERTOS)
*NationalICTAustralia
*http://www.ertos.nicta.com.au
*/
#ifndef_HASH_H
#define_HASH_H
#include<stdint.h>
structhashtable{
structhashentry**table;
unsignedintsize;
structhashentry*spares;
};
structhashentry{
structhashentry*next;
uintptr_tkey;
void*value;
};
structhashtable*hash_init(unsignedintsize);
voidhash_free(structhashtable*tablestruct);
uintptr_thash_hash(uintptr_tkey);
void*hash_lookup(structhashtable*tablestruct,uintptr_tkey);
inthash_insert(structhashtable*tablestruct,uintptr_tkey,void*value);
voidhash_remove(structhashtable*tablestruct,uintptr_tkey);
#endif/*!_HASH_H*/
hash.c
#include<stdint.h>
#include<stdlib.h>
#include<assert.h>
#include"hash.h"
structhashtable*
hash_init(unsignedintsize)
{
structhashtable*tablestruct;
intcounter;
/*Ourhashfunctiononlyworkswithpower-of-2bucketsizesforspeed.*/
assert((size&(size-1))==0);
tablestruct=malloc(sizeof(structhashtable));assert(tablestruct);
if(!tablestruct){
returnNULL;
}
tablestruct->table=malloc(size*sizeof(structhashentry*));
if(!tablestruct->table){
returnNULL;
}
for(counter=0;counter<size;counter++){
tablestruct->table[counter]=NULL;
}
assert(tablestruct->table);
tablestruct->size=size;
tablestruct->spares=NULL;
returntablestruct;
}
/*Refhttp://www.concentric.net/~Ttwang/tech/inthash.htm*/
uintptr_t
hash_hash(uintptr_tkey)
{
#if(UINTPTR_MAX==UINT32_MAX)
key+=~(key<<15);
key^=(key>>10);
key+=(key<<3);
key^=(key>>6);
key+=~(key<<11);
key^=(key>>16);
#elif(UINTPTR_MAX==UINT64_MAX)
key+=~(key<<32);
key^=(key>>22);
key+=~(key<<13);
key^=(key>>8);
key+=(key<<3);
key^=(key>>15);
key+=~(key<<27);
key^=(key>>31);
#else
#errorunsupportedwordsize
#endif
//printf("newkeyis%d ",key);
returnkey;
}
void*
hash_lookup(structhashtable*tablestruct,uintptr_tkey)
{
uintptr_thash;
structhashentry*entry;
hash=hash_hash(key)&(tablestruct->size-1);
for(entry=tablestruct->table[hash];entry!=NULL;entry=entry->next){
if(entry->key==key){
returnentry->value;
}
}
returnNULL;
}
/*Addthekeytothehashtable.Assumesthekeyisnotalreadypresent.*/
int
hash_insert(structhashtable*tablestruct,uintptr_tkey,void*value)
{
uintptr_thash;
structhashentry*entry;
hash=hash_hash(key)&(tablestruct->size-1);
//printf("bucketis%d ",hash);
entry=malloc(sizeof(structhashentry));
if(!entry){
return-1;
}
entry->key=key;
entry->value=value;
entry->next=tablestruct->table[hash];
tablestruct->table[hash]=entry;
return0;
}
/*Removesthekeyfromthehashtable.Doesnotsignalanerrorifthekey
*wasnotpresent.*/
void
hash_remove(structhashtable*tablestruct,uintptr_tkey)
{
uintptr_thash;
structhashentry*entry,*tmpentry;
hash=hash_hash(key)&(tablestruct->size-1);
entry=tablestruct->table[hash];
/*Ifthisisthefirstentrythenitneedsspecialhandling.*/
if(entry&&entry->key==key){
tmpentry=entry->next;
free(entry);
tablestruct->table[hash]=tmpentry;
}else{
while(entry){
if(entry->next&&entry->next->key==key){
tmpentry=entry->next;
entry->next=entry->next->next;
free(tmpentry);
break;
}
entry=entry->next;
}
}
}
hash_free.c
#include<stdint.h>
#include<stdlib.h>
#include"hash.h"
void
hash_free(structhashtable*tablestruct)
{
intcounter;
structhashentry*entry,*prev;
/*Needtofreebucketsandtablestructandeveryitemineverychain*/
for(counter=0;counter<tablestruct->size;counter++){
entry=tablestruct->table[counter];
while(entry){
prev=entry;
entry=entry->next;
free(prev);
}
}
free(tablestruct->table);
free(tablestruct);
}
来源:http://ertos.nicta.com.au/software/kenge/libhash/devel/
更多结果:http://www.google.cn/search?complete=1&hl=zh-CN&q=libhash&meta=
分享到:
相关推荐
毕业论文:哈希函数的构造方法,仅供参考。毕业论文 哈希函数
目前比较有名的哈希函数 C语言 数据结构
如今越来越多的物联网设备带来了对哈希函数的需求,而传统的哈希函数又因为资源受限而不能直接应用,所以必须得针对该类设备重新设计,提出了一种新的轻量哈希函数HBL(Hash Function Based on LEA),它采用了主流...
用C语言实现常用的字符串哈希函数,比如RSHash、JSHash、PJWHash、FNVHash等
用C语言实现MD5哈希函数,它是将文件的每一行进行MD5加密,输出一个128位的哈希值。
用C++实现的完美哈希函数,打印C语言的32个关键字的哈希值,并且判断所输入的字符串是否为关键字
实验设计优化字符串哈希函数 比较经典字符串哈希函数 采用斐波那契函数思想
哈希函数与数据完整性
哈希函数设计与分析.pdf 在现代密码学中,哈希函数扮演着非常重要的角色。它不仅在安 全通信中起着重要的作用,而且是许多密码算法和协议的基本结构模 块。密码学哈希函数又被称为单向散列函数,它可以将任意长度的...
平时收集的一些哈希函数,用于多种不同的环境
哈希查找: 1、 哈希表类的哈希函数采用...2、 解决哈希冲突的函数采用开放定址法中的线性探察法。 3、 建立一个由10个数据元素组成的集合; 4、 测试哈希表长度m=13和m=11两种情况下的哈希表,并查找其中的几个元素。
哈希函数&MD5.doc
数据结构中的哈希表,用c++写的,通过老师验证
哈希函数中存在的一个问题就是函数值会打乱记录原本的顺序。你可以通过 ORDER BY来解决这个问题;但是,在很多情况下,记录数量是非常庞大的。在Oracle 10g 中,你可以将一个数据定义为“natural order” ,那么就...
完美哈希函数;可以参考一下。改一下哈希函数的公式就变成自己的东西啦
哈希函数和数字签名概述.pdf
位置保留哈希函数的实现伪代码,在范围查询中采用该方法或许可以用到
大学课程、数据结构、C代码、设定哈希函数 H(key) = key MOD 11 ( 表长=11 ),输入一组关键字序列,根据线性探测再散列解决冲突的方法建立哈希表的存储结构,显示哈希表,任意输入关键字,判断是否在哈希表中。
设定哈希函数 H(key) = key MOD 11 ( 表长=11 ),输入一组关键字序列,根据线性探测再散列解决冲突的方法建立哈希表的存储结构,显示哈希表,任意输入关键字,判断是否在哈希表中。
易语言PHP哈希函数源码,PHP哈希函数,HashPJW