Skip to content

Instantly share code, notes, and snippets.

@trevorrjohn
Created November 16, 2012 21:47
Show Gist options
  • Save trevorrjohn/4091199 to your computer and use it in GitHub Desktop.
Save trevorrjohn/4091199 to your computer and use it in GitHub Desktop.
Assoc implementation
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