2010-11-19 19:00:15 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "id.h"
|
|
|
|
|
|
|
|
|
|
|
|
static struct id *free_id = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
int 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct tree *make_tree(int (*comp)(const struct id *a, const struct id *b))
|
|
|
|
{
|
|
|
|
struct tree *tree;
|
|
|
|
|
|
|
|
tree = alloc_type(struct tree);
|
|
|
|
tree->comp = comp;
|
2010-11-20 02:17:08 +02:00
|
|
|
tree->root = make_jrb();
|
2010-11-19 19:00:15 +02:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct id *make_id(struct tree *tree, const char *s, size_t len)
|
|
|
|
{
|
|
|
|
struct id *id;
|
2010-11-20 02:17:08 +02:00
|
|
|
struct jrb *node;
|
2010-11-19 19:00:15 +02:00
|
|
|
|
|
|
|
if (!free_id)
|
|
|
|
free_id = alloc_type(struct id);
|
|
|
|
id = free_id;
|
|
|
|
id->s = s;
|
|
|
|
id->len = len;
|
|
|
|
id->value = NULL;
|
2010-11-20 02:17:08 +02:00
|
|
|
node = jrb_find(tree->root, id, tree->comp);
|
2010-11-19 19:00:15 +02:00
|
|
|
if (node)
|
2010-11-20 02:17:08 +02:00
|
|
|
return node->key;
|
|
|
|
id->jrb = jrb_insert(tree->root, id, NULL, tree->comp);
|
2010-11-19 19:00:15 +02:00
|
|
|
free_id = NULL;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const struct id *find_id(const struct tree *tree, const char *s, size_t len)
|
|
|
|
{
|
|
|
|
struct id id = {
|
|
|
|
.s = s,
|
|
|
|
.len = len
|
|
|
|
};
|
2010-11-20 02:17:08 +02:00
|
|
|
struct jrb *node;
|
2010-11-19 19:00:15 +02:00
|
|
|
|
2010-11-20 02:17:08 +02:00
|
|
|
node = jrb_find(tree->root, &id, tree->comp);
|
|
|
|
return node ? node->key : NULL;
|
2010-11-19 19:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const struct id *first_id(const struct tree *tree)
|
|
|
|
{
|
2010-11-20 02:17:08 +02:00
|
|
|
struct jrb *node;
|
2010-11-19 19:00:15 +02:00
|
|
|
|
2010-11-20 02:17:08 +02:00
|
|
|
node = jrb_first(tree->root);
|
|
|
|
return node ? node->key : NULL;
|
2010-11-19 19:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const struct id *next_id(const struct id *id)
|
|
|
|
{
|
2010-11-20 02:17:08 +02:00
|
|
|
struct jrb *next;
|
|
|
|
|
|
|
|
next = jrb_next(id->jrb);
|
|
|
|
return next ? next->key : NULL;
|
2010-11-19 19:00:15 +02:00
|
|
|
}
|