Rehash efficiently

Rehash without recreating the hash table list or reallocating the
memory for holding the list nodes.

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Allan McRae 2011-01-29 22:50:55 +10:00
parent c9820ec97b
commit 9320786349
1 changed files with 21 additions and 3 deletions

View File

@ -86,7 +86,8 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
{
pmpkghash_t *newhash;
alpm_list_t *ptr;
size_t newsize;
size_t newsize, position, i;
/* Hash tables will need resized in two cases:
* - adding packages to the local database
* - poor estimation of the number of packages in sync database
@ -112,10 +113,27 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
return(oldhash);
}
for(ptr = oldhash->list; ptr != NULL; ptr = ptr->next) {
newhash = _alpm_pkghash_add(newhash, ptr->data);
newhash->list = oldhash->list;
oldhash->list = NULL;
for(i = 0; i < oldhash->buckets; i++) {
if(oldhash->hash_table[i] != NULL) {
pmpkg_t *package = oldhash->hash_table[i]->data;
position = package->name_hash % newhash->buckets;
/* collision resolution using open addressing with linear probing */
while((ptr = newhash->hash_table[position]) != NULL) {
position = (position + 1) % newhash->buckets;
}
newhash->hash_table[position] = oldhash->hash_table[i];
oldhash->hash_table[i] = NULL;
}
}
newhash->entries = oldhash->entries;
_alpm_pkghash_free(oldhash);
return(newhash);