jamesroutley/write-a-hash-table

Possible non-ending search function?

FedericoPonzi opened this issue · 2 comments

What happens if the all the buckets are filled and we are searching for an item?

char* ht_search(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) {
        if (strcmp(item->key, key) == 0) {
            return item->value;
        }
        index = ht_get_hash(key, ht->size, i);
        item = ht->items[index];
        i++;
    } 
    return NULL;
}

Seems like there is not exit condition to verify that key is not in the hashtable.

Hey @FedericoPonzi - you're absolutely right, there's no error checking there. However, in section 6 we add support for increasing the number of buckets when the hash table gets too full. The idea is that resizing will mean that the hash table is never full.

I didn't include error checking here for the sake of brevity, but I think it would be a good idea for a production-ready hash table.

Thanks @jamesroutley for the reply, you're totally right!