U_CAPI void U_EXPORT2 uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { _uhash_internalSetResizePolicy(hash, policy); hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); _uhash_rehash(hash); }
U_CAPI void U_EXPORT2 uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy) { UErrorCode status = U_ZERO_ERROR; _uhash_internalSetResizePolicy(hash, policy); hash->lowWaterMark = (int32_t)(hash->length * hash->lowWaterRatio); hash->highWaterMark = (int32_t)(hash->length * hash->highWaterRatio); _uhash_rehash(hash, &status); }
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; }