X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=pvector%2Fhashvector.c;h=11fdae845a9837e04e46ebc2450b433078af0ed0;hb=f78bdf5872a5a3dd96e26f94173d272e0227b785;hp=5efbd8af801eda3927d8265c2d4553cb4678f5ef;hpb=efd06144cd7b5ab1720ef2cda59bd7568f31d034;p=rrq%2Frrqmisc.git diff --git a/pvector/hashvector.c b/pvector/hashvector.c index 5efbd8a..11fdae8 100644 --- a/pvector/hashvector.c +++ b/pvector/hashvector.c @@ -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 ); } }