poll<=select
[tedtools.git] / psortex.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *        notice, this list of conditions and the following disclaimer in the
12  *        documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *        may be used to endorse or promote products derived from this software
15  *        without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "psort.h"
34
35 #define LENARR  (1<<10)
36 #define LIMIT   16
37
38 /*
39  * Example of struct
40  */
41 typedef struct {
42         int     id;
43         char    *value;
44 } EXMPL;
45
46 /*
47  * compare two struct
48  */
49 static int
50 exstr_cmp(const void *a, const void *b) {
51         return ((EXMPL*)a)->id - ((EXMPL*)b)->id;
52 }
53
54 /*
55  * free struct
56  */
57 static void
58 exstr_free( void *a ) {
59         if ( a ) {
60                 if ( ((EXMPL*)a)->value ) free( ((EXMPL*)a)->value );
61                 free( a );
62         }
63 }
64
65 int 
66 main(int argn, char *argv[]) {
67         int i;
68         EXMPL *ptr=NULL;
69         SORT    sobj;
70         int inserted=0;
71         EXMPL   **aptr;
72
73         if ( PS_init( &sobj, LIMIT, exstr_cmp, exstr_free ) ) {
74                 fprintf(stderr,"No memory\n");
75                 return 1;
76         }
77         srandom(1);
78         printf("Test insert:\n");
79         for(i=0;i<LENARR;i++) {
80                 if ( ! ptr ) {
81                         ptr = (EXMPL*)malloc( sizeof(EXMPL) );
82                         if ( ! ptr ) {
83                                 fprintf(stderr,"No memory\n");
84                                 return 1;       
85                         }
86                         ptr->value=(char*)malloc( 64 );
87                         if ( ! ptr->value ) {
88                                 fprintf(stderr,"No memory\n");
89                                 return 1;       
90                         }
91                 }
92                 ptr->id = random() % LENARR;
93                 sprintf( ptr->value, "Value: %d, startnum: %d", ptr->id, i ); 
94
95                 if ( PS_insert( &sobj, (void*)ptr ) ) { 
96                         /* tuple has been inserted, in 
97                          other case we can reuse space :) */
98                         ptr = NULL;
99                         inserted++;
100                 }
101         }
102
103         PS_init_iterator( &sobj, 0 );
104         i=1;
105         while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
106                 printf("%d: '%s'\n", i, ptr->value);
107                 i++;
108         }
109         printf("Stat: total %d; limit %d; inserted %d;\n", LENARR, PS_number(&sobj) , inserted); 
110         PS_reset( &sobj );
111
112         srandom(1);
113         printf("Test bulk insert:\n");
114         aptr = (EXMPL**)malloc( sizeof(EXMPL*) * LENARR );
115         if ( ! aptr ) {
116                 fprintf(stderr,"No memory\n");
117                 return 1;       
118         }
119         for(i=0;i<LENARR;i++) {
120                 aptr[i] = (EXMPL*)malloc( sizeof(EXMPL) );
121                 if ( ! aptr[i] ) {
122                         fprintf(stderr,"No memory\n");
123                         return 1;       
124                 }
125                 aptr[i]->value = (char*)malloc( 64 );
126                 if ( ! aptr[i]->value ) {
127                         fprintf(stderr,"No memory\n");
128                         return 1;       
129                 }
130                 aptr[i]->id = random() % LENARR;
131                 sprintf( aptr[i]->value, "Value: %d, startnum: %d", aptr[i]->id, i ); 
132         }
133
134         PS_bulkinsert( &sobj, (void**)aptr, LENARR );
135
136         if ( PS_init_iterator( &sobj, 7 ) ) {
137                 printf("ERROR\n");
138         } else { 
139                 i=8;
140                 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
141                         printf("%d: '%s'\n", i, ptr->value);
142                         i++;
143                 }
144                 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));
145         } 
146         PS_clear( &sobj );
147
148
149         srandom(1);
150         printf("Test bulk plain insert:\n");
151
152         if ( PS_init( &sobj, LIMIT, exstr_cmp, NULL ) ) {
153                 fprintf(stderr,"No memory\n");
154                 return 1;
155         }
156         ptr = (EXMPL*)malloc( sizeof(EXMPL) * LENARR );
157         if ( ! ptr ) {
158                 fprintf(stderr,"No memory\n");
159                 return 1;
160         }
161
162         for(i=0;i<LENARR;i++) {
163                 ptr[i].value = (char*)malloc( 64 );
164                 if ( ! ptr[i].value ) {
165                         fprintf(stderr,"No memory\n");
166                         return 1;       
167                 }
168                 ptr[i].id = random() % LENARR;
169                 sprintf( ptr[i].value, "Value: %d, startnum: %d", ptr[i].id, i ); 
170         }
171         PS_bulkplain( &sobj, (void*)ptr, sizeof(EXMPL), LENARR );
172         if ( PS_init_iterator( &sobj, 7 ) ) {
173                 printf("ERROR\n");
174         } else { 
175                 i=8;
176                 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
177                         printf("%d: '%s'\n", i, ptr->value);
178                         i++;
179                 }
180                 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));
181         } 
182         PS_clear( &sobj );
183
184         return 0;
185 }
186