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