Remove entries from gcc. They don't make much senses here since
[deliverable/binutils-gdb.git] / libiberty / hashtab.c
CommitLineData
e2eaf477
ILT
1/* An expandable hash tables datatype.
2 Copyright (C) 1999 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
5c82d20a
ZW
44#ifdef HAVE_STRING_H
45#include <string.h>
46#endif
47
e2eaf477
ILT
48#include <stdio.h>
49
50#include "libiberty.h"
51#include "hashtab.h"
52
e2eaf477
ILT
53/* This macro defines reserved value for empty table entry. */
54
b4fe2683 55#define EMPTY_ENTRY ((void *) 0)
e2eaf477
ILT
56
57/* This macro defines reserved value for table entry which contained
58 a deleted element. */
59
60#define DELETED_ENTRY ((void *) 1)
61
62/* The following function returns the nearest prime number which is
63 greater than given source number. */
64
65static unsigned long
b4fe2683
JM
66higher_prime_number (n)
67 unsigned long n;
e2eaf477
ILT
68{
69 unsigned long i;
70
b4fe2683
JM
71 n |= 0x01; /* Force N to be odd. */
72 if (n < 9)
73 return n; /* All odd numbers < 9 are prime. */
74
75 next:
76 n += 2;
77 i = 3;
78 do
e2eaf477 79 {
b4fe2683
JM
80 if (n % i == 0)
81 goto next;
82 i += 2;
e2eaf477 83 }
b4fe2683
JM
84 while ((i * i) <= n);
85
86 return n;
e2eaf477
ILT
87}
88
89/* This function creates table with length slightly longer than given
90 source length. Created hash table is initiated as empty (all the
91 hash table entries are EMPTY_ENTRY). The function returns the
92 created hash table. */
93
b4fe2683
JM
94htab_t
95htab_create (size, hash_f, eq_f, del_f)
e2eaf477 96 size_t size;
b4fe2683
JM
97 htab_hash hash_f;
98 htab_eq eq_f;
99 htab_del del_f;
e2eaf477 100{
b4fe2683 101 htab_t result;
e2eaf477
ILT
102
103 size = higher_prime_number (size);
b4fe2683
JM
104 result = (htab_t) xcalloc (1, sizeof (struct htab));
105 result->entries = (void **) xcalloc (size, sizeof (void *));
e2eaf477 106 result->size = size;
b4fe2683
JM
107 result->hash_f = hash_f;
108 result->eq_f = eq_f;
109 result->del_f = del_f;
e2eaf477
ILT
110 return result;
111}
112
113/* This function frees all memory allocated for given hash table.
114 Naturally the hash table must already exist. */
115
116void
b4fe2683
JM
117htab_delete (htab)
118 htab_t htab;
e2eaf477 119{
b4fe2683
JM
120 int i;
121 if (htab->del_f)
122 for (i = htab->size - 1; i >= 0; i--)
123 {
124 if (htab->entries[i] != EMPTY_ENTRY
125 && htab->entries[i] != DELETED_ENTRY)
126 (*htab->del_f) (htab->entries[i]);
127 }
128
e2eaf477
ILT
129 free (htab->entries);
130 free (htab);
131}
132
133/* This function clears all entries in the given hash table. */
134
135void
b4fe2683
JM
136htab_empty (htab)
137 htab_t htab;
138{
139 int i;
140 if (htab->del_f)
141 for (i = htab->size - 1; i >= 0; i--)
142 {
143 if (htab->entries[i] != EMPTY_ENTRY
144 && htab->entries[i] != DELETED_ENTRY)
145 (*htab->del_f) (htab->entries[i]);
146 }
147
148 memset (htab->entries, 0, htab->size * sizeof (void *));
149}
150
151/* Similar to htab_find_slot, but without several unwanted side effects:
152 - Does not call htab->eq_f when it finds an existing entry.
153 - Does not change the count of elements/searches/collisions in the
154 hash table.
155 This function also assumes there are no deleted entries in the table.
156 HASH is the hash value for the element to be inserted. */
157static void **
158find_empty_slot_for_expand (htab, hash)
159 htab_t htab;
160 unsigned int hash;
e2eaf477 161{
b4fe2683
JM
162 size_t size = htab->size;
163 unsigned int hash2 = 1 + hash % (size - 2);
164 unsigned int index = hash % size;
165
166 for (;;)
167 {
168 void **slot = htab->entries + index;
169 if (*slot == EMPTY_ENTRY)
170 return slot;
171
172 if (*slot == DELETED_ENTRY)
173 abort ();
174
175 index += hash2;
176 if (index >= size)
177 index -= size;
178 }
e2eaf477
ILT
179}
180
181/* The following function changes size of memory allocated for the
182 entries and repeatedly inserts the table elements. The occupancy
183 of the table after the call will be about 50%. Naturally the hash
184 table must already exist. Remember also that the place of the
185 table entries is changed. */
186
187static void
b4fe2683
JM
188htab_expand (htab)
189 htab_t htab;
e2eaf477 190{
b4fe2683
JM
191 void **oentries;
192 void **olimit;
193 void **p;
194
195 oentries = htab->entries;
196 olimit = oentries + htab->size;
197
198 htab->size = higher_prime_number (htab->size * 2);
199 htab->entries = xcalloc (htab->size, sizeof (void **));
200
201 htab->n_elements -= htab->n_deleted;
202 htab->n_deleted = 0;
203
204 p = oentries;
205 do
206 {
207 void *x = *p;
208 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
209 {
210 void **q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
211 *q = x;
212 }
213 p++;
214 }
215 while (p < olimit);
216 free (oentries);
e2eaf477
ILT
217}
218
b4fe2683
JM
219/* This function searches for a hash table entry equal to the given
220 element. It cannot be used to insert or delete an element. */
221
222void *
223htab_find_with_hash (htab, element, hash)
224 htab_t htab;
225 const void *element;
226 unsigned int hash;
e2eaf477 227{
b4fe2683
JM
228 unsigned int index, hash2;
229 size_t size;
e2eaf477 230
b4fe2683
JM
231 htab->searches++;
232 size = htab->size;
233 hash2 = 1 + hash % (size - 2);
234 index = hash % size;
235
236 for (;;)
e2eaf477 237 {
b4fe2683
JM
238 void *entry = htab->entries[index];
239 if (entry == EMPTY_ENTRY)
240 return NULL;
241 else if (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element))
242 return entry;
243
244 htab->collisions++;
245 index += hash2;
246 if (index >= size)
247 index -= size;
e2eaf477 248 }
b4fe2683
JM
249}
250
251/* Like htab_find_slot_with_hash, but compute the hash value from the
252 element. */
253void *
254htab_find (htab, element)
255 htab_t htab;
256 const void *element;
257{
258 return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
259}
260
261/* This function searches for a hash table slot containing an entry
262 equal to the given element. To delete an entry, call this with
263 INSERT = 0, then call htab_clear_slot on the slot returned (possibly
264 after doing some checks). To insert an entry, call this with
265 INSERT = 1, then write the value you want into the returned slot. */
266
267void **
268htab_find_slot_with_hash (htab, element, hash, insert)
269 htab_t htab;
270 const void *element;
271 unsigned int hash;
272 int insert;
273{
274 void **first_deleted_slot;
275 unsigned int index, hash2;
276 size_t size;
277
278 if (insert && htab->size * 3 <= htab->n_elements * 4)
279 htab_expand (htab);
280
281 size = htab->size;
282 hash2 = 1 + hash % (size - 2);
283 index = hash % size;
284
e2eaf477 285 htab->searches++;
b4fe2683
JM
286 first_deleted_slot = NULL;
287
288 for (;;)
e2eaf477 289 {
b4fe2683
JM
290 void *entry = htab->entries[index];
291 if (entry == EMPTY_ENTRY)
292 {
293 if (!insert)
294 return NULL;
295
296 htab->n_elements++;
297
298 if (first_deleted_slot)
e2eaf477 299 {
b4fe2683
JM
300 *first_deleted_slot = EMPTY_ENTRY;
301 return first_deleted_slot;
e2eaf477 302 }
b4fe2683
JM
303
304 return &htab->entries[index];
305 }
306
307 if (entry == DELETED_ENTRY)
308 {
309 if (!first_deleted_slot)
310 first_deleted_slot = &htab->entries[index];
311 }
312 else
313 {
314 if ((*htab->eq_f) (entry, element))
315 return &htab->entries[index];
316 }
317
318 htab->collisions++;
319 index += hash2;
320 if (index >= size)
321 index -= size;
e2eaf477 322 }
e2eaf477
ILT
323}
324
b4fe2683
JM
325/* Like htab_find_slot_with_hash, but compute the hash value from the
326 element. */
327void **
328htab_find_slot (htab, element, insert)
329 htab_t htab;
330 const void *element;
331 int insert;
332{
333 return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
334 insert);
335}
336
337/* This function deletes an element with the given value from hash
338 table. If there is no matching element in the hash table, this
339 function does nothing. */
e2eaf477
ILT
340
341void
b4fe2683
JM
342htab_remove_elt (htab, element)
343 htab_t htab;
344 void *element;
e2eaf477 345{
b4fe2683
JM
346 void **slot;
347
348 slot = htab_find_slot (htab, element, 0);
349 if (*slot == EMPTY_ENTRY)
350 return;
351
352 if (htab->del_f)
353 (*htab->del_f) (*slot);
e2eaf477 354
b4fe2683
JM
355 *slot = DELETED_ENTRY;
356 htab->n_deleted++;
e2eaf477
ILT
357}
358
b4fe2683
JM
359/* This function clears a specified slot in a hash table. It is
360 useful when you've already done the lookup and don't want to do it
361 again. */
e2eaf477
ILT
362
363void
b4fe2683
JM
364htab_clear_slot (htab, slot)
365 htab_t htab;
366 void **slot;
e2eaf477
ILT
367{
368 if (slot < htab->entries || slot >= htab->entries + htab->size
369 || *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY)
370 abort ();
b4fe2683
JM
371 if (htab->del_f)
372 (*htab->del_f) (*slot);
e2eaf477 373 *slot = DELETED_ENTRY;
b4fe2683 374 htab->n_deleted++;
e2eaf477
ILT
375}
376
377/* This function scans over the entire hash table calling
378 CALLBACK for each live entry. If CALLBACK returns false,
379 the iteration stops. INFO is passed as CALLBACK's second
380 argument. */
381
382void
b4fe2683
JM
383htab_traverse (htab, callback, info)
384 htab_t htab;
385 htab_trav callback;
e2eaf477
ILT
386 void *info;
387{
b4fe2683
JM
388 void **slot, **limit;
389 slot = htab->entries;
390 limit = slot + htab->size;
391 do
392 {
393 void *x = *slot;
394 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
395 if (!(*callback) (slot, info))
396 break;
397 }
398 while (++slot < limit);
e2eaf477
ILT
399}
400
401/* The following function returns current size of given hash table. */
402
403size_t
b4fe2683
JM
404htab_size (htab)
405 htab_t htab;
e2eaf477
ILT
406{
407 return htab->size;
408}
409
410/* The following function returns current number of elements in given
411 hash table. */
412
413size_t
b4fe2683
JM
414htab_elements (htab)
415 htab_t htab;
e2eaf477 416{
b4fe2683 417 return htab->n_elements - htab->n_deleted;
e2eaf477
ILT
418}
419
420/* The following function returns number of percents of fixed
421 collisions during all work with given hash table. */
422
b4fe2683
JM
423double
424htab_collisions (htab)
425 htab_t htab;
e2eaf477
ILT
426{
427 int searches;
428
429 searches = htab->searches;
430 if (searches == 0)
b4fe2683
JM
431 return 0.0;
432 return (double)htab->collisions / (double)searches;
e2eaf477 433}
This page took 0.058032 seconds and 4 git commands to generate.