#include <stdlib.h>
#include <HashVector.h>
-#define SELF hv->type
-
// Find the slot for the keyed element, and return pointer to it, or
// to the first of holes encountered while considering collisions.
// Returns a pointer to the place for the item, or 0 in case of OOM or
{
if ( itemkey ) {
// Get actual key from keying item
- key = hv->type->itemkey( SELF, key );
+ key = ItemKeyFun_itemkey( hv->type, key );
}
- unsigned long index = hv->type->hashcode( SELF, key ) % hv->table.size;
+ unsigned long index = ItemKeyFun_hashcode( hv->type, key ) % hv->table.size;
*i = index;
void **hole = 0;
void **p = 0;
p = Vector_entry( &hv->table, (*i) );
if ( p == 0 ) {
if ( itemkey ) {
- hv->type->releasekey( SELF, key );
+ ItemKeyFun_releasekey( hv->type, key );
}
return 0; // This basically means OOM, and is a failure condition.
}
if ( (*p) == 0 ) {
if ( itemkey ) {
- hv->type->releasekey( SELF, key );
+ ItemKeyFun_releasekey( hv->type, key );
}
return ( hole )? hole : p; // Not found; it's place is here.
}
if ( hole == 0 ) {
hole = p; // Remember the first hole
}
- } else if ( hv->type->haskey( SELF, (*p), key ) ) {
+ } else if ( ItemKeyFun_haskey( hv->type, (*p), key ) ) {
if ( itemkey ) {
- hv->type->releasekey( SELF, key );
+ ItemKeyFun_releasekey( hv->type, key );
}
return p; // Found
}
}
if ( (*i) == index ) {
if ( itemkey ) {
- hv->type->releasekey( SELF, key );
+ ItemKeyFun_releasekey( hv->type, key );
}
return 0; // Overfull HashVector!
}
// Find the keyed item
void *HashVector_find(HashVector *hv,void *key) {
VectorIndex index = 0;
- void **slot = HashVector_find_slot( hv, &index, key, 0 );
+ void **slot = HashVector_find_slot( hv, key, &index, 0 );
return ( slot && *slot && *slot != HV_HOLE )? *slot : 0;
}
-// Find the keyed element at or after the index. Update index and
-// return item.
-void *HashVector_next(HashVector *hv,VectorIndex *index,void *key) {
- unsigned long i = index? *index : 0;
- for ( ; i < hv->table.size; i++ ) {
- void **p = Vector_next_used( &hv->table, &i );
+// Find any element at or after the index that admits to the key.
+// Update index and return item.
+void *HashVector_next(HashVector *hv,VectorIndex *index) {
+ for ( ; (*index) < hv->table.size; (*index)++ ) {
+ void **p = Vector_next_used( &hv->table, index );
if ( p == 0 ) {
break;
}
if ( *p && *p != HV_HOLE ) {
- if ( key && hv->type->haskey( hv->type, *p, key ) == 0 ) {
- continue;
- }
- if ( index ) {
- (*index) = i;
- }
return *p;
}
}
- if ( index ) {
- (*index) = hv->table.size;
- }
+ (*index) = hv->table.size;
return 0;
}
static int capture_item(Vector *pv,unsigned long ix,void *item,void *data) {
+ (void)pv; (void)ix;
if ( item && item != HV_HOLE ) {
HashVector_add( (HashVector *) data, item );
}
}
static int iter_item(unsigned long ix,void *item,void *data) {
+ (void)ix;
if ( item && item != HV_HOLE ) {
HashVector_add( (HashVector *) data, item );
}
VectorIndex i;
VectorIndex j = 0;
for ( i = 0; i < v->size; i++, j++ ) {
- Vector_set( v, i, HashVector_next( hv, &j, 0 ) );
+ Vector_set( v, i, HashVector_next( hv, &j ) );
}
return v;
}