From 2339c8634f18b2eaf7e68ea964d28285a42cf536 Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Mon, 20 Jun 2022 22:41:47 +1000 Subject: [PATCH] Added hashvector and examples --- pvector/Makefile | 12 +++++++++++- pvector/example-pvector.c | 36 ++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/pvector/Makefile b/pvector/Makefile index 10beb6f..ea0496c 100644 --- a/pvector/Makefile +++ b/pvector/Makefile @@ -4,7 +4,11 @@ default: libpvector.a pvector.o: CFLAGS = -Wall -g pvector.o: pvector.c | pvector.h -libpvector.a: pvector.o +#.INTERMEDIATE: pvector.o +hashvector.o: CFLAGS = -Wall -g +hashvector.o: hashvector.c | pvector.h hashvector.h + +libpvector.a: pvector.o hashvector.o $(AR) r $@ $^ CLEANRM += libpvector.a @@ -14,5 +18,11 @@ example-pvector: LDLIBS = libpvector.a example-pvector: example-pvector.o libpvector.a CLEANRM += example-pvector +#.INTERMEDIATE: example-hashvector.o +example-hashvector: CFLAGS = -Wall -g +example-hashvector: LDLIBS = libpvector.a +example-hashvector: example-hashvector.o libpvector.a +CLEANRM += example-hashvector + clean: rm -f $(CLEANRM) diff --git a/pvector/example-pvector.c b/pvector/example-pvector.c index 73beeb1..12154b5 100644 --- a/pvector/example-pvector.c +++ b/pvector/example-pvector.c @@ -89,7 +89,7 @@ static void add_entry(ipslot *tmp) { ipslot *p = (ipslot *) malloc( sizeof( ipslot ) ); memmove( p, tmp, sizeof( ipslot ) ); if ( table.data.size == table.fill ) { - (void) pvector_resize( &table.data, table.fill + 256, 0 ); + (void) pvector_resize( &table.data, table.fill + 256, 0, 0 ); } pvector_set( &table.data, table.fill++, p ); } @@ -112,29 +112,45 @@ static void load_file(const char *filename) { } } -static int int_reclaim(pvector *pv,int index,void *item) { - return 1; +static int int_reclaim(pvector *pv,unsigned long index,void *item,void *data) { + return 0; } +static int dumpitem(unsigned long index,void *item) { + fprintf( stdout, "[%ld] %p\n", index, item ); + return 0; +} + +static int dump_ipslot(unsigned long index,void *item) { + static char buffer[100]; + ipslot *ip = (ipslot*) item; + const char *p = inet_ntop( (ip->bits <= 32)? AF_INET : AF_INET6, + ip->data, buffer, 100 ); + + fprintf( stdout, "[%ld] %s/%d\n", index, p, ip->bits ); + return 0; +} int main(int argc,char **argv) { - pvector test; - pvector_resize( &test, 100, 0 ); + pvector test = { 0 }; + pvector_resize( &test, 100, 0, 0 ); pvector_set( &test, 5, (void*) 500 ); pvector_set( &test, 55, (void*) 600 ); //pvector_set( &test, 550, (void*) 800 ); - pvector_resize( &test, 300, 0 ); + pvector_resize( &test, 300, 0, 0 ); pvector_set( &test, 55, (void*) 650 ); - pvector_resize( &test, 30000, 0 ); + pvector_resize( &test, 30000, 0, 0 ); pvector_set( &test, 22255, (void*) 26 ); - pvector_resize( &test, 100, int_reclaim ); + pvector_dump( &test, dumpitem ); + pvector_resize( &test, 100, int_reclaim, 0 ); pvector_set( &test, 5, (void*) 2 ); + pvector_dump( &test, dumpitem ); -#if 0 int i; for ( i = 1; i < argc; i++ ) { load_file( argv[ i ] ); } -#endif + pvector_dump( &table.data, dump_ipslot ); + return 0; } -- 2.39.2