From 9b910509031b0a0ca12774cec739c89f1b3b9ea1 Mon Sep 17 00:00:00 2001 From: ChaosD Date: Tue, 28 Nov 2023 13:41:01 +0800 Subject: [PATCH] Fix function chained_hashtable_delete_obj_id_v2 --- .gitignore | 2 + .../hashtable/chainedHashTableV2.c | 55 ++++++++++++++----- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 4deb53b8..dc00d77f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ example/cacheSimulatorC/cmake-build-debug .vscode/* *.log fig/ +# Chaos +sftp-config.json diff --git a/libCacheSim/dataStructure/hashtable/chainedHashTableV2.c b/libCacheSim/dataStructure/hashtable/chainedHashTableV2.c index 8a5eb960..5cc736bd 100644 --- a/libCacheSim/dataStructure/hashtable/chainedHashTableV2.c +++ b/libCacheSim/dataStructure/hashtable/chainedHashTableV2.c @@ -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) {