void remove_from_hash_table (agent* thisAgent, struct hash_table_struct *ht, void *item) { uint32_t hash_value; item_in_hash_table *this_one, *prev; this_one = static_cast<item_in_hash_table_struct *>(item); hash_value = (*(ht->h))(item, ht->log2size); if (*(ht->buckets+hash_value)==this_one) { /* --- hs is the first one on the list for the bucket --- */ *(ht->buckets+hash_value) = this_one->next; } else { /* --- hs is not the first one on the list, so find its predecessor --- */ prev = *(ht->buckets+hash_value); while (prev && prev->next != this_one) prev=prev->next; if ( !prev ) { /* Reaching here means that we couldn't find this_one item */ assert(prev && "Couldn't find item to remove from hash table!"); return; } prev->next = this_one->next; } this_one->next = NIL; /* just for safety */ /* --- update count and possibly resize the table --- */ ht->count--; if ((ht->count < ht->size/2) && (ht->log2size > ht->minimum_log2size)) resize_hash_table (thisAgent, ht, ht->log2size-1); }
// Add a <key, value> pair to the cache. // If key already exists, it will overwrite the old value. // If maxmem capacity is exceeded, sufficient values will be removed // from the cache to accomodate the new value. void cache_set(cache_t cache, key_type key, val_type val, uint32_t val_size){ item_t item_to_set = hash_table_find_item(cache->hash_table, key); // If key already exists, it will overwrite the old value. if (item_to_set!=NULL){ item_to_set->val = val; return; } //If maxmem capacity is exceeded, sufficient values will be removed if (cache_space_used(cache)+val_size >= cache->maxmem){ cache_evict(cache); } //printf("\ncurrent_size:%u,hash_table_buckets_num:%u\n",cache->hash_table->current_size+1,(uint32_t) ((double) cache->hash_table->buckets_num*LOAD_FACTOR)); //Check if we need to resize hash table if (cache->hash_table->current_size+1 > (uint32_t) ((double) cache->hash_table->buckets_num*LOAD_FACTOR)){ resize_hash_table(cache->hash_table); } item_t item = hash_table_set(cache->hash_table, key, val, val_size); node_t node = linked_list_set(cache->linked_list); // //connect the item in hash_table with its corresponding node in linked list connect_node_and_item(item,node); }
void add_to_hash_table (agent* thisAgent, struct hash_table_struct *ht, void *item) { uint32_t hash_value; item_in_hash_table *this_one; this_one = static_cast<item_in_hash_table_struct *>(item); ht->count++; if (ht->count >= ht->size*2) resize_hash_table (thisAgent, ht, ht->log2size+1); hash_value = (*(ht->h))(item, ht->log2size); this_one->next = *(ht->buckets+hash_value); *(ht->buckets+hash_value) = this_one; }