java - Universal Hashfunctions for string -
i trying implement 2 different universal hash functions strings. have problem hash value 0. can´t use hash function because want implement double hashing , have implement function: hash_func1(string s) + * hash_func2(string s) go through hash table. if 1 hash function 0 nothing changes , endless loop. collision detection in hash table. need 2 different universal hash functions doing that.
i have tried different hash functions cant find works.
can me problem?
this of functions have tried.
int h = 0 , r1 = 31415 , r2 = 27183; (int =0; < key.length (); ++) { h = ( r1 * h + key.charat ( )) % capacity ; r1 = r1 * r2 % (capacity -1); } return h ; or one
int seed = 131; long hash = 0; for(int = 0; < key.length(); i++) { hash = (hash * seed) + key.charat(i); } return (int) (hash % capacity);
the wikipedia article on double hashing suggests modify hash function avoid becomes zero, easiest way being add 1:
int h1 = hash_func1(s); int h2 = (hash_func2(s) % (capacity - 1)) + 1; // loop on (h1 + * h2) % capacity edit: oops, guess need bound capacity - 1, otherwise h2 == capacity, still run endless loop... or, better, have hash_func2() return value less capacity - 1, adding 1 sufficient.
Comments
Post a Comment