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:
parent
113dc524ed
commit
9517d28f72
1 changed files with 14 additions and 12 deletions
26
dlhist.c
26
dlhist.c
|
|
@ -80,22 +80,24 @@ static void hash(union hash *h, const char *s) {
|
||||||
SHA1((unsigned char *)s, n, h->sha1);
|
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;
|
unsigned int offset = he->index % table_size;
|
||||||
union hash h;
|
|
||||||
|
|
||||||
hash(&h, key);
|
|
||||||
|
|
||||||
index = h.index % table_size;
|
|
||||||
|
|
||||||
/* linear probing */
|
/* linear probing */
|
||||||
while(!he_empty(table + index)) {
|
while(!he_empty(table + offset)) {
|
||||||
if (!memcmp(table[index].hash.sha1, h.sha1, 20))
|
if (!memcmp(table[offset].hash.sha1, he->sha1, 20))
|
||||||
break;
|
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) {
|
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) {
|
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)) {
|
if (he_empty(dest)) {
|
||||||
memcpy(dest, he, sizeof(*he));
|
memcpy(dest, he, sizeof(*he));
|
||||||
|
|
|
||||||
Reference in a new issue