Archived
1
0
Fork 0

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.
This commit is contained in:
Henrik Hautakoski 2011-10-26 16:48:29 +01:00
parent 113dc524ed
commit 9517d28f72

View file

@ -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));