1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-15 08:34:05 +02:00
wernermisc/qpkg/id.c
Werner Almesberger 7058115a64 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
2010-11-21 03:11:54 -03:00

79 lines
1.5 KiB
C

/*
* id.c - Registry of unique and alphabetically sorted identifiers
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <string.h>
#include "jrb.h"
#include "util.h"
#include "id.h"
static struct id *free_id = NULL;
static int do_comp_id(const struct id *a, const struct id *b)
{
int len = a->len < b->len ? a->len : b->len;
int cmp;
cmp = memcmp(a->s, b->s, len);
if (cmp)
return cmp;
return a->len < b->len ? -1 : a->len > b->len;
}
int comp_id(const void *a, const void *b)
{
return do_comp_id(a, b);
}
struct tree *make_tree(int (*comp)(const void *a, const void *b))
{
struct tree *tree;
tree = alloc_type(struct tree);
tree->comp = comp;
tree->root = make_jrb();
return tree;
}
struct id *make_id(struct tree *tree, const char *s, size_t len)
{
struct id *id;
if (!free_id)
free_id = alloc_type(struct id);
id = free_id;
id->s = s;
id->len = len;
id->jrb = jrb_find_or_insert(tree->root, id, NULL, tree->comp);
if (id->jrb->key != id)
return id->jrb->key;
free_id = NULL;
return id;
}
const struct jrb *find_id(const struct tree *tree, const char *s, size_t len)
{
struct id id = {
.s = s,
.len = len
};
return jrb_find(tree->root, &id, tree->comp);
}