10 replies
by marco
by jpereira
Yo hope you know C++ cause here is some C++ code for ya.
int Player::Hash(const std::string& name,int tablesize)
{
int hashvalue = 0;
for(int i = 0; i < name.length();i++)
hashvalue += name[i];//hash value summation of ascii value of key
//reduce hash value to be within 0 and the size of the table
return (hashvalue % tablesize);
}
This function takes the string and goes through it character by character converting each to its ASCII value and the sums thesevalues, until the each character is converted. Then using the size of the hash table get the modulus of the hash value this ensures that the hash value is between 0 and the size of the hash table
Hope i helped man..
by jpereira
Yo hope you know C++ cause here is some C++ code for ya.
int Player::Hash(const std::string& name,int tablesize)
{
int hashvalue = 0;
for(int i = 0; i < name.length();i++)
hashvalue += name[i];//hash value summation of ascii value of key
//reduce hash value to be within 0 and the size of the table
return (hashvalue % tablesize);
}
This function takes the string and goes through it character by character converting each to its ASCII value and the sums thesevalues, until the each character is converted. Then using the size of the hash table get the modulus of the hash value this ensures that the hash value is between 0 and the size of the hash table
Hope i helped man..
by jpereira
by ahicks
The best place to look is Knuth's Art of Computer Programming Vol 2 Searching and Sorting. He covers hashing in great detail.
It is important to note that he recommended filling up no more than 80% of the hash space and making the hash space a prime number of records for optimal results.
Generally, hash routines compute a running total of the each character and the product of a prime number. For example, 13,7,5, and 3 for a key that has a max of 4 characters. The modulo arithmetic is applied to the final sum to insure that it falls into the proper range. Using larger numbers for the first letters of the key assures maximum scattering and minimizes the number of collisions (i.e. two keys yielding the same hash value). The prime numberfor the number of records also reduces the possibility of collisions. Your choice of 4096 is not a good size since it is a power of 2.
by jpereira
by sam hobbs
See: http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/html/page220.html
It is part of a "book" called "Data Structures and Algorithms with Object-Oriented Design Patterns in Java" but it describes an algorithm that can be used in other languages too.
by jpereira
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending