xref: /titanic_52/usr/src/cmd/sh/hash.c (revision 965005c81e0f731867d47892b9fb677030b102df)
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  */
22*965005c8Schin 
23*965005c8Schin /*
24*965005c8Schin  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
25*965005c8Schin  * Use is subject to license terms.
26*965005c8Schin  */
27*965005c8Schin 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
32*965005c8Schin #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	"hash.h"
357c478bd9Sstevel@tonic-gate #include	"defs.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define STRCMP(A, B)	(cf(A, B) != 0)
387c478bd9Sstevel@tonic-gate #define FACTOR 	 		035761254233	/* Magic multiplication factor */
397c478bd9Sstevel@tonic-gate #define TABLENGTH		64				/* must be multiple of 2 */
407c478bd9Sstevel@tonic-gate #define LOG2LEN			6				/* log2 of TABLENGTH */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate     NOTE: The following algorithm only works on machines where
447c478bd9Sstevel@tonic-gate     the results of multiplying two integers is the least
457c478bd9Sstevel@tonic-gate     significant part of the double word integer required to hold
467c478bd9Sstevel@tonic-gate     the result.  It is adapted from Knuth, Volume 3, section 6.4.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define hash(str)		(int)(((unsigned)(crunch(str) * FACTOR)) >> shift)
507c478bd9Sstevel@tonic-gate struct node
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	ENTRY item;
537c478bd9Sstevel@tonic-gate 	struct node *next;
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static struct node	**last;
577c478bd9Sstevel@tonic-gate static struct node	*next;
587c478bd9Sstevel@tonic-gate static struct node 	**table;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static unsigned int 	bitsper;		/* Bits per byte */
617c478bd9Sstevel@tonic-gate static unsigned int	shift;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static unsigned int crunch();
647c478bd9Sstevel@tonic-gate 
65*965005c8Schin void
66*965005c8Schin hcreate(void)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	unsigned char c = (unsigned char)~0;			/* A byte full of 1's */
697c478bd9Sstevel@tonic-gate 	int j;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	table = (struct node **)alloc(TABLENGTH * sizeof(struct node *));
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	for (j=0; j < TABLENGTH; ++j)
747c478bd9Sstevel@tonic-gate 	{
757c478bd9Sstevel@tonic-gate 		table[j] = 0;
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	bitsper = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	while (c)
817c478bd9Sstevel@tonic-gate 	{
827c478bd9Sstevel@tonic-gate 		c = (unsigned int)c >> 1;
837c478bd9Sstevel@tonic-gate 		bitsper++;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	shift = (bitsper * sizeof(int)) - LOG2LEN;
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate void hscan(uscan)
917c478bd9Sstevel@tonic-gate 	void	(*uscan)();
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	struct node		*p, *nxt;
947c478bd9Sstevel@tonic-gate 	int				j;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	for (j=0; j < TABLENGTH; ++j)
977c478bd9Sstevel@tonic-gate 	{
987c478bd9Sstevel@tonic-gate 		p = table[j];
997c478bd9Sstevel@tonic-gate 		while (p)
1007c478bd9Sstevel@tonic-gate 		{
1017c478bd9Sstevel@tonic-gate 			nxt = p->next;
1027c478bd9Sstevel@tonic-gate 			(*uscan)(&p->item);
1037c478bd9Sstevel@tonic-gate 			p = nxt;
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate ENTRY *
1117c478bd9Sstevel@tonic-gate hfind(str)
1127c478bd9Sstevel@tonic-gate 	unsigned char	*str;
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	struct node 	*p;
1157c478bd9Sstevel@tonic-gate 	struct node 	**q;
1167c478bd9Sstevel@tonic-gate 	unsigned int 	i;
1177c478bd9Sstevel@tonic-gate 	int 			res;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	i = hash(str);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if(table[i] == 0)
1227c478bd9Sstevel@tonic-gate 	{
1237c478bd9Sstevel@tonic-gate 		last = &table[i];
1247c478bd9Sstevel@tonic-gate 		next = 0;
1257c478bd9Sstevel@tonic-gate 		return(0);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	else
1287c478bd9Sstevel@tonic-gate 	{
1297c478bd9Sstevel@tonic-gate 		q = &table[i];
1307c478bd9Sstevel@tonic-gate 		p = table[i];
1317c478bd9Sstevel@tonic-gate 		while (p != 0 && (res = STRCMP(str, p->item.key)))
1327c478bd9Sstevel@tonic-gate 		{
1337c478bd9Sstevel@tonic-gate 			q = &(p->next);
1347c478bd9Sstevel@tonic-gate 			p = p->next;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (p != 0 && res == 0)
1387c478bd9Sstevel@tonic-gate 			return(&(p->item));
1397c478bd9Sstevel@tonic-gate 		else
1407c478bd9Sstevel@tonic-gate 		{
1417c478bd9Sstevel@tonic-gate 			last = q;
1427c478bd9Sstevel@tonic-gate 			next = p;
1437c478bd9Sstevel@tonic-gate 			return(0);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate ENTRY *
1497c478bd9Sstevel@tonic-gate henter(item)
1507c478bd9Sstevel@tonic-gate 	ENTRY item;
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct node	*p = (struct node *)alloc(sizeof(struct node));
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	p->item = item;
1557c478bd9Sstevel@tonic-gate 	*last = p;
1567c478bd9Sstevel@tonic-gate 	p->next = next;
1577c478bd9Sstevel@tonic-gate 	return(&(p->item));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static unsigned int
1627c478bd9Sstevel@tonic-gate crunch(key)
1637c478bd9Sstevel@tonic-gate 	unsigned char	*key;
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	unsigned int 	sum = 0;
1667c478bd9Sstevel@tonic-gate 	int s;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	for (s = 0; *key; s++)				/* Simply add up the bytes */
1697c478bd9Sstevel@tonic-gate 		sum += *key++;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return(sum + s);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
174