Skip to content

Andre Myrick - Hash Tables#149

Open
AM-Myrick wants to merge 11 commits into
bloominstituteoftechnology:masterfrom
AM-Myrick:master
Open

Andre Myrick - Hash Tables#149
AM-Myrick wants to merge 11 commits into
bloominstituteoftechnology:masterfrom
AM-Myrick:master

Conversation

@AM-Myrick

Copy link
Copy Markdown

No description provided.

@codejoncode codejoncode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 codejoncode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
  }

@codejoncode codejoncode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 codejoncode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your full hashtable code looks pretty good nothing that catches my eye as far as going against best practices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants