54 | |
55 | Using the generic macro API (C11 and later):
56 |
57 | ```c
58 | #include
59 |
60 | // Instantiating a set template.
61 | #define NAME int_set
62 | #define KEY_TY int
63 | #include "verstable.h"
64 |
65 | // Instantiating a map template.
66 | #define NAME int_int_map
67 | #define KEY_TY int
68 | #define VAL_TY int
69 | #include "verstable.h"
70 |
71 | int main( void )
72 | {
73 | // Set.
74 |
75 | int_set our_set;
76 | vt_init( &our_set );
77 |
78 | // Inserting keys.
79 | for( int i = 0; i < 10; ++i )
80 | {
81 | int_set_itr itr = vt_insert( &our_set, i );
82 | if( vt_is_end( itr ) )
83 | {
84 | // Out of memory, so abort.
85 | vt_cleanup( &our_set );
86 | return 1;
87 | }
88 | }
89 |
90 | // Erasing keys.
91 | for( int i = 0; i < 10; i += 3 )
92 | vt_erase( &our_set, i );
93 |
94 | // Retrieving keys.
95 | for( int i = 0; i < 10; ++i )
96 | {
97 | int_set_itr itr = vt_get( &our_set, i );
98 | if( !vt_is_end( itr ) )
99 | printf( "%d ", itr.data->key );
100 | }
101 | // Printed: 1 2 4 5 7 8
102 |
103 | // Iteration.
104 | for(
105 | int_set_itr itr = vt_first( &our_set );
106 | !vt_is_end( itr );
107 | itr = vt_next( itr )
108 | )
109 | printf( "%d ", itr.data->key );
110 | // Printed: 2 4 7 1 5 8
111 |
112 | vt_cleanup( &our_set );
113 |
114 | // Map.
115 |
116 | int_int_map our_map;
117 | vt_init( &our_map );
118 |
119 | // Inserting keys and values.
120 | for( int i = 0; i < 10; ++i )
121 | {
122 | int_int_map_itr itr =
123 | vt_insert( &our_map, i, i + 1 );
124 | if( vt_is_end( itr ) )
125 | {
126 | // Out of memory, so abort.
127 | vt_cleanup( &our_map );
128 | return 1;
129 | }
130 | }
131 |
132 | // Erasing keys and values.
133 | for( int i = 0; i < 10; i += 3 )
134 | vt_erase( &our_map, i );
135 |
136 | // Retrieving keys and values.
137 | for( int i = 0; i < 10; ++i )
138 | {
139 | int_int_map_itr itr = vt_get( &our_map, i );
140 | if( !vt_is_end( itr ) )
141 | printf(
142 | "%d:%d ",
143 | itr.data->key,
144 | itr.data->val
145 | );
146 | }
147 | // Printed: 1:2 2:3 4:5 5:6 7:8 8:9
148 |
149 | // Iteration.
150 | for(
151 | int_int_map_itr itr = vt_first( &our_map );
152 | !vt_is_end( itr );
153 | itr = vt_next( itr )
154 | )
155 | printf(
156 | "%d:%d ",
157 | itr.data->key,
158 | itr.data->val
159 | );
160 | // Printed: 2:3 4:5 7:8 1:2 5:6 8:9
161 |
162 | vt_cleanup( &our_map );
163 | }
164 | ```
165 |
166 | |
167 |
168 | Using the prefixed functions API (C99 and later):
169 |
170 | ```c
171 | #include
172 |
173 | // Instantiating a set template.
174 | #define NAME int_set
175 | #define KEY_TY int
176 | #define HASH_FN vt_hash_integer
177 | #define CMPR_FN vt_cmpr_integer
178 | #include "verstable.h"
179 |
180 | // Instantiating a map template.
181 | #define NAME int_int_map
182 | #define KEY_TY int
183 | #define VAL_TY int
184 | #define HASH_FN vt_hash_integer
185 | #define CMPR_FN vt_cmpr_integer
186 | #include "verstable.h"
187 |
188 | int main( void )
189 | {
190 | // Set.
191 |
192 | int_set our_set;
193 | int_set_init( &our_set );
194 |
195 | // Inserting keys.
196 | for( int i = 0; i < 10; ++i )
197 | {
198 | int_set_itr itr =
199 | int_set_insert( &our_set, i );
200 | if( int_set_is_end( itr ) )
201 | {
202 | // Out of memory, so abort.
203 | int_set_cleanup( &our_set );
204 | return 1;
205 | }
206 | }
207 |
208 | // Erasing keys.
209 | for( int i = 0; i < 10; i += 3 )
210 | int_set_erase( &our_set, i );
211 |
212 | // Retrieving keys.
213 | for( int i = 0; i < 10; ++i )
214 | {
215 | int_set_itr itr = int_set_get( &our_set, i );
216 | if( !int_set_is_end( itr ) )
217 | printf( "%d ", itr.data->key );
218 | }
219 | // Printed: 1 2 4 5 7 8
220 |
221 | // Iteration.
222 | for(
223 | int_set_itr itr =
224 | int_set_first( &our_set );
225 | !int_set_is_end( itr );
226 | itr = int_set_next( itr )
227 | )
228 | printf( "%d ", itr.data->key );
229 | // Printed: 2 4 7 1 5 8
230 |
231 | int_set_cleanup( &our_set );
232 |
233 | // Map.
234 |
235 | int_int_map our_map;
236 | int_int_map_init( &our_map );
237 |
238 | // Inserting keys and values.
239 | for( int i = 0; i < 10; ++i )
240 | {
241 | int_int_map_itr itr =
242 | int_int_map_insert( &our_map, i, i + 1 );
243 | if( int_int_map_is_end( itr ) )
244 | {
245 | // Out of memory, so abort.
246 | int_int_map_cleanup( &our_map );
247 | return 1;
248 | }
249 | }
250 |
251 | // Erasing keys and values.
252 | for( int i = 0; i < 10; i += 3 )
253 | int_int_map_erase( &our_map, i );
254 |
255 | // Retrieving keys and values.
256 | for( int i = 0; i < 10; ++i )
257 | {
258 | int_int_map_itr itr =
259 | int_int_map_get( &our_map, i );
260 | if( !int_int_map_is_end( itr ) )
261 | printf(
262 | "%d:%d ",
263 | itr.data->key,
264 | itr.data->val
265 | );
266 | }
267 | // Printed: 1:2 2:3 4:5 5:6 7:8 8:9
268 |
269 | // Iteration.
270 | for(
271 | int_int_map_itr itr =
272 | int_int_map_first( &our_map );
273 | !int_int_map_is_end( itr );
274 | itr = int_int_map_next( itr )
275 | )
276 | printf(
277 | "%d:%d ",
278 | itr.data->key,
279 | itr.data->val
280 | );
281 | // Printed: 2:3 4:5 7:8 1:2 5:6 8:9
282 |
283 | int_int_map_cleanup( &our_map );
284 | }
285 | ```
286 |
287 | |
288 |
289 |