sample.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4
5 #include "dynda.h"
6
7
8
9 static void Status( hDary h )
10 {
11 int ret;
12 Dstat st;
13
14 if ( ( ret = Dary_Status( h, &st ) ) != DARY_OK )
15 Dary_Error( ret );
16
17 printf( "----- Status -----\n" );
18 printf( "No. keys: %u\n", st.nkey );
19 printf( "No. blocks: %u\n", st.nblk );
20 printf( "No. valid blocks: %u [%u]\n", st.mblk, st.max );
21 printf( "No. empty elements: %u\n", st.nemp );
22 printf( "Len. Tail: %u\n", st.ntail );
23 printf( "Size: %u\n", st.size );
24 printf( "------------------\n" );
25 }
26
27
28 static int GetKey( char *s, int n, FILE *fp )
29 {
30 int len;
31
32 if ( fgets( s, n, fp ) == NULL )
33 return -1;
34
35 len = ( int )strlen( s );
36 if ( len > 0 && s[ len - 1 ] == '\n' ) s[ --len ] = '\0';
37 if ( len > 0 && s[ len - 1 ] == '\r' ) s[ --len ] = '\0';
38
39 return len;
40 }
41
42
43 static int Test_Insert( hDary da, FILE *fp )
44 {
45 int ret;
46 char key[256];
47
48 rewind( fp );
49 while ( GetKey( key, sizeof( key ), fp ) >= 0 )
50 {
51 if ( ( ret = Dary_Insert( da, key ) ) != DARY_OK )
52 {
53 fprintf( stderr, "Insert: %s\n", key );
54 Dary_Error( ret );
55 return ret;
56 }
57 }
58
59 return 0;
60 }
61
62
63 static int Test_Search( hDary da, FILE *fp )
64 {
65 int ret;
66 char key[256];
67
68 rewind( fp );
69 while ( GetKey( key, sizeof( key ), fp ) >= 0 )
70 {
71 if ( ( ret = Dary_Search( da, key ) ) != DARY_OK )
72 {
73 fprintf( stderr, "Search: %s\n", key );
74 Dary_Error( ret );
75 return ret;
76 }
77 }
78
79 return 0;
80 }
81
82
83 static int Test_Delete( hDary da, FILE *fp )
84 {
85 int ret;
86 char key[256];
87
88 rewind( fp );
89 while ( GetKey( key, sizeof( key ), fp ) >= 0 )
90 {
91 if ( ( ret = Dary_Delete( da, key ) ) != DARY_OK )
92 {
93 fprintf( stderr, "Delete: %s\n", key );
94 Dary_Error( ret );
95 return ret;
96 }
97 }
98
99 return 0;
100 }
101
102
103 static int Test_Dary( FILE *fp )
104 {
105 int ret;
106 hDary da;
107 clock_t cl;
108
109 if ( ( ret = Dary_Open( &da, NULL ) ) != DARY_OK )
110 Dary_Error( ret );
111
112 printf( "Building...\n" );
113 cl = clock();
114 Test_Insert( da, fp );
115 cl = clock() - cl;
116 printf( "%.2f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
117
118 printf( "Searching...\n" );
119 cl = clock();
120 Test_Search( da, fp );
121 cl = clock() - cl;
122 printf( "%.2f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
123
124 Status( da );
125
126 printf( "Adjusting...\n" );
127 cl = clock();
128 if ( ( ret = Dary_AdjustBC( da ) ) != DARY_OK )
129 Dary_Error( ret );
130 if ( ( ret = Dary_AdjustTail( da ) ) != DARY_OK )
131 Dary_Error( ret );
132 cl = clock() - cl;
133 printf( "%.2f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
134
135 Status( da );
136
137 printf( "Deleting...\n" );
138 cl = clock();
139 Test_Delete( da, fp );
140 cl = clock() - cl;
141 printf( "%.2f sec\n", 1.0 * cl / CLOCKS_PER_SEC );
142
143 if ( ( ret = Dary_Close( da ) ) != DARY_OK )
144 Dary_Error( ret );
145
146 return ret;
147 }
148
149
150 static int Test_Main( const char *path )
151 {
152 int ret;
153 FILE *fp;
154
155 if ( ( fp = fopen( path, "rb" ) ) == NULL )
156 {
157 perror( path );
158 return -1;
159 }
160
161 ret = Test_Dary( fp );
162
163 fclose( fp );
164
165 return ret;
166 }
167
168
169 int main( int argc, char *argv[] )
170 {
171 if ( argc != 2 )
172 {
173 fprintf( stderr, "Usage: %s File\n", argv[0] );
174 return -1;
175 }
176
177 return Test_Main( argv[1] );
178 }