wn_cumhashu3
Hash functions for common keys
Part of wnhash(3).
Usage
int wn_strhash(char
s
[]
)
int wn_ptrhash(ptr
p)
int wn_inthash(int
i)
Overview
A <hash function> returns a pseudo-random integer which depends on a <key> argument. The hash functions provided here are very fast and produce very random looking results.
wn_strhash hashes a string.
wn_ptrhash hashes a pointer.
wn_inthash hashes an int.
wn_memhash hashes a memory block of size len. One can use it to hash structures.
The above functions can be assembled to produce hash functions for more complicated data structures, possibly using wn_cumhasho and wn_cumhashu as glue.
wn_cumhasho is designed to produce a joint hash value from several base hash values. The base hash values are passed in hash; the result is accumulated in <accum>. The resulting hash value DEPENDS very heavily on the ORDER of accumulation.
wn_cumhashu is designed to produce a joint hash value from several base hash values. The base hash values are passed in hash; the result is accumulated in <accum>. The resulting hash value does NOT DEPEND on the ORDER of accumulation.
If you are hashing together objects of different types, use for glue the C bit exclusive-or operator <^> instead of the above functions.
Examples
The following function produces a hash value for a list of strings. The result depends very heavily on the order of the list.
int string_list_hash(list)
wn_sll list;
{
wn_sll el;
int hash;
char *string;
hash = 0;
for(el = list;el != NULL;el=el->next)
{
string = el->contents;
wn_cumhasho(&hash,wn_strhash(string));
}
return(hash);
}
The following function produces a hash value for a set of strings. The result does not depend on the order in which the hash calls happen to occur.
int string_set_hash(wn_sll list)
wn_sll list;
{
wn_sll el;
int hash;
char *string;
hash = 0;
for(el=list;el!=NULL;el=el->next)
{
string = el->contents;
wn_cumhashu(&hash,wn_strhash(string));
}
return(hash);
}
History
- Debian 4.0 (Etch) to Debian 3.0 (Woody): the text shown here
- Debian 2.2 (Potato) to Debian 2.1 (Slink)