java

位置:IT落伍者 >> java >> 浏览文章

Hash算法大全(java实现)(2)


发布日期:2024年01月31日
 
Hash算法大全(java实现)

/**

* Thomas Wang的算法整数hash

*/

public static int intHash(int key)

{

key += ~(key << );

key ^= (key >>> );

key += (key << );

key ^= (key >>> );

key += ~(key << );

key ^= (key >>> );

return key;

}

/**

* RS算法hash

* @param str 字符串

*/

public static int RSHash(String str)

{

int b = ;

int a = ;

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash = hash * a + strcharAt(i);

a = a * b;

}

return (hash & xFFFFFFF);

}

/* End Of RS Hash Function */

/**

* JS算法

*/

public static int JSHash(String str)

{

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash ^= ((hash << ) + strcharAt(i) + (hash >> ));

}

return (hash & xFFFFFFF);

}

/* End Of JS Hash Function */

/**

* PJW算法

*/

public static int PJWHash(String str)

{

int BitsInUnsignedInt = ;

int ThreeQuarters = (BitsInUnsignedInt * ) / ;

int OneEighth = BitsInUnsignedInt / ;

int HighBits = xFFFFFFFF << (BitsInUnsignedInt OneEighth);

int hash = ;

int test = ;

for(int i = ; i < strlength();i++)

{

hash = (hash << OneEighth) + strcharAt(i);

if((test = hash & HighBits) != )

{

hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));

}

}

return (hash & xFFFFFFF);

}

/* End Of P J Weinberger Hash Function */

/**

* ELF算法

*/

public static int ELFHash(String str)

{

int hash = ;

int x = ;

for(int i = ; i < strlength(); i++)

{

hash = (hash << ) + strcharAt(i);

if((x = (int)(hash & xFL)) != )

{

hash ^= (x >> );

hash &= ~x;

}

}

return (hash & xFFFFFFF);

}

/* End Of ELF Hash Function */

/**

* BKDR算法

*/

public static int BKDRHash(String str)

{

int seed = ; // etc

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash = (hash * seed) + strcharAt(i);

}

return (hash & xFFFFFFF);

}

/* End Of BKDR Hash Function */

/**

* SDBM算法

*/

public static int SDBMHash(String str)

{

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash = strcharAt(i) + (hash << ) + (hash << ) hash;

}

return (hash & xFFFFFFF);

}

/* End Of SDBM Hash Function */

/**

* DJB算法

*/

public static int DJBHash(String str)

{

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash = ((hash << ) + hash) + strcharAt(i);

}

return (hash & xFFFFFFF);

}

/* End Of DJB Hash Function */

/**

* DEK算法

*/

public static int DEKHash(String str)

{

int hash = strlength();

for(int i = ; i < strlength(); i++)

{

hash = ((hash << ) ^ (hash >> )) ^ strcharAt(i);

}

return (hash & xFFFFFFF);

}

/* End Of DEK Hash Function */

/**

* AP算法

*/

public static int APHash(String str)

{

int hash = ;

for(int i = ; i < strlength(); i++)

{

hash ^= ((i & ) == ) ? ( (hash << ) ^ strcharAt(i) ^ (hash >> )) :

(~((hash << ) ^ strcharAt(i) ^ (hash >> )));

}

// return (hash & xFFFFFFF);

return hash;

}

/* End Of AP Hash Function */

/**

* JAVA自己带的算法

*/

public static int java(String str)

{

int h = ;

int off = ;

int len = strlength();

for (int i = ; i < len; i++)

{

h = * h + strcharAt(off++);

}

return h;

}

/**

* 混合hash算法输出位的值

*/

public static long mixHash(String str)

{

long hash = strhashCode();

hash <<= ;

hash |= FNVHash(str);

return hash;

}

}

               

上一篇:Java socket 入门编程实例

下一篇:java技巧之时间计算