---
name: "wnhash"
section: "3"
platform: "linux"
package: "libwn-dev"
version: "6.0-17"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/wnhash"
---

# wnhash(3)

hash functions for common keys

## 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 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);
}
```

## SEE ALSO

[**wn\_mkhtab**(3)](/wn_mkhtab/3.md), [**wn\_mkhashhtab**(3)](/wn_mkhashhtab/3.md)

## AUTHOR

Will Naylor
