echo shar: extracting "'hash.c'" '(2203 characters)' sed 's/^X//' << \SHAR_EOF > 'hash.c' X/* Copyright (c) 1986, Greg McGary */ Xstatic char sccsid[] = "@(#)hash.c 1.1 86/10/09"; X Xchar *hashSearch(); Xint h1str(); Xint h2str(); X X/* X Look for `key' in the hash table starting at address `base'. X `base' is a table containing `nel' elements of size `width'. X The hashing strategy we use is open addressing. Apply the X primary hash function `h1' and the secondary hash function X `h2' when searching for `key' or an empty slot. `compar' X is the comparison function that should be used to compare X the key with an element of the table. It is called with two X arguments. The first argument is the address of the key, and X the second argument is the address of the hash table element X in question. `compar' should return 0 if the key matches the X element or the empty slot, and non-zero otherwise. X X If a pointer to a long is provided for `probes' we will keep X a running total of open addressing hash probes. X*/ Xchar * XhashSearch(key, base, nel, width, h1, h2, compar, probes) X char *key; /* key to locate */ X char *base; /* base of hash table */ X register int nel; /* number of elements in table */ X int width; /* width of each element */ X int (*h1)(); /* primary hash function */ X int (*h2)(); /* secondary hash function */ X int (*compar)(); /* key comparison function */ X long *probes; X{ X register int hash1; X register int hash2; X register char *slot; X X hash1 = (*h1)(key) % nel; X slot = &base[hash1 * width]; X X if (probes) X (*probes)++; X if ((*compar)(key, slot) == 0) X return slot; X X hash2 = (*h2)(key); X for (;;) { X hash1 = (hash1 + hash2) % nel; X slot = &base[hash1 * width]; X X if (probes) X (*probes)++; X if ((*compar)(key, slot) == 0) X return slot; X } X} X X#define ABS(n) ((n) < 0 ? -(n) : (n)) X X/* X A Primary hash function for string keys. X*/ Xint Xh1str(key) X register char *key; X{ X register int sum; X register int s; X X for (sum = s = 0; *key; s++) X sum += ((*key++) << s); X X return ABS(sum); X} X X/* X A Secondary hash function for string keys. X*/ Xint Xh2str(key) X register char *key; X{ X register int sum; X register int s; X char *keysav; X X keysav = key; X key = &key[strlen(key)]; X X for (sum = s = 0; key > keysav; s++) X sum += ((*--key) << s); X X return ABS(sum) | 1; X} SHAR_EOF if test 2203 -ne "`wc -c < 'hash.c'`" then echo shar: error transmitting "'hash.c'" '(should have been 2203 characters)' fi