xref: /onnv-gate/usr/src/cmd/tic/tic_hash.c (revision 350:06f97f931994)
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  */
22*350Sdp /*
23*350Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*350Sdp  * Use is subject to license terms.
25*350Sdp  */
260Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
29*350Sdp /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate *************************************************************************
430Sstevel@tonic-gate *			COPYRIGHT NOTICE				*
440Sstevel@tonic-gate *************************************************************************
450Sstevel@tonic-gate *	This software is copyright(C) 1982 by Pavel Curtis		*
460Sstevel@tonic-gate *									*
470Sstevel@tonic-gate *	Permission is granted to reproduce and distribute		*
480Sstevel@tonic-gate *	this file by any means so long as no fee is charged		*
490Sstevel@tonic-gate *	above a nominal handling fee and so long as this		*
500Sstevel@tonic-gate *	notice is always included in the copies.			*
510Sstevel@tonic-gate *									*
520Sstevel@tonic-gate *	Other rights are reserved except as explicitly granted		*
530Sstevel@tonic-gate *	by written permission of the author.				*
540Sstevel@tonic-gate *		Pavel Curtis						*
550Sstevel@tonic-gate *		Computer Science Dept.					*
560Sstevel@tonic-gate *		405 Upson Hall						*
570Sstevel@tonic-gate *		Cornell University					*
580Sstevel@tonic-gate *		Ithaca, NY 14853					*
590Sstevel@tonic-gate *									*
600Sstevel@tonic-gate *		Ph- (607) 256-4934					*
610Sstevel@tonic-gate *									*
620Sstevel@tonic-gate *		Pavel.Cornell@Udel-Relay(ARPAnet)			*
630Sstevel@tonic-gate *		decvax!cornell!pavel(UUCPnet)				*
640Sstevel@tonic-gate *********************************************************************** */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  *	comp_hash.c --- Routines to deal with the hashtable of capability
680Sstevel@tonic-gate  *			names.
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *  $Log:	RCS/comp_hash.v $
710Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:45:34  pavel
720Sstevel@tonic-gate  * Added Copyright Notice
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:16:34  pavel
750Sstevel@tonic-gate  * Beta-one Test Release
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:29:33  pavel
780Sstevel@tonic-gate  * The REAL Alpha-one Release Version
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:09:46  pavel
810Sstevel@tonic-gate  * Alpha Test Release One
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * Revision 1.1  82/08/12  18:36:23  pavel
840Sstevel@tonic-gate  * Initial revision
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
89*350Sdp #include <strings.h>
900Sstevel@tonic-gate #include "curses_inc.h"
910Sstevel@tonic-gate #include "compiler.h"
920Sstevel@tonic-gate 
93*350Sdp static void make_nte(void);
94*350Sdp static int hash_function(char *);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  *	make_hash_table()
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  *	Takes the entries in cap_table[] and hashes them into cap_hash_table[]
1000Sstevel@tonic-gate  *	by name.  There are Captabsize entries in cap_table[] and Hashtabsize
1010Sstevel@tonic-gate  *	slots in cap_hash_table[].
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate void
make_hash_table()1060Sstevel@tonic-gate make_hash_table()
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	int	i;
1090Sstevel@tonic-gate 	int	hashvalue;
1100Sstevel@tonic-gate 	int	collisions = 0;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	make_nte();
1130Sstevel@tonic-gate 	for (i = 0; i < Captabsize; i++) {
1140Sstevel@tonic-gate 		hashvalue = hash_function(cap_table[i].nte_name);
1150Sstevel@tonic-gate 		DEBUG(9, "%d\n", hashvalue);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		if (cap_hash_table[hashvalue] != (struct name_table_entry *) 0)
1180Sstevel@tonic-gate 			collisions++;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		cap_table[i].nte_link = cap_hash_table[hashvalue];
1210Sstevel@tonic-gate 		cap_hash_table[hashvalue] = &cap_table[i];
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	DEBUG(3, "Hash table complete\n%d collisions ", collisions);
1250Sstevel@tonic-gate 	DEBUG(3, "out of %d entries\n", Captabsize);
1260Sstevel@tonic-gate 	return;
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Make the name_table_entry from the capnames.c set of tables.
1310Sstevel@tonic-gate  */
132*350Sdp static void
make_nte(void)133*350Sdp make_nte(void)
1340Sstevel@tonic-gate {
135*350Sdp 	int i, n;
1360Sstevel@tonic-gate 	extern char *boolnames[], *numnames[], *strnames[];
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	n = 0;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	for (i = 0; boolnames[i]; i++) {
1410Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
1420Sstevel@tonic-gate 		cap_table[n].nte_name = boolnames[i];
1430Sstevel@tonic-gate 		cap_table[n].nte_type = BOOLEAN;
1440Sstevel@tonic-gate 		cap_table[n].nte_index = i;
1450Sstevel@tonic-gate 		n++;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	BoolCount = i;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	for (i = 0; numnames[i]; i++) {
1500Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
1510Sstevel@tonic-gate 		cap_table[n].nte_name = numnames[i];
1520Sstevel@tonic-gate 		cap_table[n].nte_type = NUMBER;
1530Sstevel@tonic-gate 		cap_table[n].nte_index = i;
1540Sstevel@tonic-gate 		n++;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	NumCount = i;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	for (i 	= 0; strnames[i]; i++) {
1590Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
1600Sstevel@tonic-gate 		cap_table[n].nte_name = strnames[i];
1610Sstevel@tonic-gate 		cap_table[n].nte_type = STRING;
1620Sstevel@tonic-gate 		cap_table[n].nte_index = i;
1630Sstevel@tonic-gate 		n++;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 	StrCount = i;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	Captabsize = n;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  *	int hash_function(string)
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  *	Computes the hashing function on the given string.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  *	The current hash function is the sum of each consectutive pair
1770Sstevel@tonic-gate  *	of characters, taken as two-byte integers, mod Hashtabsize.
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate 
181*350Sdp static int
hash_function(char * string)1820Sstevel@tonic-gate hash_function(char *string)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	long	sum = 0;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	while (*string) {
1870Sstevel@tonic-gate 		sum += *string + (*(string + 1) << 8);
1880Sstevel@tonic-gate 		string++;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (sum % Hashtabsize);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  *	struct name_table_entry *
1980Sstevel@tonic-gate  *	find_entry(string)
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  *	Finds the entry for the given string in the hash table if present.
2010Sstevel@tonic-gate  *	Returns a pointer to the entry in the table or 0 if not found.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate struct name_table_entry *
find_entry(char * string)2060Sstevel@tonic-gate find_entry(char *string)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	int	hashvalue;
2090Sstevel@tonic-gate 	struct name_table_entry	*ptr;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	hashvalue = hash_function(string);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	ptr = cap_hash_table[hashvalue];
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	while (ptr != (struct name_table_entry *) 0 &&
2160Sstevel@tonic-gate 	    strcmp(ptr->nte_name, string) != 0)
2170Sstevel@tonic-gate 		ptr = ptr->nte_link;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	return (ptr);
2200Sstevel@tonic-gate }
221