Created
November 16, 2012 21:47
-
-
Save trevorrjohn/4091199 to your computer and use it in GitHub Desktop.
Assoc implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string &Assoc::operator(const string key) { | |
int index = hash(key); | |
// Case where key is not in hash | |
if(heads[index] == NULL) { | |
heads[index] = new Elem(key); | |
return heads[index]->value; | |
} | |
Elem *iter = heads[index]; | |
// Case where key is already stored in hash | |
while(iter != NULL) { | |
if(iter->key == key) | |
return iter->value; | |
iter = iter->next; | |
} | |
// Case where key collided but not in hash | |
iter = new Elem(key); | |
iter->next = heads[index]; | |
heads[index] = iter; | |
return iter->value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment