]> git.ozlabs.org Git - ccan/blob - ccan/jmap/_info
jmap: a speed benchmark
[ccan] / ccan / jmap / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * jmap - map from indices to values (based on libJudy)
7  *
8  * This provides a convenient wrapper for using JudyL arrays; using
9  * integers or pointers as an index, Judy arrays provide an efficient
10  * map to integers or pointers.
11  *
12  * jmap.h simply contains wrappers for a size_t-indexed size_t values, and
13  * jmap_type.h contain a wrapper macro for size_t->pointer maps and pointer
14  * ->pointer maps.
15  *
16  * Example:
17  * // Silly example of associating data with arguments by pointer and int.
18  * #include <string.h>
19  * #include <stdio.h>
20  * #include <ccan/jmap/jmap_type.h>
21  * 
22  * struct opt_detail {
23  *      bool is_long;
24  *      unsigned int length; // == 1 if !is_long.
25  * };
26  * 
27  * // Define jmap_arg_<op> and jmap_arg, for int -> argv.
28  * JMAP_DEFINE_UINTIDX_TYPE(char, arg);
29  * // Define jmap_opt_<op> and jmap_opt, for argv -> struct opt_detail *.
30  * JMAP_DEFINE_PTRIDX_TYPE(char, struct opt_detail, opt);
31  * 
32  * int main(int argc, char *argv[])
33  * {
34  *      int i;
35  *      // This map is equivalent to the argv[] array.  Silly example.
36  *      struct jmap_arg *arg = jmap_arg_new();
37  *      struct jmap_opt *opt = jmap_opt_new();
38  *      struct opt_detail *d;
39  * 
40  *      // Note: this is not correct for real parsing!
41  *      for (i = 0; i < argc; i++) {
42  *              jmap_arg_add(arg, i, argv[i]);
43  *              if (i < 1 || argv[i][0] != '-')
44  *                      continue;
45  *              d = malloc(sizeof(*d));
46  *              if (argv[i][1] == '-') {
47  *                      // --<stuff>
48  *                      d->is_long = true;
49  *                      d->length = strlen(argv[i]+2);
50  *              } else {
51  *                      // -<opt1>
52  *                      d->is_long = false;
53  *                      d->length = 1;
54  *              }
55  *              jmap_opt_add(opt, argv[i], d);
56  *      }
57  * 
58  *      printf("Found %lu options:\n", jmap_opt_count(opt));
59  *      for (i = jmap_arg_first(arg,-1); i!=-1; i = jmap_arg_next(arg,i,-1)) {
60  *              char *a = jmap_arg_get(arg, i);
61  *              d = jmap_opt_get(opt, a);
62  *              printf("  Arg %i ('%s') is a %s of %u chars\n",
63  *                     i, a,
64  *                     d == NULL ? "normal argument"
65  *                     : d->is_long ? "long option"
66  *                     : "short option",
67  *                     d == NULL ? strlen(a) : d->length);
68  *              // We no longer need it, so free it here.
69  *              free(d);
70  *      }
71  *      jmap_opt_free(opt);
72  *      jmap_arg_free(arg);
73  *      return 0;
74  * }
75  * 
76  * License: LGPL (2 or any later version)
77  * Author: Rusty Russell <rusty@rustcorp.com.au>
78  */
79 int main(int argc, char *argv[])
80 {
81         if (argc != 2)
82                 return 1;
83
84         if (strcmp(argv[1], "depends") == 0) {
85                 printf("ccan/build_assert\n");
86                 printf("ccan/compiler\n");
87                 printf("Judy\n");
88                 return 0;
89         }
90
91         if (strcmp(argv[1], "libs") == 0) {
92                 printf("Judy\n");
93                 return 0;
94         }
95
96         return 1;
97 }