diff options
author | Eric Anholt <eric@anholt.net> | 2012-11-06 16:53:51 -0800 |
---|---|---|
committer | Eric Anholt <eric@anholt.net> | 2012-11-06 17:26:52 -0800 |
commit | 642faf7475edc78d59086ad859ef734482ca275c (patch) | |
tree | 5ff1ad0c915bb5d264bb6857bc9e5b325f9bfce0 | |
parent | 28be2a358009b3366002fe50dc3def0db6fe7c38 (diff) |
Clarify the loop end conditions.
Based on a comment by Chad Versace.
-rw-r--r-- | hash_table.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/hash_table.c b/hash_table.c index cb6884e..209a4bd 100644 --- a/hash_table.c +++ b/hash_table.c @@ -161,9 +161,9 @@ hash_table_destroy(struct hash_table *ht, struct hash_entry * hash_table_search(struct hash_table *ht, uint32_t hash, const void *key) { - uint32_t hash_address; + uint32_t start_hash_address = hash % ht->size; + uint32_t hash_address = start_hash_address; - hash_address = hash % ht->size; do { uint32_t double_hash; @@ -180,7 +180,7 @@ hash_table_search(struct hash_table *ht, uint32_t hash, const void *key) double_hash = 1 + hash % ht->rehash; hash_address = (hash_address + double_hash) % ht->size; - } while (hash_address != hash % ht->size); + } while (hash_address != start_hash_address); return NULL; } @@ -225,7 +225,7 @@ struct hash_entry * hash_table_insert(struct hash_table *ht, uint32_t hash, const void *key, void *data) { - uint32_t hash_address; + uint32_t start_hash_address, hash_address; if (ht->entries >= ht->max_entries) { hash_table_rehash(ht, ht->size_index + 1); @@ -233,7 +233,8 @@ hash_table_insert(struct hash_table *ht, uint32_t hash, hash_table_rehash(ht, ht->size_index); } - hash_address = hash % ht->size; + start_hash_address = hash % ht->size; + hash_address = start_hash_address; do { struct hash_entry *entry = ht->table + hash_address; uint32_t double_hash; @@ -270,7 +271,7 @@ hash_table_insert(struct hash_table *ht, uint32_t hash, double_hash = 1 + hash % ht->rehash; hash_address = (hash_address + double_hash) % ht->size; - } while (hash_address != hash % ht->size); + } while (hash_address != start_hash_address); /* We could hit here if a required resize failed. An unchecked-malloc * application could ignore this result. |