diff --git a/hashtable.c b/hashtable.c index 6052f84d89030fcb190b5f3625ca988529880771..47200e40cca0721d5de06676a89d77bb48ba11cc 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 a7bff387af7db8ce86c6d7a97f7ef294fb25023e..42805a33f2cb368d714daf7d8066e1538f407ea7 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);