wnhash(3)

NAME

wn_strhash, wn_ptrhash, wn_inthash, wn_memhash,
wn_cumhasho, wn_cumhashu - hash functions for common keys

SYNOPSIS

#include <wn/wnhash.h>
int wn_strhash(char s[])
int wn_ptrhash(ptr p)
int wn_inthash(int i)
int wn_memhash(ptr mem, int len)
wn_cumhasho(int *accum, int hash)
wn_cumhashu(int *accum, int hash)

DESCRIPTION

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 func
tions 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);
}

SEE ALSO

wn_mkhtab(3), wn_mkhashhtab(3)

AUTHOR

Will Naylor
WNLIB August 23, 1998
Copyright © 2010-2025 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout