Mercurial > vim
annotate src/hashtab.c @ 28860:3942ea75b4c0 v8.2.4953
patch 8.2.4953: with 'si' inserting '}' after completion goes wrong
Commit: https://github.com/vim/vim/commit/2e444bbef0f36535bf941f007f2961f3f66bbe87
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat May 14 12:54:23 2022 +0100
patch 8.2.4953: with 'si' inserting '}' after completion goes wrong
Problem: With 'smartindent' inserting '}' after completion goes wrong.
Solution: Check the cursor is in indent. (closes https://github.com/vim/vim/issues/10420)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 14 May 2022 14:00:03 +0200 |
parents | 3ad379c0ab28 |
children | 684e6dfa2fba |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9513
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
799 | 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 | |
32 #if 0 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
33 # define HT_DEBUG // extra checks for table consistency and statistics |
799 | 34 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
35 static long hash_count_lookup = 0; // count number of hashtab lookups |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
36 static long hash_count_perturb = 0; // count number of "misses" |
799 | 37 #endif |
38 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
39 // Magic value for algorithm that walks through the array. |
799 | 40 #define PERTURB_SHIFT 5 |
41 | |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
3970
diff
changeset
|
42 static int hash_may_resize(hashtab_T *ht, int minitems); |
799 | 43 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
44 #if 0 // currently not used |
799 | 45 /* |
46 * Create an empty hash table. | |
47 * Returns NULL when out of memory. | |
48 */ | |
49 hashtab_T * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
50 hash_create(void) |
799 | 51 { |
52 hashtab_T *ht; | |
53 | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16764
diff
changeset
|
54 ht = ALLOC_ONE(hashtab_T); |
799 | 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 | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
65 hash_init(hashtab_T *ht) |
799 | 66 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
67 // This zeroes all "ht_" entries and all the "hi_key" in "ht_smallarray". |
20007
aadd1cae2ff5
patch 8.2.0559: clearing a struct is verbose
Bram Moolenaar <Bram@vim.org>
parents:
18798
diff
changeset
|
68 CLEAR_POINTER(ht); |
799 | 69 ht->ht_array = ht->ht_smallarray; |
70 ht->ht_mask = HT_INIT_SIZE - 1; | |
71 } | |
72 | |
73 /* | |
74 * 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! | |
76 */ | |
77 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
78 hash_clear(hashtab_T *ht) |
799 | 79 { |
80 if (ht->ht_array != ht->ht_smallarray) | |
81 vim_free(ht->ht_array); | |
82 } | |
83 | |
22226
4ed106deb772
patch 8.2.1662: :mksession does not restore shared terminal buffer properly
Bram Moolenaar <Bram@vim.org>
parents:
21317
diff
changeset
|
84 #if defined(FEAT_SPELL) || defined(FEAT_TERMINAL) || defined(PROTO) |
799 | 85 /* |
86 * Free the array of a hash table and all the keys it contains. The keys must | |
87 * have been allocated. "off" is the offset from the start of the allocate | |
88 * memory to the location of the key (it's always positive). | |
89 */ | |
90 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
91 hash_clear_all(hashtab_T *ht, int off) |
799 | 92 { |
835 | 93 long todo; |
799 | 94 hashitem_T *hi; |
95 | |
835 | 96 todo = (long)ht->ht_used; |
799 | 97 for (hi = ht->ht_array; todo > 0; ++hi) |
98 { | |
99 if (!HASHITEM_EMPTY(hi)) | |
100 { | |
101 vim_free(hi->hi_key - off); | |
102 --todo; | |
103 } | |
104 } | |
105 hash_clear(ht); | |
106 } | |
15555
d89c5b339c2a
patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
107 #endif |
799 | 108 |
109 /* | |
110 * Find "key" in hashtable "ht". "key" must not be NULL. | |
111 * Always returns a pointer to a hashitem. If the item was not found then | |
112 * HASHITEM_EMPTY() is TRUE. The pointer is then the place where the key | |
113 * would be added. | |
114 * WARNING: The returned pointer becomes invalid when the hashtable is changed | |
115 * (adding, setting or removing an item)! | |
116 */ | |
117 hashitem_T * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
118 hash_find(hashtab_T *ht, char_u *key) |
799 | 119 { |
120 return hash_lookup(ht, key, hash_hash(key)); | |
121 } | |
122 | |
123 /* | |
124 * Like hash_find(), but caller computes "hash". | |
125 */ | |
126 hashitem_T * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
127 hash_lookup(hashtab_T *ht, char_u *key, hash_T hash) |
799 | 128 { |
129 hash_T perturb; | |
130 hashitem_T *freeitem; | |
131 hashitem_T *hi; | |
3970 | 132 unsigned idx; |
799 | 133 |
134 #ifdef HT_DEBUG | |
135 ++hash_count_lookup; | |
136 #endif | |
137 | |
138 /* | |
139 * Quickly handle the most common situations: | |
140 * - return if there is no item at all | |
141 * - skip over a removed item | |
142 * - return if the item matches | |
143 */ | |
3970 | 144 idx = (unsigned)(hash & ht->ht_mask); |
799 | 145 hi = &ht->ht_array[idx]; |
146 | |
147 if (hi->hi_key == NULL) | |
148 return hi; | |
149 if (hi->hi_key == HI_KEY_REMOVED) | |
150 freeitem = hi; | |
151 else if (hi->hi_hash == hash && STRCMP(hi->hi_key, key) == 0) | |
152 return hi; | |
153 else | |
154 freeitem = NULL; | |
155 | |
156 /* | |
157 * Need to search through the table to find the key. The algorithm | |
158 * to step through the table starts with large steps, gradually becoming | |
159 * smaller down to (1/4 table size + 1). This means it goes through all | |
160 * table entries in the end. | |
161 * When we run into a NULL key it's clear that the key isn't there. | |
162 * Return the first available slot found (can be a slot of a removed | |
163 * item). | |
164 */ | |
165 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) | |
166 { | |
167 #ifdef HT_DEBUG | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
168 ++hash_count_perturb; // count a "miss" for hashtab lookup |
799 | 169 #endif |
3970 | 170 idx = (unsigned)((idx << 2U) + idx + perturb + 1U); |
799 | 171 hi = &ht->ht_array[idx & ht->ht_mask]; |
172 if (hi->hi_key == NULL) | |
173 return freeitem == NULL ? hi : freeitem; | |
174 if (hi->hi_hash == hash | |
175 && hi->hi_key != HI_KEY_REMOVED | |
176 && STRCMP(hi->hi_key, key) == 0) | |
177 return hi; | |
178 if (hi->hi_key == HI_KEY_REMOVED && freeitem == NULL) | |
179 freeitem = hi; | |
180 } | |
181 } | |
182 | |
15555
d89c5b339c2a
patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
183 #if defined(FEAT_EVAL) || defined(FEAT_SYN_HL) || defined(PROTO) |
799 | 184 /* |
185 * Print the efficiency of hashtable lookups. | |
186 * Useful when trying different hash algorithms. | |
187 * Called when exiting. | |
188 */ | |
189 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
190 hash_debug_results(void) |
799 | 191 { |
27521
3ad379c0ab28
patch 8.2.4288: preprocessor indents are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
192 # ifdef HT_DEBUG |
799 | 193 fprintf(stderr, "\r\n\r\n\r\n\r\n"); |
194 fprintf(stderr, "Number of hashtable lookups: %ld\r\n", hash_count_lookup); | |
195 fprintf(stderr, "Number of perturb loops: %ld\r\n", hash_count_perturb); | |
196 fprintf(stderr, "Percentage of perturb loops: %ld%%\r\n", | |
197 hash_count_perturb * 100 / hash_count_lookup); | |
27521
3ad379c0ab28
patch 8.2.4288: preprocessor indents are inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
27018
diff
changeset
|
198 # endif |
799 | 199 } |
15555
d89c5b339c2a
patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
200 #endif |
799 | 201 |
202 /* | |
203 * Add item with key "key" to hashtable "ht". | |
204 * Returns FAIL when out of memory or the key is already present. | |
205 */ | |
206 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
207 hash_add(hashtab_T *ht, char_u *key) |
799 | 208 { |
209 hash_T hash = hash_hash(key); | |
210 hashitem_T *hi; | |
211 | |
212 hi = hash_lookup(ht, key, hash); | |
213 if (!HASHITEM_EMPTY(hi)) | |
214 { | |
10359
66f1b5bf3fa6
commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
215 internal_error("hash_add()"); |
799 | 216 return FAIL; |
217 } | |
218 return hash_add_item(ht, hi, key, hash); | |
219 } | |
220 | |
221 /* | |
222 * Add item "hi" with "key" to hashtable "ht". "key" must not be NULL and | |
223 * "hi" must have been obtained with hash_lookup() and point to an empty item. | |
224 * "hi" is invalid after this! | |
225 * Returns OK or FAIL (out of memory). | |
226 */ | |
227 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
228 hash_add_item( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
229 hashtab_T *ht, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
230 hashitem_T *hi, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
231 char_u *key, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
232 hash_T hash) |
799 | 233 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
234 // If resizing failed before and it fails again we can't add an item. |
799 | 235 if (ht->ht_error && hash_may_resize(ht, 0) == FAIL) |
236 return FAIL; | |
237 | |
238 ++ht->ht_used; | |
21317
883aa425656a
patch 8.2.1209: Vim9: test failure
Bram Moolenaar <Bram@vim.org>
parents:
20007
diff
changeset
|
239 ++ht->ht_changed; |
799 | 240 if (hi->hi_key == NULL) |
241 ++ht->ht_filled; | |
242 hi->hi_key = key; | |
243 hi->hi_hash = hash; | |
244 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
245 // When the space gets low may resize the array. |
799 | 246 return hash_may_resize(ht, 0); |
247 } | |
248 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
249 #if 0 // not used |
799 | 250 /* |
251 * Overwrite hashtable item "hi" with "key". "hi" must point to the item that | |
252 * is to be overwritten. Thus the number of items in the hashtable doesn't | |
253 * change. | |
254 * Although the key must be identical, the pointer may be different, thus it's | |
255 * set anyway (the key is part of an item with that key). | |
256 * The caller must take care of freeing the old item. | |
257 * "hi" is invalid after this! | |
258 */ | |
259 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
260 hash_set(hashitem_T *hi, char_u *key) |
799 | 261 { |
262 hi->hi_key = key; | |
263 } | |
264 #endif | |
265 | |
266 /* | |
267 * Remove item "hi" from hashtable "ht". "hi" must have been obtained with | |
268 * hash_lookup(). | |
269 * The caller must take care of freeing the item itself. | |
270 */ | |
271 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
272 hash_remove(hashtab_T *ht, hashitem_T *hi) |
799 | 273 { |
274 --ht->ht_used; | |
21317
883aa425656a
patch 8.2.1209: Vim9: test failure
Bram Moolenaar <Bram@vim.org>
parents:
20007
diff
changeset
|
275 ++ht->ht_changed; |
799 | 276 hi->hi_key = HI_KEY_REMOVED; |
277 hash_may_resize(ht, 0); | |
278 } | |
279 | |
280 /* | |
281 * Lock a hashtable: prevent that ht_array changes. | |
282 * Don't use this when items are to be added! | |
283 * Must call hash_unlock() later. | |
284 */ | |
285 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
286 hash_lock(hashtab_T *ht) |
799 | 287 { |
288 ++ht->ht_locked; | |
289 } | |
290 | |
27018
268f6a3511df
patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents:
22226
diff
changeset
|
291 #if defined(FEAT_PROP_POPUP) || defined(PROTO) |
799 | 292 /* |
293 * Lock a hashtable at the specified number of entries. | |
294 * Caller must make sure no more than "size" entries will be added. | |
295 * Must call hash_unlock() later. | |
296 */ | |
297 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
298 hash_lock_size(hashtab_T *ht, int size) |
799 | 299 { |
300 (void)hash_may_resize(ht, size); | |
301 ++ht->ht_locked; | |
302 } | |
27018
268f6a3511df
patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents:
22226
diff
changeset
|
303 #endif |
799 | 304 |
305 /* | |
306 * Unlock a hashtable: allow ht_array changes again. | |
307 * Table will be resized (shrink) when necessary. | |
308 * This must balance a call to hash_lock(). | |
309 */ | |
310 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
311 hash_unlock(hashtab_T *ht) |
799 | 312 { |
313 --ht->ht_locked; | |
314 (void)hash_may_resize(ht, 0); | |
315 } | |
316 | |
317 /* | |
318 * Shrink a hashtable when there is too much empty space. | |
319 * Grow a hashtable when there is not enough empty space. | |
320 * Returns OK or FAIL (out of memory). | |
321 */ | |
322 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
323 hash_may_resize( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
324 hashtab_T *ht, |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
325 int minitems) // minimal number of items |
799 | 326 { |
327 hashitem_T temparray[HT_INIT_SIZE]; | |
328 hashitem_T *oldarray, *newarray; | |
329 hashitem_T *olditem, *newitem; | |
3970 | 330 unsigned newi; |
799 | 331 int todo; |
332 long_u oldsize, newsize; | |
333 long_u minsize; | |
334 long_u newmask; | |
335 hash_T perturb; | |
336 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
337 // Don't resize a locked table. |
799 | 338 if (ht->ht_locked > 0) |
339 return OK; | |
340 | |
341 #ifdef HT_DEBUG | |
342 if (ht->ht_used > ht->ht_filled) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
10605
diff
changeset
|
343 emsg("hash_may_resize(): more used than filled"); |
799 | 344 if (ht->ht_filled >= ht->ht_mask + 1) |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
10605
diff
changeset
|
345 emsg("hash_may_resize(): table completely filled"); |
799 | 346 #endif |
347 | |
348 if (minitems == 0) | |
349 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
350 // Return quickly for small tables with at least two NULL items. NULL |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
351 // items are required for the lookup to decide a key isn't there. |
799 | 352 if (ht->ht_filled < HT_INIT_SIZE - 1 |
353 && ht->ht_array == ht->ht_smallarray) | |
354 return OK; | |
355 | |
356 /* | |
357 * Grow or refill the array when it's more than 2/3 full (including | |
358 * removed items, so that they get cleaned up). | |
359 * Shrink the array when it's less than 1/5 full. When growing it is | |
360 * at least 1/4 full (avoids repeated grow-shrink operations) | |
361 */ | |
362 oldsize = ht->ht_mask + 1; | |
363 if (ht->ht_filled * 3 < oldsize * 2 && ht->ht_used > oldsize / 5) | |
364 return OK; | |
365 | |
366 if (ht->ht_used > 1000) | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
367 minsize = ht->ht_used * 2; // it's big, don't make too much room |
799 | 368 else |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
369 minsize = ht->ht_used * 4; // make plenty of room |
799 | 370 } |
371 else | |
372 { | |
17508
34966be2e856
patch 8.1.1752: resizing hashtable is inefficient
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
373 // Use specified size. |
34966be2e856
patch 8.1.1752: resizing hashtable is inefficient
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
374 if ((long_u)minitems < ht->ht_used) // just in case... |
835 | 375 minitems = (int)ht->ht_used; |
17508
34966be2e856
patch 8.1.1752: resizing hashtable is inefficient
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
376 minsize = (minitems * 3 + 1) / 2; // array is up to 2/3 full |
799 | 377 } |
378 | |
379 newsize = HT_INIT_SIZE; | |
380 while (newsize < minsize) | |
381 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
382 newsize <<= 1; // make sure it's always a power of 2 |
799 | 383 if (newsize == 0) |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
384 return FAIL; // overflow |
799 | 385 } |
386 | |
387 if (newsize == HT_INIT_SIZE) | |
388 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
389 // Use the small array inside the hashdict structure. |
799 | 390 newarray = ht->ht_smallarray; |
391 if (ht->ht_array == newarray) | |
392 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
393 // Moving from ht_smallarray to ht_smallarray! Happens when there |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
394 // are many removed items. Copy the items to be able to clean up |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
395 // removed items. |
799 | 396 mch_memmove(temparray, newarray, sizeof(temparray)); |
397 oldarray = temparray; | |
398 } | |
399 else | |
400 oldarray = ht->ht_array; | |
20007
aadd1cae2ff5
patch 8.2.0559: clearing a struct is verbose
Bram Moolenaar <Bram@vim.org>
parents:
18798
diff
changeset
|
401 CLEAR_FIELD(ht->ht_smallarray); |
799 | 402 } |
403 else | |
404 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
405 // Allocate an array. |
20007
aadd1cae2ff5
patch 8.2.0559: clearing a struct is verbose
Bram Moolenaar <Bram@vim.org>
parents:
18798
diff
changeset
|
406 newarray = ALLOC_CLEAR_MULT(hashitem_T, newsize); |
799 | 407 if (newarray == NULL) |
408 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
409 // Out of memory. When there are NULL items still return OK. |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
410 // Otherwise set ht_error, because lookup may result in a hang if |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
411 // we add another item. |
799 | 412 if (ht->ht_filled < ht->ht_mask) |
413 return OK; | |
414 ht->ht_error = TRUE; | |
415 return FAIL; | |
416 } | |
417 oldarray = ht->ht_array; | |
418 } | |
419 | |
420 /* | |
421 * Move all the items from the old array to the new one, placing them in | |
422 * the right spot. The new array won't have any removed items, thus this | |
423 * is also a cleanup action. | |
424 */ | |
425 newmask = newsize - 1; | |
835 | 426 todo = (int)ht->ht_used; |
799 | 427 for (olditem = oldarray; todo > 0; ++olditem) |
428 if (!HASHITEM_EMPTY(olditem)) | |
429 { | |
430 /* | |
431 * The algorithm to find the spot to add the item is identical to | |
432 * the algorithm to find an item in hash_lookup(). But we only | |
433 * need to search for a NULL key, thus it's simpler. | |
434 */ | |
3970 | 435 newi = (unsigned)(olditem->hi_hash & newmask); |
799 | 436 newitem = &newarray[newi]; |
437 | |
438 if (newitem->hi_key != NULL) | |
439 for (perturb = olditem->hi_hash; ; perturb >>= PERTURB_SHIFT) | |
440 { | |
3970 | 441 newi = (unsigned)((newi << 2U) + newi + perturb + 1U); |
799 | 442 newitem = &newarray[newi & newmask]; |
443 if (newitem->hi_key == NULL) | |
444 break; | |
445 } | |
446 *newitem = *olditem; | |
447 --todo; | |
448 } | |
449 | |
450 if (ht->ht_array != ht->ht_smallarray) | |
451 vim_free(ht->ht_array); | |
452 ht->ht_array = newarray; | |
453 ht->ht_mask = newmask; | |
454 ht->ht_filled = ht->ht_used; | |
21317
883aa425656a
patch 8.2.1209: Vim9: test failure
Bram Moolenaar <Bram@vim.org>
parents:
20007
diff
changeset
|
455 ++ht->ht_changed; |
799 | 456 ht->ht_error = FALSE; |
457 | |
458 return OK; | |
459 } | |
460 | |
461 /* | |
462 * Get the hash number for a key. | |
463 * If you think you know a better hash function: Compile with HT_DEBUG set and | |
464 * run a script that uses hashtables a lot. Vim will then print statistics | |
465 * when exiting. Try that with the current hash algorithm and yours. The | |
466 * lower the percentage the better. | |
467 */ | |
468 hash_T | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
469 hash_hash(char_u *key) |
799 | 470 { |
471 hash_T hash; | |
472 char_u *p; | |
473 | |
474 if ((hash = *key) == 0) | |
8839
9fa567d13551
commit https://github.com/vim/vim/commit/0921ecff1c5a74541bad6c073e8ade32247403d8
Christian Brabandt <cb@256bit.org>
parents:
7823
diff
changeset
|
475 return (hash_T)0; |
799 | 476 p = key + 1; |
477 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
478 // A simplistic algorithm that appears to do very well. |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
17508
diff
changeset
|
479 // Suggested by George Reilly. |
799 | 480 while (*p != NUL) |
481 hash = hash * 101 + *p++; | |
482 | |
483 return hash; | |
484 } |