X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=tests%2Fexample-hashvector.c;h=16e43225b25b8c92556d26824bbe151275dde367;hb=4e5a12ca4bfd6541fac14c1b0fa0d6e12168ce88;hp=7bd57a5f53a3018aa7a101b501553d64db30919b;hpb=a0be49ff8fda77c328424c09d6d0ad4a9f7e8f66;p=rrq%2Frrqmisc.git diff --git a/tests/example-hashvector.c b/tests/example-hashvector.c index 7bd57a5..16e4322 100644 --- a/tests/example-hashvector.c +++ b/tests/example-hashvector.c @@ -1,6 +1,6 @@ #include #include -#include "hashvector.h" +#include "HashVector.h" typedef struct _ipslot { int family; @@ -8,38 +8,50 @@ typedef struct _ipslot { unsigned int bits; } ipslot; -static unsigned long voidp_hashcode(void *key) { - return hashvector_hashcode( key, sizeof( ipslot ) ); +static unsigned long voidp_hashcode(void *this,void *key) { + return HashVector_hashcode( key, sizeof( ipslot ) ); } -static void* voidp_itemkey(void *item) { +static void* voidp_itemkey(void *this,void *item) { return item; } -static int voidp_haskey(void *item,void *key) { +static int voidp_haskey(void *this,void *item,void *key) { return memcmp( item, key, sizeof( ipslot ) ) == 0; } -static int shrink(vector *pv,unsigned long index,void *item,void *data) { +static void voidp_releasekey(void *this,void *key) { +} + +static int voidp_tostring(void *this, void *item, char *buffer, int limit) { + return snprintf( buffer, limit, "%p", item ); +} + +static int shrink(Vector *pv,unsigned long index,void *item,void *data) { if ( item ) { if ( item == HV_HOLE ) { - ((hashvector*) data)->holes--; + ((HashVector*) data)->holes--; } else { free( item ); - ((hashvector*) data)->fill--; + ((HashVector*) data)->fill--; } } return 0; } int main(int argc,char **argv) { - hashvector hv = { - .table = { 4, 0 }, + ItemKeyFun voidpfun = { + .hashcode = voidp_hashcode, + .itemkey = voidp_itemkey, + .haskey = voidp_haskey, + .releasekey = voidp_releasekey, + .tostring = voidp_tostring + }; + HashVector hv = { + .table = { 1, 4, 0 }, .fill = 0, .holes = 0, - .keyhashcode = voidp_hashcode, - .itemkey = voidp_itemkey, - .haskey = voidp_haskey + .type = &voidpfun }; int i = 0; for ( ; i < 259; i++ ) { @@ -50,15 +62,15 @@ int main(int argc,char **argv) { } item->family = i; memcpy( item->data, "10.10.10.1", 10 ); - hashvector_add( &hv, item ); + HashVector_add( &hv, item ); } for ( i = 256; i < 260; i++ ) { - vector_index index = i; - void ** slot = vector_next_used( &hv.table, &index ); + VectorIndex index = i; + void ** slot = Vector_next_used( &hv.table, &index ); if ( slot && *slot != HV_HOLE ) { - hashvector_delete( &hv, *slot ); + HashVector_delete( &hv, *slot ); } } - vector_resize( &hv.table, 256, shrink, &hv ); + Vector_resize( &hv.table, 256, shrink, &hv ); return 0; }