[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Small problem in hash.c
From: |
Håkan Johansson |
Subject: |
Small problem in hash.c |
Date: |
Thu, 23 Oct 2003 11:15:12 +0200 (MEST) |
Hello
I've found that when I use the hash table in hash.c with small hash sizes
I get a lock-up. More specifically
hash_init with size < 8 gives a ht_size of 8 and more importantly a
ht_capacity of 8 (equal to ht_size). The problem arise when inserting
items using hash_insert and the hash-table already being full.
hash_insert first calls hash_find_slot to find a suitable location for the
new item, but it can't since the hash is full (=> infinite loop).
Rehashing is done only after inserting the item (and should have been done
after inserting the item that completely filled the table), but wasn't
since the
if (ht->ht_empty_slots < ht->ht_size - ht->ht_capacity)
in hash_insert_at can't be true if capacity == size. I suggest to change
< into <= , which should make it work for all initial hash-sizes, also
when capacity == size.
I looked around in the make source, and all hash_init seem to be either
already creating hash tables >= 16 (which work), or already know the final
size so rehashing isn't required.
By the way, ht_capacity is initialised to the same in hash_init and
hash_rehash, but with slightly different statements:
ht->ht_capacity = ht->ht_size - (ht->ht_size / 16); /* 93.75% loading
factor */
ht->ht_capacity = ht->ht_size - (ht->ht_size >> 4);
Best Regards,
Håkan Johansson
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Small problem in hash.c,
Håkan Johansson <=