-
Notifications
You must be signed in to change notification settings - Fork 236
hashtables #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
hashtables #162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,6 @@ void destroy_pair(Pair *pair) | |
|
|
||
| /**** | ||
| djb2 hash function | ||
|
|
||
| Do not modify this! | ||
| ****/ | ||
| unsigned int hash(char *str, int max) | ||
|
|
@@ -64,57 +63,86 @@ unsigned int hash(char *str, int max) | |
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| All values in storage should be initialized to NULL | ||
| (hint: look up `calloc`) | ||
| ****/ | ||
| BasicHashTable *create_hash_table(int capacity) | ||
| { | ||
| BasicHashTable *ht; | ||
| BasicHashTable *ht = malloc(sizeof(BasicHashTable)); | ||
| ht->storage = calloc(capacity, sizeof(Pair *)); | ||
| ht->capacity = capacity; | ||
|
|
||
| return ht; | ||
| } | ||
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| If you are overwriting a value with a different key, print a warning. | ||
|
|
||
| Don't forget to free any malloc'ed memory! | ||
| ****/ | ||
| void hash_table_insert(BasicHashTable *ht, char *key, char *value) | ||
| { | ||
| // hash key | ||
| int hashedkey = hash(key, ht->capacity); | ||
| // make if key exists else make a pair with key and value given | ||
| if(ht->storage[hashedkey] != NULL) { | ||
| printf("Overwriting a value."); | ||
| // clear stored value | ||
| destroy_pair(ht->storage[hashedkey]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| // make new pair at given hashed index | ||
| ht->storage[hashedkey] = create_pair(key,value); | ||
| } | ||
| else { | ||
| // make new pair at given hashed index | ||
| ht->storage[hashedkey] = create_pair(key,value); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| Don't forget to free any malloc'ed memory! | ||
| ****/ | ||
| void hash_table_remove(BasicHashTable *ht, char *key) | ||
| { | ||
|
|
||
| unsigned int hashedkey = hash(key, ht->capacity); | ||
| // if key even exists then clear ky value pair | ||
| if(ht->storage[hashedkey] != NULL) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good job checking if this is not NULL. |
||
| printf("Deleting a value."); | ||
| // clear stored value | ||
| destroy_pair(ht->storage[hashedkey]); | ||
| ht->storage[hashedkey] = NULL; | ||
| } | ||
| } | ||
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| Should return NULL if the key is not found. | ||
| ****/ | ||
| char *hash_table_retrieve(BasicHashTable *ht, char *key) | ||
| { | ||
| int hashedkey = hash(key, ht->capacity); | ||
| // check if value exists | ||
| if(ht->storage[hashedkey] != NULL) { | ||
| return ht->storage[hashedkey]->value; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great! |
||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| Don't forget to free any malloc'ed memory! | ||
| ****/ | ||
| void destroy_hash_table(BasicHashTable *ht) | ||
| { | ||
|
|
||
| // loop through each bucket | ||
| for(int i =0; i < ht->capacity; i++) { | ||
| destroy_pair(ht->storage[i]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop is spot on! |
||
| } | ||
| // free malloc'ed memory | ||
| free(ht->storage); | ||
| free(ht); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -139,4 +167,4 @@ int main(void) | |
|
|
||
| return 0; | ||
| } | ||
| #endif | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,9 @@ unsigned int hash(char *str, int max) | |
| ****/ | ||
| HashTable *create_hash_table(int capacity) | ||
| { | ||
| HashTable *ht; | ||
| HashTable *ht = malloc(sizeof(HashTable)); | ||
| ht->capacity = capacity; | ||
| ht->storage = calloc(capacity,sizeof(LinkedPair *)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is |
||
|
|
||
| return ht; | ||
| } | ||
|
|
@@ -86,14 +88,37 @@ HashTable *create_hash_table(int capacity) | |
| ****/ | ||
| void hash_table_insert(HashTable *ht, char *key, char *value) | ||
| { | ||
| // Hash the key to get an array index | ||
| unsigned int index = hash(key, ht->capacity); | ||
|
|
||
| // Check if the bucket at that index is occupied | ||
| LinkedPair *current_pair = ht->storage[index]; | ||
| LinkedPair *last_pair; | ||
|
|
||
| // If it's not occupied, walk through the LinkedPairs to see if you find | ||
| // Check for a Pair with the same key using strcmp (string compare) | ||
| while (current_pair != NULL && strcmp(current_pair->key, key) != 0) { | ||
| last_pair = current_pair; | ||
| current_pair = last_pair->next; | ||
| } | ||
| if (current_pair != NULL) { | ||
| // If you do, overwrite that value | ||
| current_pair->value = value; | ||
| } else { | ||
| // If it's not occupied, add a new LinkedPair to the bucket | ||
| // If not, create a new pair and add it to the LinkedList | ||
| LinkedPair *new_pair = create_pair(key, value); | ||
| new_pair->next = ht->storage[index]; | ||
| ht->storage[index] = new_pair; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /**** | ||
| Fill this in. | ||
|
|
||
| Should search the entire list of LinkedPairs for existing | ||
| keys and remove matching LinkedPairs safely. | ||
| keys and remove matching LinkedPairs safely.ma | ||
|
|
||
| Don't forget to free any malloc'ed memory! | ||
| ****/ | ||
|
|
||
There was a problem hiding this comment.
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).