Skip to content

hashtables #162

Open
DavidBenavidez123 wants to merge 1 commit into
bloominstituteoftechnology:masterfrom
DavidBenavidez123:master
Open

hashtables #162
DavidBenavidez123 wants to merge 1 commit into
bloominstituteoftechnology:masterfrom
DavidBenavidez123:master

Conversation

@DavidBenavidez123

Copy link
Copy Markdown

No description provided.

{
BasicHashTable *ht;
BasicHashTable *ht = malloc(sizeof(BasicHashTable));
ht->storage = calloc(capacity, sizeof(Pair *));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I believe this is sizeof(Pair).

if(ht->storage[hashedkey] != NULL) {
printf("Overwriting a value.");
// clear stored value
destroy_pair(ht->storage[hashedkey]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not sure this is strictly necessary, but it doesn't hurt.


unsigned int hashedkey = hash(key, ht->capacity);
// if key even exists then clear ky value pair
if(ht->storage[hashedkey] != NULL) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very good job checking if this is not NULL.

// check if value exists
if(ht->storage[hashedkey] != NULL) {
return ht->storage[hashedkey]->value;
}

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 great!


// loop through each bucket
for(int i =0; i < ht->capacity; i++) {
destroy_pair(ht->storage[i]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This loop is spot on!

HashTable *ht;
HashTable *ht = malloc(sizeof(HashTable));
ht->capacity = capacity;
ht->storage = calloc(capacity,sizeof(LinkedPair *));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I believe this is sizeof(LinkedPair). If you wanted the size of the pointer, it would be sizeof(*LinkedPair).

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