大量Hash算法的实现

Hash算法有很多很多种类。具体的可以参考之前我写的Hash算法的一些分析。本处给大家提供一个集合了很多使用的Hash算法的类,应该可以满足不少人的需要的:

/**
* Hash算法大全<br>
* 推荐使用FNV1算法
* @algorithm None
* @author Goodzzp 2006-11-20
* @lastEdit Goodzzp 2006-11-20 
* @editDetail Create
*/
public class HashAlgorithms
{
/**
* 加法hash
* @param key 字符串
* @param prime 一个质数
* @return hash结果
*/
public static int additiveHash(String key, int prime)
{
   int hash, i;
   for (hash = key.length(), i = 0; i < key.length(); i++)
    hash += key.charAt(i);
   return (hash % prime);
}

/**
* 旋转hash
* @param key 输入字符串
* @param prime 质数
* @return hash值
*/
public static int rotatingHash(String key, int prime)
{
   int hash, i;
   for (hash=key.length(), i=0; i<key.length(); ++i)
     hash = (hash<<4)^(hash>>28)^key.charAt(i);
   return (hash % prime);
//   return (hash ^ (hash>>10) ^ (hash>>20));
}

// 替代:
// 使用:hash = (hash ^ (hash>>10) ^ (hash>>20)) & mask;
// 替代:hash %= prime;


/**
* MASK值,随便找一个值,最好是质数
*/
static int M_MASK = 0x8765fed1;
/**
* 一次一个hash
* @param key 输入字符串
* @return 输出hash值
*/
public static int oneByOneHash(String key)
{
   int   hash, i;
   for (hash=0, i=0; i<key.length(); ++i)
   {
     hash += key.charAt(i);
     hash += (hash << 10);
     hash ^= (hash >> 6);
   }
   hash += (hash << 3);
   hash ^= (hash >> 11);
   hash += (hash << 15);
//   return (hash & M_MASK);
   return hash;
}

/**
* Bernstein's hash
* @param key 输入字节数组
* @param level 初始hash常量
* @return 结果hash
*/
public static int bernstein(String key)
{
   int hash = 0;
   int i;
   for (i=0; i<key.length(); ++i) hash = 33*hash + key.charAt(i);
   return hash;
}

//
//// Pearson's Hash
// char pearson(char[]key, ub4 len, char tab[256])
// {
//   char hash;
//   ub4 i;
//   for (hash=len, i=0; i<len; ++i) 
//     hash=tab[hash^key[i]];
//   return (hash);
// }

//// CRC Hashing,计算crc,具体代码见其他
// ub4 crc(char *key, ub4 len, ub4 mask, ub4 tab[256])
// {
//   ub4 hash, i;
//   for (hash=len, i=0; i<len; ++i)
//     hash = (hash >> 8) ^ tab[(hash & 0xff) ^ key[i]];
//   return (hash & mask);
// }

/**
* Universal Hashing
*/
public static int universal(char[]key, int mask, int[] tab)
{
   int hash = key.length, i, len = key.length;
   for (i=0; i<(len<<3); i+=8)
   {
     char k = key[i>>3];
     if ((k&0x01) == 0) hash ^= tab[i+0];
     if ((k&0x02) == 0) hash ^= tab[i+1];
     if ((k&0x04) == 0) hash ^= tab[i+2];
     if ((k&0x08) == 0) hash ^= tab[i+3];
     if ((k&0x10) == 0) hash ^= tab[i+4];
     if ((k&0x20) == 0) hash ^= tab[i+5];
     if ((k&0x40) == 0) hash ^= tab[i+6];
     if ((k&0x80) == 0) hash ^= tab[i+7];
   }
   return (hash & mask);
}

/**
* Zobrist Hashing
*/ 
public static int zobrist( char[] key,int mask, int[][] tab)
{
   int hash, i;
   for (hash=key.length, i=0; i<key.length; ++i)
     hash ^= tab[i][key[i]];
   return (hash & mask);
}

// LOOKUP3 
// 见Bob Jenkins(3).c文件

// 32位FNV算法
static int M_SHIFT = 0;
/**
* 32位的FNV算法
* @param data 数组
* @return int值
*/
    public static int FNVHash(byte[] data)
    {
        int hash = (int)2166136261L;
        for(byte b : data)
            hash = (hash * 16777619) ^ b;
        if (M_SHIFT == 0)
            return hash;
        return (hash ^ (hash >> M_SHIFT)) & M_MASK;
    }
    /**
     * 改进的32位FNV算法1
     * @param data 数组
     * @return int值
     */
    public static int FNVHash1(byte[] data)
    {
        final int p = 16777619;
        int hash = (int)2166136261L;
        for(byte b:data)
            hash = (hash ^ b) * p;
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        return hash;
    }
    /**
     * 改进的32位FNV算法1
     * @param data 字符串
     * @return int值
     */
    public static int FNVHash1(String data)
    {
        final int p = 16777619;
        int hash = (int)2166136261L;
        for(int i=0;i<data.length();i++)
            hash = (hash ^ data.charAt(i)) * p;
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        return hash;
    }

    /**
     * Thomas Wang的算法,整数hash
     */ 
    public static int intHash(int key)
    {
      key += ~(key << 15);
      key ^= (key >>> 10);
      key += (key << 3);
      key ^= (key >>> 6);
      key += ~(key << 11);
      key ^= (key >>> 16);
      return key;
    }
    /**
     * RS算法hash
     * @param str 字符串
     */
    public static int RSHash(String str)
    {
        int b    = 378551;
        int a    = 63689;
        int hash = 0;

       for(int i = 0; i < str.length(); i++)
       {
          hash = hash * a + str.charAt(i);
          a    = a * b;
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of RS Hash Function */

    /**
     * JS算法
     */
    public static int JSHash(String str)
    {
       int hash = 1315423911;

       for(int i = 0; i < str.length(); i++)
       {
          hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of JS Hash Function */

    /**
     * PJW算法
     */
    public static int PJWHash(String str)
    {
        int BitsInUnsignedInt = 32;
        int ThreeQuarters     = (BitsInUnsignedInt * 3) / 4;
        int OneEighth         = BitsInUnsignedInt / 8;
        int HighBits          = 0xFFFFFFFF << (BitsInUnsignedInt - OneEighth);
        int hash              = 0;
        int test              = 0;

       for(int i = 0; i < str.length();i++)
       {
          hash = (hash << OneEighth) + str.charAt(i);

          if((test = hash & HighBits) != 0)
          {
             hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
          }
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of P. J. Weinberger Hash Function */

    /**
     * ELF算法
     */
    public static int ELFHash(String str)
    {
        int hash = 0;
        int x    = 0;

       for(int i = 0; i < str.length(); i++)
       {
          hash = (hash << 4) + str.charAt(i);
          if((x = (int)(hash & 0xF0000000L)) != 0)
          {
             hash ^= (x >> 24);
             hash &= ~x;
          }
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of ELF Hash Function */

    /**
     * BKDR算法
     */
    public static int BKDRHash(String str)
    {
        int seed = 131; // 31 131 1313 13131 131313 etc..
        int hash = 0;

       for(int i = 0; i < str.length(); i++)
       {
          hash = (hash * seed) + str.charAt(i);
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of BKDR Hash Function */

    /**
     * SDBM算法
     */
    public static int SDBMHash(String str)
    {
        int hash = 0;

       for(int i = 0; i < str.length(); i++)
       {
          hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of SDBM Hash Function */

    /**
     * DJB算法
     */
    public static int DJBHash(String str)
    {
       int hash = 5381;

       for(int i = 0; i < str.length(); i++)
       {
          hash = ((hash << 5) + hash) + str.charAt(i);
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of DJB Hash Function */

    /**
     * DEK算法
     */
    public static int DEKHash(String str)
    {
        int hash = str.length();

       for(int i = 0; i < str.length(); i++)
       {
          hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
       }

       return (hash & 0x7FFFFFFF);
    }
    /* End Of DEK Hash Function */

    /**
     * AP算法
     */
    public static int APHash(String str)
    {
        int hash = 0;

       for(int i = 0; i < str.length(); i++)
       {
          hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str.charAt(i) ^ (hash >> 3)) :
                                   (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
       }

//       return (hash & 0x7FFFFFFF);
       return hash;
    }
    /* End Of AP Hash Function */
    
    /**
     * JAVA自己带的算法
     */
    public static int java(String str)
{
   int h = 0;
   int off = 0;
   int len = str.length();
   for (int i = 0; i < len; i++)
   {
    h = 31 * h + str.charAt(off++);
   }
   return h;
}
    
    /**
     * 混合hash算法,输出64位的值
     */
    public static long mixHash(String str)
    {
    long hash = str.hashCode();
    hash <<= 32;
    hash |= FNVHash1(str);
    return hash;
    }
}

时间: 2024-08-30 04:33:17

大量Hash算法的实现的相关文章

c++-C++计算哈密尔顿回路的优化算法的实现?请各位高手都来帮帮忙吧

问题描述 C++计算哈密尔顿回路的优化算法的实现?请各位高手都来帮帮忙吧 C++计算哈密尔顿回路的优化算法的实现?请各位高手都来帮帮忙吧 解决方案 http://wenku.baidu.com/link?url=Aue42qZXYxiqlYt5WJJ-rMyFkotcIy501YzLF2V1Eww1j17n7myWEj0Z7bNIPYZcqsmlBf9UMqfGRmn5Z6E3iHliGQaJPCLBnpG7pilNmVm

粒子群算法(5)-----标准粒子群算法的实现

标准粒子群算法的实现思想基本按照粒子群算法(2)----标准的粒子群算法的讲述实现.主要分为3个函数.第一个函数为粒子群初始化函数 InitSwarm(SwarmSize......AdaptFunc)其主要作用是初始化粒子群的粒子,并设定粒子的速度.位置在一定的范围内.本函数所采用的数据结构如下所示: 表ParSwarm记录的是粒子的位置.速度与当前的适应度值,我们用W来表示位置,用V来代表速度,用F来代表当前的适应度值.在这里我们假设粒子个数为N,每个粒子的维数为D. W1,1 W1,2 .

C++火车入轨算法的实现代码

 这篇文章主要介绍了C++火车入轨算法的实现代码,有需要的朋友可以参考一下 [问题描述]   某城市有一个火车站,铁轨铺设如图所示.有n节车厢从A方向驶入车站,按进站顺序编号为1~n.你的任务是让它们按照某种特定的顺序进入B方向的铁轨并驶出车站.为了重组车厢,你可以借助中转站C.这是一个可以停放任意多节车厢的车站,但由于末端封顶,驶入C的车厢必须按照相反的顺序驶出.对于每个车厢,一旦从A移入C,就不能再回到A了:一旦从C移入B,就不能回到C了.换句话说,在任意时刻,只有两种选择:A→C和C→B.

大神帮看一下这个算法的实现思路?

问题描述 大神帮看一下这个算法的实现思路? 字符串 = "a/b/c_d/e/f_m/n/k_o/p": 字符串的长度是不确定的,通过组合把字符串 拆分成a/d/m/o, a/d/m/p,a/d/n/o,... 依次这样组合. 解决方案 参考:http://bbs.csdn.net/topics/390951529 解决方案二: 非常感谢 ,算法不错

关于分页算法的实现 求助

问题描述 关于分页算法的实现 求助 1 2 3 ...6 下一页 这种分页算法是什么 小弟实在不会 求各位大神帮忙解答下 解决方案 这种分页插件,网上很多. 解决方案二: 可以用jquery自带的分页,只要设置几个属性,其他的都不用管

排序算法的实现及性能分析

排序算法的实现及性能分析 --(java版) 排序是对数据元素序列建立某种有序排列的过程.更确切的说,排序是把一个数据元素序列整理成按关键字递增(或递减)排列的过程. 不过首先,我们必须先解释一下关键字这个词.关键字是要排序的数据元素集合中的一个域,排序是以关键字为基准进行的.而关键字也分为主关键字和次关键字.对于要排序的数据元素集合来说,如果关键字满足数据元素值不同时,该关键字也不同,这样的关键字称为主关键字.不满足主关键字定义的就称为次关键字. 简单来说,排序分为内部排序和外部排序两种.内部

c-循环冗余校验(CRC)算法的实现

问题描述 循环冗余校验(CRC)算法的实现 循环冗余校验(CRC)算法的实现 1.设计要求 (1)利用结构体或数组模拟网络数据包结构. (2)编码实现CRC算法,并将得到的校验位附加到网络数据包相应的位置. (3)根据数据包的长度,随机生成一个数据包产生突变的位置,并对该位置的bit位模拟突变的产生. (4)重新利用CRC算法校验该数据包,并指出产生的结果. (5)CRC能够检出所有的错误吗?如果不能,你能构造出无法检错的实例吗? 2.课程设计报告内容 (1) 给出程序的流程图: (2) 给出程

数据结构-图的最短路径算法的实现

问题描述 图的最短路径算法的实现 设计内容: 设计校园平面图,所含景点不少于8个.以图中顶点表示学校内各景点,存放景点的名称.景点介绍信息等:以边表示路径,存放路径长度信息.要求将这些信息保存在文件graph.txt中,系统执行时所处理的数据要对此文件分别进行读写操作. 1.从文件graph.txt中读取相应数据, 创建一个图,使用邻接矩阵表示图 : 2.景点信息查询:为来访客人提供校园任意景点相关信息的介绍: 3.问路查询:为来访客人提供校园任意两个景点之间的一条最短路径 . 选做内容(对文件

VC++运功补偿算法的实现

问题描述 VC++运功补偿算法的实现 本人正在研究运动补偿算法,其中的钻石算法和六边形算法的实现,代码的实现有点点困难的,希望各位大神给点意见的.根本就不知道如何下手的,能给点点方向或者是要学习努力的东西吗?