[英]polynomial hash function java?
每當我嘗試生成一個索引值時,一個索引就會發生許多沖突。 在一個索引300處最高的碰撞而在第二個索引處最高的碰撞像10。我認為問題是,如果單詞的長度為1,2,3。 它們通常導致較小的功率,因此產生較小的數量。 結果將其添加到數量較小的存儲桶中。 是否有不發生這種情況的多邊形函數? 還是可以幫我解決這個問題?
public int GetHashCompress(String str ){
int ply=0;
double mathIt=0;
int size = str.length();
for(int j = 0 ; j< size ; j++){
double x0 = (double) str.charAt(j);
double firstStep = (int) Math.pow(31, (size-j))*x0;
mathIt = mathIt + firstStep ; // hash function +1 to increance the range to keep 33 to the power >1
}
//arrayOfRawHash.add(mathIt); // this is for testing it later
ply =(int) mathIt %numBucket;// this is where it is compressed
return ply;
}
Jenkin的“一次性一次哈希”可為較小的值提供良好的雪崩行為,並且非常易於編碼。
public static int oatHash(final String hashMe) {
return oatHash(hashMe.getBytes());
}
public static int oatHash(byte[] hashMe) {
int hash = 0;
for (byte aHashMe : hashMe) {
hash += aHashMe;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
您的哈希函數沖突,因為它很可怕。 31 ^ i for i = 0 to size,基本上每個步驟的i根都以31為根。31、31 * 31、31 * 31 * 31,這些不是停止碰撞或有效的質數雪崩級聯,這將花費大量時間來執行電源例程。 這些都是一遍又一遍相同數字的倍數。
private static long hash(long v) {
long hash = v;
long h = hash;
switch ((int) hash & 3) {
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return hash;
}
問題是我的壓縮功能。 它與壓縮功能的MAD方法完美配合。 感謝大家 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.