xref: /illumos-gate/usr/src/lib/libc/port/gen/hsearch.c (revision 18c77f76)
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
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
235ad42b1bSSurya Prakki  * Copyright 2009 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) 1988 AT&T	*/
287257d1b4Sraf /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Compile time switches:
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *  MULT - use a multiplicative hashing function.
347c478bd9Sstevel@tonic-gate  *  DIV - use the remainder mod table size as a hashing function.
357c478bd9Sstevel@tonic-gate  *  CHAINED - use a linked list to resolve collisions.
367c478bd9Sstevel@tonic-gate  *  OPEN - use open addressing to resolve collisions.
377c478bd9Sstevel@tonic-gate  *  BRENT - use Brent's modification to improve the OPEN algorithm.
387c478bd9Sstevel@tonic-gate  *  SORTUP - CHAINED list is sorted in increasing order.
397c478bd9Sstevel@tonic-gate  *  SORTDOWN - CHAINED list is sorted in decreasing order.
407c478bd9Sstevel@tonic-gate  *  START - CHAINED list with entries appended at front.
417c478bd9Sstevel@tonic-gate  *  DRIVER - compile in a main program to drive the tests.
42*18c77f76SBill Sommerfeld  *  HSEARCH_DEBUG - compile some debugging printout statements.
437c478bd9Sstevel@tonic-gate  *  USCR - user supplied comparison routine.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467257d1b4Sraf #pragma weak _hcreate = hcreate
477257d1b4Sraf #pragma weak _hdestroy = hdestroy
487257d1b4Sraf #pragma weak _hsearch = hsearch
497c478bd9Sstevel@tonic-gate 
507257d1b4Sraf #include "lint.h"
517c478bd9Sstevel@tonic-gate #include <mtlib.h>
527c478bd9Sstevel@tonic-gate #include <limits.h>
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include <thread.h>
577c478bd9Sstevel@tonic-gate #include <synch.h>
587c478bd9Sstevel@tonic-gate #include <search.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef char *POINTER;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	SUCCEED		0
637c478bd9Sstevel@tonic-gate #define	FAIL		1
647c478bd9Sstevel@tonic-gate #define	TRUE		1
657c478bd9Sstevel@tonic-gate #define	FALSE		0
667c478bd9Sstevel@tonic-gate #define	repeat		for (;;)
677c478bd9Sstevel@tonic-gate #define	until(A)	if (A) break;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifdef OPEN
707c478bd9Sstevel@tonic-gate #undef CHAINED
717c478bd9Sstevel@tonic-gate #else
727c478bd9Sstevel@tonic-gate #ifndef CHAINED
737c478bd9Sstevel@tonic-gate #define	OPEN
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifdef MULT
787c478bd9Sstevel@tonic-gate #undef DIV
797c478bd9Sstevel@tonic-gate #else
807c478bd9Sstevel@tonic-gate #ifndef DIV
817c478bd9Sstevel@tonic-gate #define	MULT
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifdef START
867c478bd9Sstevel@tonic-gate #undef SORTUP
877c478bd9Sstevel@tonic-gate #undef SORTDOWN
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate #ifdef SORTUP
907c478bd9Sstevel@tonic-gate #undef SORTDOWN
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifdef USCR
957c478bd9Sstevel@tonic-gate #define	COMPARE(A, B) (* hcompar)((A), (B))
967c478bd9Sstevel@tonic-gate extern int (* hcompar)();
977c478bd9Sstevel@tonic-gate #else
987c478bd9Sstevel@tonic-gate #define	COMPARE(A, B) strcmp((A), (B))
997c478bd9Sstevel@tonic-gate #endif
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #ifdef MULT
1027c478bd9Sstevel@tonic-gate #define	SHIFT ((CHAR_BIT * sizeof (int)) - m) /* Shift factor */
1037c478bd9Sstevel@tonic-gate #define	FACTOR 035761254233	/* Magic multiplication factor */
1047c478bd9Sstevel@tonic-gate #define	HASH hashm		/* Multiplicative hash function */
1057c478bd9Sstevel@tonic-gate #define	HASH2 hash2m	/* Secondary hash function */
1067c478bd9Sstevel@tonic-gate static unsigned int hashm(POINTER);
1077c478bd9Sstevel@tonic-gate static unsigned int hash2m(POINTER);
1087c478bd9Sstevel@tonic-gate #else
1097c478bd9Sstevel@tonic-gate #ifdef DIV
1107c478bd9Sstevel@tonic-gate #define	HASH hashd		/* Division hashing routine */
1117c478bd9Sstevel@tonic-gate #define	HASH2(A) 1		/* Secondary hash function */
1127c478bd9Sstevel@tonic-gate static unsigned int hashd();
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #ifdef CHAINED
1177c478bd9Sstevel@tonic-gate typedef struct node {	/* Part of the linked list of entries */
1187c478bd9Sstevel@tonic-gate 	ENTRY item;
1197c478bd9Sstevel@tonic-gate 	struct node *next;
1207c478bd9Sstevel@tonic-gate } NODE;
1217c478bd9Sstevel@tonic-gate typedef NODE *TABELEM;
1227c478bd9Sstevel@tonic-gate static NODE **table;	/* The address of the hash table */
1237c478bd9Sstevel@tonic-gate static ENTRY *build();
1247c478bd9Sstevel@tonic-gate #else
1257c478bd9Sstevel@tonic-gate #ifdef OPEN
1267c478bd9Sstevel@tonic-gate typedef ENTRY TABELEM;	/* What the table contains (TABle ELEMents) */
1277c478bd9Sstevel@tonic-gate static TABELEM *table;	/* The address of the hash table */
1287c478bd9Sstevel@tonic-gate static unsigned int count = 0;	/* Number of entries in hash table */
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static unsigned int length;	/* Size of the hash table */
1337c478bd9Sstevel@tonic-gate static unsigned int m;		/* Log base 2 of length */
1347c478bd9Sstevel@tonic-gate static unsigned int prcnt;	/* Number of probes this item */
1357c478bd9Sstevel@tonic-gate static mutex_t table_lock = DEFAULTMUTEX;
1367c478bd9Sstevel@tonic-gate #define	RETURN(n)    { lmutex_unlock(&table_lock); return (n); }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * forward declarations
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static unsigned int crunch(POINTER);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #ifdef DRIVER
1457c478bd9Sstevel@tonic-gate static void hdump();
1467c478bd9Sstevel@tonic-gate 
main()1477c478bd9Sstevel@tonic-gate main()
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	char line[80];	/* Room for the input line */
1507c478bd9Sstevel@tonic-gate 	int i = 0;		/* Data generator */
1517c478bd9Sstevel@tonic-gate 	ENTRY *res;		/* Result of hsearch */
1527c478bd9Sstevel@tonic-gate 	ENTRY *new;		/* Test entry */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate start:
1557c478bd9Sstevel@tonic-gate 	if (hcreate(5))
1567c478bd9Sstevel@tonic-gate 		printf("Length = %u, m = %u\n", length, m);
1577c478bd9Sstevel@tonic-gate 	else {
1587c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Out of core\n");
1597c478bd9Sstevel@tonic-gate 		exit(FAIL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	repeat {
1627c478bd9Sstevel@tonic-gate 	hdump();
1637c478bd9Sstevel@tonic-gate 	printf("Enter a probe: ");
1647c478bd9Sstevel@tonic-gate 	until(EOF == scanf("%s", line) || strcmp(line, "quit") == 0);
165*18c77f76SBill Sommerfeld #ifdef HSEARCH_DEBUG
1667c478bd9Sstevel@tonic-gate 	printf("%s, ", line);
1677c478bd9Sstevel@tonic-gate 	printf("division: %d, ", hashd(line));
1687c478bd9Sstevel@tonic-gate 	printf("multiplication: %d\n", hashm(line));
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate 	new = (ENTRY *) malloc(sizeof (ENTRY));
1717c478bd9Sstevel@tonic-gate 	if (new == NULL) {
1727c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Out of core \n");
1737c478bd9Sstevel@tonic-gate 		exit(FAIL);
1747257d1b4Sraf 	} else {
1757257d1b4Sraf 		new->key = malloc((unsigned)strlen(line) + 1);
1767257d1b4Sraf 		if (new->key == NULL) {
1777257d1b4Sraf 			fprintf(stderr, "Out of core \n");
1787257d1b4Sraf 			exit(FAIL);
1797257d1b4Sraf 		}
1805ad42b1bSSurya Prakki 		(void) strcpy(new->key, line);
1817257d1b4Sraf 		new->data = malloc(sizeof (int));
1827257d1b4Sraf 		if (new->data == NULL) {
1837257d1b4Sraf 			fprintf(stderr, "Out of core \n");
1847257d1b4Sraf 			exit(FAIL);
1857257d1b4Sraf 		}
1867257d1b4Sraf 		*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)
1917257d1b4Sraf 		printf("Table is full\n");
1927c478bd9Sstevel@tonic-gate 	else {
1937257d1b4Sraf 		printf("Success: ");
1947257d1b4Sraf 		printf("Key = %s, Value = %d\n", res->key, *res->data);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	printf("Do you wish to start another hash table (yes/no?)");
1987c478bd9Sstevel@tonic-gate 	if (EOF == scanf("%s", line) || strcmp(line, "no") == 0)
1997c478bd9Sstevel@tonic-gate 		exit(SUCCEED);
2007c478bd9Sstevel@tonic-gate 	hdestroy();
2017c478bd9Sstevel@tonic-gate 	goto start;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate int
hcreate(size_t size)2067c478bd9Sstevel@tonic-gate hcreate(size_t size)	/* Create a hash table no smaller than size */
2077c478bd9Sstevel@tonic-gate 	/* Minimum "size" for hash table */
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	size_t unsize;	/* Holds the shifted size */
2107c478bd9Sstevel@tonic-gate 	TABELEM *local_table;
2117c478bd9Sstevel@tonic-gate 	TABELEM *old_table;
2127c478bd9Sstevel@tonic-gate 	unsigned int local_length;
2137c478bd9Sstevel@tonic-gate 	unsigned int local_m;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (size == 0)
2167c478bd9Sstevel@tonic-gate 		return (FALSE);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	unsize = size;	/* +1 for empty table slot; -1 for ceiling */
2197c478bd9Sstevel@tonic-gate 	local_length = 1;	/* Maximum entries in table */
2207c478bd9Sstevel@tonic-gate 	local_m = 0;		/* Log2 length */
2217c478bd9Sstevel@tonic-gate 	while (unsize) {
2227c478bd9Sstevel@tonic-gate 		unsize >>= 1;
2237c478bd9Sstevel@tonic-gate 		local_length <<= 1;
2247c478bd9Sstevel@tonic-gate 		local_m++;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	local_table = (TABELEM *) calloc(local_length, sizeof (TABELEM));
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	lmutex_lock(&table_lock);
2307c478bd9Sstevel@tonic-gate 	old_table = table;
2317c478bd9Sstevel@tonic-gate 	table = local_table;
2327c478bd9Sstevel@tonic-gate 	length = local_length;
2337c478bd9Sstevel@tonic-gate 	m = local_m;
2347c478bd9Sstevel@tonic-gate 	lmutex_unlock(&table_lock);
2357c478bd9Sstevel@tonic-gate 	if (old_table != NULL)
2367c478bd9Sstevel@tonic-gate 		free(old_table);
2377c478bd9Sstevel@tonic-gate 	return (local_table != NULL);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate void
hdestroy(void)2417c478bd9Sstevel@tonic-gate hdestroy(void)	/* Reset the module to its initial state */
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	POINTER local_table;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	lmutex_lock(&table_lock);
2467c478bd9Sstevel@tonic-gate #ifdef CHAINED
2477c478bd9Sstevel@tonic-gate 	int i;
2487c478bd9Sstevel@tonic-gate 	NODE *p, *oldp;
2497c478bd9Sstevel@tonic-gate 	for (i = 0; i < length; i++) {
2507c478bd9Sstevel@tonic-gate 		if (table[i] != (NODE *)NULL) {
2517c478bd9Sstevel@tonic-gate 			p = table[i];
2527c478bd9Sstevel@tonic-gate 			while (p != (NODE *)NULL) {
2537c478bd9Sstevel@tonic-gate 				oldp = p;
2547c478bd9Sstevel@tonic-gate 				p = p -> next;
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * This is a locking vs malloc() violation.
2577c478bd9Sstevel@tonic-gate 				 * Fortunately, it is not actually compiled.
2587c478bd9Sstevel@tonic-gate 				 */
2597c478bd9Sstevel@tonic-gate 				free(oldp);
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 	local_table = (POINTER)table;
2657c478bd9Sstevel@tonic-gate 	table = 0;
2667c478bd9Sstevel@tonic-gate #ifdef OPEN
2677c478bd9Sstevel@tonic-gate 	count = 0;
2687c478bd9Sstevel@tonic-gate #endif
2697c478bd9Sstevel@tonic-gate 	lmutex_unlock(&table_lock);
2707c478bd9Sstevel@tonic-gate 	free(local_table);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate #ifdef OPEN
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * Hash search of a fixed-capacity table.  Open addressing used to
2767c478bd9Sstevel@tonic-gate  *  resolve collisions.  Algorithm modified from Knuth, Volume 3,
2777c478bd9Sstevel@tonic-gate  *  section 6.4, algorithm D.  Labels flag corresponding actions.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /* Find or insert the item into the table */
2817c478bd9Sstevel@tonic-gate ENTRY
hsearch(ENTRY item,ACTION action)2827c478bd9Sstevel@tonic-gate *hsearch(ENTRY item, ACTION action)
2837c478bd9Sstevel@tonic-gate 	/* "item" to be inserted or found */
2847c478bd9Sstevel@tonic-gate 	/* action: FIND or ENTER */
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	unsigned int i;	/* Insertion index */
2877c478bd9Sstevel@tonic-gate 	unsigned int c;	/* Secondary probe displacement */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	lmutex_lock(&table_lock);
2907c478bd9Sstevel@tonic-gate 	prcnt = 1;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /* D1: */
2937c478bd9Sstevel@tonic-gate 	i = HASH(item.key);	/* Primary hash on key */
294*18c77f76SBill Sommerfeld #ifdef HSEARCH_DEBUG
2957c478bd9Sstevel@tonic-gate 	if (action == ENTER)
2967c478bd9Sstevel@tonic-gate 		printf("hash = %o\n", i);
2977c478bd9Sstevel@tonic-gate #endif
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /* D2: */
3007c478bd9Sstevel@tonic-gate 	if (table[i].key == NULL)	/* Empty slot? */
3017c478bd9Sstevel@tonic-gate 		goto D6;
3027c478bd9Sstevel@tonic-gate 	else if (COMPARE(table[i].key, item.key) == 0)	/* Match? */
3037c478bd9Sstevel@tonic-gate 		RETURN(&table[i]);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /* D3: */
3067c478bd9Sstevel@tonic-gate 	c = HASH2(item.key);	/* No match => compute secondary hash */
307*18c77f76SBill Sommerfeld #ifdef HSEARCH_DEBUG
3087c478bd9Sstevel@tonic-gate 	if (action == ENTER)
3097c478bd9Sstevel@tonic-gate 		printf("hash2 = %o\n", c);
3107c478bd9Sstevel@tonic-gate #endif
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate D4:
3137c478bd9Sstevel@tonic-gate 	i = (i + c) % length;	/* Advance to next slot */
3147c478bd9Sstevel@tonic-gate 	prcnt++;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /* D5: */
3177c478bd9Sstevel@tonic-gate 	if (table[i].key == NULL)	/* Empty slot? */
3187c478bd9Sstevel@tonic-gate 		goto D6;
3197c478bd9Sstevel@tonic-gate 	else if (COMPARE(table[i].key, item.key) == 0)	/* Match? */
3207c478bd9Sstevel@tonic-gate 		RETURN(&table[i])
3217c478bd9Sstevel@tonic-gate 	else
3227c478bd9Sstevel@tonic-gate 		goto D4;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate D6:	if (action == FIND)		/* Insert if requested */
3257c478bd9Sstevel@tonic-gate 		RETURN((ENTRY *) NULL);
3267c478bd9Sstevel@tonic-gate 	if (count == (length - 1))	/* Table full? */
3277c478bd9Sstevel@tonic-gate 		RETURN((ENTRY *) 0);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate #ifdef BRENT
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * Brent's variation of the open addressing algorithm.  Do extra
3327c478bd9Sstevel@tonic-gate  * work during insertion to speed retrieval.  May require switching
3337c478bd9Sstevel@tonic-gate  * of previously placed items.  Adapted from Knuth, Volume 3, section
3347c478bd9Sstevel@tonic-gate  * 4.6 and Brent's article in CACM, volume 10, #2, February 1973.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	{
3387c478bd9Sstevel@tonic-gate 	unsigned int p0 = HASH(item.key);   /* First probe index */
3397c478bd9Sstevel@tonic-gate 	unsigned int c0 = HASH2(item.key);  /* Main branch increment */
3407c478bd9Sstevel@tonic-gate 	unsigned int r = prcnt - 1; /* Current minimum distance */
3417c478bd9Sstevel@tonic-gate 	unsigned int j;		/* Counts along main branch */
3427c478bd9Sstevel@tonic-gate 	unsigned int k;		/* Counts along secondary branch */
3437c478bd9Sstevel@tonic-gate 	unsigned int curj;	/* Current best main branch site */
3447c478bd9Sstevel@tonic-gate 	unsigned int curpos;	/* Current best table index */
3457c478bd9Sstevel@tonic-gate 	unsigned int pj;	/* Main branch indices */
3467c478bd9Sstevel@tonic-gate 	unsigned int cj;	/* Secondary branch increment distance */
3477c478bd9Sstevel@tonic-gate 	unsigned int pjk;	/* Secondary branch probe indices */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (prcnt >= 3) {
3507c478bd9Sstevel@tonic-gate 		for (j = 0; j < prcnt; j++) {   /* Count along main branch */
3517c478bd9Sstevel@tonic-gate 			pj = (p0 + j * c0) % length; /* New main branch index */
3527c478bd9Sstevel@tonic-gate 			cj = HASH2(table[pj].key); /* Secondary branch incr. */
3537c478bd9Sstevel@tonic-gate 			for (k = 1; j+k <= r; k++) {
3547c478bd9Sstevel@tonic-gate 					/* Count on secondary branch */
3557c478bd9Sstevel@tonic-gate 				pjk = (pj + k * cj) % length;
3567c478bd9Sstevel@tonic-gate 					/* Secondary probe */
3577c478bd9Sstevel@tonic-gate 				if (table[pjk].key == NULL) {
3587c478bd9Sstevel@tonic-gate 					/* Improvement found */
3597c478bd9Sstevel@tonic-gate 					r = j + k; /* Decrement upper bound */
3607c478bd9Sstevel@tonic-gate 					curj = pj; /* Save main probe index */
3617c478bd9Sstevel@tonic-gate 					curpos = pjk;
3627c478bd9Sstevel@tonic-gate 						/* Save secondeary index */
3637c478bd9Sstevel@tonic-gate 				}
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		if (r != prcnt - 1) {	/* If an improvement occurred */
3677c478bd9Sstevel@tonic-gate 			table[curpos] = table[curj]; /* Old key to new site */
368*18c77f76SBill Sommerfeld #ifdef HSEARCH_DEBUG
3697c478bd9Sstevel@tonic-gate 			printf("Switch curpos = %o, curj = %o, oldi = %o\n",
3707c478bd9Sstevel@tonic-gate 				curj, curpos, i);
3717c478bd9Sstevel@tonic-gate #endif
3727c478bd9Sstevel@tonic-gate 			i = curj;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate #endif
3777c478bd9Sstevel@tonic-gate 	count++;			/* Increment table occupancy count */
3787c478bd9Sstevel@tonic-gate 	table[i] = item;		/* Save item */
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	lmutex_unlock(&table_lock);
3817c478bd9Sstevel@tonic-gate 	return (&table[i]);		/* Address of item is returned */
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate #endif
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #ifdef USCR
3867c478bd9Sstevel@tonic-gate #ifdef DRIVER
3877c478bd9Sstevel@tonic-gate static int
compare(a,b)3887c478bd9Sstevel@tonic-gate compare(a, b)
3897c478bd9Sstevel@tonic-gate POINTER a;
3907c478bd9Sstevel@tonic-gate POINTER b;
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate     return (strcmp(a, b));
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate int (* hcompar)() = compare;
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate #endif
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate #ifdef CHAINED
4007c478bd9Sstevel@tonic-gate #ifdef SORTUP
4017c478bd9Sstevel@tonic-gate #define	STRCMP(A, B) (COMPARE((A), (B)) > 0)
4027c478bd9Sstevel@tonic-gate #else
4037c478bd9Sstevel@tonic-gate #ifdef SORTDOWN
4047c478bd9Sstevel@tonic-gate #define	STRCMP(A, B) (COMPARE((A), (B)) < 0)
4057c478bd9Sstevel@tonic-gate #else
4067c478bd9Sstevel@tonic-gate #define	STRCMP(A, B) (COMPARE((A), (B)) != 0)
4077c478bd9Sstevel@tonic-gate #endif
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate ENTRY
hsearch(item,action)4117c478bd9Sstevel@tonic-gate *hsearch(item, action)	/* Chained search with sorted lists */
4127c478bd9Sstevel@tonic-gate ENTRY item;		/* Item to be inserted or found */
4137c478bd9Sstevel@tonic-gate ACTION action;		/* FIND or ENTER */
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	NODE *p;		/* Searches through the linked list */
4167c478bd9Sstevel@tonic-gate 	NODE **q;		/* Where to store the pointer to a new NODE */
4177c478bd9Sstevel@tonic-gate 	unsigned int i;		/* Result of hash */
4187c478bd9Sstevel@tonic-gate 	int res;		/* Result of string comparison */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	lmutex_lock(&table_lock);
4217c478bd9Sstevel@tonic-gate 	prcnt = 1;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	i = HASH(item.key);	/* Table[i] contains list head */
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (table[i] == (NODE*)NULL) { /* List has not yet been begun */
4267c478bd9Sstevel@tonic-gate 		if (action == FIND)
4277c478bd9Sstevel@tonic-gate 			RETURN((ENTRY *) NULL);
4287c478bd9Sstevel@tonic-gate 		else
4297c478bd9Sstevel@tonic-gate 			RETURN(build(&table[i], (NODE *) NULL, item));
4307c478bd9Sstevel@tonic-gate 	} else {		/* List is not empty */
4317c478bd9Sstevel@tonic-gate 		q = &table[i];
4327c478bd9Sstevel@tonic-gate 		p = table[i];
4337c478bd9Sstevel@tonic-gate 		while (p != NULL && (res = STRCMP(item.key, p->item.key))) {
4347c478bd9Sstevel@tonic-gate 			prcnt++;
4357c478bd9Sstevel@tonic-gate 			q = &(p->next);
4367c478bd9Sstevel@tonic-gate 			p = p->next;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (p != NULL && res == 0)	/* Item has been found */
4407c478bd9Sstevel@tonic-gate 			RETURN(&(p->item));
4417c478bd9Sstevel@tonic-gate 		else {			/* Item is not yet on list */
4427c478bd9Sstevel@tonic-gate 			if (action == FIND)
4437c478bd9Sstevel@tonic-gate 				RETURN((ENTRY *) NULL);
4447c478bd9Sstevel@tonic-gate 			else
4457c478bd9Sstevel@tonic-gate #ifdef START
4467c478bd9Sstevel@tonic-gate 				RETURN(build(&table[i], table[i], item));
4477c478bd9Sstevel@tonic-gate #else
4487c478bd9Sstevel@tonic-gate 				RETURN(build(q, p, item));
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static ENTRY
build(last,next,item)4557c478bd9Sstevel@tonic-gate *build(last, next, item)
4567c478bd9Sstevel@tonic-gate NODE **last;		/* Where to store in last list item */
4577c478bd9Sstevel@tonic-gate NODE *next;		/* Link to next list item */
4587c478bd9Sstevel@tonic-gate ENTRY item;		/* Item to be kept in node */
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * This is a locking vs malloc() violation.
4627c478bd9Sstevel@tonic-gate 	 * Fortunately, it is not actually compiled.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	NODE *p = (NODE *) malloc(sizeof (NODE));
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (p != NULL) {
4677c478bd9Sstevel@tonic-gate 		p->item = item;
4687c478bd9Sstevel@tonic-gate 		*last = p;
4697c478bd9Sstevel@tonic-gate 		p->next = next;
4707c478bd9Sstevel@tonic-gate 		return (&(p->item));
4717c478bd9Sstevel@tonic-gate 	} else
4727c478bd9Sstevel@tonic-gate 		return (NULL);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate #endif
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate #ifdef DIV
4777c478bd9Sstevel@tonic-gate static unsigned int
hashd(key)4787c478bd9Sstevel@tonic-gate hashd(key)		/* Division hashing scheme */
4797c478bd9Sstevel@tonic-gate POINTER key;		/* Key to be hashed */
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate     return (crunch(key) % length);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate #else
4847c478bd9Sstevel@tonic-gate #ifdef MULT
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  *  NOTE: The following algorithm only works on machines where
4877c478bd9Sstevel@tonic-gate  *  the results of multiplying two integers is the least
4887c478bd9Sstevel@tonic-gate  *  significant part of the double word integer required to hold
4897c478bd9Sstevel@tonic-gate  *  the result.  It is adapted from Knuth, Volume 3, section 6.4.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static unsigned int
hashm(POINTER key)4937c478bd9Sstevel@tonic-gate hashm(POINTER key)	/* Multiplication hashing scheme */
4947c478bd9Sstevel@tonic-gate 	/* "key" to be hashed */
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	return ((int)(((unsigned)(crunch(key) * FACTOR)) >> SHIFT));
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  * Secondary hashing, for use with multiplicitive hashing scheme.
5017c478bd9Sstevel@tonic-gate  * Adapted from Knuth, Volume 3, section 6.4.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate static unsigned int
hash2m(POINTER key)5057c478bd9Sstevel@tonic-gate hash2m(POINTER key)	/* Secondary hashing routine */
5067c478bd9Sstevel@tonic-gate 	/* "key" is the string to be hashed */
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate     return (((unsigned int)((crunch(key) * FACTOR) << m) >> SHIFT) | 1);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate #endif
5117c478bd9Sstevel@tonic-gate #endif
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /* PJ Weinberger's hash function */
5147c478bd9Sstevel@tonic-gate static unsigned int
crunch(POINTER key)5157c478bd9Sstevel@tonic-gate crunch(POINTER key)	/* Convert multicharacter key to unsigned int */
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	unsigned int h = 0;
5187c478bd9Sstevel@tonic-gate 	unsigned int g;
5197c478bd9Sstevel@tonic-gate 	unsigned char *p = (unsigned char *)key;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	for (; *p; p++) {
5227c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
5237c478bd9Sstevel@tonic-gate 		g = h & 0xf0000000;
5247c478bd9Sstevel@tonic-gate 		if (g != 0) {
5257c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
5267c478bd9Sstevel@tonic-gate 			h ^= g;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	return (h);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate #ifdef DRIVER
5337c478bd9Sstevel@tonic-gate static void
hdump()5347c478bd9Sstevel@tonic-gate hdump()			/* Dumps loc, data, probe count, key */
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	unsigned int i;	/* Counts table slots */
5377c478bd9Sstevel@tonic-gate #ifdef OPEN
5387c478bd9Sstevel@tonic-gate 	unsigned int sum = 0;	/* Counts probes */
5397c478bd9Sstevel@tonic-gate #else
5407c478bd9Sstevel@tonic-gate #ifdef CHAINED
5417c478bd9Sstevel@tonic-gate 	NODE *a;		/* Current Node on list */
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	for (i = 0; i < length; i++)
5467c478bd9Sstevel@tonic-gate #ifdef OPEN
5477c478bd9Sstevel@tonic-gate 		if (table[i].key == NULL)
5487c478bd9Sstevel@tonic-gate 			printf("%o.\t-,\t-,\t(NULL)\n", i);
5497c478bd9Sstevel@tonic-gate 		else {
5507c478bd9Sstevel@tonic-gate 			unsigned int oldpr = prcnt;
5517c478bd9Sstevel@tonic-gate 				/* Save current probe count */
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			hsearch(table[i], FIND);
5547c478bd9Sstevel@tonic-gate 			sum += prcnt;
5557c478bd9Sstevel@tonic-gate 			printf("%o.\t%d,\t%d,\t%s\n", i,
5567257d1b4Sraf 			    *table[i].data, prcnt, table[i].key);
5577c478bd9Sstevel@tonic-gate 			prcnt = oldpr;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 	printf("Total probes = %d\n", sum);
5607c478bd9Sstevel@tonic-gate #else
5617c478bd9Sstevel@tonic-gate #ifdef CHAINED
5627c478bd9Sstevel@tonic-gate 	if (table[i] == NULL)
5637257d1b4Sraf 		printf("%o.\t-,\t-,\t(NULL)\n", i);
5647c478bd9Sstevel@tonic-gate 	else {
5657257d1b4Sraf 		printf("%o.", i);
5667257d1b4Sraf 		for (a = table[i]; a != NULL; a = a->next)
5677257d1b4Sraf 			printf("\t%d,\t%#0.4x,\t%s\n",
5687257d1b4Sraf 			    *a->item.data, a, a->item.key);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate #endif
5717c478bd9Sstevel@tonic-gate #endif
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate #endif
574