docs/src/modules.md

changeset 298
fffe3a16a3de
parent 297
ba760f2195c3
child 299
e7dfcf229625
equal deleted inserted replaced
297:ba760f2195c3 298:fffe3a16a3de
277 277
278 This module provides a hash map implementation using murmur hash 2 and separate 278 This module provides a hash map implementation using murmur hash 2 and separate
279 chaining with linked lists. Similarly to the list module, we provide a 279 chaining with linked lists. Similarly to the list module, we provide a
280 `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs. 280 `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs.
281 281
282 ### Parsing command line options
283
284 Assume you want to parse command line options and record them within a map.
285 One way to do this is shown by the following code sample:
286 ```C
287 UcxMap* options = ucx_map_new(16);
288 const char *NOARG = "";
289
290 char *option = NULL;
291 char optchar = 0;
292 for(int i=1;i<argc;i++) {
293 char *arg = argv[i];
294 size_t len = strlen(arg);
295 if(len > 1 && arg[0] == '-') {
296 if(option) {
297 fprintf(stderr,
298 "Missing argument for option -%c\n", optchar);
299 return 1;
300 }
301 for(int c=1;c<len;c++) {
302 switch(arg[c]) {
303 default: {
304 fprintf(stderr, "Unknown option -%c\n\n", arg[c]);
305 return 1;
306 }
307 case 'v': {
308 ucx_map_cstr_put(options, "verbose", NOARG);
309 break;
310 }
311 case 'o': {
312 option = "output";
313 optchar = 'o';
314 break;
315 }
316 }
317 }
318 } else if(option) {
319 ucx_map_cstr_put(options, option, arg);
320 option = NULL;
321 } else {
322 /* ... handle argument that is not an option ... */
323 }
324 }
325 if(option) {
326 fprintf(stderr,
327 "Missing argument for option -%c\n", optchar);
328 return 1;
329 }
330 ```
331 With the following loop, you can access the previously recorded options:
332 ```C
333 UcxMapIterator iter = ucx_map_iterator(options);
334 char *arg;
335 UCX_MAP_FOREACH(optkey, arg, iter) {
336 char* opt = optkey.data;
337 if (*arg) {
338 printf("%s = %s\n", opt, arg);
339 } else {
340 printf("%s active\n", opt);
341 }
342 }
343 ```
344 Don't forget to call `ucx_map_free()`, when you are done with the map.
345
282 ## Memory Pool 346 ## Memory Pool
283 347
284 *Header file:* [mempool.h](api/mempool_8h.html) 348 *Header file:* [mempool.h](api/mempool_8h.html)
285 *Required modules:* [Allocator](#allocator) 349 *Required modules:* [Allocator](#allocator)
286 350

mercurial