xref: /titanic_53/usr/src/cmd/tic/tic_hash.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
28*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
29*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
32*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
33*7c478bd9Sstevel@tonic-gate  * contributors.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate *************************************************************************
40*7c478bd9Sstevel@tonic-gate *			COPYRIGHT NOTICE				*
41*7c478bd9Sstevel@tonic-gate *************************************************************************
42*7c478bd9Sstevel@tonic-gate *	This software is copyright(C) 1982 by Pavel Curtis		*
43*7c478bd9Sstevel@tonic-gate *									*
44*7c478bd9Sstevel@tonic-gate *	Permission is granted to reproduce and distribute		*
45*7c478bd9Sstevel@tonic-gate *	this file by any means so long as no fee is charged		*
46*7c478bd9Sstevel@tonic-gate *	above a nominal handling fee and so long as this		*
47*7c478bd9Sstevel@tonic-gate *	notice is always included in the copies.			*
48*7c478bd9Sstevel@tonic-gate *									*
49*7c478bd9Sstevel@tonic-gate *	Other rights are reserved except as explicitly granted		*
50*7c478bd9Sstevel@tonic-gate *	by written permission of the author.				*
51*7c478bd9Sstevel@tonic-gate *		Pavel Curtis						*
52*7c478bd9Sstevel@tonic-gate *		Computer Science Dept.					*
53*7c478bd9Sstevel@tonic-gate *		405 Upson Hall						*
54*7c478bd9Sstevel@tonic-gate *		Cornell University					*
55*7c478bd9Sstevel@tonic-gate *		Ithaca, NY 14853					*
56*7c478bd9Sstevel@tonic-gate *									*
57*7c478bd9Sstevel@tonic-gate *		Ph- (607) 256-4934					*
58*7c478bd9Sstevel@tonic-gate *									*
59*7c478bd9Sstevel@tonic-gate *		Pavel.Cornell@Udel-Relay(ARPAnet)			*
60*7c478bd9Sstevel@tonic-gate *		decvax!cornell!pavel(UUCPnet)				*
61*7c478bd9Sstevel@tonic-gate *********************************************************************** */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  *	comp_hash.c --- Routines to deal with the hashtable of capability
65*7c478bd9Sstevel@tonic-gate  *			names.
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  *  $Log:	RCS/comp_hash.v $
68*7c478bd9Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:45:34  pavel
69*7c478bd9Sstevel@tonic-gate  * Added Copyright Notice
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:16:34  pavel
72*7c478bd9Sstevel@tonic-gate  * Beta-one Test Release
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:29:33  pavel
75*7c478bd9Sstevel@tonic-gate  * The REAL Alpha-one Release Version
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:09:46  pavel
78*7c478bd9Sstevel@tonic-gate  * Alpha Test Release One
79*7c478bd9Sstevel@tonic-gate  *
80*7c478bd9Sstevel@tonic-gate  * Revision 1.1  82/08/12  18:36:23  pavel
81*7c478bd9Sstevel@tonic-gate  * Initial revision
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #include "curses_inc.h"
87*7c478bd9Sstevel@tonic-gate #include "compiler.h"
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  *	make_hash_table()
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  *	Takes the entries in cap_table[] and hashes them into cap_hash_table[]
95*7c478bd9Sstevel@tonic-gate  *	by name.  There are Captabsize entries in cap_table[] and Hashtabsize
96*7c478bd9Sstevel@tonic-gate  *	slots in cap_hash_table[].
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate void
101*7c478bd9Sstevel@tonic-gate make_hash_table()
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	int	i;
104*7c478bd9Sstevel@tonic-gate 	int	hashvalue;
105*7c478bd9Sstevel@tonic-gate 	int	collisions = 0;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	make_nte();
108*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < Captabsize; i++) {
109*7c478bd9Sstevel@tonic-gate 		hashvalue = hash_function(cap_table[i].nte_name);
110*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "%d\n", hashvalue);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 		if (cap_hash_table[hashvalue] != (struct name_table_entry *) 0)
113*7c478bd9Sstevel@tonic-gate 			collisions++;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		cap_table[i].nte_link = cap_hash_table[hashvalue];
116*7c478bd9Sstevel@tonic-gate 		cap_hash_table[hashvalue] = &cap_table[i];
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	DEBUG(3, "Hash table complete\n%d collisions ", collisions);
120*7c478bd9Sstevel@tonic-gate 	DEBUG(3, "out of %d entries\n", Captabsize);
121*7c478bd9Sstevel@tonic-gate 	return;
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /*
125*7c478bd9Sstevel@tonic-gate  * Make the name_table_entry from the capnames.c set of tables.
126*7c478bd9Sstevel@tonic-gate  */
127*7c478bd9Sstevel@tonic-gate make_nte()
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	register int i, n;
130*7c478bd9Sstevel@tonic-gate 	extern char *boolnames[], *numnames[], *strnames[];
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	n = 0;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	for (i = 0; boolnames[i]; i++) {
135*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
136*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_name = boolnames[i];
137*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_type = BOOLEAN;
138*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_index = i;
139*7c478bd9Sstevel@tonic-gate 		n++;
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	BoolCount = i;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	for (i = 0; numnames[i]; i++) {
144*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
145*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_name = numnames[i];
146*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_type = NUMBER;
147*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_index = i;
148*7c478bd9Sstevel@tonic-gate 		n++;
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	NumCount = i;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	for (i 	= 0; strnames[i]; i++) {
153*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_link = NULL;
154*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_name = strnames[i];
155*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_type = STRING;
156*7c478bd9Sstevel@tonic-gate 		cap_table[n].nte_index = i;
157*7c478bd9Sstevel@tonic-gate 		n++;
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 	StrCount = i;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	Captabsize = n;
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate /*
166*7c478bd9Sstevel@tonic-gate  *	int hash_function(string)
167*7c478bd9Sstevel@tonic-gate  *
168*7c478bd9Sstevel@tonic-gate  *	Computes the hashing function on the given string.
169*7c478bd9Sstevel@tonic-gate  *
170*7c478bd9Sstevel@tonic-gate  *	The current hash function is the sum of each consectutive pair
171*7c478bd9Sstevel@tonic-gate  *	of characters, taken as two-byte integers, mod Hashtabsize.
172*7c478bd9Sstevel@tonic-gate  *
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate static
176*7c478bd9Sstevel@tonic-gate int
177*7c478bd9Sstevel@tonic-gate hash_function(char *string)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	long	sum = 0;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	while (*string) {
182*7c478bd9Sstevel@tonic-gate 		sum += *string + (*(string + 1) << 8);
183*7c478bd9Sstevel@tonic-gate 		string++;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (sum % Hashtabsize);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  *	struct name_table_entry *
193*7c478bd9Sstevel@tonic-gate  *	find_entry(string)
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  *	Finds the entry for the given string in the hash table if present.
196*7c478bd9Sstevel@tonic-gate  *	Returns a pointer to the entry in the table or 0 if not found.
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate struct name_table_entry *
201*7c478bd9Sstevel@tonic-gate find_entry(char *string)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate 	int	hashvalue;
204*7c478bd9Sstevel@tonic-gate 	struct name_table_entry	*ptr;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	hashvalue = hash_function(string);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	ptr = cap_hash_table[hashvalue];
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	while (ptr != (struct name_table_entry *) 0 &&
211*7c478bd9Sstevel@tonic-gate 	    strcmp(ptr->nte_name, string) != 0)
212*7c478bd9Sstevel@tonic-gate 		ptr = ptr->nte_link;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	return (ptr);
215*7c478bd9Sstevel@tonic-gate }
216