4 * Copyright (c) 1990, 1991, 1992, 1993 Open Software Foundation, Inc.
5 * ALL RIGHTS RESERVED (MOTIF). See the file named COPYRIGHT.MOTIF for
6 * the full copyright text.
13 static char rcsid[] = "$TOG: UilSymNam.c /main/13 1997/09/08 11:12:50 cshi $"
18 * (c) Copyright 1989, 1990, DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. */
24 ** User Interface Language Compiler (UIL)
28 ** This module inserts names into the name table.
44 ** DEFINE and MACRO DEFINITIONS
51 ** EXTERNAL VARIABLE DECLARATIONS
59 ** GLOBAL VARIABLE DECLARATIONS
67 ** OWN VARIABLE DECLARATIONS
75 ** FUNCTIONAL DESCRIPTION:
77 ** This routine searches for a name entry of the same name as its parameters.
78 ** If the entry is found, a pointer to that name node is
79 ** returned as the value of the function. If no entry is found, a NULL
80 ** pointer is returned.
82 ** See sym_insert_name for a description of the name lookup alorithm.
86 ** l_length length of the name not including the null
87 ** c_text pointer to a null terminated string for name
91 ** sym_az_hash_table the hash table
95 ** sym_az_hash_table may be updated with an additional name
99 ** a pointer to a name entry
109 *sym_find_name(l_length, c_text)
111 int l_length; /* length of name to find */
112 char *c_text; /* text of the name */
115 sym_name_entry_type *az_current_name;
117 int l_compare_result;
119 /* obtain the hash code of for the name */
121 l_hash_code = hash_function( l_length, c_text );
124 ** chain along hash chain looking for symbol - exit loop under 3 condition
125 ** 1) come to the end of the chain: name not found
126 ** 2) find symbol: return this symbol
127 ** 3) find node > symbol: name not found
130 for (az_current_name = sym_az_hash_table[ l_hash_code ];
131 az_current_name != NULL;
132 az_current_name = az_current_name->az_next_name_entry)
134 l_compare_result = _compare(c_text, az_current_name->c_text);
136 if (l_compare_result == 0) /* c_text = current name */
138 /* found the name we are looking for */
140 return az_current_name;
143 if (l_compare_result > 0) /* c_text > current name */
145 /* return NULL - name should be before this spot in list */
152 /* came to end of the list without finding the name */
160 ** FUNCTIONAL DESCRIPTION:
162 ** This routine searches for a name entry of the same name as its parameters.
163 ** If the entry is found, a pointer to that name node is
164 ** returned as the value of the function. If no entry is found, one is
165 ** inserted. In this case the value of the function is a pointer to
166 ** the name entry created.
168 ** Name entries are linked off of a hash table. Those
169 ** entries that have the same hash code, are sorted according to the
170 ** collating sequence. Thus the algorithm involves hashing the symbol and
171 ** then following the chain for that hash code until one of the following
172 ** conditions is met. 1) the identifier is found, then return a pointer
173 ** to that name entry. 2) come to the end of the chain or a name
174 ** entry that comes later in the collating sequence than the symbol being
175 ** searched for. In this case the name is inserted just prior to this
176 ** point in the chain.
178 ** FORMAL PARAMETERS:
180 ** l_length length of the name not including the null
181 ** c_text pointer to a null terminated string for name
185 ** sym_az_hash_table the hash table
189 ** sym_az_hash_table may be updated with an additional name
193 ** a pointer to a name entry
197 ** may create a name entry and update the hash table
202 sym_name_entry_type *sym_insert_name(l_length, c_text)
204 int l_length; /* length of name to insert */
205 char *c_text; /* text of the name */
208 sym_name_entry_type *az_previous_name;
209 sym_name_entry_type *az_current_name;
210 sym_name_entry_type *az_new_name;
212 int l_compare_result;
215 ** algorithm keeps 2 pointers, one for the previous name and one
216 ** for the current name. This permits easy insertion of a new name
220 /* obtain the hash code of for the name */
222 l_hash_code = hash_function( l_length, c_text );
225 ** chain along hash chain looking for symbol - exit loop under 3 condition
226 ** 1) come to the end of the chain: insert new node on end
227 ** 2) find symbol: return this symbol
228 ** 3) find node > symbol: insert new node prior to current node
231 for (az_current_name = sym_az_hash_table[ l_hash_code ],
232 az_previous_name = NULL;
234 az_current_name != NULL;
236 az_previous_name = az_current_name,
237 az_current_name = az_current_name->az_next_name_entry)
239 l_compare_result = _compare(c_text, az_current_name->c_text);
241 if (l_compare_result == 0) /* c_text = current name */
243 /* found the name we are looking for */
245 return az_current_name;
248 if (l_compare_result > 0) /* c_text > current name */
250 /* exit the loop to insert just prior to current name */
260 ** name is not in the table so it must be inserted between the
261 ** az_previous_name and az_current_name entries.
264 /* allocate and initialize the name entry */
266 az_new_name = (sym_name_entry_type *)
267 sem_allocate_node (sym_k_name_entry,
268 sym_k_name_entry_size + l_length + 1);
270 az_new_name->header.b_type = l_length; /* b_type holds length */
271 az_new_name->az_object = NULL;
272 az_new_name->az_cycle_id = 0;
274 _move( az_new_name->c_text, c_text, l_length+1 );
277 ** link the name entry into the hash table
280 az_new_name->az_next_name_entry = az_current_name;
282 if (az_previous_name == NULL)
283 sym_az_hash_table[ l_hash_code ] = az_new_name;
285 az_previous_name->az_next_name_entry =
286 (sym_name_entry_type *) az_new_name;
294 ** FUNCTIONAL DESCRIPTION:
296 ** This procedure is a hashing function. It takes a length and a
297 ** pointer to a value. Using this value as a string, the function
298 ** returns an integer in the range of 0 to sym_k_hash_table_limit-1.
300 ** FORMAL PARAMETERS:
302 ** l_length length of the value in bytes not including null
303 ** c_value a null terminated string
307 ** sym_k_hash_table_limit
315 ** integer (the hash code) in range 0 to sym_k_hash_table_limit-1
324 int hash_function(l_length, c_value)
331 static unsigned int XmConst mask[ 8 ] =
332 { 0x00000000000000FF, 0x000000000000FFFF,
333 0x0000000000FFFFFF, 0x00000000FFFFFFFF,
334 0x00000000FFFFFFFF, 0x0000FFFFFFFFFFFF,
335 0x00FFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, };
336 #elif defined (LONG64)
338 static long XmConst mask[ 8 ] =
339 { 0x00000000000000FF, 0x000000000000FFFF,
340 0x0000000000FFFFFF, 0x00000000FFFFFFFF,
341 0x00000000FFFFFFFF, 0x0000FFFFFFFFFFFF,
342 0x00FFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, };
345 static unsigned int XmConst mask[ 4 ] =
346 { 0x000000FF, 0x0000FFFF, 0x00FFFFFF, 0xFFFFFFFF };
360 l_limit = (l_length-1) >> _shift; /* divide by wordsize */
361 l_extra = (l_length-1) & _slm; /* remainder from divide by wordsize */
364 bzero((char *)al_value, sizeof(long) * 20);
366 bzero((char *)al_value, sizeof(int) * 20);
368 strncpy((char *)al_value, c_value, l_length);
371 for (i = 0; i < l_limit; i++)
373 l_hash_code = l_hash_code ^ al_value[ i ];
376 l_hash_code = l_hash_code ^ (al_value[ i ] & mask[ l_extra ]);
378 return (int)(l_hash_code % sym_k_hash_table_limit);
386 ** FUNCTIONAL DESCRIPTION:
388 ** This debugging routine will dump out the name entries linked
389 ** from the hash table.
391 ** FORMAL PARAMETERS:
409 ** prints out the hash table
414 void sym_dump_hash_table()
425 for (i=0; i<sym_k_hash_table_limit; i++)
428 sym_name_entry_type *az_name;
432 for (az_name = sym_az_hash_table[ i ];
434 az_name = az_name->az_next_name_entry)
438 _debug_output("\t %s \n", az_name->c_text);
441 total_count += bucket_count;
442 if (bucket_count == 0)
444 max_length = ( max_length > bucket_count )? max_length : bucket_count;
446 _debug_output("bucket: %d length: %d\n", i, bucket_count);
449 _debug_output("name count: %d \n empty count: %d \n max length: %d",
450 total_count, empty_count, max_length );