From 9517d28f72366500f1af9f277c8df6afc864108f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 26 Oct 2011 16:48:29 +0100 Subject: [PATCH] dlhist.c: do linear-probing when inserting entries. Somehow I apperently missed to do linear probing in he_insert that results in colliding entries read from file (and when resizing) to be dropped on the floor. Lets not drop things on the floor anymore, certainly there is another place in the table that will do fine instead of just giving up and throw it on the floor. --- dlhist.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/dlhist.c b/dlhist.c index 5ad22be..47aedaf 100644 --- a/dlhist.c +++ b/dlhist.c @@ -80,22 +80,24 @@ static void hash(union hash *h, const char *s) { SHA1((unsigned char *)s, n, h->sha1); } -static struct hash_entry* lookup(const char *key) { +static struct hash_entry* translate(union hash *he) { - unsigned int index; - union hash h; - - hash(&h, key); - - index = h.index % table_size; + unsigned int offset = he->index % table_size; /* linear probing */ - while(!he_empty(table + index)) { - if (!memcmp(table[index].hash.sha1, h.sha1, 20)) + while(!he_empty(table + offset)) { + if (!memcmp(table[offset].hash.sha1, he->sha1, 20)) break; - index = (index + 1) % table_size; + offset = (offset + 1) % table_size; } - return table + index; + return table + offset; +} + +static struct hash_entry* lookup(const char *key) { + + union hash h; + hash(&h, key); + return translate(&h); } static inline void he_set(struct hash_entry *he, const char *key) { @@ -108,7 +110,7 @@ static inline void he_set(struct hash_entry *he, const char *key) { static int he_insert(struct hash_entry *he) { - struct hash_entry *dest = table + (he->hash.index % table_size); + struct hash_entry *dest = translate(&he->hash); if (he_empty(dest)) { memcpy(dest, he, sizeof(*he));