summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAllan McRae <allan@archlinux.org>2011-01-29 23:24:13 +1000
committerAllan McRae <allan@archlinux.org>2011-02-04 09:55:45 +1000
commit11e5e86151f83219b1cedbca31be7530911e25eb (patch)
treec873bc0c46fd61e8d01cd0766d1d90dc542f5ca8 /lib
parent93207863493b54e794098b4ee5987d59c8c4b14b (diff)
downloadpacman-11e5e86151f83219b1cedbca31be7530911e25eb.tar.xz
Refactor finding position for new hash entry
Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/libalpm/pkghash.c37
1 files changed, 18 insertions, 19 deletions
diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c
index 44a03ab5..bdff78ea 100644
--- a/lib/libalpm/pkghash.c
+++ b/lib/libalpm/pkghash.c
@@ -81,11 +81,25 @@ pmpkghash_t *_alpm_pkghash_create(size_t size)
return(hash);
}
+static size_t get_hash_position(unsigned long name_hash, pmpkghash_t *hash)
+{
+ size_t position;
+ alpm_list_t *ptr;
+
+ position = name_hash % hash->buckets;
+
+ /* collision resolution using open addressing with linear probing */
+ while((ptr = hash->hash_table[position]) != NULL) {
+ position = (position + 1) % hash->buckets;
+ }
+
+ return(position);
+}
+
/* Expand the hash table size to the next increment and rebin the entries */
static pmpkghash_t *rehash(pmpkghash_t *oldhash)
{
pmpkghash_t *newhash;
- alpm_list_t *ptr;
size_t newsize, position, i;
/* Hash tables will need resized in two cases:
@@ -120,12 +134,7 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
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;
- }
+ position = get_hash_position(package->name_hash, newhash);
newhash->hash_table[position] = oldhash->hash_table[i];
oldhash->hash_table[i] = NULL;
@@ -152,12 +161,7 @@ pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
hash = rehash(hash);
}
- position = pkg->name_hash % hash->buckets;
-
- /* collision resolution using open addressing with linear probing */
- while((ptr = hash->hash_table[position]) != NULL) {
- position = (position + 1) % hash->buckets;
- }
+ position = get_hash_position(pkg->name_hash, hash);
ptr = calloc(1, sizeof(alpm_list_t));
if(ptr == NULL) {
@@ -188,12 +192,7 @@ pmpkghash_t *_alpm_pkghash_add_sorted(pmpkghash_t *hash, pmpkg_t *pkg)
hash = rehash(hash);
}
- position = pkg->name_hash % hash->buckets;
-
- /* collision resolution using open addressing with linear probing */
- while((ptr = hash->hash_table[position]) != NULL) {
- position = (position + 1) % hash->buckets;
- }
+ position = get_hash_position(pkg->name_hash, hash);
ptr = calloc(1, sizeof(alpm_list_t));
if(ptr == NULL) {