/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "tlog.h" #include "tmalloc.h" #include "psort.h" #define LENARR (1<<10) #define LIMIT 16 /* * Example of struct */ typedef struct { int id; char *value; } EXMPL; /* * compare two struct */ static int exstr_cmp(const void *a, const void *b) { return ((EXMPL*)a)->id - ((EXMPL*)b)->id; } /* * free struct */ static void exstr_free( void *a ) { if ( a ) { if ( ((EXMPL*)a)->value ) free( ((EXMPL*)a)->value ); free( a ); } } int main(int argn, char *argv[]) { int i; EXMPL *ptr=NULL; SORT sobj; int inserted=0; EXMPL **aptr; opentlog( TL_OPEN_STDERR, TL_DEBUG, NULL); if ( PS_init( &sobj, LIMIT, exstr_cmp, exstr_free ) ) { fprintf(stderr,"No memory\n"); return 1; } srandom(1); printf("Test insert:\n"); for(i=0;ivalue=(char*)tmalloc( 64 ); } ptr->id = random() % LENARR; sprintf( ptr->value, "Value: %d, startnum: %d", ptr->id, i ); if ( PS_insert( &sobj, (void*)ptr ) ) { /* tuple has been inserted, in other case we can reuse space :) */ ptr = NULL; inserted++; } } PS_init_iterator( &sobj, 0 ); i=1; while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) { printf("%d: '%s'\n", i, ptr->value); i++; } printf("Stat: total %d; limit %d; inserted %d;\n", LENARR, PS_number(&sobj) , inserted); PS_reset( &sobj ); srandom(1); printf("Test bulk insert:\n"); aptr = (EXMPL**)tmalloc( sizeof(EXMPL*) * LENARR ); for(i=0;ivalue = (char*)tmalloc( 64 ); aptr[i]->id = random() % LENARR; sprintf( aptr[i]->value, "Value: %d, startnum: %d", aptr[i]->id, i ); } PS_bulkinsert( &sobj, (void**)aptr, LENARR ); if ( PS_init_iterator( &sobj, 7 ) ) { printf("ERROR\n"); } else { i=8; while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) { printf("%d: '%s'\n", i, ptr->value); i++; } printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj)); } PS_clear( &sobj ); srandom(1); printf("Test bulk plain insert:\n"); if ( PS_init( &sobj, LIMIT, exstr_cmp, NULL ) ) { fprintf(stderr,"No memory\n"); return 1; } ptr = (EXMPL*)tmalloc( sizeof(EXMPL) * LENARR ); for(i=0;ivalue); i++; } printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj)); } PS_clear( &sobj ); return 0; }