1 /* hash - hashing table processing.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
6 Written by Jim Meyering, 1992.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* A generic hash table package. */
24 /* Define USE_OBSTACK to 1 if you want the allocator to use obstacks instead
25 of malloc. If you change USE_OBSTACK, you have to recompile! */
37 # ifndef __bool_true_false_are_defined
42 typedef unsigned char _Bool
;
48 # define __bool_true_false_are_defined 1
54 #ifndef HAVE_DECL_FREE
55 "this configure-time declaration test was not run"
61 #ifndef HAVE_DECL_MALLOC
62 "this configure-time declaration test was not run"
70 # ifndef obstack_chunk_alloc
71 # define obstack_chunk_alloc malloc
73 # ifndef obstack_chunk_free
74 # define obstack_chunk_free free
82 /* The array of buckets starts at BUCKET and extends to BUCKET_LIMIT-1,
83 for a possibility of N_BUCKETS. Among those, N_BUCKETS_USED buckets
84 are not empty, there are N_ENTRIES active entries in the table. */
85 struct hash_entry
*bucket
;
86 struct hash_entry
*bucket_limit
;
88 unsigned n_buckets_used
;
91 /* Tuning arguments, kept in a physicaly separate structure. */
92 const Hash_tuning
*tuning
;
94 /* Three functions are given to `hash_initialize', see the documentation
95 block for this function. In a word, HASHER randomizes a user entry
96 into a number up from 0 up to some maximum minus 1; COMPARATOR returns
97 true if two user entries compare equally; and DATA_FREER is the cleanup
98 function for a user entry. */
100 Hash_comparator comparator
;
101 Hash_data_freer data_freer
;
103 /* A linked list of freed struct hash_entry structs. */
104 struct hash_entry
*free_entry_list
;
107 /* Whenever obstacks are used, it is possible to allocate all overflowed
108 entries into a single stack, so they all can be freed in a single
109 operation. It is not clear if the speedup is worth the trouble. */
110 struct obstack entry_stack
;
114 /* A hash table contains many internal entries, each holding a pointer to
115 some user provided data (also called a user entry). An entry indistinctly
116 refers to both the internal entry and its associated user entry. A user
117 entry contents may be hashed by a randomization function (the hashing
118 function, or just `hasher' for short) into a number (or `slot') between 0
119 and the current table size. At each slot position in the hash table,
120 starts a linked chain of entries for which the user data all hash to this
121 slot. A bucket is the collection of all entries hashing to the same slot.
123 A good `hasher' function will distribute entries rather evenly in buckets.
124 In the ideal case, the length of each bucket is roughly the number of
125 entries divided by the table size. Finding the slot for a data is usually
126 done in constant time by the `hasher', and the later finding of a precise
127 entry is linear in time with the size of the bucket. Consequently, a
128 larger hash table size (that is, a larger number of buckets) is prone to
129 yielding shorter chains, *given* the `hasher' function behaves properly.
131 Long buckets slow down the lookup algorithm. One might use big hash table
132 sizes in hope to reduce the average length of buckets, but this might
133 become inordinate, as unused slots in the hash table take some space. The
134 best bet is to make sure you are using a good `hasher' function (beware
135 that those are not that easy to write! :-), and to use a table size
136 larger than the actual number of entries. */
138 /* If an insertion makes the ratio of nonempty buckets to table size larger
139 than the growth threshold (a number between 0.0 and 1.0), then increase
140 the table size by multiplying by the growth factor (a number greater than
141 1.0). The growth threshold defaults to 0.8, and the growth factor
142 defaults to 1.414, meaning that the table will have doubled its size
143 every second time 80% of the buckets get used. */
144 #define DEFAULT_GROWTH_THRESHOLD 0.8
145 #define DEFAULT_GROWTH_FACTOR 1.414
147 /* If a deletion empties a bucket and causes the ratio of used buckets to
148 table size to become smaller than the shrink threshold (a number between
149 0.0 and 1.0), then shrink the table by multiplying by the shrink factor (a
150 number greater than the shrink threshold but smaller than 1.0). The shrink
151 threshold and factor default to 0.0 and 1.0, meaning that the table never
153 #define DEFAULT_SHRINK_THRESHOLD 0.0
154 #define DEFAULT_SHRINK_FACTOR 1.0
156 /* Use this to initialize or reset a TUNING structure to
157 some sensible values. */
158 static const Hash_tuning default_tuning
=
160 DEFAULT_SHRINK_THRESHOLD
,
161 DEFAULT_SHRINK_FACTOR
,
162 DEFAULT_GROWTH_THRESHOLD
,
163 DEFAULT_GROWTH_FACTOR
,
167 /* Information and lookup. */
169 /* The following few functions provide information about the overall hash
170 table organization: the number of entries, number of buckets and maximum
171 length of buckets. */
173 /* Return the number of buckets in the hash table. The table size, the total
174 number of buckets (used plus unused), or the maximum number of slots, are
175 the same quantity. */
178 hash_get_n_buckets (const Hash_table
*table
)
180 return table
->n_buckets
;
183 /* Return the number of slots in use (non-empty buckets). */
186 hash_get_n_buckets_used (const Hash_table
*table
)
188 return table
->n_buckets_used
;
191 /* Return the number of active entries. */
194 hash_get_n_entries (const Hash_table
*table
)
196 return table
->n_entries
;
199 /* Return the length of the longest chain (bucket). */
202 hash_get_max_bucket_length (const Hash_table
*table
)
204 struct hash_entry
*bucket
;
205 unsigned max_bucket_length
= 0;
207 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
211 struct hash_entry
*cursor
= bucket
;
212 unsigned bucket_length
= 1;
214 while (cursor
= cursor
->next
, cursor
)
217 if (bucket_length
> max_bucket_length
)
218 max_bucket_length
= bucket_length
;
222 return max_bucket_length
;
225 /* Do a mild validation of a hash table, by traversing it and checking two
229 hash_table_ok (const Hash_table
*table
)
231 struct hash_entry
*bucket
;
232 unsigned n_buckets_used
= 0;
233 unsigned n_entries
= 0;
235 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
239 struct hash_entry
*cursor
= bucket
;
241 /* Count bucket head. */
245 /* Count bucket overflow. */
246 while (cursor
= cursor
->next
, cursor
)
251 if (n_buckets_used
== table
->n_buckets_used
&& n_entries
== table
->n_entries
)
258 hash_print_statistics (const Hash_table
*table
, FILE *stream
)
260 unsigned n_entries
= hash_get_n_entries (table
);
261 unsigned n_buckets
= hash_get_n_buckets (table
);
262 unsigned n_buckets_used
= hash_get_n_buckets_used (table
);
263 unsigned max_bucket_length
= hash_get_max_bucket_length (table
);
265 fprintf (stream
, "# entries: %u\n", n_entries
);
266 fprintf (stream
, "# buckets: %u\n", n_buckets
);
267 fprintf (stream
, "# buckets used: %u (%.2f%%)\n", n_buckets_used
,
268 (100.0 * n_buckets_used
) / n_buckets
);
269 fprintf (stream
, "max bucket length: %u\n", max_bucket_length
);
272 /* If ENTRY matches an entry already in the hash table, return the
273 entry from the table. Otherwise, return NULL. */
276 hash_lookup (const Hash_table
*table
, const void *entry
)
278 struct hash_entry
*bucket
279 = table
->bucket
+ table
->hasher (entry
, table
->n_buckets
);
280 struct hash_entry
*cursor
;
282 if (! (bucket
< table
->bucket_limit
))
285 if (bucket
->data
== NULL
)
288 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
289 if (table
->comparator (entry
, cursor
->data
))
297 /* The functions in this page traverse the hash table and process the
298 contained entries. For the traversal to work properly, the hash table
299 should not be resized nor modified while any particular entry is being
300 processed. In particular, entries should not be added or removed. */
302 /* Return the first data in the table, or NULL if the table is empty. */
305 hash_get_first (const Hash_table
*table
)
307 struct hash_entry
*bucket
;
309 if (table
->n_entries
== 0)
312 for (bucket
= table
->bucket
; ; bucket
++)
313 if (! (bucket
< table
->bucket_limit
))
315 else if (bucket
->data
)
319 /* Return the user data for the entry following ENTRY, where ENTRY has been
320 returned by a previous call to either `hash_get_first' or `hash_get_next'.
321 Return NULL if there are no more entries. */
324 hash_get_next (const Hash_table
*table
, const void *entry
)
326 struct hash_entry
*bucket
327 = table
->bucket
+ table
->hasher (entry
, table
->n_buckets
);
328 struct hash_entry
*cursor
;
330 if (! (bucket
< table
->bucket_limit
))
333 /* Find next entry in the same bucket. */
334 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
335 if (cursor
->data
== entry
&& cursor
->next
)
336 return cursor
->next
->data
;
338 /* Find first entry in any subsequent bucket. */
339 while (++bucket
< table
->bucket_limit
)
347 /* Fill BUFFER with pointers to active user entries in the hash table, then
348 return the number of pointers copied. Do not copy more than BUFFER_SIZE
352 hash_get_entries (const Hash_table
*table
, void **buffer
,
353 unsigned buffer_size
)
355 unsigned counter
= 0;
356 struct hash_entry
*bucket
;
357 struct hash_entry
*cursor
;
359 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
363 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
365 if (counter
>= buffer_size
)
367 buffer
[counter
++] = cursor
->data
;
375 /* Call a PROCESSOR function for each entry of a hash table, and return the
376 number of entries for which the processor function returned success. A
377 pointer to some PROCESSOR_DATA which will be made available to each call to
378 the processor function. The PROCESSOR accepts two arguments: the first is
379 the user entry being walked into, the second is the value of PROCESSOR_DATA
380 as received. The walking continue for as long as the PROCESSOR function
381 returns nonzero. When it returns zero, the walking is interrupted. */
384 hash_do_for_each (const Hash_table
*table
, Hash_processor processor
,
385 void *processor_data
)
387 unsigned counter
= 0;
388 struct hash_entry
*bucket
;
389 struct hash_entry
*cursor
;
391 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
395 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
397 if (!(*processor
) (cursor
->data
, processor_data
))
407 /* Allocation and clean-up. */
409 /* Return a hash index for a NUL-terminated STRING between 0 and N_BUCKETS-1.
410 This is a convenience routine for constructing other hashing functions. */
414 /* About hashings, Paul Eggert writes to me (FP), on 1994-01-01: "Please see
415 B. J. McKenzie, R. Harries & T. Bell, Selecting a hashing algorithm,
416 Software--practice & experience 20, 2 (Feb 1990), 209-224. Good hash
417 algorithms tend to be domain-specific, so what's good for [diffutils'] io.c
418 may not be good for your application." */
421 hash_string (const char *string
, unsigned n_buckets
)
426 # define ROTATE_LEFT(Value, Shift) \
427 ((Value) << (Shift) | (Value) >> ((sizeof (unsigned) * CHAR_BIT) - (Shift)))
428 # define HASH_ONE_CHAR(Value, Byte) \
429 ((Byte) + ROTATE_LEFT (Value, 7))
433 for (; *string
; string
++)
434 value
= HASH_ONE_CHAR (value
, *(const unsigned char *) string
);
435 return value
% n_buckets
;
438 # undef HASH_ONE_CHAR
441 #else /* not USE_DIFF_HASH */
443 /* This one comes from `recode', and performs a bit better than the above as
444 per a few experiments. It is inspired from a hashing routine found in the
445 very old Cyber `snoop', itself written in typical Greg Mansfield style.
446 (By the way, what happened to this excellent man? Is he still alive?) */
449 hash_string (const char *string
, unsigned n_buckets
)
454 value
= ((value
* 31 + (int) *(const unsigned char *) string
++)
459 #endif /* not USE_DIFF_HASH */
461 /* Return true if CANDIDATE is a prime number. CANDIDATE should be an odd
462 number at least equal to 11. */
465 is_prime (unsigned long candidate
)
467 unsigned long divisor
= 3;
468 unsigned long square
= divisor
* divisor
;
470 while (square
< candidate
&& (candidate
% divisor
))
473 square
+= 4 * divisor
;
477 return (candidate
% divisor
? true : false);
480 /* Round a given CANDIDATE number up to the nearest prime, and return that
481 prime. Primes lower than 10 are merely skipped. */
484 next_prime (unsigned long candidate
)
486 /* Skip small primes. */
490 /* Make it definitely odd. */
493 while (!is_prime (candidate
))
500 hash_reset_tuning (Hash_tuning
*tuning
)
502 *tuning
= default_tuning
;
505 /* For the given hash TABLE, check the user supplied tuning structure for
506 reasonable values, and return true if there is no gross error with it.
507 Otherwise, definitively reset the TUNING field to some acceptable default
508 in the hash table (that is, the user loses the right of further modifying
509 tuning arguments), and return false. */
512 check_tuning (Hash_table
*table
)
514 const Hash_tuning
*tuning
= table
->tuning
;
516 if (tuning
->growth_threshold
> 0.0
517 && tuning
->growth_threshold
< 1.0
518 && tuning
->growth_factor
> 1.0
519 && tuning
->shrink_threshold
>= 0.0
520 && tuning
->shrink_threshold
< 1.0
521 && tuning
->shrink_factor
> tuning
->shrink_threshold
522 && tuning
->shrink_factor
<= 1.0
523 && tuning
->shrink_threshold
< tuning
->growth_threshold
)
526 table
->tuning
= &default_tuning
;
530 /* Allocate and return a new hash table, or NULL upon failure. The initial
531 number of buckets is automatically selected so as to _guarantee_ that you
532 may insert at least CANDIDATE different user entries before any growth of
533 the hash table size occurs. So, if have a reasonably tight a-priori upper
534 bound on the number of entries you intend to insert in the hash table, you
535 may save some table memory and insertion time, by specifying it here. If
536 the IS_N_BUCKETS field of the TUNING structure is true, the CANDIDATE
537 argument has its meaning changed to the wanted number of buckets.
539 TUNING points to a structure of user-supplied values, in case some fine
540 tuning is wanted over the default behavior of the hasher. If TUNING is
541 NULL, the default tuning parameters are used instead.
543 The user-supplied HASHER function should be provided. It accepts two
544 arguments ENTRY and TABLE_SIZE. It computes, by hashing ENTRY contents, a
545 slot number for that entry which should be in the range 0..TABLE_SIZE-1.
546 This slot number is then returned.
548 The user-supplied COMPARATOR function should be provided. It accepts two
549 arguments pointing to user data, it then returns true for a pair of entries
550 that compare equal, or false otherwise. This function is internally called
551 on entries which are already known to hash to the same bucket index.
553 The user-supplied DATA_FREER function, when not NULL, may be later called
554 with the user data as an argument, just before the entry containing the
555 data gets freed. This happens from within `hash_free' or `hash_clear'.
556 You should specify this function only if you want these functions to free
557 all of your `data' data. This is typically the case when your data is
558 simply an auxiliary struct that you have malloc'd to aggregate several
562 hash_initialize (unsigned candidate
, const Hash_tuning
*tuning
,
563 Hash_hasher hasher
, Hash_comparator comparator
,
564 Hash_data_freer data_freer
)
567 struct hash_entry
*bucket
;
569 if (hasher
== NULL
|| comparator
== NULL
)
572 table
= (Hash_table
*) malloc (sizeof (Hash_table
));
577 tuning
= &default_tuning
;
578 table
->tuning
= tuning
;
579 if (!check_tuning (table
))
581 /* Fail if the tuning options are invalid. This is the only occasion
582 when the user gets some feedback about it. Once the table is created,
583 if the user provides invalid tuning options, we silently revert to
584 using the defaults, and ignore further request to change the tuning
591 = next_prime (tuning
->is_n_buckets
? candidate
592 : (unsigned) (candidate
/ tuning
->growth_threshold
));
594 table
->bucket
= (struct hash_entry
*)
595 malloc (table
->n_buckets
* sizeof (struct hash_entry
));
596 if (table
->bucket
== NULL
)
601 table
->bucket_limit
= table
->bucket
+ table
->n_buckets
;
603 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
608 table
->n_buckets_used
= 0;
609 table
->n_entries
= 0;
611 table
->hasher
= hasher
;
612 table
->comparator
= comparator
;
613 table
->data_freer
= data_freer
;
615 table
->free_entry_list
= NULL
;
617 obstack_init (&table
->entry_stack
);
622 /* Make all buckets empty, placing any chained entries on the free list.
623 Apply the user-specified function data_freer (if any) to the datas of any
627 hash_clear (Hash_table
*table
)
629 struct hash_entry
*bucket
;
631 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
635 struct hash_entry
*cursor
;
636 struct hash_entry
*next
;
638 /* Free the bucket overflow. */
639 for (cursor
= bucket
->next
; cursor
; cursor
= next
)
641 if (table
->data_freer
)
642 (*table
->data_freer
) (cursor
->data
);
646 /* Relinking is done one entry at a time, as it is to be expected
647 that overflows are either rare or short. */
648 cursor
->next
= table
->free_entry_list
;
649 table
->free_entry_list
= cursor
;
652 /* Free the bucket head. */
653 if (table
->data_freer
)
654 (*table
->data_freer
) (bucket
->data
);
660 table
->n_buckets_used
= 0;
661 table
->n_entries
= 0;
664 /* Reclaim all storage associated with a hash table. If a data_freer
665 function has been supplied by the user when the hash table was created,
666 this function applies it to the data of each entry before freeing that
670 hash_free (Hash_table
*table
)
672 struct hash_entry
*bucket
;
673 struct hash_entry
*cursor
;
674 struct hash_entry
*next
;
676 /* Call the user data_freer function. */
677 if (table
->data_freer
&& table
->n_entries
)
679 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
683 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
685 (*table
->data_freer
) (cursor
->data
);
693 obstack_free (&table
->entry_stack
, NULL
);
697 /* Free all bucket overflowed entries. */
698 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
700 for (cursor
= bucket
->next
; cursor
; cursor
= next
)
707 /* Also reclaim the internal list of previously freed entries. */
708 for (cursor
= table
->free_entry_list
; cursor
; cursor
= next
)
716 /* Free the remainder of the hash table structure. */
717 free (table
->bucket
);
721 /* Insertion and deletion. */
723 /* Get a new hash entry for a bucket overflow, possibly by reclying a
724 previously freed one. If this is not possible, allocate a new one. */
726 static struct hash_entry
*
727 allocate_entry (Hash_table
*table
)
729 struct hash_entry
*new;
731 if (table
->free_entry_list
)
733 new = table
->free_entry_list
;
734 table
->free_entry_list
= new->next
;
739 new = (struct hash_entry
*)
740 obstack_alloc (&table
->entry_stack
, sizeof (struct hash_entry
));
742 new = (struct hash_entry
*) malloc (sizeof (struct hash_entry
));
749 /* Free a hash entry which was part of some bucket overflow,
750 saving it for later recycling. */
753 free_entry (Hash_table
*table
, struct hash_entry
*entry
)
756 entry
->next
= table
->free_entry_list
;
757 table
->free_entry_list
= entry
;
760 /* This private function is used to help with insertion and deletion. When
761 ENTRY matches an entry in the table, return a pointer to the corresponding
762 user data and set *BUCKET_HEAD to the head of the selected bucket.
763 Otherwise, return NULL. When DELETE is true and ENTRY matches an entry in
764 the table, unlink the matching entry. */
767 hash_find_entry (Hash_table
*table
, const void *entry
,
768 struct hash_entry
**bucket_head
, bool delete)
770 struct hash_entry
*bucket
771 = table
->bucket
+ table
->hasher (entry
, table
->n_buckets
);
772 struct hash_entry
*cursor
;
774 if (! (bucket
< table
->bucket_limit
))
777 *bucket_head
= bucket
;
779 /* Test for empty bucket. */
780 if (bucket
->data
== NULL
)
783 /* See if the entry is the first in the bucket. */
784 if ((*table
->comparator
) (entry
, bucket
->data
))
786 void *data
= bucket
->data
;
792 struct hash_entry
*next
= bucket
->next
;
794 /* Bump the first overflow entry into the bucket head, then save
795 the previous first overflow entry for later recycling. */
797 free_entry (table
, next
);
808 /* Scan the bucket overflow. */
809 for (cursor
= bucket
; cursor
->next
; cursor
= cursor
->next
)
811 if ((*table
->comparator
) (entry
, cursor
->next
->data
))
813 void *data
= cursor
->next
->data
;
817 struct hash_entry
*next
= cursor
->next
;
819 /* Unlink the entry to delete, then save the freed entry for later
821 cursor
->next
= next
->next
;
822 free_entry (table
, next
);
829 /* No entry found. */
833 /* For an already existing hash table, change the number of buckets through
834 specifying CANDIDATE. The contents of the hash table are preserved. The
835 new number of buckets is automatically selected so as to _guarantee_ that
836 the table may receive at least CANDIDATE different user entries, including
837 those already in the table, before any other growth of the hash table size
838 occurs. If TUNING->IS_N_BUCKETS is true, then CANDIDATE specifies the
839 exact number of buckets desired. */
842 hash_rehash (Hash_table
*table
, unsigned candidate
)
844 Hash_table
*new_table
;
845 struct hash_entry
*bucket
;
846 struct hash_entry
*cursor
;
847 struct hash_entry
*next
;
849 new_table
= hash_initialize (candidate
, table
->tuning
, table
->hasher
,
850 table
->comparator
, table
->data_freer
);
851 if (new_table
== NULL
)
854 /* Merely reuse the extra old space into the new table. */
856 obstack_free (&new_table
->entry_stack
, NULL
);
857 new_table
->entry_stack
= table
->entry_stack
;
859 new_table
->free_entry_list
= table
->free_entry_list
;
861 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
863 for (cursor
= bucket
; cursor
; cursor
= next
)
865 void *data
= cursor
->data
;
866 struct hash_entry
*new_bucket
868 + new_table
->hasher (data
, new_table
->n_buckets
));
870 if (! (new_bucket
< new_table
->bucket_limit
))
875 if (new_bucket
->data
)
877 if (cursor
== bucket
)
879 /* Allocate or recycle an entry, when moving from a bucket
880 header into a bucket overflow. */
881 struct hash_entry
*new_entry
= allocate_entry (new_table
);
883 if (new_entry
== NULL
)
886 new_entry
->data
= data
;
887 new_entry
->next
= new_bucket
->next
;
888 new_bucket
->next
= new_entry
;
892 /* Merely relink an existing entry, when moving from a
893 bucket overflow into a bucket overflow. */
894 cursor
->next
= new_bucket
->next
;
895 new_bucket
->next
= cursor
;
900 /* Free an existing entry, when moving from a bucket
901 overflow into a bucket header. Also take care of the
902 simple case of moving from a bucket header into a bucket
904 new_bucket
->data
= data
;
905 new_table
->n_buckets_used
++;
906 if (cursor
!= bucket
)
907 free_entry (new_table
, cursor
);
911 free (table
->bucket
);
912 table
->bucket
= new_table
->bucket
;
913 table
->bucket_limit
= new_table
->bucket_limit
;
914 table
->n_buckets
= new_table
->n_buckets
;
915 table
->n_buckets_used
= new_table
->n_buckets_used
;
916 table
->free_entry_list
= new_table
->free_entry_list
;
917 /* table->n_entries already holds its value. */
919 table
->entry_stack
= new_table
->entry_stack
;
926 /* If ENTRY matches an entry already in the hash table, return the pointer
927 to the entry from the table. Otherwise, insert ENTRY and return ENTRY.
928 Return NULL if the storage required for insertion cannot be allocated. */
931 hash_insert (Hash_table
*table
, const void *entry
)
934 struct hash_entry
*bucket
;
936 /* The caller cannot insert a NULL entry. */
940 /* If there's a matching entry already in the table, return that. */
941 if ((data
= hash_find_entry (table
, entry
, &bucket
, false)) != NULL
)
944 /* ENTRY is not matched, it should be inserted. */
948 struct hash_entry
*new_entry
= allocate_entry (table
);
950 if (new_entry
== NULL
)
953 /* Add ENTRY in the overflow of the bucket. */
955 new_entry
->data
= (void *) entry
;
956 new_entry
->next
= bucket
->next
;
957 bucket
->next
= new_entry
;
959 return (void *) entry
;
962 /* Add ENTRY right in the bucket head. */
964 bucket
->data
= (void *) entry
;
966 table
->n_buckets_used
++;
968 /* If the growth threshold of the buckets in use has been reached, increase
969 the table size and rehash. There's no point in checking the number of
970 entries: if the hashing function is ill-conditioned, rehashing is not
971 likely to improve it. */
973 if (table
->n_buckets_used
974 > table
->tuning
->growth_threshold
* table
->n_buckets
)
976 /* Check more fully, before starting real work. If tuning arguments
977 became invalid, the second check will rely on proper defaults. */
978 check_tuning (table
);
979 if (table
->n_buckets_used
980 > table
->tuning
->growth_threshold
* table
->n_buckets
)
982 const Hash_tuning
*tuning
= table
->tuning
;
984 = (unsigned) (tuning
->is_n_buckets
985 ? (table
->n_buckets
* tuning
->growth_factor
)
986 : (table
->n_buckets
* tuning
->growth_factor
987 * tuning
->growth_threshold
));
989 /* If the rehash fails, arrange to return NULL. */
990 if (!hash_rehash (table
, candidate
))
995 return (void *) entry
;
998 /* If ENTRY is already in the table, remove it and return the just-deleted
999 data (the user may want to deallocate its storage). If ENTRY is not in the
1000 table, don't modify the table and return NULL. */
1003 hash_delete (Hash_table
*table
, const void *entry
)
1006 struct hash_entry
*bucket
;
1008 data
= hash_find_entry (table
, entry
, &bucket
, true);
1015 table
->n_buckets_used
--;
1017 /* If the shrink threshold of the buckets in use has been reached,
1018 rehash into a smaller table. */
1020 if (table
->n_buckets_used
1021 < table
->tuning
->shrink_threshold
* table
->n_buckets
)
1023 /* Check more fully, before starting real work. If tuning arguments
1024 became invalid, the second check will rely on proper defaults. */
1025 check_tuning (table
);
1026 if (table
->n_buckets_used
1027 < table
->tuning
->shrink_threshold
* table
->n_buckets
)
1029 const Hash_tuning
*tuning
= table
->tuning
;
1031 = (unsigned) (tuning
->is_n_buckets
1032 ? table
->n_buckets
* tuning
->shrink_factor
1033 : (table
->n_buckets
* tuning
->shrink_factor
1034 * tuning
->growth_threshold
));
1036 hash_rehash (table
, candidate
);
1049 hash_print (const Hash_table
*table
)
1051 struct hash_entry
*bucket
;
1053 for (bucket
= table
->bucket
; bucket
< table
->bucket_limit
; bucket
++)
1055 struct hash_entry
*cursor
;
1058 printf ("%d:\n", bucket
- table
->bucket
);
1060 for (cursor
= bucket
; cursor
; cursor
= cursor
->next
)
1062 char *s
= (char *) cursor
->data
;
1065 printf (" %s\n", s
);
1070 #endif /* TESTING */