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

Last change on this file since 1642 was 1392, checked in by bird, 22 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
RevLine 
[2]1/* An expandable hash tables datatype.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov ([email protected]).
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
[1391]48#ifdef HAVE_MALLOC_H
49#include <malloc.h>
50#endif
51
[2]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
[1391]165 created hash table, or NULL if memory allocation fails. */
[2]166
167htab_t
[1391]168htab_create_alloc (size, hash_f, eq_f, del_f, alloc_f, free_f)
[2]169 size_t size;
170 htab_hash hash_f;
171 htab_eq eq_f;
172 htab_del del_f;
[1391]173 htab_alloc alloc_f;
174 htab_free free_f;
[2]175{
176 htab_t result;
177
178 size = higher_prime_number (size);
[1391]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 }
[2]189 result->size = size;
190 result->hash_f = hash_f;
191 result->eq_f = eq_f;
192 result->del_f = del_f;
[1391]193 result->alloc_f = alloc_f;
194 result->free_f = free_f;
[2]195 return result;
196}
197
[1391]198/* These functions exist solely for backward compatibility. */
[2]199
[1391]200#undef htab_create
[2]201htab_t
[1391]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
[2]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{
[1391]218 return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free);
[2]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
[1391]236 if (htab->free_f != NULL)
237 {
238 (*htab->free_f) (htab->entries);
239 (*htab->free_f) (htab);
240 }
[2]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;
[1391]312 PTR *nentries;
[2]313 size_t nsize;
314
315 oentries = htab->entries;
316 olimit = oentries + htab->size;
317
318 nsize = higher_prime_number (htab->size * 2);
319
[1391]320 nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR));
321 if (nentries == NULL)
322 return 0;
323 htab->entries = nentries;
324 htab->size = nsize;
[2]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
[1391]345 if (htab->free_f != NULL)
346 (*htab->free_f) (oentries);
[2]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)))