1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-25 19:05:56 +02:00

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
This commit is contained in:
Werner Almesberger 2010-11-20 13:11:41 -03:00
parent a63823e26a
commit 7058115a64
3 changed files with 44 additions and 37 deletions

View File

@ -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 = { struct id id = {
.s = s, .s = s,
.len = len .len = len
}; };
struct jrb *node;
node = jrb_find(tree->root, &id, tree->comp); return 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;
} }

View File

@ -18,6 +18,33 @@
#include "jrb.h" #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 id;
struct tree { 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 tree *make_tree(int (*comp)(const void *a, const void *b));
struct id *make_id(struct tree *tree, const char *s, size_t len); 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);
const struct id *first_id(const struct tree *tree);
const struct id *next_id(const struct id *id);
#endif /* !ID_H */ #endif /* !ID_H */

View File

@ -15,6 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "jrb.h"
#include "id.h" #include "id.h"
#include "prereq.h" #include "prereq.h"
#include "gobble.h" #include "gobble.h"
@ -27,10 +28,11 @@ struct tree *versions = NULL;
static void list_all_packages(void) static void list_all_packages(void)
{ {
const struct id *id; const struct jrb *n;
for (id = first_id(packages); id; id = next_id(id)) { for (n = jrb_first(packages->root); n; n = jrb_next(n)) {
struct pkg *pkg = id->jrb->val; const struct id *id = n->key;
struct pkg *pkg = n->val;
printf("%.*s", (int) id->len, id->s); printf("%.*s", (int) id->len, id->s);
if (!pkg) if (!pkg)
@ -49,40 +51,40 @@ static void list_all_packages(void)
static void list_one_package(const char *name) 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; const struct pkg *pkg;
if (!id) { if (!n) {
fprintf(stderr, "no such package \"%s\"\n", name); fprintf(stderr, "no such package \"%s\"\n", name);
exit(1); 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); printf("%.*s\n", (int) pkg->version->len, pkg->version->s);
} }
static void find_prereq(const char *name, const char *version) 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 *pkg;
struct pkg **pkgs; struct pkg **pkgs;
if (!id) { if (!n) {
fprintf(stderr, "no such package \"%s\"\n", name); fprintf(stderr, "no such package \"%s\"\n", name);
exit(1); exit(1);
} }
pkg = id->jrb->val; pkg = n->val;
if (!pkg) { if (!pkg) {
fprintf(stderr, "package %s is a ghost\n", name); fprintf(stderr, "package %s is a ghost\n", name);
exit(1); exit(1);
} }
if (version) { if (version) {
id = find_id(versions, version, strlen(version)); n = find_id(versions, version, strlen(version));
if (!id) { if (!n) {
fprintf(stderr, "no such version\"%s\"\n", version); fprintf(stderr, "no such version\"%s\"\n", version);
exit(1); exit(1);
} }
while (pkg && pkg->version != id) while (pkg && pkg->version != n->key)
pkg = pkg->more; pkg = pkg->more;
if (pkg) { if (pkg) {
fprintf(stderr, "no %s version\"%s\" found\n", fprintf(stderr, "no %s version\"%s\" found\n",