U_CAPI int32_t U_EXPORT2 uhash_igeti(const UHashtable *hash, int32_t key) { UHashTok keyholder; keyholder.integer = key; return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.integer; }
U_CAPI int32_t U_EXPORT2 uhash_geti(const UHashtable *hash, const void* key) { UHashTok keyholder; keyholder.pointer = (void*) key; return _uhash_find(hash, keyholder, hash->keyHasher(keyholder))->value.integer; }
U_CAPI const UHashElement* U_EXPORT2 uhash_find(const UHashtable *hash, const void* key) { UHashTok keyholder; const UHashElement *e; keyholder.pointer = (void*) key; e = _uhash_find(hash, keyholder, hash->keyHasher(keyholder)); return IS_EMPTY_OR_DELETED(e->hashcode) ? NULL : e; }
U_CAPI UBool U_EXPORT2 uhash_equals(const UHashtable* hash1, const UHashtable* hash2){ int32_t count1, count2, pos, i; if(hash1==hash2){ return TRUE; } /* * Make sure that we are comparing 2 valid hashes of the same type * with valid comparison functions. * Without valid comparison functions, a binary comparison * of the hash values will yield random results on machines * with 64-bit pointers and 32-bit integer hashes. * A valueComparator is normally optional. */ if (hash1==NULL || hash2==NULL || hash1->keyComparator != hash2->keyComparator || hash1->valueComparator != hash2->valueComparator || hash1->valueComparator == NULL) { /* Normally we would return an error here about incompatible hash tables, but we return FALSE instead. */ return FALSE; } count1 = uhash_count(hash1); count2 = uhash_count(hash2); if(count1!=count2){ return FALSE; } pos=-1; for(i=0; i<count1; i++){ const UHashElement* elem1 = uhash_nextElement(hash1, &pos); const UHashTok key1 = elem1->key; const UHashTok val1 = elem1->value; /* here the keys are not compared, instead the key form hash1 is used to fetch * value from hash2. If the hashes are equal then then both hashes should * contain equal values for the same key! */ const UHashElement* elem2 = _uhash_find(hash2, key1, hash2->keyHasher(key1)); const UHashTok val2 = elem2->value; if(hash1->valueComparator(val1, val2)==FALSE){ return FALSE; } } return TRUE; }
/** * Attempt to grow or shrink the data arrays in order to make the * count fit between the high and low water marks. hash_put() and * hash_remove() call this method when the count exceeds the high or * low water marks. This method may do nothing, if memory allocation * fails, or if the count is already in range, or if the length is * already at the low or high limit. In any case, upon return the * arrays will be valid. */ static void _uhash_rehash(UHashtable *hash) { UHashElement *old = hash->elements; int32_t oldLength = hash->length; int32_t newPrimeIndex = hash->primeIndex; int32_t i; UErrorCode status = U_ZERO_ERROR; if (hash->count > hash->highWaterMark) { if (++newPrimeIndex >= PRIMES_LENGTH) { return; } } else if (hash->count < hash->lowWaterMark) { if (--newPrimeIndex < 0) { return; } } else { return; } _uhash_allocate(hash, newPrimeIndex, &status); if (U_FAILURE(status)) { hash->elements = old; hash->length = oldLength; return; } for (i = oldLength - 1; i >= 0; --i) { if (!IS_EMPTY_OR_DELETED(old[i].hashcode)) { UHashElement *e = _uhash_find(hash, old[i].key, old[i].hashcode); U_ASSERT(e != NULL); U_ASSERT(e->hashcode == HASH_EMPTY); e->key = old[i].key; e->value = old[i].value; e->hashcode = old[i].hashcode; ++hash->count; } } uprv_free(old); }
static UHashTok _uhash_remove(UHashtable *hash, UHashTok key) { /* First find the position of the key in the table. If the object * has not been removed already, remove it. If the user wanted * keys deleted, then delete it also. We have to put a special * hashcode in that position that means that something has been * deleted, since when we do a find, we have to continue PAST any * deleted values. */ UHashTok result; UHashElement* e = _uhash_find(hash, key, hash->keyHasher(key)); U_ASSERT(e != NULL); result.pointer = NULL; result.integer = 0; if (!IS_EMPTY_OR_DELETED(e->hashcode)) { result = _uhash_internalRemoveElement(hash, e); if (hash->count < hash->lowWaterMark) { _uhash_rehash(hash); } } return result; }
static UHashTok _uhash_put(UHashtable *hash, UHashTok key, UHashTok value, int8_t hint, UErrorCode *status) { /* Put finds the position in the table for the new value. If the * key is already in the table, it is deleted, if there is a * non-NULL keyDeleter. Then the key, the hash and the value are * all put at the position in their respective arrays. */ int32_t hashcode; UHashElement* e; UHashTok emptytok; if (U_FAILURE(*status)) { goto err; } U_ASSERT(hash != NULL); /* Cannot always check pointer here or iSeries sees NULL every time. */ if ((hint & HINT_VALUE_POINTER) && value.pointer == NULL) { /* Disallow storage of NULL values, since NULL is returned by * get() to indicate an absent key. Storing NULL == removing. */ return _uhash_remove(hash, key); } if (hash->count > hash->highWaterMark) { _uhash_rehash(hash); } hashcode = (*hash->keyHasher)(key); e = _uhash_find(hash, key, hashcode); U_ASSERT(e != NULL); if (IS_EMPTY_OR_DELETED(e->hashcode)) { /* Important: We must never actually fill the table up. If we * do so, then _uhash_find() will return NULL, and we'll have * to check for NULL after every call to _uhash_find(). To * avoid this we make sure there is always at least one empty * or deleted slot in the table. This only is a problem if we * are out of memory and rehash isn't working. */ ++hash->count; if (hash->count == hash->length) { /* Don't allow count to reach length */ --hash->count; *status = U_MEMORY_ALLOCATION_ERROR; goto err; } } /* We must in all cases handle storage properly. If there was an * old key, then it must be deleted (if the deleter != NULL). * Make hashcodes stored in table positive. */ return _uhash_setElement(hash, e, hashcode & 0x7FFFFFFF, key, value, hint); err: /* If the deleters are non-NULL, this method adopts its key and/or * value arguments, and we must be sure to delete the key and/or * value in all cases, even upon failure. */ HASH_DELETE_KEY_VALUE(hash, key.pointer, value.pointer); emptytok.pointer = NULL; emptytok.integer = 0; return emptytok; }