]>
git.saurik.com Git - bison.git/blob - lib/hash.c
1 /* hash.c -- hash table maintenance
2 Copyright 1995, 2001 Free Software Foundation, Inc.
3 Written by Greg McGary <gkm@gnu.ai.mit.edu>
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.
27 static void hash_rehash
__P((struct hash_table
* ht
));
28 static unsigned long round_up_2
__P((unsigned long rough
));
30 /* Implement double hashing with open addressing. The table size is
31 always a power of two. The secondary (`increment') hash function
32 is forced to return an odd-value, in order to be relatively prime
33 to the table size. This guarantees that the increment can
34 potentially hit every slot in the table during collision
37 void *hash_deleted_item
= &hash_deleted_item
;
39 /* Force the table size to be a power of two, possibly rounding up the
43 hash_init (struct hash_table
* ht
, unsigned long size
,
44 hash_func_t hash_1
, hash_func_t hash_2
, hash_cmp_func_t hash_cmp
)
46 ht
->ht_size
= round_up_2 (size
);
47 if (ht
->ht_size
> (128 * 1024)) /* prevent size from getting out of hand */
49 ht
->ht_vec
= (void**) XCALLOC (struct token
*, ht
->ht_size
);
51 error (1, 0, _("can't allocate %ld bytes for hash table: memory exhausted"),
52 ht
->ht_size
* sizeof(struct token
*));
53 ht
->ht_capacity
= ht
->ht_size
* 15 / 16; /* 93.75% loading factor */
55 ht
->ht_collisions
= 0;
58 ht
->ht_hash_1
= hash_1
;
59 ht
->ht_hash_2
= hash_2
;
60 ht
->ht_compare
= hash_cmp
;
63 /* Load an array of items into `ht'. */
66 hash_load (struct hash_table
* ht
, void *item_table
, unsigned long cardinality
, unsigned long size
)
68 char *items
= (char *) item_table
;
71 hash_insert (ht
, items
);
76 /* Returns the address of the table slot matching `key'. If `key' is
77 not found, return the address of an empty slot suitable for
78 inserting `key'. The caller is responsible for incrementing
79 ht_fill on insertion. */
82 hash_find_slot (struct hash_table
* ht
, void const *key
)
85 void **deleted_slot
= 0;
86 unsigned int hash_2
= 0;
87 unsigned int hash_1
= (*ht
->ht_hash_1
) (key
);
92 hash_1
%= ht
->ht_size
;
93 slot
= &ht
->ht_vec
[hash_1
];
97 if (*slot
== hash_deleted_item
)
99 if (deleted_slot
== 0)
106 if ((*ht
->ht_compare
) (key
, *slot
) == 0)
111 hash_2
= (*ht
->ht_hash_2
) (key
) | 1;
117 hash_find_item (struct hash_table
* ht
, void const *key
)
119 void **slot
= hash_find_slot (ht
, key
);
120 return ((HASH_VACANT (*slot
)) ? 0 : *slot
);
124 hash_insert (struct hash_table
* ht
, void *item
)
126 void **slot
= hash_find_slot (ht
, item
);
127 return hash_insert_at (ht
, item
, slot
);
131 hash_insert_at (struct hash_table
* ht
, void *item
, void const *slot
)
133 const void *old_item
= *(const void **) slot
;
134 if (HASH_VACANT (old_item
))
139 *(void const **) slot
= item
;
140 if (ht
->ht_fill
>= ht
->ht_capacity
)
146 hash_delete (struct hash_table
* ht
, void const *item
)
148 void **slot
= hash_find_slot (ht
, item
);
149 return hash_delete_at (ht
, slot
);
153 hash_delete_at (struct hash_table
* ht
, void const *slot
)
155 const void *item
= *(const void **) slot
;
156 if (!HASH_VACANT (item
))
158 *(void const **) slot
= hash_deleted_item
;
167 hash_free_items (struct hash_table
* ht
)
169 void **vec
= ht
->ht_vec
;
170 void **end
= &vec
[ht
->ht_size
];
171 for (; vec
< end
; vec
++)
174 if (!HASH_VACANT (item
))
182 hash_delete_items (struct hash_table
* ht
)
184 void **vec
= ht
->ht_vec
;
185 void **end
= &vec
[ht
->ht_size
];
186 for (; vec
< end
; vec
++)
189 ht
->ht_collisions
= 0;
195 hash_free (struct hash_table
* ht
, int free_items
)
198 hash_free_items (ht
);
206 hash_map (struct hash_table
*ht
, hash_map_func_t map
)
209 void **end
= &ht
->ht_vec
[ht
->ht_size
];
211 for (slot
= ht
->ht_vec
; slot
< end
; slot
++)
213 if (!HASH_VACANT (*slot
))
218 /* Double the size of the hash table in the event of overflow... */
221 hash_rehash (struct hash_table
* ht
)
223 unsigned long old_ht_size
= ht
->ht_size
;
224 void **old_vec
= ht
->ht_vec
;
230 ht
->ht_capacity
= ht
->ht_size
- (ht
->ht_size
>> 4);
231 ht
->ht_vec
= (void **) XCALLOC (struct token
*, ht
->ht_size
);
233 for (ovp
= old_vec
; ovp
< &old_vec
[old_ht_size
]; ovp
++)
237 slot
= hash_find_slot (ht
, *ovp
);
244 hash_print_stats (struct hash_table
*ht
, FILE *out_FILE
)
246 fprintf (out_FILE
, _("Load=%ld/%ld=%.0f%%, "), ht
->ht_fill
, ht
->ht_size
,
247 100.0 * (double) ht
->ht_fill
/ (double) ht
->ht_size
);
248 fprintf (out_FILE
, _("Rehash=%d, "), ht
->ht_rehashes
);
249 fprintf (out_FILE
, _("Collisions=%ld/%ld=%.0f%%"), ht
->ht_collisions
, ht
->ht_lookups
,
251 ? (100.0 * (double) ht
->ht_collisions
/ (double) ht
->ht_lookups
)
255 /* Dump all items into a NULL-terminated vector. Use the
256 user-supplied vector, or malloc one. */
259 hash_dump (struct hash_table
*ht
, void **vector_0
, qsort_cmp_t compare
)
263 void **end
= &ht
->ht_vec
[ht
->ht_size
];
266 vector_0
= XMALLOC (void *, ht
->ht_fill
+ 1);
269 for (slot
= ht
->ht_vec
; slot
< end
; slot
++)
270 if (!HASH_VACANT (*slot
))
275 qsort (vector_0
, ht
->ht_fill
, sizeof (void *), compare
);
279 /* Round a given number up to the nearest power of 2. */
282 round_up_2 (unsigned long rough
)