xref: /titanic_53/usr/src/lib/libbc/libc/gen/common/hsearch.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
337c478bd9Sstevel@tonic-gate /* Compile time switches:
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate    MULT - use a multiplicative hashing function.
367c478bd9Sstevel@tonic-gate    DIV - use the remainder mod table size as a hashing function.
377c478bd9Sstevel@tonic-gate    CHAINED - use a linked list to resolve collisions.
387c478bd9Sstevel@tonic-gate    OPEN - use open addressing to resolve collisions.
397c478bd9Sstevel@tonic-gate    BRENT - use Brent's modification to improve the OPEN algorithm.
407c478bd9Sstevel@tonic-gate    SORTUP - CHAINED list is sorted in increasing order.
417c478bd9Sstevel@tonic-gate    SORTDOWN - CHAINED list is sorted in decreasing order.
427c478bd9Sstevel@tonic-gate    START - CHAINED list with entries appended at front.
437c478bd9Sstevel@tonic-gate    DRIVER - compile in a main program to drive the tests.
447c478bd9Sstevel@tonic-gate    DEBUG - compile some debugging printout statements.
457c478bd9Sstevel@tonic-gate    USCR - user supplied comparison routine.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
50*5d54f3d8Smuffin #include <malloc.h>
51*5d54f3d8Smuffin #include <string.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define SUCCEED		0
547c478bd9Sstevel@tonic-gate #define FAIL		1
557c478bd9Sstevel@tonic-gate #define TRUE		1
567c478bd9Sstevel@tonic-gate #define FALSE		0
577c478bd9Sstevel@tonic-gate #define repeat		for(;;)
587c478bd9Sstevel@tonic-gate #define until(A)	if(A) break;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #ifdef OPEN
617c478bd9Sstevel@tonic-gate #    undef CHAINED
627c478bd9Sstevel@tonic-gate #else
637c478bd9Sstevel@tonic-gate #ifndef CHAINED
647c478bd9Sstevel@tonic-gate #    define OPEN
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef MULT
697c478bd9Sstevel@tonic-gate #    undef DIV
707c478bd9Sstevel@tonic-gate #else
717c478bd9Sstevel@tonic-gate #ifndef DIV
727c478bd9Sstevel@tonic-gate #    define MULT
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef START
777c478bd9Sstevel@tonic-gate #    undef SORTUP
787c478bd9Sstevel@tonic-gate #    undef SORTDOWN
797c478bd9Sstevel@tonic-gate #else
807c478bd9Sstevel@tonic-gate #ifdef SORTUP
817c478bd9Sstevel@tonic-gate #    undef SORTDOWN
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifdef USCR
867c478bd9Sstevel@tonic-gate #    define COMPARE(A, B) (* hcompar)((A), (B))
877c478bd9Sstevel@tonic-gate      extern int (* hcompar)();
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate #    define COMPARE(A, B) strcmp((A), (B))
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef MULT
937c478bd9Sstevel@tonic-gate #    define SHIFT ((bitsper * sizeof(int)) - m) /* Shift factor */
947c478bd9Sstevel@tonic-gate #    define FACTOR 035761254233	/* Magic multiplication factor */
957c478bd9Sstevel@tonic-gate #    define HASH hashm		/* Multiplicative hash function */
967c478bd9Sstevel@tonic-gate #    define HASH2 hash2m	/* Secondary hash function */
977c478bd9Sstevel@tonic-gate static unsigned int bitsper;	/* Bits per byte */
987c478bd9Sstevel@tonic-gate static unsigned int hashm();
997c478bd9Sstevel@tonic-gate static unsigned int hash2m();
1007c478bd9Sstevel@tonic-gate #else
1017c478bd9Sstevel@tonic-gate #ifdef DIV
1027c478bd9Sstevel@tonic-gate #    define HASH hashd		/* Division hashing routine */
1037c478bd9Sstevel@tonic-gate #    define HASH2(A) 1		/* Secondary hash function */
1047c478bd9Sstevel@tonic-gate static unsigned int hashd();
1057c478bd9Sstevel@tonic-gate #endif
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate typedef enum {
1097c478bd9Sstevel@tonic-gate     FIND,		/* Find, if present */
1107c478bd9Sstevel@tonic-gate     ENTER		/* Find; enter if not present */
1117c478bd9Sstevel@tonic-gate } ACTION;
1127c478bd9Sstevel@tonic-gate typedef char *POINTER;
1137c478bd9Sstevel@tonic-gate typedef struct entry {	/* Hash table entry */
1147c478bd9Sstevel@tonic-gate     POINTER key;
1157c478bd9Sstevel@tonic-gate     POINTER data;
1167c478bd9Sstevel@tonic-gate } ENTRY;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef CHAINED
1197c478bd9Sstevel@tonic-gate typedef struct node {	/* Part of the linked list of entries */
1207c478bd9Sstevel@tonic-gate 	ENTRY item;
1217c478bd9Sstevel@tonic-gate 	struct node *next;
1227c478bd9Sstevel@tonic-gate } NODE;
1237c478bd9Sstevel@tonic-gate typedef NODE *TABELEM;
1247c478bd9Sstevel@tonic-gate static NODE **table;	/* The address of the hash table */
1257c478bd9Sstevel@tonic-gate static ENTRY *build();
1267c478bd9Sstevel@tonic-gate #else
1277c478bd9Sstevel@tonic-gate #ifdef OPEN
1287c478bd9Sstevel@tonic-gate typedef ENTRY TABELEM;	/* What the table contains (TABle ELEMents) */
1297c478bd9Sstevel@tonic-gate static TABELEM *table;	/* The address of the hash table */
1307c478bd9Sstevel@tonic-gate static unsigned int count = 0;	/* Number of entries in hash table */
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static unsigned int length;	/* Size of the hash table */
1357c478bd9Sstevel@tonic-gate static unsigned int m;		/* Log base 2 of length */
1367c478bd9Sstevel@tonic-gate static unsigned int prcnt;	/* Number of probes this item */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int hcreate();
1397c478bd9Sstevel@tonic-gate void hdestroy();
1407c478bd9Sstevel@tonic-gate ENTRY *hsearch();
1417c478bd9Sstevel@tonic-gate static unsigned int crunch();
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #ifdef DRIVER
1447c478bd9Sstevel@tonic-gate static void hdump();
1457c478bd9Sstevel@tonic-gate 
main()1467c478bd9Sstevel@tonic-gate main()
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate     char line[80];	/* Room for the input line */
1497c478bd9Sstevel@tonic-gate     int i = 0;		/* Data generator */
1507c478bd9Sstevel@tonic-gate     ENTRY *res;		/* Result of hsearch */
1517c478bd9Sstevel@tonic-gate     ENTRY *new;		/* Test entry */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate     if(hcreate(5))
1547c478bd9Sstevel@tonic-gate 	printf("Length = %u, m = %u\n", length, m);
1557c478bd9Sstevel@tonic-gate     else {
1567c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Out of core\n");
1577c478bd9Sstevel@tonic-gate 	exit(FAIL);
1587c478bd9Sstevel@tonic-gate     }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate     repeat {
1617c478bd9Sstevel@tonic-gate 	hdump();
1627c478bd9Sstevel@tonic-gate 	printf("Enter a probe: ");
1637c478bd9Sstevel@tonic-gate 	until (EOF == scanf("%s", line));
1647c478bd9Sstevel@tonic-gate #ifdef DEBUG
1657c478bd9Sstevel@tonic-gate 	printf("%s, ", line);
1667c478bd9Sstevel@tonic-gate 	printf("division: %d, ", hashd(line));
1677c478bd9Sstevel@tonic-gate 	printf("multiplication: %d\n", hashm(line));
1687c478bd9Sstevel@tonic-gate #endif
1697c478bd9Sstevel@tonic-gate 	new = (ENTRY *) malloc(sizeof(ENTRY));
1707c478bd9Sstevel@tonic-gate 	if(new == NULL) {
1717c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Out of core \n");
1727c478bd9Sstevel@tonic-gate 	    exit(FAIL);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	else {
1757c478bd9Sstevel@tonic-gate 	    new->key = malloc((unsigned) strlen(line) + 1);
1767c478bd9Sstevel@tonic-gate 	    if(new->key == NULL) {
1777c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Out of core \n");
1787c478bd9Sstevel@tonic-gate 		exit(FAIL);
1797c478bd9Sstevel@tonic-gate 	    }
1807c478bd9Sstevel@tonic-gate 	    strcpy(new->key, line);
1817c478bd9Sstevel@tonic-gate 	    new->data = malloc(sizeof(int));
1827c478bd9Sstevel@tonic-gate 	    if(new->data == NULL) {
1837c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Out of core \n");
1847c478bd9Sstevel@tonic-gate 		exit(FAIL);
1857c478bd9Sstevel@tonic-gate 	    }
1867c478bd9Sstevel@tonic-gate 	    *new->data = i++;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	res = hsearch(*new, ENTER);
1897c478bd9Sstevel@tonic-gate 	printf("The number of probes required was %d\n", prcnt);
1907c478bd9Sstevel@tonic-gate 	if(res == (ENTRY *) 0)
1917c478bd9Sstevel@tonic-gate 	    printf("Table is full\n");
1927c478bd9Sstevel@tonic-gate 	else {
1937c478bd9Sstevel@tonic-gate 	    printf("Success: ");
1947c478bd9Sstevel@tonic-gate 	    printf("Key = %s, Value = %d\n", res->key, *res->data);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate     }
1977c478bd9Sstevel@tonic-gate     exit(SUCCEED);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate #endif
2007c478bd9Sstevel@tonic-gate 
201*5d54f3d8Smuffin /*
202*5d54f3d8Smuffin  * Create a hash table no smaller than size
203*5d54f3d8Smuffin  *
204*5d54f3d8Smuffin  *	size:	Minimum size for hash table
205*5d54f3d8Smuffin  */
2067c478bd9Sstevel@tonic-gate int
hcreate(int size)207*5d54f3d8Smuffin hcreate(int size)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate     unsigned int unsize;	/* Holds the shifted size */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate     if(size <= 0)
2127c478bd9Sstevel@tonic-gate 	return(FALSE);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate     unsize = size;	/* +1 for empty table slot; -1 for ceiling */
2157c478bd9Sstevel@tonic-gate     length = 1;		/* Maximum entries in tabbe */
2167c478bd9Sstevel@tonic-gate     m = 0;		/* Log2 length */
2177c478bd9Sstevel@tonic-gate     while(unsize) {
2187c478bd9Sstevel@tonic-gate 	unsize >>= 1;
2197c478bd9Sstevel@tonic-gate 	length <<= 1;
2207c478bd9Sstevel@tonic-gate 	m++;
2217c478bd9Sstevel@tonic-gate     }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate     table = (TABELEM *) calloc(length, sizeof(TABELEM));
2247c478bd9Sstevel@tonic-gate     return (table != NULL);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate void
hdestroy(void)228*5d54f3d8Smuffin hdestroy(void)	/* Reset the module to its initial state */
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate     free((POINTER) table);
2317c478bd9Sstevel@tonic-gate #ifdef OPEN
2327c478bd9Sstevel@tonic-gate     count = 0;
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #ifdef OPEN
2377c478bd9Sstevel@tonic-gate /* Hash search of a fixed-capacity table.  Open addressing used to
2387c478bd9Sstevel@tonic-gate    resolve collisions.  Algorithm modified from Knuth, Volume 3,
2397c478bd9Sstevel@tonic-gate    section 6.4, algorithm D.  Labels flag corresponding actions.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate 
242*5d54f3d8Smuffin /*
243*5d54f3d8Smuffin  * Find or insert the item into the table
244*5d54f3d8Smuffin  *
245*5d54f3d8Smuffin  *	item:	Item to be inserted or found
246*5d54f3d8Smuffin  *	action:	FIND or ENTER
247*5d54f3d8Smuffin  */
248*5d54f3d8Smuffin ENTRY *
hsearch(ENTRY item,ACTION action)249*5d54f3d8Smuffin hsearch(ENTRY item, ACTION action)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate     unsigned int i;	/* Insertion index */
2527c478bd9Sstevel@tonic-gate     unsigned int c;	/* Secondary probe displacement */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate     prcnt = 1;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /* D1: */
2577c478bd9Sstevel@tonic-gate     i = HASH(item.key);	/* Primary hash on key */
2587c478bd9Sstevel@tonic-gate #ifdef DEBUG
2597c478bd9Sstevel@tonic-gate     if(action == ENTER)
2607c478bd9Sstevel@tonic-gate 	printf("hash = %o\n", i);
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /* D2: */
2647c478bd9Sstevel@tonic-gate     if(table[i].key == NULL)	/* Empty slot? */
2657c478bd9Sstevel@tonic-gate 	goto D6;
2667c478bd9Sstevel@tonic-gate     else if(COMPARE(table[i].key, item.key) == 0)	/* Match? */
2677c478bd9Sstevel@tonic-gate 	return(&table[i]);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /* D3: */
2707c478bd9Sstevel@tonic-gate     c = HASH2(item.key);	/* No match => compute secondary hash */
2717c478bd9Sstevel@tonic-gate #ifdef DEBUG
2727c478bd9Sstevel@tonic-gate     if(action == ENTER)
2737c478bd9Sstevel@tonic-gate 	printf("hash2 = %o\n", c);
2747c478bd9Sstevel@tonic-gate #endif
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate D4:
2777c478bd9Sstevel@tonic-gate     i = (i + c) % length;	/* Advance to next slot */
2787c478bd9Sstevel@tonic-gate     prcnt++;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /* D5: */
2817c478bd9Sstevel@tonic-gate     if(table[i].key == NULL)	/* Empty slot? */
2827c478bd9Sstevel@tonic-gate 	goto D6;
2837c478bd9Sstevel@tonic-gate     else if(COMPARE(table[i].key, item.key) == 0)	/* Match? */
2847c478bd9Sstevel@tonic-gate 	return(&table[i]);
2857c478bd9Sstevel@tonic-gate     else
2867c478bd9Sstevel@tonic-gate 	goto D4;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate D6: if(action == FIND)		/* Insert if requested */
2897c478bd9Sstevel@tonic-gate 	return((ENTRY *) NULL);
2907c478bd9Sstevel@tonic-gate     if(count == (length - 1))	/* Table full? */
2917c478bd9Sstevel@tonic-gate 	return((ENTRY *) 0);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #ifdef BRENT
2947c478bd9Sstevel@tonic-gate /* Brent's variation of the open addressing algorithm.  Do extra
2957c478bd9Sstevel@tonic-gate    work during insertion to speed retrieval.  May require switching
2967c478bd9Sstevel@tonic-gate    of previously placed items.  Adapted from Knuth, Volume 3, section
2977c478bd9Sstevel@tonic-gate    4.6 and Brent's article in CACM, volume 10, #2, February 1973.
2987c478bd9Sstevel@tonic-gate */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate     {   unsigned int p0 = HASH(item.key);   /* First probe index */
3017c478bd9Sstevel@tonic-gate 	unsigned int c0 = HASH2(item.key);  /* Main branch increment */
3027c478bd9Sstevel@tonic-gate 	unsigned int r = prcnt - 1; /* Current minimum distance */
3037c478bd9Sstevel@tonic-gate 	unsigned int j;         /* Counts along main branch */
3047c478bd9Sstevel@tonic-gate 	unsigned int k;         /* Counts along secondary branch */
3057c478bd9Sstevel@tonic-gate 	unsigned int curj;      /* Current best main branch site */
3067c478bd9Sstevel@tonic-gate 	unsigned int curpos;    /* Current best table index */
3077c478bd9Sstevel@tonic-gate 	unsigned int pj;        /* Main branch indices */
3087c478bd9Sstevel@tonic-gate 	unsigned int cj;        /* Secondary branch increment distance*/
3097c478bd9Sstevel@tonic-gate 	unsigned int pjk;       /* Secondary branch probe indices */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if(prcnt >= 3) {
3127c478bd9Sstevel@tonic-gate 	    for(j = 0; j < prcnt; j++) {   /* Count along main branch */
3137c478bd9Sstevel@tonic-gate 		pj = (p0 + j * c0) % length; /* New main branch index */
3147c478bd9Sstevel@tonic-gate 		cj = HASH2(table[pj].key); /* Secondary branch incr. */
3157c478bd9Sstevel@tonic-gate 		for(k=1; j+k <= r; k++) { /* Count on secondary branch*/
3167c478bd9Sstevel@tonic-gate 		    pjk = (pj + k * cj) % length; /* Secondary probe */
3177c478bd9Sstevel@tonic-gate 		    if(table[pjk].key == NULL) { /* Improvement found */
3187c478bd9Sstevel@tonic-gate 		        r = j + k;	/* Decrement upper bound */
3197c478bd9Sstevel@tonic-gate 		        curj = pj;	/* Save main probe index */
3207c478bd9Sstevel@tonic-gate 		        curpos = pjk;	/* Save secondeary index */
3217c478bd9Sstevel@tonic-gate 		    }
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 	    }
3247c478bd9Sstevel@tonic-gate 	    if(r != prcnt - 1) {       /* If an improvement occurred */
3257c478bd9Sstevel@tonic-gate 		table[curpos] = table[curj]; /* Old key to new site */
3267c478bd9Sstevel@tonic-gate #ifdef DEBUG
3277c478bd9Sstevel@tonic-gate 		printf("Switch curpos = %o, curj = %o, oldi = %o\n",
3287c478bd9Sstevel@tonic-gate 		    curj, curpos, i);
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate 		i = curj;
3317c478bd9Sstevel@tonic-gate 	    }
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate     }
3347c478bd9Sstevel@tonic-gate #endif
3357c478bd9Sstevel@tonic-gate     count++;			/* Increment table occupancy count */
3367c478bd9Sstevel@tonic-gate     table[i] = item;		/* Save item */
3377c478bd9Sstevel@tonic-gate     return(&table[i]);		/* Address of item is returned */
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate #ifdef USCR
3427c478bd9Sstevel@tonic-gate #    ifdef DRIVER
3437c478bd9Sstevel@tonic-gate static int
compare(POINTER a,POINTER b)344*5d54f3d8Smuffin compare(POINTER a, POINTER b)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate     return (strcmp(a, b));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate int (* hcompar)() = compare;
3507c478bd9Sstevel@tonic-gate #    endif
3517c478bd9Sstevel@tonic-gate #endif
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate #ifdef CHAINED
3547c478bd9Sstevel@tonic-gate #    ifdef SORTUP
3557c478bd9Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) > 0)
3567c478bd9Sstevel@tonic-gate #    else
3577c478bd9Sstevel@tonic-gate #    ifdef SORTDOWN
3587c478bd9Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) < 0)
3597c478bd9Sstevel@tonic-gate #    else
3607c478bd9Sstevel@tonic-gate #        define STRCMP(A, B) (COMPARE((A), (B)) != 0)
3617c478bd9Sstevel@tonic-gate #    endif
3627c478bd9Sstevel@tonic-gate #    endif
3637c478bd9Sstevel@tonic-gate 
364*5d54f3d8Smuffin /*
365*5d54f3d8Smuffin  * Chained search with sorted lists
366*5d54f3d8Smuffin  *
367*5d54f3d8Smuffin  *	item:	Item to be inserted or found
368*5d54f3d8Smuffin  *	action: FIND or ENTER
369*5d54f3d8Smuffin  */
370*5d54f3d8Smuffin ENTRY *
hsearch(ENTRY item,ACTION action)371*5d54f3d8Smuffin hsearch(ENTRY item, ACTION action)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate     NODE *p;		/* Searches through the linked list */
3747c478bd9Sstevel@tonic-gate     NODE **q;		/* Where to store the pointer to a new NODE */
3757c478bd9Sstevel@tonic-gate     unsigned int i;	/* Result of hash */
3767c478bd9Sstevel@tonic-gate     int res;		/* Result of string comparison */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate     prcnt = 1;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate     i = HASH(item.key);	/* Table[i] contains list head */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate     if(table[i] == (NODE*)NULL) { /* List has not yet been begun */
3837c478bd9Sstevel@tonic-gate 	if(action == FIND)
3847c478bd9Sstevel@tonic-gate 	    return((ENTRY *) NULL);
3857c478bd9Sstevel@tonic-gate 	else
3867c478bd9Sstevel@tonic-gate 	    return(build(&table[i], (NODE *) NULL, item));
3877c478bd9Sstevel@tonic-gate     }
3887c478bd9Sstevel@tonic-gate     else {			/* List is not empty */
3897c478bd9Sstevel@tonic-gate 	q = &table[i];
3907c478bd9Sstevel@tonic-gate 	p = table[i];
3917c478bd9Sstevel@tonic-gate 	while(p != NULL && (res = STRCMP(item.key, p->item.key))) {
3927c478bd9Sstevel@tonic-gate 	    prcnt++;
3937c478bd9Sstevel@tonic-gate 	    q = &(p->next);
3947c478bd9Sstevel@tonic-gate 	    p = p->next;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if(p != NULL && res == 0)	/* Item has been found */
3987c478bd9Sstevel@tonic-gate 	    return(&(p->item));
3997c478bd9Sstevel@tonic-gate 	else {			/* Item is not yet on list */
4007c478bd9Sstevel@tonic-gate 	    if(action == FIND)
4017c478bd9Sstevel@tonic-gate 		return((ENTRY *) NULL);
4027c478bd9Sstevel@tonic-gate 	    else
4037c478bd9Sstevel@tonic-gate #ifdef START
4047c478bd9Sstevel@tonic-gate 		return(build(&table[i], table[i], item));
4057c478bd9Sstevel@tonic-gate #else
4067c478bd9Sstevel@tonic-gate 		return(build(q, p, item));
4077c478bd9Sstevel@tonic-gate #endif
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate     }
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
412*5d54f3d8Smuffin /*
413*5d54f3d8Smuffin  *	last:		Where to store in last list item
414*5d54f3d8Smuffin  *	next:		Link to next list item
415*5d54f3d8Smuffin  *	item:		Item to be kept in node
416*5d54f3d8Smuffin  */
417*5d54f3d8Smuffin static ENTRY *
build(NODE ** last,NODE * next,ENTRY item)418*5d54f3d8Smuffin build(NODE **last, NODE *next, ENTRY item)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate     NODE *p = (NODE *) malloc(sizeof(NODE));
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate     if(p != NULL) {
4237c478bd9Sstevel@tonic-gate 	p->item = item;
4247c478bd9Sstevel@tonic-gate 	*last = p;
4257c478bd9Sstevel@tonic-gate 	p->next = next;
4267c478bd9Sstevel@tonic-gate 	return(&(p->item));
4277c478bd9Sstevel@tonic-gate     }
4287c478bd9Sstevel@tonic-gate     else
4297c478bd9Sstevel@tonic-gate 	return(NULL);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate #endif
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate #ifdef DIV
434*5d54f3d8Smuffin /*
435*5d54f3d8Smuffin  * Division hashing scheme
436*5d54f3d8Smuffin  *
437*5d54f3d8Smuffin  *	key:	Key to be hashed
438*5d54f3d8Smuffin  */
4397c478bd9Sstevel@tonic-gate static unsigned int
hashd(POINTER key)440*5d54f3d8Smuffin hashd(POINTER key)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate     return (crunch(key) % length);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate #else
4457c478bd9Sstevel@tonic-gate #ifdef MULT
4467c478bd9Sstevel@tonic-gate /*
447*5d54f3d8Smuffin  *    NOTE: The following algorithm only works on machines where
448*5d54f3d8Smuffin  *    the results of multiplying two integers is the least
449*5d54f3d8Smuffin  *    significant part of the double word integer required to hold
450*5d54f3d8Smuffin  *    the result.  It is adapted from Knuth, Volume 3, section 6.4.
4517c478bd9Sstevel@tonic-gate  */
4527c478bd9Sstevel@tonic-gate 
453*5d54f3d8Smuffin /*
454*5d54f3d8Smuffin  * Multiplication hashing scheme
455*5d54f3d8Smuffin  *
456*5d54f3d8Smuffin  *	key:	Key to be hashed
457*5d54f3d8Smuffin  */
4587c478bd9Sstevel@tonic-gate static unsigned int
hashm(POINTER key)459*5d54f3d8Smuffin hashm(POINTER key)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate     static int first = TRUE;	/* TRUE on the first call only */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate     if(first) {		/* Compute the number of bits in a byte */
4647c478bd9Sstevel@tonic-gate 	unsigned char c = UCHAR_MAX;	/* A byte full of 1's */
4657c478bd9Sstevel@tonic-gate 	bitsper = 0;
4667c478bd9Sstevel@tonic-gate 	while(c) {		/* Shift until no more 1's */
4677c478bd9Sstevel@tonic-gate 	    c >>= 1;
4687c478bd9Sstevel@tonic-gate 	    bitsper++;		/* Count number of shifts */
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	first = FALSE;
4717c478bd9Sstevel@tonic-gate     }
4727c478bd9Sstevel@tonic-gate     return ((int) (((unsigned) (crunch(key) * FACTOR)) >> SHIFT));
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * Secondary hashing, for use with multiplicitive hashing scheme.
4777c478bd9Sstevel@tonic-gate  * Adapted from Knuth, Volume 3, section 6.4.
4787c478bd9Sstevel@tonic-gate  */
4797c478bd9Sstevel@tonic-gate 
480*5d54f3d8Smuffin /*
481*5d54f3d8Smuffin  * Secondary hashing routine
482*5d54f3d8Smuffin  *
483*5d54f3d8Smuffin  *	key:	String to be hashed
484*5d54f3d8Smuffin  */
4857c478bd9Sstevel@tonic-gate static unsigned int
hash2m(POINTER key)486*5d54f3d8Smuffin hash2m(POINTER key)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate     return ((int) (((unsigned) ((crunch(key) * FACTOR) << m) >> SHIFT) | 1));
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate #endif
4927c478bd9Sstevel@tonic-gate 
493*5d54f3d8Smuffin /* Convert multicharacter key to unsigned int */
4947c478bd9Sstevel@tonic-gate static unsigned int
crunch(POINTER key)495*5d54f3d8Smuffin crunch(POINTER key)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate     unsigned int sum = 0;	/* Results */
4987c478bd9Sstevel@tonic-gate     int s;			/* Length of the key */
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate     for(s = 0; *key; s++)	/* Simply add up the bytes */
5017c478bd9Sstevel@tonic-gate 	sum += *key++;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate     return (sum + s);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate #ifdef DRIVER
5077c478bd9Sstevel@tonic-gate static void
hdump()5087c478bd9Sstevel@tonic-gate hdump()			/* Dumps loc, data, probe count, key */
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate     unsigned int i;	/* Counts table slots */
5117c478bd9Sstevel@tonic-gate #ifdef OPEN
5127c478bd9Sstevel@tonic-gate     unsigned int sum = 0;	/* Counts probes */
5137c478bd9Sstevel@tonic-gate #else
5147c478bd9Sstevel@tonic-gate #ifdef CHAINED
5157c478bd9Sstevel@tonic-gate     NODE *a;		/* Current Node on list */
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate #endif
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate     for(i = 0; i < length; i++)
5207c478bd9Sstevel@tonic-gate #ifdef OPEN
5217c478bd9Sstevel@tonic-gate 	if(table[i].key == NULL)
5227c478bd9Sstevel@tonic-gate 	    printf("%o.\t-,\t-,\t(NULL)\n", i);
5237c478bd9Sstevel@tonic-gate 	else {
5247c478bd9Sstevel@tonic-gate 	    unsigned int oldpr = prcnt; /* Save current probe count */
5257c478bd9Sstevel@tonic-gate 	    hsearch(table[i], FIND);
5267c478bd9Sstevel@tonic-gate 	    sum += prcnt;
5277c478bd9Sstevel@tonic-gate 	    printf("%o.\t%d,\t%d,\t%s\n", i,
5287c478bd9Sstevel@tonic-gate 		*table[i].data, prcnt, table[i].key);
5297c478bd9Sstevel@tonic-gate 	    prcnt = oldpr;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate     printf("Total probes = %d\n", sum);
5327c478bd9Sstevel@tonic-gate #else
5337c478bd9Sstevel@tonic-gate #ifdef CHAINED
5347c478bd9Sstevel@tonic-gate 	if(table[i] == NULL)
5357c478bd9Sstevel@tonic-gate 	    printf("%o.\t-,\t-,\t(NULL)\n", i);
5367c478bd9Sstevel@tonic-gate 	else {
5377c478bd9Sstevel@tonic-gate 	    printf("%o.", i);
5387c478bd9Sstevel@tonic-gate 	    for(a = table[i]; a != NULL; a = a->next)
5397c478bd9Sstevel@tonic-gate 		printf("\t%d,\t%#0.4x,\t%s\n",
5407c478bd9Sstevel@tonic-gate 		    *a->item.data, a, a->item.key);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate #endif
546