source: trunk/src/gcc/libiberty/hashtab.c@ 1392

Last change on this file since 1392 was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 15.1 KB
Line 
1/* An expandable hash tables datatype.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov (vmakarov@cygnus.com).
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21/* This package implements basic hash table functionality. It is possible
22 to search for an entry, create an entry and destroy an entry.
23
24 Elements in the table are generic pointers.
25
26 The size of the table is not fixed; if the occupancy of the table
27 grows too high the hash table will be expanded.
28
29 The abstract data implementation is based on generalized Algorithm D
30 from Knuth's book "The art of computer programming". Hash table is
31 expanded by creation of new hash table and transferring elements from
32 the old table to the new table. */
33
34#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#include <sys/types.h>
39
40#ifdef HAVE_STDLIB_H
41#include <stdlib.h>
42#endif
43
44#ifdef HAVE_STRING_H
45#include <string.h>
46#endif
47
48#ifdef HAVE_MALLOC_H
49#include <malloc.h>
50#endif
51
52#include <stdio.h>
53
54#include "libiberty.h"
55#include "hashtab.h"
56
57/* This macro defines reserved value for empty table entry. */
58
59#define EMPTY_ENTRY ((PTR) 0)
60
61/* This macro defines reserved value for table entry which contained
62 a deleted element. */
63
64#define DELETED_ENTRY ((PTR) 1)
65
66static unsigned long higher_prime_number PARAMS ((unsigned long));
67static hashval_t hash_pointer PARAMS ((const void *));
68static int eq_pointer PARAMS ((const void *, const void *));
69static int htab_expand PARAMS ((htab_t));
70static PTR *find_empty_slot_for_expand PARAMS ((htab_t, hashval_t));
71
72/* At some point, we could make these be NULL, and modify the
73 hash-table routines to handle NULL specially; that would avoid
74 function-call overhead for the common case of hashing pointers. */
75htab_hash htab_hash_pointer = hash_pointer;
76htab_eq htab_eq_pointer = eq_pointer;
77
78/* The following function returns a nearest prime number which is
79 greater than N, and near a power of two. */
80
81static unsigned long
82higher_prime_number (n)
83 unsigned long n;
84{
85 /* These are primes that are near, but slightly smaller than, a
86 power of two. */
87 static const unsigned long primes[] = {
88 (unsigned long) 7,
89 (unsigned long) 13,
90 (unsigned long) 31,
91 (unsigned long) 61,
92 (unsigned long) 127,
93 (unsigned long) 251,
94 (unsigned long) 509,
95 (unsigned long) 1021,
96 (unsigned long) 2039,
97 (unsigned long) 4093,
98 (unsigned long) 8191,
99 (unsigned long) 16381,
100 (unsigned long) 32749,
101 (unsigned long) 65521,
102 (unsigned long) 131071,
103 (unsigned long) 262139,
104 (unsigned long) 524287,
105 (unsigned long) 1048573,
106 (unsigned long) 2097143,
107 (unsigned long) 4194301,
108 (unsigned long) 8388593,
109 (unsigned long) 16777213,
110 (unsigned long) 33554393,
111 (unsigned long) 67108859,
112 (unsigned long) 134217689,
113 (unsigned long) 268435399,
114 (unsigned long) 536870909,
115 (unsigned long) 1073741789,
116 (unsigned long) 2147483647,
117 /* 4294967291L */
118 ((unsigned long) 2147483647) + ((unsigned long) 2147483644),
119 };
120
121 const unsigned long *low = &primes[0];
122 const unsigned long *high = &primes[sizeof(primes) / sizeof(primes[0])];
123
124 while (low != high)
125 {
126 const unsigned long *mid = low + (high - low) / 2;
127 if (n > *mid)
128 low = mid + 1;
129 else
130 high = mid;
131 }
132
133 /* If we've run out of primes, abort. */
134 if (n > *low)
135 {
136 fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
137 abort ();
138 }
139
140 return *low;
141}
142
143/* Returns a hash code for P. */
144
145static hashval_t
146hash_pointer (p)
147 const PTR p;
148{
149 return (hashval_t) ((long)p >> 3);
150}
151
152/* Returns non-zero if P1 and P2 are equal. */
153
154static int
155eq_pointer (p1, p2)
156 const PTR p1;
157 const PTR p2;
158{
159 return p1 == p2;
160}
161
162/* This function creates table with length slightly longer than given
163 source length. Created hash table is initiated as empty (all the
164 hash table entries are EMPTY_ENTRY). The function returns the
165 created hash table, or NULL if memory allocation fails. */
166
167htab_t
168htab_create_alloc (size, hash_f, eq_f, del_f, alloc_f, free_f)
169 size_t size;
170 htab_hash hash_f;
171 htab_eq eq_f;
172 htab_del del_f;
173 htab_alloc alloc_f;
174 htab_free free_f;
175{
176 htab_t result;
177
178 size = higher_prime_number (size);
179 result = (htab_t) (*alloc_f) (1, sizeof (struct htab));
180 if (result == NULL)
181 return NULL;
182 result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
183 if (result->entries == NULL)
184 {
185 if (free_f != NULL)
186 (*free_f) (result);
187 return NULL;
188 }
189 result->size = size;
190 result->hash_f = hash_f;
191 result->eq_f = eq_f;
192 result->del_f = del_f;
193 result->alloc_f = alloc_f;
194 result->free_f = free_f;
195 return result;
196}
197
198/* These functions exist solely for backward compatibility. */
199
200#undef htab_create
201htab_t
202htab_create (size, hash_f, eq_f, del_f)
203 size_t size;
204 htab_hash hash_f;
205 htab_eq eq_f;
206 htab_del del_f;
207{
208 return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free);
209}
210
211htab_t
212htab_try_create (size, hash_f, eq_f, del_f)
213 size_t size;
214 htab_hash hash_f;
215 htab_eq eq_f;
216 htab_del del_f;
217{
218 return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free);
219}
220
221/* This function frees all memory allocated for given hash table.
222 Naturally the hash table must already exist. */
223
224void
225htab_delete (htab)
226 htab_t htab;
227{
228 int i;
229
230 if (htab->del_f)
231 for (i = htab->size - 1; i >= 0; i--)
232 if (htab->entries[i] != EMPTY_ENTRY
233 && htab->entries[i] != DELETED_ENTRY)
234 (*htab->del_f) (htab->entries[i]);
235
236 if (htab->free_f != NULL)
237 {
238 (*htab->free_f) (htab->entries);
239 (*htab->free_f) (htab);
240 }
241}
242
243/* This function clears all entries in the given hash table. */
244
245void
246htab_empty (htab)
247 htab_t htab;
248{
249 int i;
250
251 if (htab->del_f)
252 for (i = htab->size - 1; i >= 0; i--)
253 if (htab->entries[i] != EMPTY_ENTRY
254 && htab->entries[i] != DELETED_ENTRY)
255 (*htab->del_f) (htab->entries[i]);
256
257 memset (htab->entries, 0, htab->size * sizeof (PTR));
258}
259
260/* Similar to htab_find_slot, but without several unwanted side effects:
261 - Does not call htab->eq_f when it finds an existing entry.
262 - Does not change the count of elements/searches/collisions in the
263 hash table.
264 This function also assumes there are no deleted entries in the table.
265 HASH is the hash value for the element to be inserted. */
266
267static PTR *
268find_empty_slot_for_expand (htab, hash)
269 htab_t htab;
270 hashval_t hash;
271{
272 size_t size = htab->size;
273 unsigned int index = hash % size;
274 PTR *slot = htab->entries + index;
275 hashval_t hash2;
276
277 if (*slot == EMPTY_ENTRY)
278 return slot;
279 else if (*slot == DELETED_ENTRY)
280 abort ();
281
282 hash2 = 1 + hash % (size - 2);
283 for (;;)
284 {
285 index += hash2;
286 if (index >= size)
287 index -= size;
288
289 slot = htab->entries + index;
290 if (*slot == EMPTY_ENTRY)
291 return slot;
292 else if (*slot == DELETED_ENTRY)
293 abort ();
294 }
295}
296
297/* The following function changes size of memory allocated for the
298 entries and repeatedly inserts the table elements. The occupancy
299 of the table after the call will be about 50%. Naturally the hash
300 table must already exist. Remember also that the place of the
301 table entries is changed. If memory allocation failures are allowed,
302 this function will return zero, indicating that the table could not be
303 expanded. If all goes well, it will return a non-zero value. */
304
305static int
306htab_expand (htab)
307 htab_t htab;
308{
309 PTR *oentries;
310 PTR *olimit;
311 PTR *p;
312 PTR *nentries;
313 size_t nsize;
314
315 oentries = htab->entries;
316 olimit = oentries + htab->size;
317
318 nsize = higher_prime_number (htab->size * 2);
319
320 nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR));
321 if (nentries == NULL)
322 return 0;
323 htab->entries = nentries;
324 htab->size = nsize;
325
326 htab->n_elements -= htab->n_deleted;
327 htab->n_deleted = 0;
328
329 p = oentries;
330 do
331 {
332 PTR x = *p;
333
334 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
335 {
336 PTR *q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
337
338 *q = x;
339 }
340
341 p++;
342 }
343 while (p < olimit);
344
345 if (htab->free_f != NULL)
346 (*htab->free_f) (oentries);
347 return 1;
348}
349
350/* This function searches for a hash table entry equal to the given
351 element. It cannot be used to insert or delete an element. */
352
353PTR
354htab_find_with_hash (htab, element, hash)
355 htab_t htab;
356 const PTR element;
357 hashval_t hash;
358{
359 unsigned int index;
360 hashval_t hash2;
361 size_t size;
362 PTR entry;
363
364 htab->searches++;
365 size = htab->size;
366 index = hash % size;
367
368 entry = htab->entries[index];
369 if (entry == EMPTY_ENTRY
370 || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
371 return entry;
372
373 hash2 = 1 + hash % (size - 2);
374
375 for (;;)
376 {
377 htab->collisions++;
378 index += hash2;
379 if (index >= size)
380 index -= size;
381
382 entry = htab->entries[index];
383 if (entry == EMPTY_ENTRY
384 || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
385 return entry;
386 }
387}
388
389/* Like htab_find_slot_with_hash, but compute the hash value from the
390 element. */
391
392PTR
393htab_find (htab, element)
394 htab_t htab;
395 const PTR element;
396{
397 return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
398}
399
400/* This function searches for a hash table slot containing an entry
401 equal to the given element. To delete an entry, call this with
402 INSERT = 0, then call htab_clear_slot on the slot returned (possibly
403 after doing some checks). To insert an entry, call this with
404 INSERT = 1, then write the value you want into the returned slot.
405 When inserting an entry, NULL may be returned if memory allocation
406 fails. */
407
408PTR *
409htab_find_slot_with_hash (htab, element, hash, insert)
410 htab_t htab;
411 const PTR element;
412 hashval_t hash;
413 enum insert_option insert;
414{
415 PTR *first_deleted_slot;
416 unsigned int index;
417 hashval_t hash2;
418 size_t size;
419 PTR entry;
420
421 if (insert == INSERT && htab->size * 3 <= htab->n_elements * 4
422 && htab_expand (htab) == 0)
423 return NULL;
424
425 size = htab->size;
426 index = hash % size;
427
428 htab->searches++;
429 first_deleted_slot = NULL;
430
431 entry = htab->entries[index];
432 if (entry == EMPTY_ENTRY)
433 goto empty_entry;
434 else if (entry == DELETED_ENTRY)
435 first_deleted_slot = &htab->entries[index];
436 else if ((*htab->eq_f) (entry, element))
437 return &htab->entries[index];
438
439 hash2 = 1 + hash % (size - 2);
440 for (;;)
441 {
442 htab->collisions++;
443 index += hash2;
444 if (index >= size)
445 index -= size;
446
447 entry = htab->entries[index];
448 if (entry == EMPTY_ENTRY)
449 goto empty_entry;
450 else if (entry == DELETED_ENTRY)
451 {
452 if (!first_deleted_slot)
453 first_deleted_slot = &htab->entries[index];
454 }
455 else if ((*htab->eq_f) (entry, element))
456 return &htab->entries[index];
457 }
458
459 empty_entry:
460 if (insert == NO_INSERT)
461 return NULL;
462
463 htab->n_elements++;
464
465 if (first_deleted_slot)
466 {
467 *first_deleted_slot = EMPTY_ENTRY;
468 return first_deleted_slot;
469 }
470
471 return &htab->entries[index];
472}
473
474/* Like htab_find_slot_with_hash, but compute the hash value from the
475 element. */
476
477PTR *
478htab_find_slot (htab, element, insert)
479 htab_t htab;
480 const PTR element;
481 enum insert_option insert;
482{
483 return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
484 insert);
485}
486
487/* This function deletes an element with the given value from hash
488 table. If there is no matching element in the hash table, this
489 function does nothing. */
490
491void
492htab_remove_elt (htab, element)
493 htab_t htab;
494 PTR element;
495{
496 PTR *slot;
497
498 slot = htab_find_slot (htab, element, NO_INSERT);
499 if (*slot == EMPTY_ENTRY)
500 return;
501
502 if (htab->del_f)
503 (*htab->del_f) (*slot);
504
505 *slot = DELETED_ENTRY;
506 htab->n_deleted++;
507}
508
509/* This function clears a specified slot in a hash table. It is
510 useful when you've already done the lookup and don't want to do it
511 again. */
512
513void
514htab_clear_slot (htab, slot)
515 htab_t htab;
516 PTR *slot;
517{
518 if (slot < htab->entries || slot >= htab->entries + htab->size
519 || *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY)
520 abort ();
521
522 if (htab->del_f)
523 (*htab->del_f) (*slot);
524
525 *slot = DELETED_ENTRY;
526 htab->n_deleted++;
527}
528
529/* This function scans over the entire hash table calling
530 CALLBACK for each live entry. If CALLBACK returns false,
531 the iteration stops. INFO is passed as CALLBACK's second
532 argument. */
533
534void
535htab_traverse (htab, callback, info)
536 htab_t htab;
537 htab_trav callback;
538 PTR info;
539{
540 PTR *slot = htab->entries;
541 PTR *limit = slot + htab->size;
542
543 do
544 {
545 PTR x = *slot;
546
547 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
548 if (!(*callback) (slot, info))
549 break;
550 }
551 while (++slot < limit);
552}
553
554/* Return the current size of given hash table. */
555
556size_t
557htab_size (htab)
558 htab_t htab;
559{
560 return htab->size;
561}
562
563/* Return the current number of elements in given hash table. */
564
565size_t
566htab_elements (htab)
567 htab_t htab;
568{
569 return htab->n_elements - htab->n_deleted;
570}
571
572/* Return the fraction of fixed collisions during all work with given
573 hash table. */
574
575double
576htab_collisions (htab)
577 htab_t htab;
578{
579 if (htab->searches == 0)
580 return 0.0;
581
582 return (double) htab->collisions / (double) htab->searches;
583}
584
585/* Hash P as a null-terminated string.
586
587 Copied from gcc/hashtable.c. Zack had the following to say with respect
588 to applicability, though note that unlike hashtable.c, this hash table
589 implementation re-hashes rather than chain buckets.
590
591 http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html
592 From: Zack Weinberg <zackw@panix.com>
593 Date: Fri, 17 Aug 2001 02:15:56 -0400
594
595 I got it by extracting all the identifiers from all the source code
596 I had lying around in mid-1999, and testing many recurrences of
597 the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either
598 prime numbers or the appropriate identity. This was the best one.
599 I don't remember exactly what constituted "best", except I was
600 looking at bucket-length distributions mostly.
601
602 So it should be very good at hashing identifiers, but might not be
603 as good at arbitrary strings.
604
605 I'll add that it thoroughly trounces the hash functions recommended
606 for this use at http://burtleburtle.net/bob/hash/index.html, both
607 on speed and bucket distribution. I haven't tried it against the
608 function they just started using for Perl's hashes. */
609
610hashval_t
611htab_hash_string (p)
612 const PTR p;
613{
614 const unsigned char *str = (const unsigned char *) p;
615 hashval_t r = 0;
616 unsigned char c;
617
618 while ((c = *str++) != 0)
619 r = r * 67 + c - 113;
620
621 return r;
622}
Note: See TracBrowser for help on using the repository browser.