xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/hsearch.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate /* Compile time switches:
340Sstevel@tonic-gate 
350Sstevel@tonic-gate    MULT - use a multiplicative hashing function.
360Sstevel@tonic-gate    DIV - use the remainder mod table size as a hashing function.
370Sstevel@tonic-gate    CHAINED - use a linked list to resolve collisions.
380Sstevel@tonic-gate    OPEN - use open addressing to resolve collisions.
390Sstevel@tonic-gate    BRENT - use Brent's modification to improve the OPEN algorithm.
400Sstevel@tonic-gate    SORTUP - CHAINED list is sorted in increasing order.
410Sstevel@tonic-gate    SORTDOWN - CHAINED list is sorted in decreasing order.
420Sstevel@tonic-gate    START - CHAINED list with entries appended at front.
430Sstevel@tonic-gate    DRIVER - compile in a main program to drive the tests.
440Sstevel@tonic-gate    DEBUG - compile some debugging printout statements.
450Sstevel@tonic-gate    USCR - user supplied comparison routine.
460Sstevel@tonic-gate */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate #include <limits.h>
50*722Smuffin #include <malloc.h>
51*722Smuffin #include <string.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define SUCCEED		0
540Sstevel@tonic-gate #define FAIL		1
550Sstevel@tonic-gate #define TRUE		1
560Sstevel@tonic-gate #define FALSE		0
570Sstevel@tonic-gate #define repeat		for(;;)
580Sstevel@tonic-gate #define until(A)	if(A) break;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #ifdef OPEN
610Sstevel@tonic-gate #    undef CHAINED
620Sstevel@tonic-gate #else
630Sstevel@tonic-gate #ifndef CHAINED
640Sstevel@tonic-gate #    define OPEN
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifdef MULT
690Sstevel@tonic-gate #    undef DIV
700Sstevel@tonic-gate #else
710Sstevel@tonic-gate #ifndef DIV
720Sstevel@tonic-gate #    define MULT
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifdef START
770Sstevel@tonic-gate #    undef SORTUP
780Sstevel@tonic-gate #    undef SORTDOWN
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate #ifdef SORTUP
810Sstevel@tonic-gate #    undef SORTDOWN
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifdef USCR
860Sstevel@tonic-gate #    define COMPARE(A, B) (* hcompar)((A), (B))
870Sstevel@tonic-gate      extern int (* hcompar)();
880Sstevel@tonic-gate #else
890Sstevel@tonic-gate #    define COMPARE(A, B) strcmp((A), (B))
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifdef MULT
930Sstevel@tonic-gate #    define SHIFT ((bitsper * sizeof(int)) - m) /* Shift factor */
940Sstevel@tonic-gate #    define FACTOR 035761254233	/* Magic multiplication factor */
950Sstevel@tonic-gate #    define HASH hashm		/* Multiplicative hash function */
960Sstevel@tonic-gate #    define HASH2 hash2m	/* Secondary hash function */
970Sstevel@tonic-gate static unsigned int bitsper;	/* Bits per byte */
980Sstevel@tonic-gate static unsigned int hashm();
990Sstevel@tonic-gate static unsigned int hash2m();
1000Sstevel@tonic-gate #else
1010Sstevel@tonic-gate #ifdef DIV
1020Sstevel@tonic-gate #    define HASH hashd		/* Division hashing routine */
1030Sstevel@tonic-gate #    define HASH2(A) 1		/* Secondary hash function */
1040Sstevel@tonic-gate static unsigned int hashd();
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate typedef enum {
1090Sstevel@tonic-gate     FIND,		/* Find, if present */
1100Sstevel@tonic-gate     ENTER		/* Find; enter if not present */
1110Sstevel@tonic-gate } ACTION;
1120Sstevel@tonic-gate typedef char *POINTER;
1130Sstevel@tonic-gate typedef struct entry {	/* Hash table entry */
1140Sstevel@tonic-gate     POINTER key;
1150Sstevel@tonic-gate     POINTER data;
1160Sstevel@tonic-gate } ENTRY;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #ifdef CHAINED
1190Sstevel@tonic-gate typedef struct node {	/* Part of the linked list of entries */
1200Sstevel@tonic-gate 	ENTRY item;
1210Sstevel@tonic-gate 	struct node *next;
1220Sstevel@tonic-gate } NODE;
1230Sstevel@tonic-gate typedef NODE *TABELEM;
1240Sstevel@tonic-gate static NODE **table;	/* The address of the hash table */
1250Sstevel@tonic-gate static ENTRY *build();
1260Sstevel@tonic-gate #else
1270Sstevel@tonic-gate #ifdef OPEN
1280Sstevel@tonic-gate typedef ENTRY TABELEM;	/* What the table contains (TABle ELEMents) */
1290Sstevel@tonic-gate static TABELEM *table;	/* The address of the hash table */
1300Sstevel@tonic-gate static unsigned int count = 0;	/* Number of entries in hash table */
1310Sstevel@tonic-gate #endif
1320Sstevel@tonic-gate #endif
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static unsigned int length;	/* Size of the hash table */
1350Sstevel@tonic-gate static unsigned int m;		/* Log base 2 of length */
1360Sstevel@tonic-gate static unsigned int prcnt;	/* Number of probes this item */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate int hcreate();
1390Sstevel@tonic-gate void hdestroy();
1400Sstevel@tonic-gate ENTRY *hsearch();
1410Sstevel@tonic-gate static unsigned int crunch();
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #ifdef DRIVER
1440Sstevel@tonic-gate static void hdump();
1450Sstevel@tonic-gate 
main()1460Sstevel@tonic-gate main()
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate     char line[80];	/* Room for the input line */
1490Sstevel@tonic-gate     int i = 0;		/* Data generator */
1500Sstevel@tonic-gate     ENTRY *res;		/* Result of hsearch */
1510Sstevel@tonic-gate     ENTRY *new;		/* Test entry */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate     if(hcreate(5))
1540Sstevel@tonic-gate 	printf("Length = %u, m = %u\n", length, m);
1550Sstevel@tonic-gate     else {
1560Sstevel@tonic-gate 	fprintf(stderr, "Out of core\n");
1570Sstevel@tonic-gate 	exit(FAIL);
1580Sstevel@tonic-gate     }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate     repeat {
1610Sstevel@tonic-gate 	hdump();
1620Sstevel@tonic-gate 	printf("Enter a probe: ");
1630Sstevel@tonic-gate 	until (EOF == scanf("%s", line));
1640Sstevel@tonic-gate #ifdef DEBUG
1650Sstevel@tonic-gate 	printf("%s, ", line);
1660Sstevel@tonic-gate 	printf("division: %d, ", hashd(line));
1670Sstevel@tonic-gate 	printf("multiplication: %d\n", hashm(line));
1680Sstevel@tonic-gate #endif
1690Sstevel@tonic-gate 	new = (ENTRY *) malloc(sizeof(ENTRY));
1700Sstevel@tonic-gate 	if(new == NULL) {
1710Sstevel@tonic-gate 	    fprintf(stderr, "Out of core \n");
1720Sstevel@tonic-gate 	    exit(FAIL);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	else {
1750Sstevel@tonic-gate 	    new->key = malloc((unsigned) strlen(line) + 1);
1760Sstevel@tonic-gate 	    if(new->key == NULL) {
1770Sstevel@tonic-gate 		fprintf(stderr, "Out of core \n");
1780Sstevel@tonic-gate 		exit(FAIL);
1790Sstevel@tonic-gate 	    }
1800Sstevel@tonic-gate 	    strcpy(new->key, line);
1810Sstevel@tonic-gate 	    new->data = malloc(sizeof(int));
1820Sstevel@tonic-gate 	    if(new->data == NULL) {
1830Sstevel@tonic-gate 		fprintf(stderr, "Out of core \n");
1840Sstevel@tonic-gate 		exit(FAIL);
1850Sstevel@tonic-gate 	    }
1860Sstevel@tonic-gate 	    *new->data = i++;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	res = hsearch(*new, ENTER);
1890Sstevel@tonic-gate 	printf("The number of probes required was %d\n", prcnt);
1900Sstevel@tonic-gate 	if(res == (ENTRY *) 0)
1910Sstevel@tonic-gate 	    printf("Table is full\n");
1920Sstevel@tonic-gate 	else {
1930Sstevel@tonic-gate 	    printf("Success: ");
1940Sstevel@tonic-gate 	    printf("Key = %s, Value = %d\n", res->key, *res->data);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate     }
1970Sstevel@tonic-gate     exit(SUCCEED);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 
201*722Smuffin /*
202*722Smuffin  * Create a hash table no smaller than size
203*722Smuffin  *
204*722Smuffin  *	size:	Minimum size for hash table
205*722Smuffin  */
2060Sstevel@tonic-gate int
hcreate(int size)207*722Smuffin hcreate(int size)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate     unsigned int unsize;	/* Holds the shifted size */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate     if(size <= 0)
2120Sstevel@tonic-gate 	return(FALSE);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate     unsize = size;	/* +1 for empty table slot; -1 for ceiling */
2150Sstevel@tonic-gate     length = 1;		/* Maximum entries in tabbe */
2160Sstevel@tonic-gate     m = 0;		/* Log2 length */
2170Sstevel@tonic-gate     while(unsize) {
2180Sstevel@tonic-gate 	unsize >>= 1;
2190Sstevel@tonic-gate 	length <<= 1;
2200Sstevel@tonic-gate 	m++;
2210Sstevel@tonic-gate     }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate     table = (TABELEM *) calloc(length, sizeof(TABELEM));
224*722Smuffin     return (table != NULL);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate void
hdestroy(void)228*722Smuffin hdestroy(void)	/* Reset the module to its initial state */
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate     free((POINTER) table);
2310Sstevel@tonic-gate #ifdef OPEN
2320Sstevel@tonic-gate     count = 0;
2330Sstevel@tonic-gate #endif
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate #ifdef OPEN
2370Sstevel@tonic-gate /* Hash search of a fixed-capacity table.  Open addressing used to
2380Sstevel@tonic-gate    resolve collisions.  Algorithm modified from Knuth, Volume 3,
2390Sstevel@tonic-gate    section 6.4, algorithm D.  Labels flag corresponding actions.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate 
242*722Smuffin /*
243*722Smuffin  * Find or insert the item into the table
244*722Smuffin  *
245*722Smuffin  *	item:	Item to be inserted or found
246*722Smuffin  *	action:	FIND or ENTER
247*722Smuffin  */
248*722Smuffin ENTRY *
hsearch(ENTRY item,ACTION action)249*722Smuffin hsearch(ENTRY item, ACTION action)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate     unsigned int i;	/* Insertion index */
2520Sstevel@tonic-gate     unsigned int c;	/* Secondary probe displacement */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate     prcnt = 1;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /* D1: */
2570Sstevel@tonic-gate     i = HASH(item.key);	/* Primary hash on key */
2580Sstevel@tonic-gate #ifdef DEBUG
2590Sstevel@tonic-gate     if(action == ENTER)
2600Sstevel@tonic-gate 	printf("hash = %o\n", i);
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate /* D2: */
2640Sstevel@tonic-gate     if(table[i].key == NULL)	/* Empty slot? */
2650Sstevel@tonic-gate 	goto D6;
2660Sstevel@tonic-gate     else if(COMPARE(table[i].key, item.key) == 0)	/* Match? */
2670Sstevel@tonic-gate 	return(&table[i]);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /* D3: */
2700Sstevel@tonic-gate     c = HASH2(item.key);	/* No match => compute secondary hash */
2710Sstevel@tonic-gate #ifdef DEBUG
2720Sstevel@tonic-gate     if(action == ENTER)
2730Sstevel@tonic-gate 	printf("hash2 = %o\n", c);
2740Sstevel@tonic-gate #endif
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate D4:
2770Sstevel@tonic-gate     i = (i + c) % length;	/* Advance to next slot */
2780Sstevel@tonic-gate     prcnt++;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /* D5: */
2810Sstevel@tonic-gate     if(table[i].key == NULL)	/* Empty slot? */
2820Sstevel@tonic-gate 	goto D6;
2830Sstevel@tonic-gate     else if(COMPARE(table[i].key, item.key) == 0)	/* Match? */
2840Sstevel@tonic-gate 	return(&table[i]);
2850Sstevel@tonic-gate     else
2860Sstevel@tonic-gate 	goto D4;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate D6: if(action == FIND)		/* Insert if requested */
2890Sstevel@tonic-gate 	return((ENTRY *) NULL);
2900Sstevel@tonic-gate     if(count == (length - 1))	/* Table full? */
2910Sstevel@tonic-gate 	return((ENTRY *) 0);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #ifdef BRENT
2940Sstevel@tonic-gate /* Brent's variation of the open addressing algorithm.  Do extra
2950Sstevel@tonic-gate    work during insertion to speed retrieval.  May require switching
2960Sstevel@tonic-gate    of previously placed items.  Adapted from Knuth, Volume 3, section
2970Sstevel@tonic-gate    4.6 and Brent's article in CACM, volume 10, #2, February 1973.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate     {   unsigned int p0 = HASH(item.key);   /* First probe index */
3010Sstevel@tonic-gate 	unsigned int c0 = HASH2(item.key);  /* Main branch increment */
3020Sstevel@tonic-gate 	unsigned int r = prcnt - 1; /* Current minimum distance */
3030Sstevel@tonic-gate 	unsigned int j;         /* Counts along main branch */
3040Sstevel@tonic-gate 	unsigned int k;         /* Counts along secondary branch */
3050Sstevel@tonic-gate 	unsigned int curj;      /* Current best main branch site */
3060Sstevel@tonic-gate 	unsigned int curpos;    /* Current best table index */
3070Sstevel@tonic-gate 	unsigned int pj;        /* Main branch indices */
3080Sstevel@tonic-gate 	unsigned int cj;        /* Secondary branch increment distance*/
3090Sstevel@tonic-gate 	unsigned int pjk;       /* Secondary branch probe indices */
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if(prcnt >= 3) {
3120Sstevel@tonic-gate 	    for(j = 0; j < prcnt; j++) {   /* Count along main branch */
3130Sstevel@tonic-gate 		pj = (p0 + j * c0) % length; /* New main branch index */
3140Sstevel@tonic-gate 		cj = HASH2(table[pj].key); /* Secondary branch incr. */
3150Sstevel@tonic-gate 		for(k=1; j+k <= r; k++) { /* Count on secondary branch*/
3160Sstevel@tonic-gate 		    pjk = (pj + k * cj) % length; /* Secondary probe */
3170Sstevel@tonic-gate 		    if(table[pjk].key == NULL) { /* Improvement found */
3180Sstevel@tonic-gate 		        r = j + k;	/* Decrement upper bound */
3190Sstevel@tonic-gate 		        curj = pj;	/* Save main probe index */
3200Sstevel@tonic-gate 		        curpos = pjk;	/* Save secondeary index */
3210Sstevel@tonic-gate 		    }
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 	    }
3240Sstevel@tonic-gate 	    if(r != prcnt - 1) {       /* If an improvement occurred */
3250Sstevel@tonic-gate 		table[curpos] = table[curj]; /* Old key to new site */
3260Sstevel@tonic-gate #ifdef DEBUG
3270Sstevel@tonic-gate 		printf("Switch curpos = %o, curj = %o, oldi = %o\n",
3280Sstevel@tonic-gate 		    curj, curpos, i);
3290Sstevel@tonic-gate #endif
3300Sstevel@tonic-gate 		i = curj;
3310Sstevel@tonic-gate 	    }
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate     }
3340Sstevel@tonic-gate #endif
3350Sstevel@tonic-gate     count++;			/* Increment table occupancy count */
3360Sstevel@tonic-gate     table[i] = item;		/* Save item */
3370Sstevel@tonic-gate     return(&table[i]);		/* Address of item is returned */
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate #endif
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate #ifdef USCR
3420Sstevel@tonic-gate #    ifdef DRIVER
3430Sstevel@tonic-gate static int
compare(POINTER a,POINTER b)344*722Smuffin compare(POINTER a, POINTER b)
3450Sstevel@tonic-gate {
346*722Smuffin     return (strcmp(a, b));
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate int (* hcompar)() = compare;
3500Sstevel@tonic-gate #    endif
3510Sstevel@tonic-gate #endif
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate #ifdef CHAINED
3540Sstevel@tonic-gate #    ifdef SORTUP
3550Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) > 0)
3560Sstevel@tonic-gate #    else
3570Sstevel@tonic-gate #    ifdef SORTDOWN
3580Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) < 0)
3590Sstevel@tonic-gate #    else
3600Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) != 0)
3610Sstevel@tonic-gate #    endif
3620Sstevel@tonic-gate #    endif
3630Sstevel@tonic-gate 
364*722Smuffin /*
365*722Smuffin  * Chained search with sorted lists
366*722Smuffin  *
367*722Smuffin  *	item:	Item to be inserted or found
368*722Smuffin  *	action: FIND or ENTER
369*722Smuffin  */
370*722Smuffin ENTRY *
hsearch(ENTRY item,ACTION action)371*722Smuffin hsearch(ENTRY item, ACTION action)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate     NODE *p;		/* Searches through the linked list */
3740Sstevel@tonic-gate     NODE **q;		/* Where to store the pointer to a new NODE */
3750Sstevel@tonic-gate     unsigned int i;	/* Result of hash */
3760Sstevel@tonic-gate     int res;		/* Result of string comparison */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate     prcnt = 1;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate     i = HASH(item.key);	/* Table[i] contains list head */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate     if(table[i] == (NODE*)NULL) { /* List has not yet been begun */
3830Sstevel@tonic-gate 	if(action == FIND)
3840Sstevel@tonic-gate 	    return((ENTRY *) NULL);
3850Sstevel@tonic-gate 	else
3860Sstevel@tonic-gate 	    return(build(&table[i], (NODE *) NULL, item));
3870Sstevel@tonic-gate     }
3880Sstevel@tonic-gate     else {			/* List is not empty */
3890Sstevel@tonic-gate 	q = &table[i];
3900Sstevel@tonic-gate 	p = table[i];
3910Sstevel@tonic-gate 	while(p != NULL && (res = STRCMP(item.key, p->item.key))) {
3920Sstevel@tonic-gate 	    prcnt++;
3930Sstevel@tonic-gate 	    q = &(p->next);
3940Sstevel@tonic-gate 	    p = p->next;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if(p != NULL && res == 0)	/* Item has been found */
3980Sstevel@tonic-gate 	    return(&(p->item));
3990Sstevel@tonic-gate 	else {			/* Item is not yet on list */
4000Sstevel@tonic-gate 	    if(action == FIND)
4010Sstevel@tonic-gate 		return((ENTRY *) NULL);
4020Sstevel@tonic-gate 	    else
4030Sstevel@tonic-gate #ifdef START
4040Sstevel@tonic-gate 		return(build(&table[i], table[i], item));
4050Sstevel@tonic-gate #else
4060Sstevel@tonic-gate 		return(build(q, p, item));
4070Sstevel@tonic-gate #endif
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate     }
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
412*722Smuffin /*
413*722Smuffin  *	last:		Where to store in last list item
414*722Smuffin  *	next:		Link to next list item
415*722Smuffin  *	item:		Item to be kept in node
416*722Smuffin  */
417*722Smuffin static ENTRY *
build(NODE ** last,NODE * next,ENTRY item)418*722Smuffin build(NODE **last, NODE *next, ENTRY item)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate     NODE *p = (NODE *) malloc(sizeof(NODE));
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate     if(p != NULL) {
4230Sstevel@tonic-gate 	p->item = item;
4240Sstevel@tonic-gate 	*last = p;
4250Sstevel@tonic-gate 	p->next = next;
4260Sstevel@tonic-gate 	return(&(p->item));
4270Sstevel@tonic-gate     }
4280Sstevel@tonic-gate     else
4290Sstevel@tonic-gate 	return(NULL);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate #endif
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate #ifdef DIV
434*722Smuffin /*
435*722Smuffin  * Division hashing scheme
436*722Smuffin  *
437*722Smuffin  *	key:	Key to be hashed
438*722Smuffin  */
4390Sstevel@tonic-gate static unsigned int
hashd(POINTER key)440*722Smuffin hashd(POINTER key)
4410Sstevel@tonic-gate {
442*722Smuffin     return (crunch(key) % length);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate #else
4450Sstevel@tonic-gate #ifdef MULT
4460Sstevel@tonic-gate /*
447*722Smuffin  *    NOTE: The following algorithm only works on machines where
448*722Smuffin  *    the results of multiplying two integers is the least
449*722Smuffin  *    significant part of the double word integer required to hold
450*722Smuffin  *    the result.  It is adapted from Knuth, Volume 3, section 6.4.
451*722Smuffin  */
4520Sstevel@tonic-gate 
453*722Smuffin /*
454*722Smuffin  * Multiplication hashing scheme
455*722Smuffin  *
456*722Smuffin  *	key:	Key to be hashed
457*722Smuffin  */
4580Sstevel@tonic-gate static unsigned int
hashm(POINTER key)459*722Smuffin hashm(POINTER key)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate     static int first = TRUE;	/* TRUE on the first call only */
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate     if(first) {		/* Compute the number of bits in a byte */
4640Sstevel@tonic-gate 	unsigned char c = UCHAR_MAX;	/* A byte full of 1's */
4650Sstevel@tonic-gate 	bitsper = 0;
4660Sstevel@tonic-gate 	while(c) {		/* Shift until no more 1's */
4670Sstevel@tonic-gate 	    c >>= 1;
4680Sstevel@tonic-gate 	    bitsper++;		/* Count number of shifts */
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	first = FALSE;
4710Sstevel@tonic-gate     }
472*722Smuffin     return ((int) (((unsigned) (crunch(key) * FACTOR)) >> SHIFT));
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Secondary hashing, for use with multiplicitive hashing scheme.
4770Sstevel@tonic-gate  * Adapted from Knuth, Volume 3, section 6.4.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate 
480*722Smuffin /*
481*722Smuffin  * Secondary hashing routine
482*722Smuffin  *
483*722Smuffin  *	key:	String to be hashed
484*722Smuffin  */
4850Sstevel@tonic-gate static unsigned int
hash2m(POINTER key)486*722Smuffin hash2m(POINTER key)
4870Sstevel@tonic-gate {
488*722Smuffin     return ((int) (((unsigned) ((crunch(key) * FACTOR) << m) >> SHIFT) | 1));
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate #endif
4910Sstevel@tonic-gate #endif
4920Sstevel@tonic-gate 
493*722Smuffin /* Convert multicharacter key to unsigned int */
4940Sstevel@tonic-gate static unsigned int
crunch(POINTER key)495*722Smuffin crunch(POINTER key)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate     unsigned int sum = 0;	/* Results */
4980Sstevel@tonic-gate     int s;			/* Length of the key */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate     for(s = 0; *key; s++)	/* Simply add up the bytes */
5010Sstevel@tonic-gate 	sum += *key++;
5020Sstevel@tonic-gate 
503*722Smuffin     return (sum + s);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate #ifdef DRIVER
5070Sstevel@tonic-gate static void
hdump()5080Sstevel@tonic-gate hdump()			/* Dumps loc, data, probe count, key */
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate     unsigned int i;	/* Counts table slots */
5110Sstevel@tonic-gate #ifdef OPEN
5120Sstevel@tonic-gate     unsigned int sum = 0;	/* Counts probes */
5130Sstevel@tonic-gate #else
5140Sstevel@tonic-gate #ifdef CHAINED
5150Sstevel@tonic-gate     NODE *a;		/* Current Node on list */
5160Sstevel@tonic-gate #endif
5170Sstevel@tonic-gate #endif
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate     for(i = 0; i < length; i++)
5200Sstevel@tonic-gate #ifdef OPEN
5210Sstevel@tonic-gate 	if(table[i].key == NULL)
5220Sstevel@tonic-gate 	    printf("%o.\t-,\t-,\t(NULL)\n", i);
5230Sstevel@tonic-gate 	else {
5240Sstevel@tonic-gate 	    unsigned int oldpr = prcnt; /* Save current probe count */
5250Sstevel@tonic-gate 	    hsearch(table[i], FIND);
5260Sstevel@tonic-gate 	    sum += prcnt;
5270Sstevel@tonic-gate 	    printf("%o.\t%d,\t%d,\t%s\n", i,
5280Sstevel@tonic-gate 		*table[i].data, prcnt, table[i].key);
5290Sstevel@tonic-gate 	    prcnt = oldpr;
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate     printf("Total probes = %d\n", sum);
5320Sstevel@tonic-gate #else
5330Sstevel@tonic-gate #ifdef CHAINED
5340Sstevel@tonic-gate 	if(table[i] == NULL)
5350Sstevel@tonic-gate 	    printf("%o.\t-,\t-,\t(NULL)\n", i);
5360Sstevel@tonic-gate 	else {
5370Sstevel@tonic-gate 	    printf("%o.", i);
5380Sstevel@tonic-gate 	    for(a = table[i]; a != NULL; a = a->next)
5390Sstevel@tonic-gate 		printf("\t%d,\t%#0.4x,\t%s\n",
5400Sstevel@tonic-gate 		    *a->item.data, a, a->item.key);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate #endif
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate #endif
546