From b27957a68679778d91d76e65b6fbfabf0706a6ac Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Thu, 25 Oct 2001 11:38:19 +0000 Subject: [PATCH] o Trivial binary tree --- include/.symlinks | 1 + lib/Makefile.in | 2 +- lib/datastruct/btree.c | 130 +++++++++++++++++++++++++++++++++++++++++ lib/datastruct/btree.h | 26 +++++++++ 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 lib/datastruct/btree.c create mode 100644 lib/datastruct/btree.h diff --git a/include/.symlinks b/include/.symlinks index be4517679..7882084d7 100644 --- a/include/.symlinks +++ b/include/.symlinks @@ -1,6 +1,7 @@ ../lib/activate/activate.h ../lib/config/config.h ../lib/datastruct/bitset.h +../lib/datastruct/btree.h ../lib/datastruct/hash.h ../lib/datastruct/list.h ../lib/datastruct/lvm-types.h diff --git a/lib/Makefile.in b/lib/Makefile.in index 7d3ffbfe0..a171123a8 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -12,8 +12,8 @@ SOURCES=\ activate/activate.c \ config/config.c \ datastruct/bitset.c \ + datastruct/btree.c \ datastruct/hash.c \ - device/btree.c \ device/dev-cache.c \ device/dev-io.c \ device/device.c \ diff --git a/lib/datastruct/btree.c b/lib/datastruct/btree.c new file mode 100644 index 000000000..6392a6438 --- /dev/null +++ b/lib/datastruct/btree.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the GPL. + */ + +#include "btree.h" +#include "log.h" + +struct node { + uint32_t key; + struct node *l, *r, *p; + + void *data; +}; + +struct btree { + struct pool *mem; + struct node *root; +}; + +struct btree *btree_create(struct pool *mem) +{ + struct btree *t = pool_alloc(mem, sizeof(*t)); + + if (t) { + t->mem = mem; + t->root = NULL; + } + + return t; +} + +/* + * Shuffle the bits in a key, to try and remove + * any ordering. + */ +static uint32_t _shuffle(uint32_t k) +{ +#if 0 + return ((k & 0xff) << 24 | + (k & 0xff00) << 16 | + (k & 0xff0000) >> 8 | + (k & 0xff000000) >> 24); +#else + return k; +#endif +} + +struct node **_lookup(struct node **c, uint32_t key, struct node **p) +{ + *p = NULL; + while (*c) { + *p = *c; + if ((*c)->key == key) + break; + + if (key < (*c)->key) + c = &(*c)->l; + + else + c = &(*c)->r; + } + + return c; +} + +void *btree_lookup(struct btree *t, uint32_t k) +{ + uint32_t key = _shuffle(k); + struct node *p, **c = _lookup(&t->root, key, &p); + return (*c) ? (*c)->data : NULL; +} + +int btree_insert(struct btree *t, uint32_t k, void *data) +{ + uint32_t key = _shuffle(k); + struct node *p, **c = _lookup(&t->root, key, &p), *n; + + if (!*c) { + if (!(n = pool_alloc(t->mem, sizeof(*n)))) { + stack; + return 0; + } + + n->key = key; + n->data = data; + n->l = n->r = NULL; + n->p = p; + + *c = n; + } + + return 1; +} + +void *btree_get_data(struct btree_iter *it) +{ + return ((struct node *) it)->data; +} + +static inline struct node *_left(struct node *n) +{ + while (n->l) + n = n->l; + return n; +} + +struct btree_iter *btree_first(struct btree *t) +{ + if (!t->root) + return NULL; + + return (struct btree_iter *) _left(t->root); +} + +struct btree_iter *btree_next(struct btree_iter *it) +{ + struct node *n = (struct node *) it; + uint32_t k = n->key; + + if (n->r) + return (struct btree_iter *) _left(n->r); + + do + n = n->p; + while (n && k > n->key); + + return (struct btree_iter *) n; +} diff --git a/lib/datastruct/btree.h b/lib/datastruct/btree.h new file mode 100644 index 000000000..4ae8770cd --- /dev/null +++ b/lib/datastruct/btree.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2001 Sistina Software (UK) Limited. + * + * This file is released under the GPL. + */ + +#ifndef _LVM_BTREE_H +#define _LVM_BTREE_H + +#include "lvm-types.h" +#include "pool.h" + +struct btree; + +struct btree *btree_create(struct pool *mem); + +void *btree_lookup(struct btree *t, uint32_t k); +int btree_insert(struct btree *t, uint32_t k, void *data); + +struct btree_iter; +void *btree_get_data(struct btree_iter *it); + +struct btree_iter *btree_first(struct btree *t); +struct btree_iter *btree_next(struct btree_iter *it); + +#endif