Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / datastruct / btree.c
blobea36288c83b9bb5af7983776b818f3b42f790c33
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "lib.h"
19 #include "btree.h"
21 struct node {
22 uint32_t key;
23 struct node *l, *r, *p;
25 void *data;
28 struct btree {
29 struct dm_pool *mem;
30 struct node *root;
33 struct btree *btree_create(struct dm_pool *mem)
35 struct btree *t = dm_pool_alloc(mem, sizeof(*t));
37 if (t) {
38 t->mem = mem;
39 t->root = NULL;
42 return t;
46 * Shuffle the bits in a key, to try and remove
47 * any ordering.
49 static uint32_t _shuffle(uint32_t k)
51 #if 1
52 return ((k & 0xff) << 24 |
53 (k & 0xff00) << 8 |
54 (k & 0xff0000) >> 8 | (k & 0xff000000) >> 24);
55 #else
56 return k;
57 #endif
60 static struct node **_lookup(struct node *const *c, uint32_t key,
61 struct node **p)
63 *p = NULL;
64 while (*c) {
65 *p = *c;
66 if ((*c)->key == key)
67 break;
69 if (key < (*c)->key)
70 c = &(*c)->l;
72 else
73 c = &(*c)->r;
76 return (struct node **)c;
79 void *btree_lookup(const struct btree *t, uint32_t k)
81 uint32_t key = _shuffle(k);
82 struct node *p, **c = _lookup(&t->root, key, &p);
83 return (*c) ? (*c)->data : NULL;
86 int btree_insert(struct btree *t, uint32_t k, void *data)
88 uint32_t key = _shuffle(k);
89 struct node *p, **c = _lookup(&t->root, key, &p), *n;
91 if (!*c) {
92 if (!(n = dm_pool_alloc(t->mem, sizeof(*n))))
93 return_0;
95 n->key = key;
96 n->data = data;
97 n->l = n->r = NULL;
98 n->p = p;
100 *c = n;
103 return 1;
106 void *btree_get_data(const struct btree_iter *it)
108 return ((struct node *) it)->data;
111 static struct node *_left(struct node *n)
113 while (n->l)
114 n = n->l;
115 return n;
118 struct btree_iter *btree_first(const struct btree *t)
120 if (!t->root)
121 return NULL;
123 return (struct btree_iter *) _left(t->root);
126 struct btree_iter *btree_next(const struct btree_iter *it)
128 struct node *n = (struct node *) it;
129 uint32_t k = n->key;
131 if (n->r)
132 return (struct btree_iter *) _left(n->r);
135 n = n->p;
136 while (n && k > n->key);
138 return (struct btree_iter *) n;