using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
static class HashCode
{
// BKDR Hash Function
public static int Hash1(string str)
{
int seed = 131; // 31 131 1313 13131 131313 etc..
int hash = 0;
int count;
char[] bitarray=str .ToCharArray ();
count = bitarray.Length;
while (count>0)
{
hash = hash * seed + (bitarray [bitarray .Length -count ]);
count--;
}
return (hash & 0x7FFFFFFF);
}
//AP hash function
public static int Hash2(string str)
{
int hash = 0;
int i;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
for (i=0; i<count ; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (bitarray [i]) ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ (bitarray [i]) ^ (hash >> 5)));
}
count--;
}
return (hash & 0x7FFFFFFF);
}
//SDBM Hash function
public static int Hash3(string str)
{
int hash = 0;
int i;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
while (count >0)
{
// equivalent to: hash = 65599*hash + (*str++);
hash = (bitarray [bitarray .Length -count ]) + (hash << 6) + (hash << 16) - hash;
count--;
}
return (hash & 0x7FFFFFFF);
}
// RS Hash Function
public static int Hash4(string str)
{
int b = 378551;
int a = 63689;
int hash = 0;
int i;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
while (count >0)
{
hash = hash * a + (bitarray [bitarray .Length -count ]);
a *= b;
count--;
}
return (hash & 0x7FFFFFFF);
}
// JS Hash Function
public static int Hash5(string str)
{
int hash = 1315423911;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
while (count >0)
{
hash ^= ((hash << 5) + (bitarray [bitarray .Length -count ]) + (hash >> 2));
count--;
}
return (hash & 0x7FFFFFFF);
}
// P. J. Weinberger Hash Function
public static int Hash6(string str)
{
int BitsInUnignedInt = ( int)(sizeof( int) * 8);
int ThreeQuarters = ( int)((BitsInUnignedInt * 3) / 4);
int OneEighth = ( int)(BitsInUnignedInt / 8);
int hash = 0;
unchecked {
int HighBits = ( int)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth);
int test = 0;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
while (count >0)
{
hash = (hash << OneEighth) + (bitarray [bitarray .Length -count ]);
if ((test = hash & HighBits) != 0)
{
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
count--;
}
}
return (hash & 0x7FFFFFFF);
}
// ELF Hash Function
public static int Hash7(string str)
{
int hash = 0;
int x = 0;
int i;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
unchecked
{
while (count >0)
{
hash = (hash << 4) + (bitarray [bitarray .Length -count ]);
if ((x = hash & (int)0xF0000000) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
count--;
}
}
return (hash & 0x7FFFFFFF);
}
// DJB Hash Function
public static int Hash8(string str)
{
int hash = 5381;
int i;
int count;
char[] bitarray = str.ToCharArray();
count = bitarray.Length;
while (count >0)
{
hash += (hash << 5) + (bitarray [bitarray .Length -count ]);
count--;
}
return (hash & 0x7FFFFFFF);
}
}
}
在处理大数据统计时,运用了bloom filter方法,里面使用了如上的散列函数,我只能说,内存确实降下来了。精确率确实还不错。但是,散列函数怎么会那么耗时啊。
分享到:
相关推荐
C#实现字符串SHA-256加密算法,非常好用,供大家学习参考。
散列函数hash 基于SHA-1。MATLAB可运行实现。
该文档中包含 bloomFilter过滤器中用到的对于字符串进行hash的hash函数共十一个,并带有测试程序..
Hash函数集合,包含主流的hash函数: nginx_hash算法,OpenSSL_hash算法,RSHash,JSHash,PJWHash,ELFHash,BKDRHash,DJBHash,DEKHash,APHash等等!
hash算法C代码实现 标准接口函数 方便修改hash函数
最快的排序算法 javahash实现-Java-哈希算法-最快的实现,排序算法数据结构
md5和sha-1算法代码及验证文件 在vc6.0调试运行无误,
这是一个java版的bloomFilter Hash函数集,并带有测试程序。在我的资源里还有一个c版的,函数功能相同,在我的应用中具有良好表现。
采用java实现的常用hash算法归总。
1)利用C\C++语言实现DSA算法。 2)DSA中的Hash函数采用SHA算法。 (1)消息填充:因为我们存储的时候是以字节为单位存储的,所以消息的长度(单位:位)一定是 8 的倍数。而我们填充的时候也一定是 8 位、8 位...
webqq最新hash算法c#版,20160218完成,由webqq的js算法翻译而来。 计算结果完全一致,请放心使用。 /// /// QQ hash算法 /// /// <param name="b">QQ号 /// <param name="j">cookie的ptwebqq值 /// <returns>hash...
这是几种经典的Hash算法的实现(源代码),里面源代码和文字解说都有
C语言实现hash算法源码,实现了sha256,sha384,sha512三种哈希算法,项目中用到的,提取出来测试使用的。
Hash算法的介绍,属于课件形式,图文并茂介绍Hash的原理和应用。
最快的排序算法 最快的内容查找算法-----暴雪的Hash算法,排序算法数据结构
1993 年美国国家标准局(NIST)公布了安全散列算法SHA,SHA 已经被美国政府核准作为标准,即FIPS 180 Secure Hash Standard (SHS),FIPS 规定必须用SHS 实施数字签名算法,该算法主要是和数字签名算法(DSA)配合的...
Hash函数是提供数据完整性保障的一个重要工具。本次实验,我们希望通过上机操作,使同学们对安全Hash算法SHA-1的基本原理有一个全面的理解。通过本次实验,使学生掌握对Hash函数的应用,为后面数字签名方案的学习...
Hash函数是提供数据完整性保障的一个重要工具。本次实验,我们希望通过上机操作,使 同学们对安全Hash算法SHA-1的基本原理有一个全面的理解。通过本次实验,使学生掌握对 Hash函数的应用,为后面数字签名方案的学习...
【数据结构】散列函数
本程序主要是BloomFilter算法的简化实现 因为C#非安全代码无法直接分配内存块,使用了int型数组代替,暂时为了简单没有使用位运算,比位运算消耗内存多16倍。 算法原理: 其首先申请一块大内存,并把内存中...