]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ****************************************************************************** | |
3 | * Copyright (C) 1997-2001, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ****************************************************************************** | |
6 | * Date Name Description | |
7 | * 03/22/00 aliu Adapted from original C++ ICU Hashtable. | |
8 | * 07/06/01 aliu Modified to support int32_t keys on | |
9 | * platforms with sizeof(void*) < 32. | |
10 | ****************************************************************************** | |
11 | */ | |
12 | ||
13 | #include "uhash.h" | |
14 | #include "unicode/ustring.h" | |
15 | #include "cstring.h" | |
16 | #include "cmemory.h" | |
17 | #include "uassert.h" | |
18 | ||
19 | /* This hashtable is implemented as a double hash. All elements are | |
20 | * stored in a single array with no secondary storage for collision | |
21 | * resolution (no linked list, etc.). When there is a hash collision | |
22 | * (when two unequal keys have the same hashcode) we resolve this by | |
23 | * using a secondary hash. The secondary hash is an increment | |
24 | * computed as a hash function (a different one) of the primary | |
25 | * hashcode. This increment is added to the initial hash value to | |
26 | * obtain further slots assigned to the same hash code. For this to | |
27 | * work, the length of the array and the increment must be relatively | |
28 | * prime. The easiest way to achieve this is to have the length of | |
29 | * the array be prime, and the increment be any value from | |
30 | * 1..length-1. | |
31 | * | |
32 | * Hashcodes are 32-bit integers. We make sure all hashcodes are | |
33 | * non-negative by masking off the top bit. This has two effects: (1) | |
34 | * modulo arithmetic is simplified. If we allowed negative hashcodes, | |
35 | * then when we computed hashcode % length, we could get a negative | |
36 | * result, which we would then have to adjust back into range. It's | |
37 | * simpler to just make hashcodes non-negative. (2) It makes it easy | |
38 | * to check for empty vs. occupied slots in the table. We just mark | |
39 | * empty or deleted slots with a negative hashcode. | |
40 | * | |
41 | * The central function is _uhash_find(). This function looks for a | |
42 | * slot matching the given key and hashcode. If one is found, it | |
43 | * returns a pointer to that slot. If the table is full, and no match | |
44 | * is found, it returns NULL -- in theory. This would make the code | |
45 | * more complicated, since all callers of _uhash_find() would then | |
46 | * have to check for a NULL result. To keep this from happening, we | |
47 | * don't allow the table to fill. When there is only one | |
48 | * empty/deleted slot left, uhash_put() will refuse to increase the | |
49 | * count, and fail. This simplifies the code. In practice, one will | |
50 | * seldom encounter this using default UHashtables. However, if a | |
51 | * hashtable is set to a U_FIXED resize policy, or if memory is | |
52 | * exhausted, then the table may fill. | |
53 | * | |
54 | * High and low water ratios control rehashing. They establish levels | |
55 | * of fullness (from 0 to 1) outside of which the data array is | |
56 | * reallocated and repopulated. Setting the low water ratio to zero | |
57 | * means the table will never shrink. Setting the high water ratio to | |
58 | * one means the table will never grow. The ratios should be | |
59 | * coordinated with the ratio between successive elements of the | |
60 | * PRIMES table, so that when the primeIndex is incremented or | |
61 | * decremented during rehashing, it brings the ratio of count / length | |
62 | * back into the desired range (between low and high water ratios). | |
63 | */ | |
64 | ||
65 | /******************************************************************** | |
66 | * PRIVATE Constants, Macros | |
67 | ********************************************************************/ | |
68 | ||
69 | /* This is a list of non-consecutive primes chosen such that | |
70 | * PRIMES[i+1] ~ 2*PRIMES[i]. (Currently, the ratio ranges from 1.81 | |
71 | * to 2.18; the inverse ratio ranges from 0.459 to 0.552.) If this | |
72 | * ratio is changed, the low and high water ratios should also be | |
73 | * adjusted to suit. | |
74 | */ | |
75 | static const int32_t PRIMES[] = { | |
76 | 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, 16411, 32771, | |
77 | 65537, 131101, 262147, 524309, 1048583, 2097169, 4194319, 8388617, | |
78 | 16777259, 33554467, 67108879, 134217757, 268435459, 536870923, | |
79 | 1073741827, 2147483647 | |
80 | }; | |
81 | ||
82 | #define PRIMES_LENGTH (sizeof(PRIMES) / sizeof(PRIMES[0])) | |
83 | ||
84 | /* These ratios are tuned to the PRIMES array such that a resize | |
85 | * places the table back into the zone of non-resizing. That is, | |
86 | * after a call to _uhash_rehash(), a subsequent call to | |
87 | * _uhash_rehash() should do nothing (should not churn). This is only | |
88 | * a potential problem with U_GROW_AND_SHRINK. | |
89 | */ | |
90 | static const float RESIZE_POLICY_RATIO_TABLE[6] = { | |
91 | /* low, high water ratio */ | |
92 | 0.0F, 0.5F, /* U_GROW: Grow on demand, do not shrink */ | |
93 | 0.1F, 0.5F, /* U_GROW_AND_SHRINK: Grow and shrink on demand */ | |
94 | 0.0F, 1.0F /* U_FIXED: Never change size */ | |
95 | }; | |
96 | ||
97 | /* | |
98 | Invariants for hashcode values: | |
99 | ||
100 | * DELETED < 0 | |
101 | * EMPTY < 0 | |
102 | * Real hashes >= 0 | |
103 | ||
104 | Hashcodes may not start out this way, but internally they are | |
105 | adjusted so that they are always positive. We assume 32-bit | |
106 | hashcodes; adjust these constants for other hashcode sizes. | |
107 | */ | |
108 | #define HASH_DELETED ((int32_t) 0x80000000) | |
109 | #define HASH_EMPTY ((int32_t) HASH_DELETED + 1) | |
110 | ||
111 | #define IS_EMPTY_OR_DELETED(x) ((x) < 0) | |
112 | ||
113 | /* This macro expects a UHashTok.pointer as its keypointer and | |
114 | valuepointer parameters */ | |
115 | #define HASH_DELETE_KEY_VALUE(hash, keypointer, valuepointer) \ | |
116 | if (hash->keyDeleter != NULL && keypointer != NULL) { \ | |
117 | (*hash->keyDeleter)(keypointer); \ | |
118 | } \ | |
119 | if (hash->valueDeleter != NULL && valuepointer != NULL) { \ | |
120 | (*hash->valueDeleter)(valuepointer); \ | |
121 | } | |
122 | ||
123 | /* | |
124 | * Constants for hinting whether a key or value is an integer | |
125 | * or a pointer. If a hint bit is zero, then the associated | |
126 | * token is assumed to be an integer. | |
127 | */ | |
128 | #define HINT_KEY_POINTER (1) | |
129 | #define HINT_VALUE_POINTER (2) | |
130 | ||
131 | /******************************************************************** | |
132 | * Debugging | |
133 | ********************************************************************/ | |
134 | ||
135 | ||
136 | /******************************************************************** | |
137 | * PRIVATE Prototypes | |
138 | ********************************************************************/ | |
139 | ||
140 | static UHashtable* _uhash_create(UHashFunction *keyHash, UKeyComparator *keyComp, | |
141 | int32_t primeIndex, UErrorCode *status); | |
142 | ||
143 | static void _uhash_allocate(UHashtable *hash, int32_t primeIndex, | |
144 | UErrorCode *status); | |
145 | ||
146 | static void _uhash_rehash(UHashtable *hash); | |
147 | ||
148 | static UHashElement* _uhash_find(const UHashtable *hash, UHashTok key, | |
149 | int32_t hashcode); | |
150 | ||
151 | static UHashTok _uhash_put(UHashtable *hash, | |
152 | UHashTok key, | |
153 | UHashTok value, | |
154 | int8_t hint, | |
155 | UErrorCode *status); | |
156 | ||
157 | static UHashTok _uhash_remove(UHashtable *hash, | |
158 | UHashTok key); | |
159 | ||
160 | static UHashTok _uhash_internalRemoveElement(UHashtable *hash, UHashElement* e); | |
161 | ||
162 | static UHashTok _uhash_setElement(UHashtable* hash, UHashElement* e, | |
163 | int32_t hashcode, | |
164 | UHashTok key, UHashTok value, | |
165 | int8_t hint); | |
166 | ||
167 | static void _uhash_internalSetResizePolicy(UHashtable *hash, enum UHashResizePolicy policy); | |
168 | ||
169 | /******************************************************************** | |
170 | * PUBLIC API | |
171 | ********************************************************************/ | |
172 | ||
173 | U_CAPI UHashtable* U_EXPORT2 | |
174 | uhash_open(UHashFunction *keyHash, UKeyComparator *keyComp, | |
175 | UErrorCode *status) { | |
176 | ||
177 | return _uhash_create(keyHash, keyComp, 3, status); | |
178 | } | |
179 | ||
180 | U_CAPI UHashtable* U_EXPORT2 | |
181 | uhash_openSize(UHashFunction *keyHash, UKeyComparator *keyComp, | |
182 | int32_t size, | |
183 | UErrorCode *status) { | |
184 | ||
185 | /* Find the smallest index i for which PRIMES[i] >= size. */ | |
186 | int32_t i = 0; | |
187 | while (i<(PRIMES_LENGTH-1) && PRIMES[i]<size) { | |
188 | ++i; | |
189 | } | |
190 | ||
191 | return _uhash_create(keyHash, keyComp, i, status); | |
192 | } | |
193 | ||
194 | U_CAPI void U_EXPORT2 | |
195 | uhash_close(UHashtable *hash) { | |
196 | U_ASSERT(hash != NULL); | |
197 | if (hash->elements != NULL) { | |
198 | if (hash->keyDeleter != NULL || hash->valueDeleter != NULL) { | |
199 | int32_t pos=-1; | |
200 | UHashElement *e; | |
201 | while ((e = (UHashElement*) uhash_nextElement(hash, &pos)) != NULL) { | |
202 | HASH_DELETE_KEY_VALUE(hash, e->key.pointer, e->value.pointer); | |
203 | } | |
204 | } | |
205 | uprv_free(hash->elements); | |
206 | hash->elements = NULL; | |
207 | } | |
208 | uprv_free(hash); | |
209 | } | |
210 | ||
211 | U_CAPI UHashFunction *U_EXPORT2 | |
212 | uhash_setKeyHasher(UHashtable *hash, UHashFunction *fn) { | |
213 | UHashFunction *result = hash->keyHasher; | |
214 | hash->keyHasher = fn; | |
215 | return result; | |
216 | } | |
217 | ||
218 | U_CAPI UKeyComparator *U_EXPORT2 | |
219 | uhash_setKeyComparator(UHashtable *hash, UKeyComparator *fn) { | |
220 | UKeyComparator *result = hash->keyComparator; | |
221 | hash->keyComparator = fn; | |
222 | return result; | |
223 | } | |
224 | ||
225 | U_CAPI UObjectDeleter *U_EXPORT2 | |
226 | uhash_setKeyDeleter(UHashtable *hash, UObjectDeleter *fn) { | |
227 | UObjectDeleter *result = hash->keyDeleter; | |
228 | hash->keyDeleter = fn; | |
229 | return result; | |
230 | } | |
231 | ||
232 | U_CAPI UObjectDeleter *U_EXPORT2 | |
233 | uhash_setValueDeleter(UHashtable *hash, UObjectDeleter *fn) { | |
234 | UObjectDeleter *result = hash->valueDeleter; | |
235 | hash->valueDeleter = fn; | |
236 | return result; | |
237 | } | |
238 | ||
239 | U_CAPI void U_EXPORT2 | |
240 | uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { | |
241 | _uhash_internalSetResizePolicy(hash, policy); | |
242 | hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); | |
243 | hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); | |
244 | _uhash_rehash(hash); | |
245 | } | |
246 | ||
247 | U_CAPI int32_t U_EXPORT2 | |
248 | uhash_count(const UHashtable *hash) { | |
249 | return hash->count; | |
250 | } | |
251 | ||
252 | U_CAPI void* U_EXPORT2 | |
253 | uhash_get(const UHashtable *hash, | |
254 | const void* key) { | |
255 | UHashTok keyholder; | |
256 | keyholder.pointer = (void*) key; | |
257 | return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.pointer; | |
258 | } | |
259 | ||
260 | U_CAPI void* U_EXPORT2 | |
261 | uhash_iget(const UHashtable *hash, | |
262 | int32_t key) { | |
263 | UHashTok keyholder; | |
264 | keyholder.integer = key; | |
265 | return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.pointer; | |
266 | } | |
267 | ||
268 | U_CAPI int32_t U_EXPORT2 | |
269 | uhash_geti(const UHashtable *hash, | |
270 | const void* key) { | |
271 | UHashTok keyholder; | |
272 | keyholder.pointer = (void*) key; | |
273 | return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.integer; | |
274 | } | |
275 | ||
276 | U_CAPI void* U_EXPORT2 | |
277 | uhash_put(UHashtable *hash, | |
278 | void* key, | |
279 | void* value, | |
280 | UErrorCode *status) { | |
281 | UHashTok keyholder, valueholder; | |
282 | keyholder.pointer = key; | |
283 | valueholder.pointer = value; | |
284 | return _uhash_put(hash, keyholder, valueholder, | |
285 | HINT_KEY_POINTER | HINT_VALUE_POINTER, | |
286 | status).pointer; | |
287 | } | |
288 | ||
289 | U_CAPI void* U_EXPORT2 | |
290 | uhash_iput(UHashtable *hash, | |
291 | int32_t key, | |
292 | void* value, | |
293 | UErrorCode *status) { | |
294 | UHashTok keyholder, valueholder; | |
295 | keyholder.integer = key; | |
296 | valueholder.pointer = value; | |
297 | return _uhash_put(hash, keyholder, valueholder, | |
298 | HINT_VALUE_POINTER, | |
299 | status).pointer; | |
300 | } | |
301 | ||
302 | U_CAPI int32_t U_EXPORT2 | |
303 | uhash_puti(UHashtable *hash, | |
304 | void* key, | |
305 | int32_t value, | |
306 | UErrorCode *status) { | |
307 | UHashTok keyholder, valueholder; | |
308 | keyholder.pointer = key; | |
309 | valueholder.integer = value; | |
310 | return _uhash_put(hash, keyholder, valueholder, | |
311 | HINT_KEY_POINTER, | |
312 | status).integer; | |
313 | } | |
314 | ||
315 | U_CAPI void* U_EXPORT2 | |
316 | uhash_remove(UHashtable *hash, | |
317 | const void* key) { | |
318 | UHashTok keyholder; | |
319 | keyholder.pointer = (void*) key; | |
320 | return _uhash_remove(hash, keyholder).pointer; | |
321 | } | |
322 | ||
323 | U_CAPI void* U_EXPORT2 | |
324 | uhash_iremove(UHashtable *hash, | |
325 | int32_t key) { | |
326 | UHashTok keyholder; | |
327 | keyholder.integer = key; | |
328 | return _uhash_remove(hash, keyholder).pointer; | |
329 | } | |
330 | ||
331 | U_CAPI int32_t U_EXPORT2 | |
332 | uhash_removei(UHashtable *hash, | |
333 | const void* key) { | |
334 | UHashTok keyholder; | |
335 | keyholder.pointer = (void*) key; | |
336 | return _uhash_remove(hash, keyholder).integer; | |
337 | } | |
338 | ||
339 | U_CAPI void U_EXPORT2 | |
340 | uhash_removeAll(UHashtable *hash) { | |
341 | int32_t pos = -1; | |
342 | const UHashElement *e; | |
343 | U_ASSERT(hash != NULL); | |
344 | if (hash->count != 0) { | |
345 | while ((e = uhash_nextElement(hash, &pos)) != NULL) { | |
346 | uhash_removeElement(hash, e); | |
347 | } | |
348 | } | |
349 | U_ASSERT(hash->count == 0); | |
350 | } | |
351 | ||
352 | U_CAPI const UHashElement* U_EXPORT2 | |
353 | uhash_find(const UHashtable *hash, const void* key) { | |
354 | UHashTok keyholder; | |
355 | const UHashElement *e; | |
356 | keyholder.pointer = (void*) key; | |
357 | e = _uhash_find(hash, keyholder, hash->keyHasher(keyholder)); | |
358 | return IS_EMPTY_OR_DELETED(e->hashcode) ? NULL : e; | |
359 | } | |
360 | ||
361 | U_CAPI const UHashElement* U_EXPORT2 | |
362 | uhash_nextElement(const UHashtable *hash, int32_t *pos) { | |
363 | /* Walk through the array until we find an element that is not | |
364 | * EMPTY and not DELETED. | |
365 | */ | |
366 | int32_t i; | |
367 | U_ASSERT(hash != NULL); | |
368 | for (i = *pos + 1; i < hash->length; ++i) { | |
369 | if (!IS_EMPTY_OR_DELETED(hash->elements[i].hashcode)) { | |
370 | *pos = i; | |
371 | return &(hash->elements[i]); | |
372 | } | |
373 | } | |
374 | ||
375 | /* No more elements */ | |
376 | return NULL; | |
377 | } | |
378 | ||
379 | U_CAPI void* U_EXPORT2 | |
380 | uhash_removeElement(UHashtable *hash, const UHashElement* e) { | |
381 | U_ASSERT(hash != NULL); | |
382 | U_ASSERT(e != NULL); | |
383 | if (!IS_EMPTY_OR_DELETED(e->hashcode)) { | |
384 | return _uhash_internalRemoveElement(hash, (UHashElement*) e).pointer; | |
385 | } | |
386 | return NULL; | |
387 | } | |
388 | ||
389 | /******************************************************************** | |
390 | * UHashTok convenience | |
391 | ********************************************************************/ | |
392 | ||
393 | /** | |
394 | * Return a UHashTok for an integer. | |
395 | */ | |
396 | U_CAPI UHashTok U_EXPORT2 | |
397 | uhash_toki(int32_t i) { | |
398 | UHashTok tok; | |
399 | tok.integer = i; | |
400 | return tok; | |
401 | } | |
402 | ||
403 | /** | |
404 | * Return a UHashTok for a pointer. | |
405 | */ | |
406 | U_CAPI UHashTok U_EXPORT2 | |
407 | uhash_tokp(void* p) { | |
408 | UHashTok tok; | |
409 | tok.pointer = p; | |
410 | return tok; | |
411 | } | |
412 | ||
413 | /******************************************************************** | |
414 | * PUBLIC Key Hash Functions | |
415 | ********************************************************************/ | |
416 | ||
417 | /* | |
418 | Compute the hash by iterating sparsely over about 32 (up to 63) | |
419 | characters spaced evenly through the string. For each character, | |
420 | multiply the previous hash value by a prime number and add the new | |
421 | character in, like a linear congruential random number generator, | |
422 | producing a pseudorandom deterministic value well distributed over | |
423 | the output range. [LIU] | |
424 | */ | |
425 | ||
426 | #define STRING_HASH(TYPE, STR, STRLEN, DEREF) \ | |
427 | int32_t hash = 0; \ | |
428 | const TYPE *p = (const TYPE*) STR; \ | |
429 | if (p != NULL) { \ | |
430 | int32_t len = (int32_t)(STRLEN); \ | |
431 | int32_t inc = ((len - 32) / 32) + 1; \ | |
432 | const TYPE *limit = p + len; \ | |
433 | while (p<limit) { \ | |
434 | hash = (hash * 37) + DEREF; \ | |
435 | p += inc; \ | |
436 | } \ | |
437 | } \ | |
438 | return hash | |
439 | ||
440 | U_CAPI int32_t U_EXPORT2 | |
441 | uhash_hashUChars(const UHashTok key) { | |
442 | STRING_HASH(UChar, key.pointer, u_strlen(p), *p); | |
443 | } | |
444 | ||
445 | /* Used by UnicodeString to compute its hashcode - Not public API. */ | |
446 | U_CAPI int32_t U_EXPORT2 | |
447 | uhash_hashUCharsN(const UChar *str, int32_t length) { | |
448 | STRING_HASH(UChar, str, length, *p); | |
449 | } | |
450 | ||
451 | U_CAPI int32_t U_EXPORT2 | |
452 | uhash_hashChars(const UHashTok key) { | |
453 | STRING_HASH(uint8_t, key.pointer, uprv_strlen((char*)p), *p); | |
454 | } | |
455 | ||
456 | U_CAPI int32_t U_EXPORT2 | |
457 | uhash_hashIChars(const UHashTok key) { | |
458 | STRING_HASH(uint8_t, key.pointer, uprv_strlen((char*)p), uprv_tolower(*p)); | |
459 | } | |
460 | ||
461 | /******************************************************************** | |
462 | * PUBLIC Comparator Functions | |
463 | ********************************************************************/ | |
464 | ||
465 | U_CAPI UBool U_EXPORT2 | |
466 | uhash_compareUChars(const UHashTok key1, const UHashTok key2) { | |
467 | const UChar *p1 = (const UChar*) key1.pointer; | |
468 | const UChar *p2 = (const UChar*) key2.pointer; | |
469 | if (p1 == p2) { | |
470 | return TRUE; | |
471 | } | |
472 | if (p1 == NULL || p2 == NULL) { | |
473 | return FALSE; | |
474 | } | |
475 | while (*p1 != 0 && *p1 == *p2) { | |
476 | ++p1; | |
477 | ++p2; | |
478 | } | |
479 | return (UBool)(*p1 == *p2); | |
480 | } | |
481 | ||
482 | U_CAPI UBool U_EXPORT2 | |
483 | uhash_compareChars(const UHashTok key1, const UHashTok key2) { | |
484 | const char *p1 = (const char*) key1.pointer; | |
485 | const char *p2 = (const char*) key2.pointer; | |
486 | if (p1 == p2) { | |
487 | return TRUE; | |
488 | } | |
489 | if (p1 == NULL || p2 == NULL) { | |
490 | return FALSE; | |
491 | } | |
492 | while (*p1 != 0 && *p1 == *p2) { | |
493 | ++p1; | |
494 | ++p2; | |
495 | } | |
496 | return (UBool)(*p1 == *p2); | |
497 | } | |
498 | ||
499 | U_CAPI UBool U_EXPORT2 | |
500 | uhash_compareIChars(const UHashTok key1, const UHashTok key2) { | |
501 | const char *p1 = (const char*) key1.pointer; | |
502 | const char *p2 = (const char*) key2.pointer; | |
503 | if (p1 == p2) { | |
504 | return TRUE; | |
505 | } | |
506 | if (p1 == NULL || p2 == NULL) { | |
507 | return FALSE; | |
508 | } | |
509 | while (*p1 != 0 && uprv_tolower(*p1) == uprv_tolower(*p2)) { | |
510 | ++p1; | |
511 | ++p2; | |
512 | } | |
513 | return (UBool)(*p1 == *p2); | |
514 | } | |
515 | ||
516 | /******************************************************************** | |
517 | * PUBLIC int32_t Support Functions | |
518 | ********************************************************************/ | |
519 | ||
520 | U_CAPI int32_t U_EXPORT2 | |
521 | uhash_hashLong(const UHashTok key) { | |
522 | return key.integer; | |
523 | } | |
524 | ||
525 | U_CAPI UBool U_EXPORT2 | |
526 | uhash_compareLong(const UHashTok key1, const UHashTok key2) { | |
527 | return (UBool)(key1.integer == key2.integer); | |
528 | } | |
529 | ||
530 | /******************************************************************** | |
531 | * PUBLIC Deleter Functions | |
532 | ********************************************************************/ | |
533 | ||
534 | U_CAPI void U_EXPORT2 | |
535 | uhash_freeBlock(void *obj) { | |
536 | uprv_free(obj); | |
537 | } | |
538 | ||
539 | /******************************************************************** | |
540 | * PRIVATE Implementation | |
541 | ********************************************************************/ | |
542 | ||
543 | static UHashtable* | |
544 | _uhash_create(UHashFunction *keyHash, UKeyComparator *keyComp, | |
545 | int32_t primeIndex, | |
546 | UErrorCode *status) { | |
547 | UHashtable *result; | |
548 | ||
549 | if (U_FAILURE(*status)) return NULL; | |
550 | U_ASSERT(keyHash != NULL); | |
551 | U_ASSERT(keyComp != NULL); | |
552 | ||
553 | result = (UHashtable*) uprv_malloc(sizeof(UHashtable)); | |
554 | if (result == NULL) { | |
555 | *status = U_MEMORY_ALLOCATION_ERROR; | |
556 | return NULL; | |
557 | } | |
558 | ||
559 | result->keyHasher = keyHash; | |
560 | result->keyComparator = keyComp; | |
561 | result->keyDeleter = NULL; | |
562 | result->valueDeleter = NULL; | |
563 | _uhash_internalSetResizePolicy(result, U_GROW); | |
564 | ||
565 | _uhash_allocate(result, primeIndex, status); | |
566 | ||
567 | if (U_FAILURE(*status)) { | |
568 | uprv_free(result); | |
569 | return NULL; | |
570 | } | |
571 | ||
572 | return result; | |
573 | } | |
574 | ||
575 | /** | |
576 | * Allocate internal data array of a size determined by the given | |
577 | * prime index. If the index is out of range it is pinned into range. | |
578 | * If the allocation fails the status is set to | |
579 | * U_MEMORY_ALLOCATION_ERROR and all array storage is freed. In | |
580 | * either case the previous array pointer is overwritten. | |
581 | * | |
582 | * Caller must ensure primeIndex is in range 0..PRIME_LENGTH-1. | |
583 | */ | |
584 | static void | |
585 | _uhash_allocate(UHashtable *hash, | |
586 | int32_t primeIndex, | |
587 | UErrorCode *status) { | |
588 | ||
589 | UHashElement *p, *limit; | |
590 | UHashTok emptytok; | |
591 | ||
592 | if (U_FAILURE(*status)) return; | |
593 | ||
594 | U_ASSERT(primeIndex >= 0 && primeIndex < PRIMES_LENGTH); | |
595 | ||
596 | hash->primeIndex = primeIndex; | |
597 | hash->length = PRIMES[primeIndex]; | |
598 | ||
599 | p = hash->elements = (UHashElement*) | |
600 | uprv_malloc(sizeof(UHashElement) * hash->length); | |
601 | ||
602 | if (hash->elements == NULL) { | |
603 | *status = U_MEMORY_ALLOCATION_ERROR; | |
604 | return; | |
605 | } | |
606 | ||
607 | emptytok.pointer = NULL; /* Only one of these two is needed */ | |
608 | emptytok.integer = 0; /* but we don't know which one. */ | |
609 | ||
610 | limit = p + hash->length; | |
611 | while (p < limit) { | |
612 | p->key = emptytok; | |
613 | p->value = emptytok; | |
614 | p->hashcode = HASH_EMPTY; | |
615 | ++p; | |
616 | } | |
617 | ||
618 | hash->count = 0; | |
619 | hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); | |
620 | hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); | |
621 | } | |
622 | ||
623 | /** | |
624 | * Attempt to grow or shrink the data arrays in order to make the | |
625 | * count fit between the high and low water marks. hash_put() and | |
626 | * hash_remove() call this method when the count exceeds the high or | |
627 | * low water marks. This method may do nothing, if memory allocation | |
628 | * fails, or if the count is already in range, or if the length is | |
629 | * already at the low or high limit. In any case, upon return the | |
630 | * arrays will be valid. | |
631 | */ | |
632 | static void | |
633 | _uhash_rehash(UHashtable *hash) { | |
634 | ||
635 | UHashElement *old = hash->elements; | |
636 | int32_t oldLength = hash->length; | |
637 | int32_t newPrimeIndex = hash->primeIndex; | |
638 | int32_t i; | |
639 | UErrorCode status = U_ZERO_ERROR; | |
640 | ||
641 | if (hash->count > hash->highWaterMark) { | |
642 | if (++newPrimeIndex >= PRIMES_LENGTH) { | |
643 | return; | |
644 | } | |
645 | } else if (hash->count < hash->lowWaterMark) { | |
646 | if (--newPrimeIndex < 0) { | |
647 | return; | |
648 | } | |
649 | } else { | |
650 | return; | |
651 | } | |
652 | ||
653 | _uhash_allocate(hash, newPrimeIndex, &status); | |
654 | ||
655 | if (U_FAILURE(status)) { | |
656 | hash->elements = old; | |
657 | hash->length = oldLength; | |
658 | return; | |
659 | } | |
660 | ||
661 | for (i = oldLength - 1; i >= 0; --i) { | |
662 | if (!IS_EMPTY_OR_DELETED(old[i].hashcode)) { | |
663 | UHashElement *e = _uhash_find(hash, old[i].key, old[i].hashcode); | |
664 | U_ASSERT(e != NULL); | |
665 | U_ASSERT(e->hashcode == HASH_EMPTY); | |
666 | e->key = old[i].key; | |
667 | e->value = old[i].value; | |
668 | e->hashcode = old[i].hashcode; | |
669 | ++hash->count; | |
670 | } | |
671 | } | |
672 | ||
673 | uprv_free(old); | |
674 | } | |
675 | ||
676 | /** | |
677 | * Look for a key in the table, or if no such key exists, the first | |
678 | * empty slot matching the given hashcode. Keys are compared using | |
679 | * the keyComparator function. | |
680 | * | |
681 | * First find the start position, which is the hashcode modulo | |
682 | * the length. Test it to see if it is: | |
683 | * | |
684 | * a. identical: First check the hash values for a quick check, | |
685 | * then compare keys for equality using keyComparator. | |
686 | * b. deleted | |
687 | * c. empty | |
688 | * | |
689 | * Stop if it is identical or empty, otherwise continue by adding a | |
690 | * "jump" value (moduloing by the length again to keep it within | |
691 | * range) and retesting. For efficiency, there need enough empty | |
692 | * values so that the searchs stop within a reasonable amount of time. | |
693 | * This can be changed by changing the high/low water marks. | |
694 | * | |
695 | * In theory, this function can return NULL, if it is full (no empty | |
696 | * or deleted slots) and if no matching key is found. In practice, we | |
697 | * prevent this elsewhere (in uhash_put) by making sure the last slot | |
698 | * in the table is never filled. | |
699 | * | |
700 | * The size of the table should be prime for this algorithm to work; | |
701 | * otherwise we are not guaranteed that the jump value (the secondary | |
702 | * hash) is relatively prime to the table length. | |
703 | */ | |
704 | static UHashElement* | |
705 | _uhash_find(const UHashtable *hash, UHashTok key, | |
706 | int32_t hashcode) { | |
707 | ||
708 | int32_t firstDeleted = -1; /* assume invalid index */ | |
709 | int32_t theIndex, startIndex; | |
710 | int32_t jump = 0; /* lazy evaluate */ | |
711 | int32_t tableHash; | |
712 | ||
713 | hashcode &= 0x7FFFFFFF; /* must be positive */ | |
714 | startIndex = theIndex = (hashcode ^ 0x4000000) % hash->length; | |
715 | ||
716 | do { | |
717 | tableHash = hash->elements[theIndex].hashcode; | |
718 | if (tableHash == hashcode) { /* quick check */ | |
719 | if ((*hash->keyComparator)(key, hash->elements[theIndex].key)) { | |
720 | return &(hash->elements[theIndex]); | |
721 | } | |
722 | } else if (!IS_EMPTY_OR_DELETED(tableHash)) { | |
723 | /* We have hit a slot which contains a key-value pair, | |
724 | * but for which the hash code does not match. Keep | |
725 | * looking. | |
726 | */ | |
727 | } else if (tableHash == HASH_EMPTY) { /* empty, end o' the line */ | |
728 | break; | |
729 | } else if (firstDeleted < 0) { /* remember first deleted */ | |
730 | firstDeleted = theIndex; | |
731 | } | |
732 | if (jump == 0) { /* lazy compute jump */ | |
733 | /* The jump value must be relatively prime to the table | |
734 | * length. As long as the length is prime, then any value | |
735 | * 1..length-1 will be relatively prime to it. | |
736 | */ | |
737 | jump = (hashcode % (hash->length - 1)) + 1; | |
738 | } | |
739 | theIndex = (theIndex + jump) % hash->length; | |
740 | } while (theIndex != startIndex); | |
741 | ||
742 | if (firstDeleted >= 0) { | |
743 | theIndex = firstDeleted; /* reset if had deleted slot */ | |
744 | } else if (tableHash != HASH_EMPTY) { | |
745 | /* We get to this point if the hashtable is full (no empty or | |
746 | * deleted slots), and we've failed to find a match. THIS | |
747 | * WILL NEVER HAPPEN as long as uhash_put() makes sure that | |
748 | * count is always < length. | |
749 | */ | |
750 | U_ASSERT(FALSE); | |
751 | return NULL; /* Never happens if uhash_put() behaves */ | |
752 | } | |
753 | return &(hash->elements[theIndex]); | |
754 | } | |
755 | ||
756 | static UHashTok | |
757 | _uhash_put(UHashtable *hash, | |
758 | UHashTok key, | |
759 | UHashTok value, | |
760 | int8_t hint, | |
761 | UErrorCode *status) { | |
762 | ||
763 | /* Put finds the position in the table for the new value. If the | |
764 | * key is already in the table, it is deleted, if there is a | |
765 | * non-NULL keyDeleter. Then the key, the hash and the value are | |
766 | * all put at the position in their respective arrays. | |
767 | */ | |
768 | int32_t hashcode; | |
769 | UHashElement* e; | |
770 | UHashTok emptytok; | |
771 | ||
772 | if (U_FAILURE(*status)) { | |
773 | goto err; | |
774 | } | |
775 | U_ASSERT(hash != NULL); | |
776 | /* Cannot always check pointer here or iSeries sees NULL every time. */ | |
777 | if ((hint & HINT_VALUE_POINTER) && value.pointer == NULL) { | |
778 | /* Disallow storage of NULL values, since NULL is returned by | |
779 | * get() to indicate an absent key. Storing NULL == removing. | |
780 | */ | |
781 | return _uhash_remove(hash, key); | |
782 | } | |
783 | if (hash->count > hash->highWaterMark) { | |
784 | _uhash_rehash(hash); | |
785 | } | |
786 | ||
787 | hashcode = (*hash->keyHasher)(key); | |
788 | e = _uhash_find(hash, key, hashcode); | |
789 | U_ASSERT(e != NULL); | |
790 | ||
791 | if (IS_EMPTY_OR_DELETED(e->hashcode)) { | |
792 | /* Important: We must never actually fill the table up. If we | |
793 | * do so, then _uhash_find() will return NULL, and we'll have | |
794 | * to check for NULL after every call to _uhash_find(). To | |
795 | * avoid this we make sure there is always at least one empty | |
796 | * or deleted slot in the table. This only is a problem if we | |
797 | * are out of memory and rehash isn't working. | |
798 | */ | |
799 | ++hash->count; | |
800 | if (hash->count == hash->length) { | |
801 | /* Don't allow count to reach length */ | |
802 | --hash->count; | |
803 | *status = U_MEMORY_ALLOCATION_ERROR; | |
804 | goto err; | |
805 | } | |
806 | } | |
807 | ||
808 | /* We must in all cases handle storage properly. If there was an | |
809 | * old key, then it must be deleted (if the deleter != NULL). | |
810 | * Make hashcodes stored in table positive. | |
811 | */ | |
812 | return _uhash_setElement(hash, e, hashcode & 0x7FFFFFFF, key, value, hint); | |
813 | ||
814 | err: | |
815 | /* If the deleters are non-NULL, this method adopts its key and/or | |
816 | * value arguments, and we must be sure to delete the key and/or | |
817 | * value in all cases, even upon failure. | |
818 | */ | |
819 | HASH_DELETE_KEY_VALUE(hash, key.pointer, value.pointer); | |
820 | emptytok.pointer = NULL; emptytok.integer = 0; | |
821 | return emptytok; | |
822 | } | |
823 | ||
824 | static UHashTok | |
825 | _uhash_remove(UHashtable *hash, | |
826 | UHashTok key) { | |
827 | /* First find the position of the key in the table. If the object | |
828 | * has not been removed already, remove it. If the user wanted | |
829 | * keys deleted, then delete it also. We have to put a special | |
830 | * hashcode in that position that means that something has been | |
831 | * deleted, since when we do a find, we have to continue PAST any | |
832 | * deleted values. | |
833 | */ | |
834 | UHashTok result; | |
835 | UHashElement* e = _uhash_find(hash, key, hash->keyHasher(key)); | |
836 | U_ASSERT(e != NULL); | |
837 | result.pointer = NULL; result.integer = 0; | |
838 | if (!IS_EMPTY_OR_DELETED(e->hashcode)) { | |
839 | result = _uhash_internalRemoveElement(hash, e); | |
840 | if (hash->count < hash->lowWaterMark) { | |
841 | _uhash_rehash(hash); | |
842 | } | |
843 | } | |
844 | return result; | |
845 | } | |
846 | ||
847 | static UHashTok | |
848 | _uhash_setElement(UHashtable *hash, UHashElement* e, | |
849 | int32_t hashcode, | |
850 | UHashTok key, UHashTok value, int8_t hint) { | |
851 | ||
852 | UHashTok oldValue = e->value; | |
853 | if (hash->keyDeleter != NULL && e->key.pointer != NULL && | |
854 | e->key.pointer != key.pointer) { /* Avoid double deletion */ | |
855 | (*hash->keyDeleter)(e->key.pointer); | |
856 | } | |
857 | if (hash->valueDeleter != NULL) { | |
858 | if (oldValue.pointer != NULL && | |
859 | oldValue.pointer != value.pointer) { /* Avoid double deletion */ | |
860 | (*hash->valueDeleter)(oldValue.pointer); | |
861 | } | |
862 | oldValue.pointer = NULL; | |
863 | } | |
864 | /* Compilers should copy the UHashTok union correctly, but even if | |
865 | * they do, memory heap tools (e.g. BoundsChecker) can get | |
866 | * confused when a pointer is cloaked in a union and then copied. | |
867 | * TO ALLEVIATE THIS, we use hints (based on what API the user is | |
868 | * calling) to copy pointers when we know the user thinks | |
869 | * something is a pointer. */ | |
870 | if (hint & HINT_KEY_POINTER) { | |
871 | e->key.pointer = key.pointer; | |
872 | } else { | |
873 | e->key = key; | |
874 | } | |
875 | if (hint & HINT_VALUE_POINTER) { | |
876 | e->value.pointer = value.pointer; | |
877 | } else { | |
878 | e->value = value; | |
879 | } | |
880 | e->hashcode = hashcode; | |
881 | return oldValue; | |
882 | } | |
883 | ||
884 | /** | |
885 | * Assumes that the given element is not empty or deleted. | |
886 | */ | |
887 | static UHashTok | |
888 | _uhash_internalRemoveElement(UHashtable *hash, UHashElement* e) { | |
889 | UHashTok empty; | |
890 | U_ASSERT(!IS_EMPTY_OR_DELETED(e->hashcode)); | |
891 | --hash->count; | |
892 | empty.pointer = NULL; empty.integer = 0; | |
893 | return _uhash_setElement(hash, e, HASH_DELETED, empty, empty, 0); | |
894 | } | |
895 | ||
896 | static void | |
897 | _uhash_internalSetResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { | |
898 | U_ASSERT(hash != NULL); | |
899 | U_ASSERT(((int32_t)policy) >= 0); | |
900 | U_ASSERT(((int32_t)policy) < 3); | |
901 | hash->lowWaterRatio = RESIZE_POLICY_RATIO_TABLE[policy * 2]; | |
902 | hash->highWaterRatio = RESIZE_POLICY_RATIO_TABLE[policy * 2 + 1]; | |
903 | } |