1 /* hash.c -- hash table maintenance
2 Copyright (C) 1995, 1999, 2002 Free Software Foundation, Inc.
3 Written by Greg McGary <gkm@gnu.org> <greg@mcgary.org>
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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #define CALLOC(t, n) ((t *) calloc (sizeof (t), (n)))
24 #define MALLOC(t, n) ((t *) xmalloc (sizeof (t) * (n)))
25 #define REALLOC(o, t, n) ((t *) xrealloc ((o), sizeof (t) * (n)))
26 #define CLONE(o, t, n) ((t *) memcpy (MALLOC (t, (n)), (o), sizeof (t) * (n)))
28 static void hash_rehash
__P((struct hash_table
* ht
));
29 static unsigned long round_up_2
__P((unsigned long rough
));
31 /* Implement double hashing with open addressing. The table size is
32 always a power of two. The secondary (`increment') hash function
33 is forced to return an odd-value, in order to be relatively prime
34 to the table size. This guarantees that the increment can
35 potentially hit every slot in the table during collision
38 void *hash_deleted_item
= &hash_deleted_item
;
40 /* Force the table size to be a power of two, possibly rounding up the
44 hash_init (struct hash_table
*ht
, unsigned long size
,
45 hash_func_t hash_1
, hash_func_t hash_2
, hash_cmp_func_t hash_cmp
)
47 ht
->ht_size
= round_up_2 (size
);
48 ht
->ht_empty_slots
= ht
->ht_size
;
49 ht
->ht_vec
= (void**) CALLOC (struct token
*, ht
->ht_size
);
52 fprintf (stderr
, _("can't allocate %ld bytes for hash table: memory exhausted"),
53 ht
->ht_size
* sizeof(struct token
*));
57 ht
->ht_capacity
= ht
->ht_size
- (ht
->ht_size
/ 16); /* 93.75% loading factor */
59 ht
->ht_collisions
= 0;
62 ht
->ht_hash_1
= hash_1
;
63 ht
->ht_hash_2
= hash_2
;
64 ht
->ht_compare
= hash_cmp
;
67 /* Load an array of items into `ht'. */
70 hash_load (struct hash_table
*ht
, void *item_table
,
71 unsigned long cardinality
, unsigned long size
)
73 char *items
= (char *) item_table
;
76 hash_insert (ht
, items
);
81 /* Returns the address of the table slot matching `key'. If `key' is
82 not found, return the address of an empty slot suitable for
83 inserting `key'. The caller is responsible for incrementing
84 ht_fill on insertion. */
87 hash_find_slot (struct hash_table
*ht
, const void *key
)
90 void **deleted_slot
= 0;
91 unsigned int hash_2
= 0;
92 unsigned int hash_1
= (*ht
->ht_hash_1
) (key
);
97 hash_1
&= (ht
->ht_size
- 1);
98 slot
= &ht
->ht_vec
[hash_1
];
101 return (deleted_slot
? deleted_slot
: slot
);
102 if (*slot
== hash_deleted_item
)
104 if (deleted_slot
== 0)
111 if ((*ht
->ht_compare
) (key
, *slot
) == 0)
116 hash_2
= (*ht
->ht_hash_2
) (key
) | 1;
122 hash_find_item (struct hash_table
*ht
, const void *key
)
124 void **slot
= hash_find_slot (ht
, key
);
125 return ((HASH_VACANT (*slot
)) ? 0 : *slot
);
129 hash_insert (struct hash_table
*ht
, void *item
)
131 void **slot
= hash_find_slot (ht
, item
);
132 void *old_item
= slot
? *slot
: 0;
133 hash_insert_at (ht
, item
, slot
);
134 return ((HASH_VACANT (old_item
)) ? 0 : old_item
);
138 hash_insert_at (struct hash_table
*ht
, void *item
, const void *slot
)
140 void *old_item
= *(void **) slot
;
141 if (HASH_VACANT (old_item
))
145 ht
->ht_empty_slots
--;
148 *(void const **) slot
= item
;
149 if (ht
->ht_empty_slots
< ht
->ht_size
- ht
->ht_capacity
)
152 return (void *) hash_find_slot (ht
, item
);
155 return (void *) slot
;
159 hash_delete (struct hash_table
*ht
, const void *item
)
161 void **slot
= hash_find_slot (ht
, item
);
162 return hash_delete_at (ht
, slot
);
166 hash_delete_at (struct hash_table
*ht
, const void *slot
)
168 void *item
= *(void **) slot
;
169 if (!HASH_VACANT (item
))
171 *(void const **) slot
= hash_deleted_item
;
180 hash_free_items (struct hash_table
*ht
)
182 void **vec
= ht
->ht_vec
;
183 void **end
= &vec
[ht
->ht_size
];
184 for (; vec
< end
; vec
++)
187 if (!HASH_VACANT (item
))
192 ht
->ht_empty_slots
= ht
->ht_size
;
196 hash_delete_items (struct hash_table
*ht
)
198 void **vec
= ht
->ht_vec
;
199 void **end
= &vec
[ht
->ht_size
];
200 for (; vec
< end
; vec
++)
203 ht
->ht_collisions
= 0;
206 ht
->ht_empty_slots
= ht
->ht_size
;
210 hash_free (struct hash_table
*ht
, int free_items
)
213 hash_free_items (ht
);
217 ht
->ht_empty_slots
= ht
->ht_size
;
225 hash_map (struct hash_table
*ht
, hash_map_func_t map
)
228 void **end
= &ht
->ht_vec
[ht
->ht_size
];
230 for (slot
= ht
->ht_vec
; slot
< end
; slot
++)
232 if (!HASH_VACANT (*slot
))
238 hash_map_arg (struct hash_table
*ht
, hash_map_arg_func_t map
, void *arg
)
241 void **end
= &ht
->ht_vec
[ht
->ht_size
];
243 for (slot
= ht
->ht_vec
; slot
< end
; slot
++)
245 if (!HASH_VACANT (*slot
))
250 /* Double the size of the hash table in the event of overflow... */
253 hash_rehash (struct hash_table
*ht
)
255 unsigned long old_ht_size
= ht
->ht_size
;
256 void **old_vec
= ht
->ht_vec
;
259 if (ht
->ht_fill
>= ht
->ht_capacity
)
262 ht
->ht_capacity
= ht
->ht_size
- (ht
->ht_size
>> 4);
265 ht
->ht_vec
= (void **) CALLOC (struct token
*, ht
->ht_size
);
267 for (ovp
= old_vec
; ovp
< &old_vec
[old_ht_size
]; ovp
++)
269 if (! HASH_VACANT (*ovp
))
271 void **slot
= hash_find_slot (ht
, *ovp
);
275 ht
->ht_empty_slots
= ht
->ht_size
- ht
->ht_fill
;
280 hash_print_stats (struct hash_table
*ht
, FILE *out_FILE
)
282 /* GKM FIXME: honor NO_FLOAT */
283 fprintf (out_FILE
, _("Load=%ld/%ld=%.0f%%, "), ht
->ht_fill
, ht
->ht_size
,
284 100.0 * (double) ht
->ht_fill
/ (double) ht
->ht_size
);
285 fprintf (out_FILE
, _("Rehash=%d, "), ht
->ht_rehashes
);
286 fprintf (out_FILE
, _("Collisions=%ld/%ld=%.0f%%"), ht
->ht_collisions
, ht
->ht_lookups
,
288 ? (100.0 * (double) ht
->ht_collisions
/ (double) ht
->ht_lookups
)
292 /* Dump all items into a NULL-terminated vector. Use the
293 user-supplied vector, or malloc one. */
296 hash_dump (struct hash_table
*ht
, void **vector_0
, qsort_cmp_t compare
)
300 void **end
= &ht
->ht_vec
[ht
->ht_size
];
303 vector_0
= MALLOC (void *, ht
->ht_fill
+ 1);
306 for (slot
= ht
->ht_vec
; slot
< end
; slot
++)
307 if (!HASH_VACANT (*slot
))
312 qsort (vector_0
, ht
->ht_fill
, sizeof (void *), compare
);
316 /* Round a given number up to the nearest power of 2. */
319 round_up_2 (unsigned long n
)
327 #if !defined(HAVE_LIMITS_H) || ULONG_MAX > 4294967295
328 /* We only need this on systems where unsigned long is >32 bits. */