Skip to content

Commit

Permalink
Merge pull request #9 from ChaosD/develop
Browse files Browse the repository at this point in the history
Fix function chained_hashtable_delete_obj_id_v2
  • Loading branch information
1a1a11a authored Nov 28, 2023
2 parents 892ff04 + 9b91050 commit 8e10f0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ example/cacheSimulatorC/cmake-build-debug
.vscode/*
*.log
fig/
# Chaos
sftp-config.json
55 changes: 40 additions & 15 deletions libCacheSim/dataStructure/hashtable/chainedHashTableV2.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,51 @@ bool chained_hashtable_try_delete_v2(hashtable_t *hashtable,
return false;
}

void chained_hashtable_delete_obj_id_v2(hashtable_t *hashtable,
/**
* This function is used to delete an object from the hash table by object id.
* - if the object is in the hash table
* remove it
* return true.
* - if the object is not in the hash table
* return false.
*
* @method chained_hashtable_delete_obj_id_v2
* @date 2023-11-28
* @param hashtable [Handle to the hashtable]
* @param obj_id [The object id to remove]
* @return [true or false]
*/
bool chained_hashtable_delete_obj_id_v2(hashtable_t *hashtable,
const obj_id_t obj_id) {
hashtable->n_obj -= 1;
uint64_t hv = get_hash_value_int_64(obj_id) & hashmask(hashtable->hashpower);
cache_obj_t *cache_obj = hashtable->ptr_table[hv];
if (cache_obj != NULL && cache_obj->obj_id == obj_id) {
hashtable->ptr_table[hv] = cache_obj->hash_next;
if (!hashtable->external_obj) free_cache_obj(cache_obj);
return;
}
uint64_t hv = get_hash_value_int_64(&obj_id) & hashmask(hashtable->hashpower);
cache_obj_t *cur_obj = hashtable->ptr_table[hv];
// the hash bucket is empty
if(cur_obj == NULL) return false;

cache_obj = cache_obj->hash_next;
while (cache_obj != NULL && cache_obj->obj_id != obj_id) {
cache_obj = cache_obj->hash_next;
// the object to remove is the first object in the hash bucket
if (cur_obj->obj_id == obj_id) {
hashtable->ptr_table[hv] = cur_obj->hash_next;
if (!hashtable->external_obj) free_cache_obj(cur_obj);
hashtable->n_obj -= 1;
return true;
}

if (cache_obj != NULL) {
cache_obj->hash_next = cache_obj->hash_next;
if (!hashtable->external_obj) free_cache_obj(cache_obj);
cache_obj_t *prev_obj;

do {
prev_obj = cur_obj;
cur_obj = cur_obj->hash_next;
} while(cur_obj != NULL && cur_obj->obj_id != obj_id);

// the object to remove is in the hash bucket
if (cur_obj != NULL) {
prev_obj->hash_next = cur_obj->hash_next;
if (!hashtable->external_obj) free_cache_obj(cur_obj);
hashtable->n_obj -= 1;
return true;
}
// the object to remove is not in the hash table
return false;
}

cache_obj_t *chained_hashtable_rand_obj_v2(const hashtable_t *hashtable) {
Expand Down

0 comments on commit 8e10f0b

Please sign in to comment.