From 7058115a643d70979e2523943cb00e12107bf9f9 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 20 Nov 2010 13:11:41 -0300 Subject: [PATCH] qpkg: towards "struct id" without jrb reference - change iteration and lookup - id.h, id.c (first_id, next_id): removed - qpkg.c (list_all_packages): use jrb_first/jrb_next instead of first_id/next_id - id.h, id.c (find_id): return the RB tree node instead of the ID - qpkg.c (list_one_package, find_prereq): adapted for above change - id.h: explain the role of key and value --- qpkg/id.c | 24 ++---------------------- qpkg/id.h | 31 ++++++++++++++++++++++++++++--- qpkg/qpkg.c | 26 ++++++++++++++------------ 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/qpkg/id.c b/qpkg/id.c index d80bd17..6c935c1 100644 --- a/qpkg/id.c +++ b/qpkg/id.c @@ -67,32 +67,12 @@ struct id *make_id(struct tree *tree, const char *s, size_t len) } -const struct id *find_id(const struct tree *tree, const char *s, size_t len) +const struct jrb *find_id(const struct tree *tree, const char *s, size_t len) { struct id id = { .s = s, .len = len }; - struct jrb *node; - node = jrb_find(tree->root, &id, tree->comp); - return node ? node->key : NULL; -} - - -const struct id *first_id(const struct tree *tree) -{ - struct jrb *node; - - node = jrb_first(tree->root); - return node ? node->key : NULL; -} - - -const struct id *next_id(const struct id *id) -{ - struct jrb *next; - - next = jrb_next(id->jrb); - return next ? next->key : NULL; + return jrb_find(tree->root, &id, tree->comp); } diff --git a/qpkg/id.h b/qpkg/id.h index 5c77cfc..a9943b2 100644 --- a/qpkg/id.h +++ b/qpkg/id.h @@ -18,6 +18,33 @@ #include "jrb.h" +/* + * The role of key and value in jrb nodes: + * + * - the key points to a "struct id". This "struct id" is unique for the + * identifier in question and its location can be used to check for equality + * with a simple pointer comparison. + * + * A caller to find_id may therefore be interested in a pointer to the key, + * even if the content of the key is known. + * + * - for the value, we have to distinguish between packages and versions: + * + * - package: pointer to the first package definition. If there are multiple + * packages with the same name, the rest is linked via pkg->more. + * + * The value can be NULL if a reference to the package has been + * encountered, but + * + * - the package definition has not been parsed yet, + * - no corresponding package definition exist (which would be an error), + * - the reference is an item listed in Provides: (this will change once + * we handle Provides: properly) + * + * - version: the value is not used + */ + + struct id; struct tree { @@ -36,8 +63,6 @@ int comp_id(const void *a, const void *b); struct tree *make_tree(int (*comp)(const void *a, const void *b)); struct id *make_id(struct tree *tree, const char *s, size_t len); -const struct id *find_id(const struct tree *tree, const char *s, size_t len); -const struct id *first_id(const struct tree *tree); -const struct id *next_id(const struct id *id); +const struct jrb *find_id(const struct tree *tree, const char *s, size_t len); #endif /* !ID_H */ diff --git a/qpkg/qpkg.c b/qpkg/qpkg.c index e7b4c70..f79dc51 100644 --- a/qpkg/qpkg.c +++ b/qpkg/qpkg.c @@ -15,6 +15,7 @@ #include #include +#include "jrb.h" #include "id.h" #include "prereq.h" #include "gobble.h" @@ -27,10 +28,11 @@ struct tree *versions = NULL; static void list_all_packages(void) { - const struct id *id; + const struct jrb *n; - for (id = first_id(packages); id; id = next_id(id)) { - struct pkg *pkg = id->jrb->val; + for (n = jrb_first(packages->root); n; n = jrb_next(n)) { + const struct id *id = n->key; + struct pkg *pkg = n->val; printf("%.*s", (int) id->len, id->s); if (!pkg) @@ -49,40 +51,40 @@ static void list_all_packages(void) static void list_one_package(const char *name) { - const struct id *id = find_id(packages, name, strlen(name)); + const struct jrb *n = find_id(packages, name, strlen(name)); const struct pkg *pkg; - if (!id) { + if (!n) { fprintf(stderr, "no such package \"%s\"\n", name); exit(1); } - for (pkg = id->jrb->val; pkg; pkg = pkg->more) + for (pkg = n->val; pkg; pkg = pkg->more) printf("%.*s\n", (int) pkg->version->len, pkg->version->s); } static void find_prereq(const char *name, const char *version) { - const struct id *id = find_id(packages, name, strlen(name)); + const struct jrb *n = find_id(packages, name, strlen(name)); struct pkg *pkg; struct pkg **pkgs; - if (!id) { + if (!n) { fprintf(stderr, "no such package \"%s\"\n", name); exit(1); } - pkg = id->jrb->val; + pkg = n->val; if (!pkg) { fprintf(stderr, "package %s is a ghost\n", name); exit(1); } if (version) { - id = find_id(versions, version, strlen(version)); - if (!id) { + n = find_id(versions, version, strlen(version)); + if (!n) { fprintf(stderr, "no such version\"%s\"\n", version); exit(1); } - while (pkg && pkg->version != id) + while (pkg && pkg->version != n->key) pkg = pkg->more; if (pkg) { fprintf(stderr, "no %s version\"%s\" found\n",