blob: ad1163c4e68093357fc823c0222acf8ce95ddbe1 [file] [log] [blame]
Bram Moolenaar6abd8e92005-01-19 22:21:15 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * hashtable.c: Handling of a hashtable with Vim-specific properties.
12 *
13 * Each item in a hashtable has a NUL terminated string key. A key can appear
14 * only once in the table.
15 *
16 * A hash number is computed from the key for quick lookup. When the hashes
17 * of two different keys point to the same entry an algorithm is used to
18 * iterate over other entries in the table until the right one is found.
19 * To make the iteration work removed keys are different from entries where a
20 * key was never present.
21 *
22 * The mechanism has been partly based on how Python Dictionaries are
23 * implemented. The algorithm is from Knuth Vol. 3, Sec. 6.4.
24 *
25 * The hashtable grows to accommodate more entries when needed. At least 1/3
26 * of the entries is empty to keep the lookup efficient (at the cost of extra
27 * memory).
28 */
29
30#include "vim.h"
31
32#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL) || defined(PROTO)
33
Bram Moolenaar6ac54292005-02-02 23:07:25 +000034#if 0
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000035# define HT_DEBUG /* extra checks for table consistency */
36#endif
37
38/* Magic value for algorithm that walks through the array. */
39#define PERTURB_SHIFT 5
40
Bram Moolenaar7df2d662005-01-25 22:18:08 +000041static int hash_may_resize __ARGS((hashtab_T *ht));
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000042
43#if 0 /* not used */
44/*
45 * Create an empty hash table.
46 * Returns NULL when out of memory.
47 */
Bram Moolenaar7df2d662005-01-25 22:18:08 +000048 hashtab_T *
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000049hash_create()
50{
Bram Moolenaar7df2d662005-01-25 22:18:08 +000051 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000052
Bram Moolenaar7df2d662005-01-25 22:18:08 +000053 ht = (hashtab_T *)alloc(sizeof(hashtab_T));
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000054 if (ht != NULL)
55 hash_init(ht);
56 return ht;
57}
58#endif
59
60/*
61 * Initialize an empty hash table.
62 */
63 void
64hash_init(ht)
Bram Moolenaar7df2d662005-01-25 22:18:08 +000065 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000066{
67 /* This zeroes all "ht_" entries and all the "hi_key" in "ht_smallarray". */
Bram Moolenaar7df2d662005-01-25 22:18:08 +000068 vim_memset(ht, 0, sizeof(hashtab_T));
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000069 ht->ht_array = ht->ht_smallarray;
70 ht->ht_mask = HT_INIT_SIZE - 1;
71}
72
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000073/*
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000074 * Free the array of a hash table. Does not free the items it contains!
75 * If "ht" is not freed then you should call hash_init() next!
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000076 */
77 void
Bram Moolenaardcaf10e2005-01-21 11:55:25 +000078hash_clear(ht)
Bram Moolenaar7df2d662005-01-25 22:18:08 +000079 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000080{
81 if (ht->ht_array != ht->ht_smallarray)
82 vim_free(ht->ht_array);
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000083}
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000084
85/*
86 * Find "key" in hashtable "ht". "key" must not be NULL.
87 * Always returns a pointer to a hashitem. If the item was not found then
88 * HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key
89 * would be added.
90 * WARNING: The returned pointer becomes invalid when the hashtable is changed
91 * (adding, setting or removing an item)!
92 */
Bram Moolenaar7df2d662005-01-25 22:18:08 +000093 hashitem_T *
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000094hash_find(ht, key)
Bram Moolenaar7df2d662005-01-25 22:18:08 +000095 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +000096 char_u *key;
97{
98 return hash_lookup(ht, key, hash_hash(key));
99}
100
101/*
102 * Like hash_find(), but caller computes "hash".
103 */
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000104 hashitem_T *
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000105hash_lookup(ht, key, hash)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000106 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000107 char_u *key;
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000108 hash_T hash;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000109{
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000110 hash_T perturb;
111 hashitem_T *freeitem;
112 hashitem_T *hi;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000113 int idx;
114
115 /*
116 * Quickly handle the most common situations:
117 * - return if there is no item at all
118 * - skip over a removed item
119 * - return if the item matches
120 */
121 idx = hash & ht->ht_mask;
122 hi = &ht->ht_array[idx];
123
124 if (hi->hi_key == NULL)
125 return hi;
126 if (hi->hi_key == HI_KEY_REMOVED)
127 freeitem = hi;
128 else if (hi->hi_hash == hash && STRCMP(hi->hi_key, key) == 0)
129 return hi;
130 else
131 freeitem = NULL;
132
133 /*
134 * Need to search through the table to find the key. The algorithm
135 * to step through the table starts with large steps, gradually becoming
136 * smaller down to (1/4 table size + 1). This means it goes through all
137 * table entries in the end.
138 * When we run into a NULL key it's clear that the key isn't there.
139 * Return the first available slot found (can be a slot of a removed
140 * item).
141 */
142 for (perturb = hash; ; perturb >>= PERTURB_SHIFT)
143 {
144 idx = (idx << 2) + idx + perturb + 1;
145 hi = &ht->ht_array[idx & ht->ht_mask];
146 if (hi->hi_key == NULL)
147 return freeitem == NULL ? hi : freeitem;
148 if (hi->hi_hash == hash
149 && hi->hi_key != HI_KEY_REMOVED
150 && STRCMP(hi->hi_key, key) == 0)
151 return hi;
152 if (hi->hi_key == HI_KEY_REMOVED && freeitem == NULL)
153 freeitem = hi;
154 }
155}
156
157/*
158 * Add item with key "key" to hashtable "ht".
159 * Returns FAIL when out of memory or the key is already present.
160 */
161 int
162hash_add(ht, key)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000163 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000164 char_u *key;
165{
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000166 hash_T hash = hash_hash(key);
167 hashitem_T *hi;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000168
169 hi = hash_lookup(ht, key, hash);
170 if (!HASHITEM_EMPTY(hi))
171 {
172 EMSG2(_(e_intern2), "hash_add()");
173 return FAIL;
174 }
175 return hash_add_item(ht, hi, key, hash);
176}
177
178/*
179 * Add item "hi" with "key" to hashtable "ht". "key" must not be NULL and
180 * "hi" must have been obtained with hash_lookup() and point to an empty item.
181 * "hi" is invalid after this!
182 * Returns OK or FAIL (out of memory).
183 */
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000184 int
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000185hash_add_item(ht, hi, key, hash)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000186 hashtab_T *ht;
187 hashitem_T *hi;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000188 char_u *key;
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000189 hash_T hash;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000190{
191 /* If resizing failed before and it fails again we can't add an item. */
192 if (ht->ht_error && hash_may_resize(ht) == FAIL)
193 return FAIL;
194
195 ++ht->ht_used;
196 if (hi->hi_key == NULL)
197 ++ht->ht_filled;
198 hi->hi_key = key;
199 hi->hi_hash = hash;
200
201 /* When the space gets low may resize the array. */
202 return hash_may_resize(ht);
203}
204
205#if 0 /* not used */
206/*
207 * Overwrite hashtable item "hi" with "key". "hi" must point to the item that
208 * is to be overwritten. Thus the number of items in the hashtable doesn't
209 * change.
210 * Although the key must be identical, the pointer may be different, thus it's
211 * set anyway (the key is part of an item with that key).
212 * The caller must take care of freeing the old item.
213 * "hi" is invalid after this!
214 */
215 void
216hash_set(hi, key)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000217 hashitem_T *hi;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000218 char_u *key;
219{
220 hi->hi_key = key;
221}
222#endif
223
224/*
225 * Remove item "hi" from hashtable "ht". "hi" must have been obtained with
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000226 * hash_lookup() and point to an empty item.
227 * The caller must take care of freeing the item itself.
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000228 */
229 void
230hash_remove(ht, hi)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000231 hashtab_T *ht;
232 hashitem_T *hi;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000233{
234 --ht->ht_used;
235 hi->hi_key = HI_KEY_REMOVED;
236 hash_may_resize(ht);
237}
238
239/*
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000240 * Lock a hashtable: prevent that ht_array changes.
241 * Don't use this when items are to be added!
242 * Must call hash_unlock() later.
243 */
244 void
245hash_lock(ht)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000246 hashtab_T *ht;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000247{
248 ++ht->ht_locked;
249}
250
251/*
252 * Unlock a hashtable: allow ht_array changes again.
253 * Table will be resized (shrink) when necessary.
254 * This must balance a call to hash_lock().
255 */
256 void
257hash_unlock(ht)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000258 hashtab_T *ht;
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000259{
260 --ht->ht_locked;
261 (void)hash_may_resize(ht);
262}
263
264/*
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000265 * Shrink a hashtable when there is too much empty space.
266 * Grow a hashtable when there is not enough empty space.
267 * Returns OK or FAIL (out of memory).
268 */
269 static int
270hash_may_resize(ht)
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000271 hashtab_T *ht;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000272{
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000273 hashitem_T temparray[HT_INIT_SIZE];
274 hashitem_T *oldarray, *newarray;
275 hashitem_T *olditem, *newitem;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000276 int newi;
277 int todo;
278 long_u oldsize, newsize;
279 long_u minsize;
280 long_u newmask;
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000281 hash_T perturb;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000282
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000283 /* Don't resize a locked table. */
284 if (ht->ht_locked > 0)
285 return OK;
286
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000287#ifdef HT_DEBUG
288 if (ht->ht_used > ht->ht_filled)
289 EMSG("hash_may_resize(): more used than filled");
290 if (ht->ht_filled >= ht->ht_mask + 1)
291 EMSG("hash_may_resize(): table completely filled");
292#endif
293
294 /* Return quickly for small tables with at least two NULL items. NULL
295 * items are required for the lookup to decide a key isn't there. */
296 if (ht->ht_filled < HT_INIT_SIZE - 1 && ht->ht_array == ht->ht_smallarray)
297 return OK;
298
299 /*
300 * Grow or refill the array when it's more than 2/3 full (including
301 * removed items, so that they get cleaned up).
302 * Shrink the array when it's less than 1/5 full. When growing it is at
303 * least 1/4 full (avoids repeated grow-shrink operations)
304 */
305 oldsize = ht->ht_mask + 1;
306 if (ht->ht_filled * 3 < oldsize * 2 && ht->ht_used > oldsize / 5)
307 return OK;
308
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000309 if (ht->ht_used > 1000)
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000310 minsize = ht->ht_used * 2; /* it's big, don't make too much room */
311 else
312 minsize = ht->ht_used * 4; /* make plenty of room */
313 newsize = HT_INIT_SIZE;
314 while (newsize < minsize)
315 {
316 newsize <<= 1; /* make sure it's always a power of 2 */
317 if (newsize == 0)
318 return FAIL; /* overflow */
319 }
320
321 if (newsize == HT_INIT_SIZE)
322 {
323 /* Use the small array inside the hashdict structure. */
324 newarray = ht->ht_smallarray;
325 if (ht->ht_array == newarray)
326 {
327 /* Moving from ht_smallarray to ht_smallarray! Happens when there
328 * are many removed items. Copy the items to be able to clean up
329 * removed items. */
330 mch_memmove(temparray, newarray, sizeof(temparray));
331 oldarray = temparray;
332 }
333 else
334 oldarray = ht->ht_array;
335 }
336 else
337 {
338 /* Allocate an array. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000339 newarray = (hashitem_T *)alloc((unsigned)
340 (sizeof(hashitem_T) * newsize));
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000341 if (newarray == NULL)
342 {
343 /* Out of memory. When there are NULL items still return OK.
344 * Otherwise set ht_error, because lookup may result in a hang if
345 * we add another item. */
346 if (ht->ht_filled < ht->ht_mask)
347 return OK;
348 ht->ht_error = TRUE;
349 return FAIL;
350 }
351 oldarray = ht->ht_array;
352 }
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000353 vim_memset(newarray, 0, (size_t)(sizeof(hashitem_T) * newsize));
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000354
355 /*
356 * Move all the items from the old array to the new one, placing them in
357 * the right spot. The new array won't have any removed items, thus this
358 * is also a cleanup action.
359 */
360 newmask = newsize - 1;
361 todo = ht->ht_used;
362 for (olditem = oldarray; todo > 0; ++olditem)
Bram Moolenaar6ac54292005-02-02 23:07:25 +0000363 if (!HASHITEM_EMPTY(olditem))
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000364 {
365 /*
366 * The algorithm to find the spot to add the item is identical to
367 * the algorithm to find an item in hash_lookup(). But we only
368 * need to search for a NULL key, thus it's simpler.
369 */
370 newi = olditem->hi_hash & newmask;
371 newitem = &newarray[newi];
372
373 if (newitem->hi_key != NULL)
374 for (perturb = olditem->hi_hash; ; perturb >>= PERTURB_SHIFT)
375 {
376 newi = (newi << 2) + newi + perturb + 1;
377 newitem = &newarray[newi & newmask];
378 if (newitem->hi_key == NULL)
379 break;
380 }
381 *newitem = *olditem;
382 --todo;
383 }
384
385 if (ht->ht_array != ht->ht_smallarray)
386 vim_free(ht->ht_array);
387 ht->ht_array = newarray;
388 ht->ht_mask = newmask;
389 ht->ht_filled = ht->ht_used;
390 ht->ht_error = FALSE;
391
392 return OK;
393}
394
395/*
396 * Get the hash number for a key. Uses the ElfHash algorithm, which is
397 * supposed to have an even distribution (suggested by Charles Campbell).
398 */
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000399 hash_T
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000400hash_hash(key)
401 char_u *key;
402{
Bram Moolenaar7df2d662005-01-25 22:18:08 +0000403 hash_T hash = 0;
404 hash_T g;
Bram Moolenaar6abd8e92005-01-19 22:21:15 +0000405 char_u *p = key;
406
407 while (*p != NUL)
408 {
409 hash = (hash << 4) + *p++; /* clear low 4 bits of hash, add char */
410 g = hash & 0xf0000000L; /* g has high 4 bits of hash only */
411 if (g != 0)
412 hash ^= g >> 24; /* xor g's high 4 bits into hash */
413 }
414
415 return hash;
416}
417
418#endif