michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 1997-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * Date Name Description michael@0: * 03/22/00 aliu Adapted from original C++ ICU Hashtable. michael@0: * 07/06/01 aliu Modified to support int32_t keys on michael@0: * platforms with sizeof(void*) < 32. michael@0: ****************************************************************************** michael@0: */ michael@0: michael@0: #include "uhash.h" michael@0: #include "unicode/ustring.h" michael@0: #include "cstring.h" michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: /* This hashtable is implemented as a double hash. All elements are michael@0: * stored in a single array with no secondary storage for collision michael@0: * resolution (no linked list, etc.). When there is a hash collision michael@0: * (when two unequal keys have the same hashcode) we resolve this by michael@0: * using a secondary hash. The secondary hash is an increment michael@0: * computed as a hash function (a different one) of the primary michael@0: * hashcode. This increment is added to the initial hash value to michael@0: * obtain further slots assigned to the same hash code. For this to michael@0: * work, the length of the array and the increment must be relatively michael@0: * prime. The easiest way to achieve this is to have the length of michael@0: * the array be prime, and the increment be any value from michael@0: * 1..length-1. michael@0: * michael@0: * Hashcodes are 32-bit integers. We make sure all hashcodes are michael@0: * non-negative by masking off the top bit. This has two effects: (1) michael@0: * modulo arithmetic is simplified. If we allowed negative hashcodes, michael@0: * then when we computed hashcode % length, we could get a negative michael@0: * result, which we would then have to adjust back into range. It's michael@0: * simpler to just make hashcodes non-negative. (2) It makes it easy michael@0: * to check for empty vs. occupied slots in the table. We just mark michael@0: * empty or deleted slots with a negative hashcode. michael@0: * michael@0: * The central function is _uhash_find(). This function looks for a michael@0: * slot matching the given key and hashcode. If one is found, it michael@0: * returns a pointer to that slot. If the table is full, and no match michael@0: * is found, it returns NULL -- in theory. This would make the code michael@0: * more complicated, since all callers of _uhash_find() would then michael@0: * have to check for a NULL result. To keep this from happening, we michael@0: * don't allow the table to fill. When there is only one michael@0: * empty/deleted slot left, uhash_put() will refuse to increase the michael@0: * count, and fail. This simplifies the code. In practice, one will michael@0: * seldom encounter this using default UHashtables. However, if a michael@0: * hashtable is set to a U_FIXED resize policy, or if memory is michael@0: * exhausted, then the table may fill. michael@0: * michael@0: * High and low water ratios control rehashing. They establish levels michael@0: * of fullness (from 0 to 1) outside of which the data array is michael@0: * reallocated and repopulated. Setting the low water ratio to zero michael@0: * means the table will never shrink. Setting the high water ratio to michael@0: * one means the table will never grow. The ratios should be michael@0: * coordinated with the ratio between successive elements of the michael@0: * PRIMES table, so that when the primeIndex is incremented or michael@0: * decremented during rehashing, it brings the ratio of count / length michael@0: * back into the desired range (between low and high water ratios). michael@0: */ michael@0: michael@0: /******************************************************************** michael@0: * PRIVATE Constants, Macros michael@0: ********************************************************************/ michael@0: michael@0: /* This is a list of non-consecutive primes chosen such that michael@0: * PRIMES[i+1] ~ 2*PRIMES[i]. (Currently, the ratio ranges from 1.81 michael@0: * to 2.18; the inverse ratio ranges from 0.459 to 0.552.) If this michael@0: * ratio is changed, the low and high water ratios should also be michael@0: * adjusted to suit. michael@0: * michael@0: * These prime numbers were also chosen so that they are the largest michael@0: * prime number while being less than a power of two. michael@0: */ michael@0: static const int32_t PRIMES[] = { michael@0: 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, michael@0: 65521, 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593, michael@0: 16777213, 33554393, 67108859, 134217689, 268435399, 536870909, michael@0: 1073741789, 2147483647 /*, 4294967291 */ michael@0: }; michael@0: michael@0: #define PRIMES_LENGTH (sizeof(PRIMES) / sizeof(PRIMES[0])) michael@0: #define DEFAULT_PRIME_INDEX 3 michael@0: michael@0: /* These ratios are tuned to the PRIMES array such that a resize michael@0: * places the table back into the zone of non-resizing. That is, michael@0: * after a call to _uhash_rehash(), a subsequent call to michael@0: * _uhash_rehash() should do nothing (should not churn). This is only michael@0: * a potential problem with U_GROW_AND_SHRINK. michael@0: */ michael@0: static const float RESIZE_POLICY_RATIO_TABLE[6] = { michael@0: /* low, high water ratio */ michael@0: 0.0F, 0.5F, /* U_GROW: Grow on demand, do not shrink */ michael@0: 0.1F, 0.5F, /* U_GROW_AND_SHRINK: Grow and shrink on demand */ michael@0: 0.0F, 1.0F /* U_FIXED: Never change size */ michael@0: }; michael@0: michael@0: /* michael@0: Invariants for hashcode values: michael@0: michael@0: * DELETED < 0 michael@0: * EMPTY < 0 michael@0: * Real hashes >= 0 michael@0: michael@0: Hashcodes may not start out this way, but internally they are michael@0: adjusted so that they are always positive. We assume 32-bit michael@0: hashcodes; adjust these constants for other hashcode sizes. michael@0: */ michael@0: #define HASH_DELETED ((int32_t) 0x80000000) michael@0: #define HASH_EMPTY ((int32_t) HASH_DELETED + 1) michael@0: michael@0: #define IS_EMPTY_OR_DELETED(x) ((x) < 0) michael@0: michael@0: /* This macro expects a UHashTok.pointer as its keypointer and michael@0: valuepointer parameters */ michael@0: #define HASH_DELETE_KEY_VALUE(hash, keypointer, valuepointer) \ michael@0: if (hash->keyDeleter != NULL && keypointer != NULL) { \ michael@0: (*hash->keyDeleter)(keypointer); \ michael@0: } \ michael@0: if (hash->valueDeleter != NULL && valuepointer != NULL) { \ michael@0: (*hash->valueDeleter)(valuepointer); \ michael@0: } michael@0: michael@0: /* michael@0: * Constants for hinting whether a key or value is an integer michael@0: * or a pointer. If a hint bit is zero, then the associated michael@0: * token is assumed to be an integer. michael@0: */ michael@0: #define HINT_KEY_POINTER (1) michael@0: #define HINT_VALUE_POINTER (2) michael@0: michael@0: /******************************************************************** michael@0: * PRIVATE Implementation michael@0: ********************************************************************/ michael@0: michael@0: static UHashTok michael@0: _uhash_setElement(UHashtable *hash, UHashElement* e, michael@0: int32_t hashcode, michael@0: UHashTok key, UHashTok value, int8_t hint) { michael@0: michael@0: UHashTok oldValue = e->value; michael@0: if (hash->keyDeleter != NULL && e->key.pointer != NULL && michael@0: e->key.pointer != key.pointer) { /* Avoid double deletion */ michael@0: (*hash->keyDeleter)(e->key.pointer); michael@0: } michael@0: if (hash->valueDeleter != NULL) { michael@0: if (oldValue.pointer != NULL && michael@0: oldValue.pointer != value.pointer) { /* Avoid double deletion */ michael@0: (*hash->valueDeleter)(oldValue.pointer); michael@0: } michael@0: oldValue.pointer = NULL; michael@0: } michael@0: /* Compilers should copy the UHashTok union correctly, but even if michael@0: * they do, memory heap tools (e.g. BoundsChecker) can get michael@0: * confused when a pointer is cloaked in a union and then copied. michael@0: * TO ALLEVIATE THIS, we use hints (based on what API the user is michael@0: * calling) to copy pointers when we know the user thinks michael@0: * something is a pointer. */ michael@0: if (hint & HINT_KEY_POINTER) { michael@0: e->key.pointer = key.pointer; michael@0: } else { michael@0: e->key = key; michael@0: } michael@0: if (hint & HINT_VALUE_POINTER) { michael@0: e->value.pointer = value.pointer; michael@0: } else { michael@0: e->value = value; michael@0: } michael@0: e->hashcode = hashcode; michael@0: return oldValue; michael@0: } michael@0: michael@0: /** michael@0: * Assumes that the given element is not empty or deleted. michael@0: */ michael@0: static UHashTok michael@0: _uhash_internalRemoveElement(UHashtable *hash, UHashElement* e) { michael@0: UHashTok empty; michael@0: U_ASSERT(!IS_EMPTY_OR_DELETED(e->hashcode)); michael@0: --hash->count; michael@0: empty.pointer = NULL; empty.integer = 0; michael@0: return _uhash_setElement(hash, e, HASH_DELETED, empty, empty, 0); michael@0: } michael@0: michael@0: static void michael@0: _uhash_internalSetResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { michael@0: U_ASSERT(hash != NULL); michael@0: U_ASSERT(((int32_t)policy) >= 0); michael@0: U_ASSERT(((int32_t)policy) < 3); michael@0: hash->lowWaterRatio = RESIZE_POLICY_RATIO_TABLE[policy * 2]; michael@0: hash->highWaterRatio = RESIZE_POLICY_RATIO_TABLE[policy * 2 + 1]; michael@0: } michael@0: michael@0: /** michael@0: * Allocate internal data array of a size determined by the given michael@0: * prime index. If the index is out of range it is pinned into range. michael@0: * If the allocation fails the status is set to michael@0: * U_MEMORY_ALLOCATION_ERROR and all array storage is freed. In michael@0: * either case the previous array pointer is overwritten. michael@0: * michael@0: * Caller must ensure primeIndex is in range 0..PRIME_LENGTH-1. michael@0: */ michael@0: static void michael@0: _uhash_allocate(UHashtable *hash, michael@0: int32_t primeIndex, michael@0: UErrorCode *status) { michael@0: michael@0: UHashElement *p, *limit; michael@0: UHashTok emptytok; michael@0: michael@0: if (U_FAILURE(*status)) return; michael@0: michael@0: U_ASSERT(primeIndex >= 0 && primeIndex < PRIMES_LENGTH); michael@0: michael@0: hash->primeIndex = primeIndex; michael@0: hash->length = PRIMES[primeIndex]; michael@0: michael@0: p = hash->elements = (UHashElement*) michael@0: uprv_malloc(sizeof(UHashElement) * hash->length); michael@0: michael@0: if (hash->elements == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: michael@0: emptytok.pointer = NULL; /* Only one of these two is needed */ michael@0: emptytok.integer = 0; /* but we don't know which one. */ michael@0: michael@0: limit = p + hash->length; michael@0: while (p < limit) { michael@0: p->key = emptytok; michael@0: p->value = emptytok; michael@0: p->hashcode = HASH_EMPTY; michael@0: ++p; michael@0: } michael@0: michael@0: hash->count = 0; michael@0: hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); michael@0: hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); michael@0: } michael@0: michael@0: static UHashtable* michael@0: _uhash_init(UHashtable *result, michael@0: UHashFunction *keyHash, michael@0: UKeyComparator *keyComp, michael@0: UValueComparator *valueComp, michael@0: int32_t primeIndex, michael@0: UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) return NULL; michael@0: U_ASSERT(keyHash != NULL); michael@0: U_ASSERT(keyComp != NULL); michael@0: michael@0: result->keyHasher = keyHash; michael@0: result->keyComparator = keyComp; michael@0: result->valueComparator = valueComp; michael@0: result->keyDeleter = NULL; michael@0: result->valueDeleter = NULL; michael@0: result->allocated = FALSE; michael@0: _uhash_internalSetResizePolicy(result, U_GROW); michael@0: michael@0: _uhash_allocate(result, primeIndex, status); michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static UHashtable* michael@0: _uhash_create(UHashFunction *keyHash, michael@0: UKeyComparator *keyComp, michael@0: UValueComparator *valueComp, michael@0: int32_t primeIndex, michael@0: UErrorCode *status) { michael@0: UHashtable *result; michael@0: michael@0: if (U_FAILURE(*status)) return NULL; michael@0: michael@0: result = (UHashtable*) uprv_malloc(sizeof(UHashtable)); michael@0: if (result == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: _uhash_init(result, keyHash, keyComp, valueComp, primeIndex, status); michael@0: result->allocated = TRUE; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: uprv_free(result); michael@0: return NULL; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Look for a key in the table, or if no such key exists, the first michael@0: * empty slot matching the given hashcode. Keys are compared using michael@0: * the keyComparator function. michael@0: * michael@0: * First find the start position, which is the hashcode modulo michael@0: * the length. Test it to see if it is: michael@0: * michael@0: * a. identical: First check the hash values for a quick check, michael@0: * then compare keys for equality using keyComparator. michael@0: * b. deleted michael@0: * c. empty michael@0: * michael@0: * Stop if it is identical or empty, otherwise continue by adding a michael@0: * "jump" value (moduloing by the length again to keep it within michael@0: * range) and retesting. For efficiency, there need enough empty michael@0: * values so that the searchs stop within a reasonable amount of time. michael@0: * This can be changed by changing the high/low water marks. michael@0: * michael@0: * In theory, this function can return NULL, if it is full (no empty michael@0: * or deleted slots) and if no matching key is found. In practice, we michael@0: * prevent this elsewhere (in uhash_put) by making sure the last slot michael@0: * in the table is never filled. michael@0: * michael@0: * The size of the table should be prime for this algorithm to work; michael@0: * otherwise we are not guaranteed that the jump value (the secondary michael@0: * hash) is relatively prime to the table length. michael@0: */ michael@0: static UHashElement* michael@0: _uhash_find(const UHashtable *hash, UHashTok key, michael@0: int32_t hashcode) { michael@0: michael@0: int32_t firstDeleted = -1; /* assume invalid index */ michael@0: int32_t theIndex, startIndex; michael@0: int32_t jump = 0; /* lazy evaluate */ michael@0: int32_t tableHash; michael@0: UHashElement *elements = hash->elements; michael@0: michael@0: hashcode &= 0x7FFFFFFF; /* must be positive */ michael@0: startIndex = theIndex = (hashcode ^ 0x4000000) % hash->length; michael@0: michael@0: do { michael@0: tableHash = elements[theIndex].hashcode; michael@0: if (tableHash == hashcode) { /* quick check */ michael@0: if ((*hash->keyComparator)(key, elements[theIndex].key)) { michael@0: return &(elements[theIndex]); michael@0: } michael@0: } else if (!IS_EMPTY_OR_DELETED(tableHash)) { michael@0: /* We have hit a slot which contains a key-value pair, michael@0: * but for which the hash code does not match. Keep michael@0: * looking. michael@0: */ michael@0: } else if (tableHash == HASH_EMPTY) { /* empty, end o' the line */ michael@0: break; michael@0: } else if (firstDeleted < 0) { /* remember first deleted */ michael@0: firstDeleted = theIndex; michael@0: } michael@0: if (jump == 0) { /* lazy compute jump */ michael@0: /* The jump value must be relatively prime to the table michael@0: * length. As long as the length is prime, then any value michael@0: * 1..length-1 will be relatively prime to it. michael@0: */ michael@0: jump = (hashcode % (hash->length - 1)) + 1; michael@0: } michael@0: theIndex = (theIndex + jump) % hash->length; michael@0: } while (theIndex != startIndex); michael@0: michael@0: if (firstDeleted >= 0) { michael@0: theIndex = firstDeleted; /* reset if had deleted slot */ michael@0: } else if (tableHash != HASH_EMPTY) { michael@0: /* We get to this point if the hashtable is full (no empty or michael@0: * deleted slots), and we've failed to find a match. THIS michael@0: * WILL NEVER HAPPEN as long as uhash_put() makes sure that michael@0: * count is always < length. michael@0: */ michael@0: U_ASSERT(FALSE); michael@0: return NULL; /* Never happens if uhash_put() behaves */ michael@0: } michael@0: return &(elements[theIndex]); michael@0: } michael@0: michael@0: /** michael@0: * Attempt to grow or shrink the data arrays in order to make the michael@0: * count fit between the high and low water marks. hash_put() and michael@0: * hash_remove() call this method when the count exceeds the high or michael@0: * low water marks. This method may do nothing, if memory allocation michael@0: * fails, or if the count is already in range, or if the length is michael@0: * already at the low or high limit. In any case, upon return the michael@0: * arrays will be valid. michael@0: */ michael@0: static void michael@0: _uhash_rehash(UHashtable *hash, UErrorCode *status) { michael@0: michael@0: UHashElement *old = hash->elements; michael@0: int32_t oldLength = hash->length; michael@0: int32_t newPrimeIndex = hash->primeIndex; michael@0: int32_t i; michael@0: michael@0: if (hash->count > hash->highWaterMark) { michael@0: if (++newPrimeIndex >= PRIMES_LENGTH) { michael@0: return; michael@0: } michael@0: } else if (hash->count < hash->lowWaterMark) { michael@0: if (--newPrimeIndex < 0) { michael@0: return; michael@0: } michael@0: } else { michael@0: return; michael@0: } michael@0: michael@0: _uhash_allocate(hash, newPrimeIndex, status); michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: hash->elements = old; michael@0: hash->length = oldLength; michael@0: return; michael@0: } michael@0: michael@0: for (i = oldLength - 1; i >= 0; --i) { michael@0: if (!IS_EMPTY_OR_DELETED(old[i].hashcode)) { michael@0: UHashElement *e = _uhash_find(hash, old[i].key, old[i].hashcode); michael@0: U_ASSERT(e != NULL); michael@0: U_ASSERT(e->hashcode == HASH_EMPTY); michael@0: e->key = old[i].key; michael@0: e->value = old[i].value; michael@0: e->hashcode = old[i].hashcode; michael@0: ++hash->count; michael@0: } michael@0: } michael@0: michael@0: uprv_free(old); michael@0: } michael@0: michael@0: static UHashTok michael@0: _uhash_remove(UHashtable *hash, michael@0: UHashTok key) { michael@0: /* First find the position of the key in the table. If the object michael@0: * has not been removed already, remove it. If the user wanted michael@0: * keys deleted, then delete it also. We have to put a special michael@0: * hashcode in that position that means that something has been michael@0: * deleted, since when we do a find, we have to continue PAST any michael@0: * deleted values. michael@0: */ michael@0: UHashTok result; michael@0: UHashElement* e = _uhash_find(hash, key, hash->keyHasher(key)); michael@0: U_ASSERT(e != NULL); michael@0: result.pointer = NULL; michael@0: result.integer = 0; michael@0: if (!IS_EMPTY_OR_DELETED(e->hashcode)) { michael@0: result = _uhash_internalRemoveElement(hash, e); michael@0: if (hash->count < hash->lowWaterMark) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: _uhash_rehash(hash, &status); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: static UHashTok michael@0: _uhash_put(UHashtable *hash, michael@0: UHashTok key, michael@0: UHashTok value, michael@0: int8_t hint, michael@0: UErrorCode *status) { michael@0: michael@0: /* Put finds the position in the table for the new value. If the michael@0: * key is already in the table, it is deleted, if there is a michael@0: * non-NULL keyDeleter. Then the key, the hash and the value are michael@0: * all put at the position in their respective arrays. michael@0: */ michael@0: int32_t hashcode; michael@0: UHashElement* e; michael@0: UHashTok emptytok; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: goto err; michael@0: } michael@0: U_ASSERT(hash != NULL); michael@0: /* Cannot always check pointer here or iSeries sees NULL every time. */ michael@0: if ((hint & HINT_VALUE_POINTER) && value.pointer == NULL) { michael@0: /* Disallow storage of NULL values, since NULL is returned by michael@0: * get() to indicate an absent key. Storing NULL == removing. michael@0: */ michael@0: return _uhash_remove(hash, key); michael@0: } michael@0: if (hash->count > hash->highWaterMark) { michael@0: _uhash_rehash(hash, status); michael@0: if (U_FAILURE(*status)) { michael@0: goto err; michael@0: } michael@0: } michael@0: michael@0: hashcode = (*hash->keyHasher)(key); michael@0: e = _uhash_find(hash, key, hashcode); michael@0: U_ASSERT(e != NULL); michael@0: michael@0: if (IS_EMPTY_OR_DELETED(e->hashcode)) { michael@0: /* Important: We must never actually fill the table up. If we michael@0: * do so, then _uhash_find() will return NULL, and we'll have michael@0: * to check for NULL after every call to _uhash_find(). To michael@0: * avoid this we make sure there is always at least one empty michael@0: * or deleted slot in the table. This only is a problem if we michael@0: * are out of memory and rehash isn't working. michael@0: */ michael@0: ++hash->count; michael@0: if (hash->count == hash->length) { michael@0: /* Don't allow count to reach length */ michael@0: --hash->count; michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto err; michael@0: } michael@0: } michael@0: michael@0: /* We must in all cases handle storage properly. If there was an michael@0: * old key, then it must be deleted (if the deleter != NULL). michael@0: * Make hashcodes stored in table positive. michael@0: */ michael@0: return _uhash_setElement(hash, e, hashcode & 0x7FFFFFFF, key, value, hint); michael@0: michael@0: err: michael@0: /* If the deleters are non-NULL, this method adopts its key and/or michael@0: * value arguments, and we must be sure to delete the key and/or michael@0: * value in all cases, even upon failure. michael@0: */ michael@0: HASH_DELETE_KEY_VALUE(hash, key.pointer, value.pointer); michael@0: emptytok.pointer = NULL; emptytok.integer = 0; michael@0: return emptytok; michael@0: } michael@0: michael@0: michael@0: /******************************************************************** michael@0: * PUBLIC API michael@0: ********************************************************************/ michael@0: michael@0: U_CAPI UHashtable* U_EXPORT2 michael@0: uhash_open(UHashFunction *keyHash, michael@0: UKeyComparator *keyComp, michael@0: UValueComparator *valueComp, michael@0: UErrorCode *status) { michael@0: michael@0: return _uhash_create(keyHash, keyComp, valueComp, DEFAULT_PRIME_INDEX, status); michael@0: } michael@0: michael@0: U_CAPI UHashtable* U_EXPORT2 michael@0: uhash_openSize(UHashFunction *keyHash, michael@0: UKeyComparator *keyComp, michael@0: UValueComparator *valueComp, michael@0: int32_t size, michael@0: UErrorCode *status) { michael@0: michael@0: /* Find the smallest index i for which PRIMES[i] >= size. */ michael@0: int32_t i = 0; michael@0: while (i<(PRIMES_LENGTH-1) && PRIMES[i]elements != NULL) { michael@0: if (hash->keyDeleter != NULL || hash->valueDeleter != NULL) { michael@0: int32_t pos=-1; michael@0: UHashElement *e; michael@0: while ((e = (UHashElement*) uhash_nextElement(hash, &pos)) != NULL) { michael@0: HASH_DELETE_KEY_VALUE(hash, e->key.pointer, e->value.pointer); michael@0: } michael@0: } michael@0: uprv_free(hash->elements); michael@0: hash->elements = NULL; michael@0: } michael@0: if (hash->allocated) { michael@0: uprv_free(hash); michael@0: } michael@0: } michael@0: michael@0: U_CAPI UHashFunction *U_EXPORT2 michael@0: uhash_setKeyHasher(UHashtable *hash, UHashFunction *fn) { michael@0: UHashFunction *result = hash->keyHasher; michael@0: hash->keyHasher = fn; michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UKeyComparator *U_EXPORT2 michael@0: uhash_setKeyComparator(UHashtable *hash, UKeyComparator *fn) { michael@0: UKeyComparator *result = hash->keyComparator; michael@0: hash->keyComparator = fn; michael@0: return result; michael@0: } michael@0: U_CAPI UValueComparator *U_EXPORT2 michael@0: uhash_setValueComparator(UHashtable *hash, UValueComparator *fn){ michael@0: UValueComparator *result = hash->valueComparator; michael@0: hash->valueComparator = fn; michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UObjectDeleter *U_EXPORT2 michael@0: uhash_setKeyDeleter(UHashtable *hash, UObjectDeleter *fn) { michael@0: UObjectDeleter *result = hash->keyDeleter; michael@0: hash->keyDeleter = fn; michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UObjectDeleter *U_EXPORT2 michael@0: uhash_setValueDeleter(UHashtable *hash, UObjectDeleter *fn) { michael@0: UObjectDeleter *result = hash->valueDeleter; michael@0: hash->valueDeleter = fn; michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: _uhash_internalSetResizePolicy(hash, policy); michael@0: hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); michael@0: hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); michael@0: _uhash_rehash(hash, &status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_count(const UHashtable *hash) { michael@0: return hash->count; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_get(const UHashtable *hash, michael@0: const void* key) { michael@0: UHashTok keyholder; michael@0: keyholder.pointer = (void*) key; michael@0: return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.pointer; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_iget(const UHashtable *hash, michael@0: int32_t key) { michael@0: UHashTok keyholder; michael@0: keyholder.integer = key; michael@0: return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.pointer; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_geti(const UHashtable *hash, michael@0: const void* key) { michael@0: UHashTok keyholder; michael@0: keyholder.pointer = (void*) key; michael@0: return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.integer; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_igeti(const UHashtable *hash, michael@0: int32_t key) { michael@0: UHashTok keyholder; michael@0: keyholder.integer = key; michael@0: return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.integer; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_put(UHashtable *hash, michael@0: void* key, michael@0: void* value, michael@0: UErrorCode *status) { michael@0: UHashTok keyholder, valueholder; michael@0: keyholder.pointer = key; michael@0: valueholder.pointer = value; michael@0: return _uhash_put(hash, keyholder, valueholder, michael@0: HINT_KEY_POINTER | HINT_VALUE_POINTER, michael@0: status).pointer; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_iput(UHashtable *hash, michael@0: int32_t key, michael@0: void* value, michael@0: UErrorCode *status) { michael@0: UHashTok keyholder, valueholder; michael@0: keyholder.integer = key; michael@0: valueholder.pointer = value; michael@0: return _uhash_put(hash, keyholder, valueholder, michael@0: HINT_VALUE_POINTER, michael@0: status).pointer; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_puti(UHashtable *hash, michael@0: void* key, michael@0: int32_t value, michael@0: UErrorCode *status) { michael@0: UHashTok keyholder, valueholder; michael@0: keyholder.pointer = key; michael@0: valueholder.integer = value; michael@0: return _uhash_put(hash, keyholder, valueholder, michael@0: HINT_KEY_POINTER, michael@0: status).integer; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_iputi(UHashtable *hash, michael@0: int32_t key, michael@0: int32_t value, michael@0: UErrorCode *status) { michael@0: UHashTok keyholder, valueholder; michael@0: keyholder.integer = key; michael@0: valueholder.integer = value; michael@0: return _uhash_put(hash, keyholder, valueholder, michael@0: 0, /* neither is a ptr */ michael@0: status).integer; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_remove(UHashtable *hash, michael@0: const void* key) { michael@0: UHashTok keyholder; michael@0: keyholder.pointer = (void*) key; michael@0: return _uhash_remove(hash, keyholder).pointer; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_iremove(UHashtable *hash, michael@0: int32_t key) { michael@0: UHashTok keyholder; michael@0: keyholder.integer = key; michael@0: return _uhash_remove(hash, keyholder).pointer; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_removei(UHashtable *hash, michael@0: const void* key) { michael@0: UHashTok keyholder; michael@0: keyholder.pointer = (void*) key; michael@0: return _uhash_remove(hash, keyholder).integer; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_iremovei(UHashtable *hash, michael@0: int32_t key) { michael@0: UHashTok keyholder; michael@0: keyholder.integer = key; michael@0: return _uhash_remove(hash, keyholder).integer; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uhash_removeAll(UHashtable *hash) { michael@0: int32_t pos = -1; michael@0: const UHashElement *e; michael@0: U_ASSERT(hash != NULL); michael@0: if (hash->count != 0) { michael@0: while ((e = uhash_nextElement(hash, &pos)) != NULL) { michael@0: uhash_removeElement(hash, e); michael@0: } michael@0: } michael@0: U_ASSERT(hash->count == 0); michael@0: } michael@0: michael@0: U_CAPI const UHashElement* U_EXPORT2 michael@0: uhash_find(const UHashtable *hash, const void* key) { michael@0: UHashTok keyholder; michael@0: const UHashElement *e; michael@0: keyholder.pointer = (void*) key; michael@0: e = _uhash_find(hash, keyholder, hash->keyHasher(keyholder)); michael@0: return IS_EMPTY_OR_DELETED(e->hashcode) ? NULL : e; michael@0: } michael@0: michael@0: U_CAPI const UHashElement* U_EXPORT2 michael@0: uhash_nextElement(const UHashtable *hash, int32_t *pos) { michael@0: /* Walk through the array until we find an element that is not michael@0: * EMPTY and not DELETED. michael@0: */ michael@0: int32_t i; michael@0: U_ASSERT(hash != NULL); michael@0: for (i = *pos + 1; i < hash->length; ++i) { michael@0: if (!IS_EMPTY_OR_DELETED(hash->elements[i].hashcode)) { michael@0: *pos = i; michael@0: return &(hash->elements[i]); michael@0: } michael@0: } michael@0: michael@0: /* No more elements */ michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI void* U_EXPORT2 michael@0: uhash_removeElement(UHashtable *hash, const UHashElement* e) { michael@0: U_ASSERT(hash != NULL); michael@0: U_ASSERT(e != NULL); michael@0: if (!IS_EMPTY_OR_DELETED(e->hashcode)) { michael@0: UHashElement *nce = (UHashElement *)e; michael@0: return _uhash_internalRemoveElement(hash, nce).pointer; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: /******************************************************************** michael@0: * UHashTok convenience michael@0: ********************************************************************/ michael@0: michael@0: /** michael@0: * Return a UHashTok for an integer. michael@0: */ michael@0: /*U_CAPI UHashTok U_EXPORT2 michael@0: uhash_toki(int32_t i) { michael@0: UHashTok tok; michael@0: tok.integer = i; michael@0: return tok; michael@0: }*/ michael@0: michael@0: /** michael@0: * Return a UHashTok for a pointer. michael@0: */ michael@0: /*U_CAPI UHashTok U_EXPORT2 michael@0: uhash_tokp(void* p) { michael@0: UHashTok tok; michael@0: tok.pointer = p; michael@0: return tok; michael@0: }*/ michael@0: michael@0: /******************************************************************** michael@0: * PUBLIC Key Hash Functions michael@0: ********************************************************************/ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_hashUChars(const UHashTok key) { michael@0: const UChar *s = (const UChar *)key.pointer; michael@0: return s == NULL ? 0 : ustr_hashUCharsN(s, u_strlen(s)); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_hashChars(const UHashTok key) { michael@0: const char *s = (const char *)key.pointer; michael@0: return s == NULL ? 0 : ustr_hashCharsN(s, uprv_strlen(s)); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_hashIChars(const UHashTok key) { michael@0: const char *s = (const char *)key.pointer; michael@0: return s == NULL ? 0 : ustr_hashICharsN(s, uprv_strlen(s)); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uhash_equals(const UHashtable* hash1, const UHashtable* hash2){ michael@0: int32_t count1, count2, pos, i; michael@0: michael@0: if(hash1==hash2){ michael@0: return TRUE; michael@0: } michael@0: michael@0: /* michael@0: * Make sure that we are comparing 2 valid hashes of the same type michael@0: * with valid comparison functions. michael@0: * Without valid comparison functions, a binary comparison michael@0: * of the hash values will yield random results on machines michael@0: * with 64-bit pointers and 32-bit integer hashes. michael@0: * A valueComparator is normally optional. michael@0: */ michael@0: if (hash1==NULL || hash2==NULL || michael@0: hash1->keyComparator != hash2->keyComparator || michael@0: hash1->valueComparator != hash2->valueComparator || michael@0: hash1->valueComparator == NULL) michael@0: { michael@0: /* michael@0: Normally we would return an error here about incompatible hash tables, michael@0: but we return FALSE instead. michael@0: */ michael@0: return FALSE; michael@0: } michael@0: michael@0: count1 = uhash_count(hash1); michael@0: count2 = uhash_count(hash2); michael@0: if(count1!=count2){ michael@0: return FALSE; michael@0: } michael@0: michael@0: pos=-1; michael@0: for(i=0; ikey; michael@0: const UHashTok val1 = elem1->value; michael@0: /* here the keys are not compared, instead the key form hash1 is used to fetch michael@0: * value from hash2. If the hashes are equal then then both hashes should michael@0: * contain equal values for the same key! michael@0: */ michael@0: const UHashElement* elem2 = _uhash_find(hash2, key1, hash2->keyHasher(key1)); michael@0: const UHashTok val2 = elem2->value; michael@0: if(hash1->valueComparator(val1, val2)==FALSE){ michael@0: return FALSE; michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: /******************************************************************** michael@0: * PUBLIC Comparator Functions michael@0: ********************************************************************/ michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uhash_compareUChars(const UHashTok key1, const UHashTok key2) { michael@0: const UChar *p1 = (const UChar*) key1.pointer; michael@0: const UChar *p2 = (const UChar*) key2.pointer; michael@0: if (p1 == p2) { michael@0: return TRUE; michael@0: } michael@0: if (p1 == NULL || p2 == NULL) { michael@0: return FALSE; michael@0: } michael@0: while (*p1 != 0 && *p1 == *p2) { michael@0: ++p1; michael@0: ++p2; michael@0: } michael@0: return (UBool)(*p1 == *p2); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uhash_compareChars(const UHashTok key1, const UHashTok key2) { michael@0: const char *p1 = (const char*) key1.pointer; michael@0: const char *p2 = (const char*) key2.pointer; michael@0: if (p1 == p2) { michael@0: return TRUE; michael@0: } michael@0: if (p1 == NULL || p2 == NULL) { michael@0: return FALSE; michael@0: } michael@0: while (*p1 != 0 && *p1 == *p2) { michael@0: ++p1; michael@0: ++p2; michael@0: } michael@0: return (UBool)(*p1 == *p2); michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uhash_compareIChars(const UHashTok key1, const UHashTok key2) { michael@0: const char *p1 = (const char*) key1.pointer; michael@0: const char *p2 = (const char*) key2.pointer; michael@0: if (p1 == p2) { michael@0: return TRUE; michael@0: } michael@0: if (p1 == NULL || p2 == NULL) { michael@0: return FALSE; michael@0: } michael@0: while (*p1 != 0 && uprv_tolower(*p1) == uprv_tolower(*p2)) { michael@0: ++p1; michael@0: ++p2; michael@0: } michael@0: return (UBool)(*p1 == *p2); michael@0: } michael@0: michael@0: /******************************************************************** michael@0: * PUBLIC int32_t Support Functions michael@0: ********************************************************************/ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uhash_hashLong(const UHashTok key) { michael@0: return key.integer; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: uhash_compareLong(const UHashTok key1, const UHashTok key2) { michael@0: return (UBool)(key1.integer == key2.integer); michael@0: }