From d1576130c1e49188452ac164352fba73c8ab8538 Mon Sep 17 00:00:00 2001 From: Abhisha Nirmalathas <a.nirmalathas1@student.unimelb.edu.au> Date: Wed, 15 May 2019 20:51:44 +1000 Subject: [PATCH] remove vals from hsh table --- hashtable.c | 11 +++++++++++ hashtable.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/hashtable.c b/hashtable.c index 6052f84..47200e4 100644 --- a/hashtable.c +++ b/hashtable.c @@ -22,6 +22,7 @@ struct bucket { struct table { int size; //number of buckets Bucket *buckets; //list of buckets + int remaining; }; /************************************************************************/ @@ -72,6 +73,7 @@ HashTable *new_hash_table(int size) { assert(table); table->size = size; table->buckets = malloc(size * (sizeof *table->buckets)); + table->remaining = size; assert(table->buckets); int i; for (i = 0; i < size; i++) { @@ -150,7 +152,9 @@ int hash_table_get(HashTable *table, char *key) { for (int i=0; i < bucket->n_elems; i++){ if (equal(bucket->keys[i], key)){ freq = bucket->values[i]; + bucket->keys[i] = ""; move_to_front(bucket, i); + table->remaining--; break; } } @@ -169,8 +173,15 @@ bool hash_table_has(HashTable *table, char *key) { Bucket *bucket = &table->buckets[hash_value]; for (int i=0; i < bucket->n_elems; i++){ if (equal(bucket->keys[i], key)){ + // reset hash to avoid duplicate keys + bucket->keys[i] = ""; + table->remaining--; return true; } } return false; } +/************************************************************************/ +int remaining_hashes(HashTable *table){ + return table->remaining; +} \ No newline at end of file diff --git a/hashtable.h b/hashtable.h index a7bff38..42805a3 100644 --- a/hashtable.h +++ b/hashtable.h @@ -22,3 +22,6 @@ int hash_table_get(HashTable *table, char *key); //Checks if the key exists in hash table bool hash_table_has(HashTable *table, char *key); + +// Returns the remaining values inside hashtable +int remaining_hashes(HashTable *table); -- GitLab