/** * Assumes that the given element is not empty or deleted. */ static UHashTok _uhash_internalRemoveElement(UHashtable *hash, UHashElement* e) { UHashTok empty; U_ASSERT(!IS_EMPTY_OR_DELETED(e->hashcode)); --hash->count; empty.pointer = NULL; empty.integer = 0; return _uhash_setElement(hash, e, HASH_DELETED, empty, empty, 0); }
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 void* U_EXPORT2 uhash_removeElement(UHashtable *hash, const UHashElement* e) { U_ASSERT(hash != NULL); U_ASSERT(e != NULL); if (!IS_EMPTY_OR_DELETED(e->hashcode)) { return _uhash_internalRemoveElement(hash, (UHashElement*) e).pointer; } return NULL; }
/** * Look for a key in the table, or if no such key exists, the first * empty slot matching the given hashcode. Keys are compared using * the keyComparator function. * * First find the start position, which is the hashcode modulo * the length. Test it to see if it is: * * a. identical: First check the hash values for a quick check, * then compare keys for equality using keyComparator. * b. deleted * c. empty * * Stop if it is identical or empty, otherwise continue by adding a * "jump" value (moduloing by the length again to keep it within * range) and retesting. For efficiency, there need enough empty * values so that the searchs stop within a reasonable amount of time. * This can be changed by changing the high/low water marks. * * In theory, this function can return NULL, if it is full (no empty * or deleted slots) and if no matching key is found. In practice, we * prevent this elsewhere (in uhash_put) by making sure the last slot * in the table is never filled. * * The size of the table should be prime for this algorithm to work; * otherwise we are not guaranteed that the jump value (the secondary * hash) is relatively prime to the table length. */ static UHashElement* _uhash_find(const UHashtable *hash, UHashTok key, int32_t hashcode) { int32_t firstDeleted = -1; /* assume invalid index */ int32_t theIndex, startIndex; int32_t jump = 0; /* lazy evaluate */ int32_t tableHash; UHashElement *elements = hash->elements; hashcode &= 0x7FFFFFFF; /* must be positive */ startIndex = theIndex = (hashcode ^ 0x4000000) % hash->length; do { tableHash = elements[theIndex].hashcode; if (tableHash == hashcode) { /* quick check */ if ((*hash->keyComparator)(key, elements[theIndex].key)) { return &(elements[theIndex]); } } else if (!IS_EMPTY_OR_DELETED(tableHash)) { /* We have hit a slot which contains a key-value pair, * but for which the hash code does not match. Keep * looking. */ } else if (tableHash == HASH_EMPTY) { /* empty, end o' the line */ break; } else if (firstDeleted < 0) { /* remember first deleted */ firstDeleted = theIndex; } if (jump == 0) { /* lazy compute jump */ /* The jump value must be relatively prime to the table * length. As long as the length is prime, then any value * 1..length-1 will be relatively prime to it. */ jump = (hashcode % (hash->length - 1)) + 1; } theIndex = (theIndex + jump) % hash->length; } while (theIndex != startIndex); if (firstDeleted >= 0) { theIndex = firstDeleted; /* reset if had deleted slot */ } else if (tableHash != HASH_EMPTY) { /* We get to this point if the hashtable is full (no empty or * deleted slots), and we've failed to find a match. THIS * WILL NEVER HAPPEN as long as uhash_put() makes sure that * count is always < length. */ U_ASSERT(FALSE); return NULL; /* Never happens if uhash_put() behaves */ } return &(elements[theIndex]); }
U_CAPI const UHashElement* U_EXPORT2 uhash_nextElement(const UHashtable *hash, int32_t *pos) { /* Walk through the array until we find an element that is not * EMPTY and not DELETED. */ int32_t i; U_ASSERT(hash != NULL); for (i = *pos + 1; i < hash->length; ++i) { if (!IS_EMPTY_OR_DELETED(hash->elements[i].hashcode)) { *pos = i; return &(hash->elements[i]); } } /* No more elements */ return NULL; }
/** * 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; }