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