Skip to content

Commit

Permalink
Merge pull request jamesroutley#6 from badagent/master
Browse files Browse the repository at this point in the history
Fixed logical flaw concerning HT_DELETED_ITEM handling
  • Loading branch information
jamesroutley authored Aug 24, 2017
2 parents b68b3ab + 1fa983f commit bd97680
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions 05-methods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ void ht_delete(ht_hash_table* ht, const char* key) {
int index = ht_get_hash(key, ht->size, 0);
ht_item* item = ht->items[index];
int i = 1;
while (item != NULL && item != &HT_DELETED_ITEM) {
if (strcmp(item->key, key) == 0) {
ht_del_item(item);
ht->items[index] = &HT_DELETED_ITEM;
while (item != NULL) {
       if (item != &HT_DELETED_ITEM) {
if (strcmp(item->key, key) == 0) {
ht_del_item(item);
ht->items[index] = &HT_DELETED_ITEM;
}
}
index = ht_get_hash(key, ht->size, i);
item = ht->items[index];
Expand Down Expand Up @@ -113,7 +115,12 @@ void ht_insert(ht_hash_table* ht, const char* key, const char* value) {

char* ht_search(ht_hash_table* ht, const char* key) {
// ...
while (item != NULL && item != &HT_DELETED_ITEM) {
while (item != NULL) {
       if (item != &HT_DELETED_ITEM) {
if (strcmp(item->key, key) == 0) {
return item->value;
}
}
// ...
}
// ...
Expand All @@ -134,11 +141,13 @@ the new item at its location.
// hash_table.c
void ht_insert(ht_hash_table* ht, const char* key, const char* value) {
// ...
while (cur_item != NULL && cur_item != &HT_DELETED_ITEM) {
if (strcmp(cur_item->key, key) == 0) {
ht_del_item(cur_item);
ht->items[index] = item;
return;
while (cur_item != NULL) {
       if (cur_item != &HT_DELETED_ITEM) {
if (strcmp(cur_item->key, key) == 0) {
ht_del_item(cur_item);
ht->items[index] = item;
return;
}
}
// ...
}
Expand Down

0 comments on commit bd97680

Please sign in to comment.