revise hashvector_contents to optionally create the target vector
[rrq/rrqmisc.git] / vector / hashvector.c
index b39ef5ed2851168e78b0db7c4806b7ec0570c1cb..8b88135f0b660fec87e9994cc73803cb3af09a31 100644 (file)
@@ -158,23 +158,28 @@ int hashvector_delete(hashvector *hv,void *item) {
     return 1;
 }
 
-// Copy items into a vector. Returns 0 on success and -1 on failure.
-int hashvector_contents(hashvector *hv,vector *pv) {
-    if ( vector_resize( pv, hv->fill, 0, 0 ) ) {
-       return -1;
-    }
-    unsigned long from = 0;
-    unsigned long to = 0;
-    for ( ; to < hv->fill; from++ ) {
-       void **slot = vector_next_used( &hv->table, &from );
-       if ( slot == 0 ) {
-           break;
-       }
-       if ( *slot != HV_HOLE ) {
-           vector_set( pv, to++, *slot );
+vector *hashvector_contents(
+    hashvector *hv,enum vector_variant variant,vector *v)
+{
+    if ( v == 0 ) {
+       if ( hv->fill == 0 ) {
+           return 0;
        }
+       v = (vector*) malloc( sizeof( vector ) );
+    } else {
+       vector_resize( v, 0, vector_clear_any, 0 );
     }
-    return 0;
+    (*v) = (vector) { .variant = variant, 0, 0 };
+    if ( hv->fill == 0 ) {
+       return v;
+    }
+    vector_resize( v, hv->fill, 0, 0 );
+    vector_index i;
+    vector_index j = 0;
+    for ( i = 0; i < v->size; i++, j++ ) {
+       vector_set( v, i, hashvector_next( hv, &j, 0 ) );
+    }
+    return v;
 }
 
 // A simple binary hashcode, (before modulo table size)