quote:
Originally posted by miNus:
Dear God, is that ALL you think about!!??!?
Yes. And thanks for calling me God
DeadAnztac, implementing a hash is fairly straightforward. Essentially, you create an array of known size whose elements are a simple struct (forgive my C syntax, I'm pretty rusty) :
struct hashrec
{
char key[32];
void* data;
}
Then you define a hashing function :
unsigned int hash_func(char* key)
{
int keylen,genlen,count,return_value;
return_value = 0;
keylen = strlen(key);
// sum up all the characters in our key
for (count=0; count<keylen; count++)
{
return_value += key[count];
}
// return the modulus of our sum and our array size. For efficient hashing,
// HASH_ARRAY_SIZE should be a prime number
return_value = return_value \% HASH_ARRAY_SIZE;
return return_value;
}
Your array should be as large as the *maximum* value your hash function will ever return. For the function above, it will be HASH_ARRAY_SIZE - 1 :
struct hashrec hash_array[HASH_ARRAY_SIZE -1];
I'm just going to pseudo-code retrieval :
int hash_retrieve(char *key, char *data)
{
// figure out what our hash value is
int hash_array_offset = hash_func(key);
// Is our hashed struct null?
if (hash_array[hash_array_offset].key == NULL)
{
// yes, return failure
return 0;
}
// no. see if keys match
if (strcmp(hash_array[hash_array_offset].key,key) == 0)
{
// we found it. Copy out our data and return success.
copy_data(hash_array[hash_array_offset],data); // you'll have to define this
return 1;
}
// No match. Fall back on one of many collision resolution schemes.
}
Insertion is almost the same as retrieval. I'll leave it to you to figure out how to do it.
The only tricky part is collision resolution. With the hash function listed above, the keys 'fuck you' and 'you fuck' will both produce the same hash value, so if you try and insert those two keys into the hash you'll end up with a collision.
There are a lot of ways to resolve this - my favorite is to expand your hash_struct so it can become a linked list. After hashing, you search thru the list until you find a match or run off the end of the list.
Obviously there's more to hash implementation than just the above, but that should give you an idea of how they work at a fundamental level. Have fun!
This message has been edited by damien_s_lucifer on October 14, 2001 at 12:01 AM