X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=pvector%2Fexample-hashvector.c;h=0d64c9e5cbbbcae9543032214c98bfda870db4fd;hb=b901ebd3cd786e8cec0f10c461f8bcc8e4219081;hp=214738f30e4695a2908af1c134e57917e947d4ef;hpb=ba5cab4faddcde50d4321267f775f0ab884da39e;p=rrq%2Frrqmisc.git diff --git a/pvector/example-hashvector.c b/pvector/example-hashvector.c index 214738f..0d64c9e 100644 --- a/pvector/example-hashvector.c +++ b/pvector/example-hashvector.c @@ -15,14 +15,8 @@ typedef struct _ipslot { unsigned int bits; } ipslot; -static int voidp_hashcode(void *key) { - unsigned char *p = (unsigned char *) key; - int value = 5381; - int i = 0; - for ( ; i < sizeof( ipslot ); i++ ) { - value += ( value << 5 ) + *(p++); - } - return value; +static unsigned long voidp_hashcode(void *key) { + return hashvector_hashcode( key, sizeof( ipslot ) ); } static void* voidp_itemkey(void *item) { @@ -35,7 +29,7 @@ static int voidp_haskey(void *item,void *key) { static struct { hashvector hv; - int fill; + long fill; } table = { .hv = { .table = { 12000, 0 }, @@ -154,12 +148,12 @@ static int int_reclaim(pvector *pv,unsigned long index,void *item,void *data) { return 0; } -static int dumpitem(unsigned long index,void *item) { +static int dumpitem(const unsigned long index,const void *item) { fprintf( stdout, "[%ld] %p\n", index, item ); return 0; } -static int dump_ipslot(unsigned long index,void *item) { +static int dump_ipslot(const unsigned long index,const void *item) { static char buffer[100]; ipslot *ip = (ipslot*) item; const char *p = inet_ntop( ip->family, ip->data, buffer, 100 ); @@ -167,7 +161,7 @@ static int dump_ipslot(unsigned long index,void *item) { return 0; } -static int compare_ipslot(void *ax,void *bx) { +static int compare_ipslot(const void *ax,const void *bx) { ipslot *a = (ipslot *) ax; ipslot *b = (ipslot *) bx; int x = b->family - a->family; @@ -198,7 +192,20 @@ static int compare_ipslot(void *ax,void *bx) { return 0; } +static int shrink(pvector *pv,unsigned long index,void *item,void *data) { + if ( item ) { + if ( item == HV_HOLE ) { + ((hashvector*) data)->holes--; + } else { + free( item ); + ((hashvector*) data)->fill--; + } + } + return 0; +} + int main(int argc,char **argv) { +#if TEST0 pvector test = { 0 }; pvector_resize( &test, 100, 0, 0 ); pvector_set( &test, 5, (void*) 500 ); @@ -218,14 +225,39 @@ int main(int argc,char **argv) { for ( i = 1; i < argc; i++ ) { load_file( argv[ i ] ); } + fprintf( stdout, "---- hashvector after filling it %ld/%ld/%ld\n", + table.hv.fill, table.hv.holes, table.hv.table.size ); pvector_dump( &table.hv.table, dump_ipslot ); - fprintf( stdout, "--------------\n" ); - if ( hashvector_pack( &table.hv, &test ) < 0 ) { + if ( hashvector_contents( &table.hv, &test ) < 0 ) { fprintf( stdout, "test is not empty\n" ); } + fprintf( stdout, "---- hashvector contents in hash order\n" ); pvector_dump( &test, dump_ipslot ); - fprintf( stdout, "--------------\n" ); pvector_qsort( &test, compare_ipslot ); + fprintf( stdout, "---- contents after sorting\n" ); pvector_dump( &test, dump_ipslot ); +#endif +#if TEST1 + hashvector hv = { + .table = { 4, 0 }, + .fill = 0, + .holes = 0, + .keyhashcode = voidp_hashcode, + .itemkey = voidp_itemkey, + .haskey = voidp_haskey + }; + int i = 0; + for ( ; i < 259; i++ ) { + ipslot *item = (ipslot*) calloc( 1, sizeof( ipslot ) ); + if ( i > 250 ) { + int n = i; + i = n; + } + item->family = i; + memcpy( item->data, "10.10.10.1", 10 ); + hashvector_add( &hv, item ); + } + pvector_resize( &hv.table, 256, shrink, &hv ); +#endif return 0; }