1 /* hash - hashing table processing.
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Written by Jim Meyering <meyering@ascend.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* A generic hash table package. */
21 /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
22 obstacks instead of malloc, and recompile `hash.c' with same setting. */
25 # if PROTOTYPES || __STDC__
26 # define PARAMS(Args) Args
28 # define PARAMS(Args) ()
32 typedef unsigned (*Hash_hasher
) PARAMS ((const void *, unsigned));
33 typedef bool (*Hash_comparator
) PARAMS ((const void *, const void *));
34 typedef void (*Hash_data_freer
) PARAMS ((void *));
35 typedef bool (*Hash_processor
) PARAMS ((void *, void *));
40 struct hash_entry
*next
;
45 /* This structure is mainly used for `hash_initialize', see the block
46 documentation of `hash_reset_tuning' for more complete comments. */
48 float shrink_threshold
; /* ratio of used buckets to trigger a shrink */
49 float shrink_factor
; /* ratio of new smaller size to original size */
50 float growth_threshold
; /* ratio of used buckets to trigger a growth */
51 float growth_factor
; /* ratio of new bigger size to original size */
52 bool is_n_buckets
; /* if CANDIDATE really means table size */
55 typedef struct hash_tuning Hash_tuning
;
59 /* The array of buckets starts at BUCKET and extends to BUCKET_LIMIT-1,
60 for a possibility of N_BUCKETS. Among those, N_BUCKETS_USED buckets
61 are not empty, there are N_ENTRIES active entries in the table. */
62 struct hash_entry
*bucket
;
63 struct hash_entry
*bucket_limit
;
65 unsigned n_buckets_used
;
68 /* Tuning arguments, kept in a physicaly separate structure. */
69 const Hash_tuning
*tuning
;
71 /* Three functions are given to `hash_initialize', see the documentation
72 block for this function. In a word, HASHER randomizes a user entry
73 into a number up from 0 up to some maximum minus 1; COMPARATOR returns
74 true if two user entries compare equally; and DATA_FREER is the cleanup
75 function for a user entry. */
77 Hash_comparator comparator
;
78 Hash_data_freer data_freer
;
80 /* A linked list of freed struct hash_entry structs. */
81 struct hash_entry
*free_entry_list
;
84 /* Whenever obstacks are used, it is possible to allocate all overflowed
85 entries into a single stack, so they all can be freed in a single
86 operation. It is not clear if the speedup is worth the trouble. */
87 struct obstack entry_stack
;
91 typedef struct hash_table Hash_table
;
93 /* Information and lookup. */
94 unsigned hash_get_n_buckets
PARAMS ((const Hash_table
*));
95 unsigned hash_get_n_buckets_used
PARAMS ((const Hash_table
*));
96 unsigned hash_get_n_entries
PARAMS ((const Hash_table
*));
97 unsigned hash_get_max_bucket_length
PARAMS ((const Hash_table
*));
98 bool hash_table_ok
PARAMS ((const Hash_table
*));
99 void hash_print_statistics
PARAMS ((const Hash_table
*, FILE *));
100 void *hash_lookup
PARAMS ((const Hash_table
*, const void *));
103 void *hash_get_first
PARAMS ((const Hash_table
*));
104 void *hash_get_next
PARAMS ((const Hash_table
*, const void *));
105 unsigned hash_get_entries
PARAMS ((const Hash_table
*, void **, unsigned));
106 unsigned hash_do_for_each
PARAMS ((const Hash_table
*, Hash_processor
, void *));
108 /* Allocation and clean-up. */
109 unsigned hash_string
PARAMS ((const char *, unsigned));
110 void hash_reset_tuning
PARAMS ((Hash_tuning
*));
111 Hash_table
*hash_initialize
PARAMS ((unsigned, const Hash_tuning
*,
112 Hash_hasher
, Hash_comparator
,
114 void hash_clear
PARAMS ((Hash_table
*));
115 void hash_free
PARAMS ((Hash_table
*));
117 /* Insertion and deletion. */
118 bool hash_rehash
PARAMS ((Hash_table
*, unsigned));
119 void *hash_insert
PARAMS ((Hash_table
*, const void *));
120 void *hash_delete
PARAMS ((Hash_table
*, const void *));