Andre Myrick - Hash Tables#149
Conversation
There was a problem hiding this comment.
Looks good so far Andre great job getting through the functions.
For your create function:
ht->storage = calloc(capacity, sizeof(char *)); Storage will hold Pair pointers so we would want to have it like this ht->storage = calloc(capacity, sizeof(Pair *)); Based off the struct being set up like this Pair **storage;
codejoncode
left a comment
There was a problem hiding this comment.
For your insert function you have the following:
if (ht->storage[index]) {
printf("\n%s\n", "You are overwriting a value.");
free(ht->storage[index]);
}
it is only considred over writing if the key we are using is not the key that is already there. So we want to compare the key we receive with the key already there before our warning.
Pair *current_pair = ht->storage[index];
if (ht->storage[index]) {
if(strcmp(key,current_pair->key) != 0){
printf("\n%s\n", "You are overwriting a value.");
}
destroy_pair(current_pair)/// don't use free it won't destroy all parts of the pair
}
There was a problem hiding this comment.
void hash_table_remove(BasicHashTable *ht, char *key)
{
unsigned int index = hash(key, ht->capacity);
if (ht->storage[index])
{
ht->storage[index]->value = NULL;
ht->storage[index]->key = NULL;
destroy_pair(ht->storage[index]);
}
}
void hash_table_remove(BasicHashTable *ht, char *key)
{
unsigned int index = hash(key, ht->capacity);
if (ht->storage[index])
{
ht->storage[index]->value = NULL; // not needed
ht->storage[index]->key = NULL; // not needed
destroy_pair(ht->storage[index]);
}
ht->storage[index] = NULL; // This is needed because once we destroy the index will point to garbage and we want to set it up so that it is just like it started when we used calloc. Initialized to NULL.
}
You may also want to have some type of error for if the key does not exist in the hashtable.
codejoncode
left a comment
There was a problem hiding this comment.
Your full hashtable code looks pretty good nothing that catches my eye as far as going against best practices.
No description provided.