cleanup
[rrq/rrqmisc.git] / pvector / hashvector.c
index 5efbd8af801eda3927d8265c2d4553cb4678f5ef..11fdae845a9837e04e46ebc2450b433078af0ed0 100644 (file)
@@ -39,7 +39,9 @@ static void **hashvector_find_slot(hashvector *hv,void *key) {
 int hashvector_find(hashvector *hv,void *key,void **x) {
     void **p = hashvector_find_slot( hv, key );
     if ( p && *p && *p != HV_HOLE ) {
-       *x = *p;
+       if ( x ) {
+           *x = *p;
+       }
        return 1;
     }
     return 0;
@@ -52,13 +54,24 @@ static int capture_item(pvector *pv,unsigned long ix,void *item,void *data) {
     return 0;
 }
 
+static int iter_item(unsigned long ix,void *item,void *data) {
+    if ( item && item != HV_HOLE ) {
+       hashvector_add( (hashvector *) data, item );
+    }
+    return 0;
+}
+
 static void hashvector_resize(hashvector *hv,unsigned long new_size) {
     hashvector tmp = *hv;
     hv->table.size = new_size;
     hv->table.entries = 0;
     hv->fill = 0;
     hv->holes = 0;
-    pvector_resize( &tmp.table, 0, capture_item, hv );
+    if ( new_size < hv->table.size ) {
+       pvector_resize( &tmp.table, 0, capture_item, hv );
+    } else {
+       pvector_iterate( &tmp.table, iter_item, hv );
+    }
 }
     
 // Add the given element.
@@ -92,8 +105,9 @@ int hashvector_delete(hashvector *hv,void *item) {
     }
     *p = HV_HOLE;
     hv->holes++;
+    hv->fill--;
     if ( hv->table.size > 256 ) {
-       if ( hv->fill < hv->table.size / 2 ) {
+       if ( hv->fill < hv->table.size / 4 ) {
            hashvector_resize( hv, hv->table.size / 2 );
        }
     }