Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 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 | * hashtab.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 | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 32 | #if 0 |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 33 | # define HT_DEBUG // extra checks for table consistency and statistics |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 34 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 35 | static long hash_count_lookup = 0; // count number of hashtab lookups |
| 36 | static long hash_count_perturb = 0; // count number of "misses" |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 39 | // Magic value for algorithm that walks through the array. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 40 | #define PERTURB_SHIFT 5 |
| 41 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 42 | static int hash_may_resize(hashtab_T *ht, int minitems); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 43 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 44 | #if 0 // currently not used |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Create an empty hash table. |
| 47 | * Returns NULL when out of memory. |
| 48 | */ |
| 49 | hashtab_T * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 50 | hash_create(void) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 51 | { |
| 52 | hashtab_T *ht; |
| 53 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 54 | ht = ALLOC_ONE(hashtab_T); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 55 | if (ht != NULL) |
| 56 | hash_init(ht); |
| 57 | return ht; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | * Initialize an empty hash table. |
| 63 | */ |
| 64 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 65 | hash_init(hashtab_T *ht) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 66 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 67 | // This zeroes all "ht_" entries and all the "hi_key" in "ht_smallarray". |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 68 | CLEAR_POINTER(ht); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 69 | ht->ht_array = ht->ht_smallarray; |
| 70 | ht->ht_mask = HT_INIT_SIZE - 1; |
| 71 | } |
| 72 | |
| 73 | /* |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 74 | * If "ht->ht_flags" has HTFLAGS_FROZEN then give an error message using |
| 75 | * "command" and return TRUE. |
| 76 | */ |
| 77 | int |
| 78 | check_hashtab_frozen(hashtab_T *ht, char *command) |
| 79 | { |
| 80 | if ((ht->ht_flags & HTFLAGS_FROZEN) == 0) |
| 81 | return FALSE; |
| 82 | |
| 83 | semsg(_(e_not_allowed_to_add_or_remove_entries_str), command); |
| 84 | return TRUE; |
| 85 | } |
| 86 | |
| 87 | /* |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 88 | * Free the array of a hash table. Does not free the items it contains! |
| 89 | * If "ht" is not freed then you should call hash_init() next! |
| 90 | */ |
| 91 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 92 | hash_clear(hashtab_T *ht) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 93 | { |
| 94 | if (ht->ht_array != ht->ht_smallarray) |
| 95 | vim_free(ht->ht_array); |
| 96 | } |
| 97 | |
Bram Moolenaar | 0e65511 | 2020-09-11 20:36:36 +0200 | [diff] [blame] | 98 | #if defined(FEAT_SPELL) || defined(FEAT_TERMINAL) || defined(PROTO) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 99 | /* |
| 100 | * Free the array of a hash table and all the keys it contains. The keys must |
| 101 | * have been allocated. "off" is the offset from the start of the allocate |
| 102 | * memory to the location of the key (it's always positive). |
| 103 | */ |
| 104 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 105 | hash_clear_all(hashtab_T *ht, int off) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 106 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 107 | long todo; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 108 | hashitem_T *hi; |
| 109 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 110 | todo = (long)ht->ht_used; |
Yegappan Lakshmanan | 14113fd | 2023-03-07 17:13:51 +0000 | [diff] [blame] | 111 | FOR_ALL_HASHTAB_ITEMS(ht, hi, todo) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 112 | { |
| 113 | if (!HASHITEM_EMPTY(hi)) |
| 114 | { |
| 115 | vim_free(hi->hi_key - off); |
| 116 | --todo; |
| 117 | } |
| 118 | } |
| 119 | hash_clear(ht); |
| 120 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 121 | #endif |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Find "key" in hashtable "ht". "key" must not be NULL. |
| 125 | * Always returns a pointer to a hashitem. If the item was not found then |
| 126 | * HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key |
| 127 | * would be added. |
| 128 | * WARNING: The returned pointer becomes invalid when the hashtable is changed |
| 129 | * (adding, setting or removing an item)! |
| 130 | */ |
| 131 | hashitem_T * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 132 | hash_find(hashtab_T *ht, char_u *key) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 133 | { |
| 134 | return hash_lookup(ht, key, hash_hash(key)); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Like hash_find(), but caller computes "hash". |
| 139 | */ |
| 140 | hashitem_T * |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 141 | hash_lookup(hashtab_T *ht, char_u *key, hash_T hash) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 142 | { |
| 143 | hash_T perturb; |
| 144 | hashitem_T *freeitem; |
| 145 | hashitem_T *hi; |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 146 | unsigned idx; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 147 | |
| 148 | #ifdef HT_DEBUG |
| 149 | ++hash_count_lookup; |
| 150 | #endif |
| 151 | |
| 152 | /* |
| 153 | * Quickly handle the most common situations: |
| 154 | * - return if there is no item at all |
| 155 | * - skip over a removed item |
| 156 | * - return if the item matches |
| 157 | */ |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 158 | idx = (unsigned)(hash & ht->ht_mask); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 159 | hi = &ht->ht_array[idx]; |
| 160 | |
| 161 | if (hi->hi_key == NULL) |
| 162 | return hi; |
| 163 | if (hi->hi_key == HI_KEY_REMOVED) |
| 164 | freeitem = hi; |
| 165 | else if (hi->hi_hash == hash && STRCMP(hi->hi_key, key) == 0) |
| 166 | return hi; |
| 167 | else |
| 168 | freeitem = NULL; |
| 169 | |
| 170 | /* |
| 171 | * Need to search through the table to find the key. The algorithm |
| 172 | * to step through the table starts with large steps, gradually becoming |
| 173 | * smaller down to (1/4 table size + 1). This means it goes through all |
| 174 | * table entries in the end. |
| 175 | * When we run into a NULL key it's clear that the key isn't there. |
| 176 | * Return the first available slot found (can be a slot of a removed |
| 177 | * item). |
| 178 | */ |
| 179 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) |
| 180 | { |
| 181 | #ifdef HT_DEBUG |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 182 | ++hash_count_perturb; // count a "miss" for hashtab lookup |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 183 | #endif |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 184 | idx = (unsigned)((idx << 2U) + idx + perturb + 1U); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 185 | hi = &ht->ht_array[idx & ht->ht_mask]; |
| 186 | if (hi->hi_key == NULL) |
| 187 | return freeitem == NULL ? hi : freeitem; |
| 188 | if (hi->hi_hash == hash |
| 189 | && hi->hi_key != HI_KEY_REMOVED |
| 190 | && STRCMP(hi->hi_key, key) == 0) |
| 191 | return hi; |
| 192 | if (hi->hi_key == HI_KEY_REMOVED && freeitem == NULL) |
| 193 | freeitem = hi; |
| 194 | } |
| 195 | } |
| 196 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 197 | #if defined(FEAT_EVAL) || defined(FEAT_SYN_HL) || defined(PROTO) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Print the efficiency of hashtable lookups. |
| 200 | * Useful when trying different hash algorithms. |
| 201 | * Called when exiting. |
| 202 | */ |
| 203 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 204 | hash_debug_results(void) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 205 | { |
K.Takata | 6e1d31e | 2022-02-03 13:05:32 +0000 | [diff] [blame] | 206 | # ifdef HT_DEBUG |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 207 | fprintf(stderr, "\r\n\r\n\r\n\r\n"); |
| 208 | fprintf(stderr, "Number of hashtable lookups: %ld\r\n", hash_count_lookup); |
| 209 | fprintf(stderr, "Number of perturb loops: %ld\r\n", hash_count_perturb); |
| 210 | fprintf(stderr, "Percentage of perturb loops: %ld%%\r\n", |
| 211 | hash_count_perturb * 100 / hash_count_lookup); |
K.Takata | 6e1d31e | 2022-02-03 13:05:32 +0000 | [diff] [blame] | 212 | # endif |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 213 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 214 | #endif |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Add item with key "key" to hashtable "ht". |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 218 | * "command" is used for the error message when the hashtab if frozen. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 219 | * Returns FAIL when out of memory or the key is already present. |
| 220 | */ |
| 221 | int |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 222 | hash_add(hashtab_T *ht, char_u *key, char *command) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 223 | { |
| 224 | hash_T hash = hash_hash(key); |
| 225 | hashitem_T *hi; |
| 226 | |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 227 | if (check_hashtab_frozen(ht, command)) |
| 228 | return FAIL; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 229 | hi = hash_lookup(ht, key, hash); |
| 230 | if (!HASHITEM_EMPTY(hi)) |
| 231 | { |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 232 | internal_error("hash_add()"); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 233 | return FAIL; |
| 234 | } |
| 235 | return hash_add_item(ht, hi, key, hash); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Add item "hi" with "key" to hashtable "ht". "key" must not be NULL and |
| 240 | * "hi" must have been obtained with hash_lookup() and point to an empty item. |
| 241 | * "hi" is invalid after this! |
| 242 | * Returns OK or FAIL (out of memory). |
| 243 | */ |
| 244 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 245 | hash_add_item( |
| 246 | hashtab_T *ht, |
| 247 | hashitem_T *hi, |
| 248 | char_u *key, |
| 249 | hash_T hash) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 250 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 251 | // If resizing failed before and it fails again we can't add an item. |
Bram Moolenaar | 81b7ecc | 2022-12-26 13:08:06 +0000 | [diff] [blame] | 252 | if (ht->ht_flags & HTFLAGS_ERROR) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 253 | return FAIL; |
| 254 | |
| 255 | ++ht->ht_used; |
Bram Moolenaar | 21c16f8 | 2020-07-14 16:15:34 +0200 | [diff] [blame] | 256 | ++ht->ht_changed; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 257 | if (hi->hi_key == NULL) |
| 258 | ++ht->ht_filled; |
| 259 | hi->hi_key = key; |
| 260 | hi->hi_hash = hash; |
| 261 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 262 | // When the space gets low may resize the array. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 263 | return hash_may_resize(ht, 0); |
| 264 | } |
| 265 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 266 | #if 0 // not used |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 267 | /* |
| 268 | * Overwrite hashtable item "hi" with "key". "hi" must point to the item that |
| 269 | * is to be overwritten. Thus the number of items in the hashtable doesn't |
| 270 | * change. |
| 271 | * Although the key must be identical, the pointer may be different, thus it's |
| 272 | * set anyway (the key is part of an item with that key). |
| 273 | * The caller must take care of freeing the old item. |
| 274 | * "hi" is invalid after this! |
| 275 | */ |
| 276 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 277 | hash_set(hashitem_T *hi, char_u *key) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 278 | { |
| 279 | hi->hi_key = key; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | /* |
| 284 | * Remove item "hi" from hashtable "ht". "hi" must have been obtained with |
| 285 | * hash_lookup(). |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 286 | * "command" is used for the error message when the hashtab if frozen. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 287 | * The caller must take care of freeing the item itself. |
| 288 | */ |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 289 | int |
| 290 | hash_remove(hashtab_T *ht, hashitem_T *hi, char *command) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 291 | { |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 292 | if (check_hashtab_frozen(ht, command)) |
| 293 | return FAIL; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 294 | --ht->ht_used; |
Bram Moolenaar | 21c16f8 | 2020-07-14 16:15:34 +0200 | [diff] [blame] | 295 | ++ht->ht_changed; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 296 | hi->hi_key = HI_KEY_REMOVED; |
| 297 | hash_may_resize(ht, 0); |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 298 | return OK; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Lock a hashtable: prevent that ht_array changes. |
| 303 | * Don't use this when items are to be added! |
| 304 | * Must call hash_unlock() later. |
| 305 | */ |
| 306 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 307 | hash_lock(hashtab_T *ht) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 308 | { |
| 309 | ++ht->ht_locked; |
| 310 | } |
| 311 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 312 | #if defined(FEAT_PROP_POPUP) || defined(PROTO) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 313 | /* |
| 314 | * Lock a hashtable at the specified number of entries. |
| 315 | * Caller must make sure no more than "size" entries will be added. |
| 316 | * Must call hash_unlock() later. |
| 317 | */ |
| 318 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 319 | hash_lock_size(hashtab_T *ht, int size) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 320 | { |
| 321 | (void)hash_may_resize(ht, size); |
| 322 | ++ht->ht_locked; |
| 323 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 324 | #endif |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Unlock a hashtable: allow ht_array changes again. |
| 328 | * Table will be resized (shrink) when necessary. |
| 329 | * This must balance a call to hash_lock(). |
| 330 | */ |
| 331 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 332 | hash_unlock(hashtab_T *ht) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 333 | { |
| 334 | --ht->ht_locked; |
| 335 | (void)hash_may_resize(ht, 0); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Shrink a hashtable when there is too much empty space. |
| 340 | * Grow a hashtable when there is not enough empty space. |
| 341 | * Returns OK or FAIL (out of memory). |
| 342 | */ |
| 343 | static int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 344 | hash_may_resize( |
| 345 | hashtab_T *ht, |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 346 | int minitems) // minimal number of items |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 347 | { |
| 348 | hashitem_T temparray[HT_INIT_SIZE]; |
| 349 | hashitem_T *oldarray, *newarray; |
| 350 | hashitem_T *olditem, *newitem; |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 351 | unsigned newi; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 352 | int todo; |
Bram Moolenaar | d0883fa | 2022-12-26 13:51:26 +0000 | [diff] [blame] | 353 | long_u newsize; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 354 | long_u minsize; |
| 355 | long_u newmask; |
| 356 | hash_T perturb; |
| 357 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 358 | // Don't resize a locked table. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 359 | if (ht->ht_locked > 0) |
| 360 | return OK; |
| 361 | |
| 362 | #ifdef HT_DEBUG |
| 363 | if (ht->ht_used > ht->ht_filled) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 364 | emsg("hash_may_resize(): more used than filled"); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 365 | if (ht->ht_filled >= ht->ht_mask + 1) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 366 | emsg("hash_may_resize(): table completely filled"); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
Bram Moolenaar | d0883fa | 2022-12-26 13:51:26 +0000 | [diff] [blame] | 369 | long_u oldsize = ht->ht_mask + 1; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 370 | if (minitems == 0) |
| 371 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 372 | // Return quickly for small tables with at least two NULL items. NULL |
| 373 | // items are required for the lookup to decide a key isn't there. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 374 | if (ht->ht_filled < HT_INIT_SIZE - 1 |
| 375 | && ht->ht_array == ht->ht_smallarray) |
| 376 | return OK; |
| 377 | |
| 378 | /* |
| 379 | * Grow or refill the array when it's more than 2/3 full (including |
| 380 | * removed items, so that they get cleaned up). |
| 381 | * Shrink the array when it's less than 1/5 full. When growing it is |
| 382 | * at least 1/4 full (avoids repeated grow-shrink operations) |
| 383 | */ |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 384 | if (ht->ht_filled * 3 < oldsize * 2 && ht->ht_used > oldsize / 5) |
| 385 | return OK; |
| 386 | |
| 387 | if (ht->ht_used > 1000) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 388 | minsize = ht->ht_used * 2; // it's big, don't make too much room |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 389 | else |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 390 | minsize = ht->ht_used * 4; // make plenty of room |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
Bram Moolenaar | 7b73d7e | 2019-07-26 21:26:34 +0200 | [diff] [blame] | 394 | // Use specified size. |
| 395 | if ((long_u)minitems < ht->ht_used) // just in case... |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 396 | minitems = (int)ht->ht_used; |
Bram Moolenaar | 7b73d7e | 2019-07-26 21:26:34 +0200 | [diff] [blame] | 397 | minsize = (minitems * 3 + 1) / 2; // array is up to 2/3 full |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | newsize = HT_INIT_SIZE; |
| 401 | while (newsize < minsize) |
| 402 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 403 | newsize <<= 1; // make sure it's always a power of 2 |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 404 | if (newsize == 0) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 405 | return FAIL; // overflow |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (newsize == HT_INIT_SIZE) |
| 409 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 410 | // Use the small array inside the hashdict structure. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 411 | newarray = ht->ht_smallarray; |
| 412 | if (ht->ht_array == newarray) |
| 413 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 414 | // Moving from ht_smallarray to ht_smallarray! Happens when there |
| 415 | // are many removed items. Copy the items to be able to clean up |
| 416 | // removed items. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 417 | mch_memmove(temparray, newarray, sizeof(temparray)); |
| 418 | oldarray = temparray; |
| 419 | } |
| 420 | else |
| 421 | oldarray = ht->ht_array; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 422 | CLEAR_FIELD(ht->ht_smallarray); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 423 | } |
Bram Moolenaar | b3d6143 | 2022-12-25 21:32:09 +0000 | [diff] [blame] | 424 | |
Bram Moolenaar | d0883fa | 2022-12-26 13:51:26 +0000 | [diff] [blame] | 425 | else if (newsize == oldsize && ht->ht_filled * 3 < oldsize * 2) |
Bram Moolenaar | b3d6143 | 2022-12-25 21:32:09 +0000 | [diff] [blame] | 426 | { |
Bram Moolenaar | d0883fa | 2022-12-26 13:51:26 +0000 | [diff] [blame] | 427 | // The hashtab is already at the desired size, and there are not too |
| 428 | // many removed items, bail out. |
Bram Moolenaar | b3d6143 | 2022-12-25 21:32:09 +0000 | [diff] [blame] | 429 | return OK; |
| 430 | } |
| 431 | |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 432 | else |
| 433 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 434 | // Allocate an array. |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 435 | newarray = ALLOC_CLEAR_MULT(hashitem_T, newsize); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 436 | if (newarray == NULL) |
| 437 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 438 | // Out of memory. When there are NULL items still return OK. |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 439 | // Otherwise set ht_flags to HTFLAGS_ERROR, because lookup may |
| 440 | // result in a hang if we add another item. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 441 | if (ht->ht_filled < ht->ht_mask) |
| 442 | return OK; |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 443 | ht->ht_flags |= HTFLAGS_ERROR; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 444 | return FAIL; |
| 445 | } |
| 446 | oldarray = ht->ht_array; |
| 447 | } |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Move all the items from the old array to the new one, placing them in |
| 451 | * the right spot. The new array won't have any removed items, thus this |
| 452 | * is also a cleanup action. |
| 453 | */ |
| 454 | newmask = newsize - 1; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 455 | todo = (int)ht->ht_used; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 456 | for (olditem = oldarray; todo > 0; ++olditem) |
| 457 | if (!HASHITEM_EMPTY(olditem)) |
| 458 | { |
| 459 | /* |
| 460 | * The algorithm to find the spot to add the item is identical to |
| 461 | * the algorithm to find an item in hash_lookup(). But we only |
| 462 | * need to search for a NULL key, thus it's simpler. |
| 463 | */ |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 464 | newi = (unsigned)(olditem->hi_hash & newmask); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 465 | newitem = &newarray[newi]; |
| 466 | |
| 467 | if (newitem->hi_key != NULL) |
| 468 | for (perturb = olditem->hi_hash; ; perturb >>= PERTURB_SHIFT) |
| 469 | { |
Bram Moolenaar | 97d4ea7 | 2012-11-28 18:31:54 +0100 | [diff] [blame] | 470 | newi = (unsigned)((newi << 2U) + newi + perturb + 1U); |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 471 | newitem = &newarray[newi & newmask]; |
| 472 | if (newitem->hi_key == NULL) |
| 473 | break; |
| 474 | } |
| 475 | *newitem = *olditem; |
| 476 | --todo; |
| 477 | } |
| 478 | |
| 479 | if (ht->ht_array != ht->ht_smallarray) |
| 480 | vim_free(ht->ht_array); |
| 481 | ht->ht_array = newarray; |
| 482 | ht->ht_mask = newmask; |
| 483 | ht->ht_filled = ht->ht_used; |
Bram Moolenaar | 21c16f8 | 2020-07-14 16:15:34 +0200 | [diff] [blame] | 484 | ++ht->ht_changed; |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 485 | ht->ht_flags &= ~HTFLAGS_ERROR; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 486 | |
| 487 | return OK; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Get the hash number for a key. |
| 492 | * If you think you know a better hash function: Compile with HT_DEBUG set and |
| 493 | * run a script that uses hashtables a lot. Vim will then print statistics |
| 494 | * when exiting. Try that with the current hash algorithm and yours. The |
| 495 | * lower the percentage the better. |
| 496 | */ |
| 497 | hash_T |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 498 | hash_hash(char_u *key) |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 499 | { |
| 500 | hash_T hash; |
| 501 | char_u *p; |
| 502 | |
| 503 | if ((hash = *key) == 0) |
Bram Moolenaar | 0921ecf | 2016-04-03 22:44:36 +0200 | [diff] [blame] | 504 | return (hash_T)0; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 505 | p = key + 1; |
| 506 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 507 | // A simplistic algorithm that appears to do very well. |
| 508 | // Suggested by George Reilly. |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 509 | while (*p != NUL) |
| 510 | hash = hash * 101 + *p++; |
Bram Moolenaar | c01140a | 2006-03-24 22:21:52 +0000 | [diff] [blame] | 511 | |
| 512 | return hash; |
| 513 | } |