2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 struct node *l, *r, *p;
31 struct btree *btree_create(struct dm_pool *mem)
33 struct btree *t = dm_pool_alloc(mem, sizeof(*t));
44 * Shuffle the bits in a key, to try and remove
47 static uint32_t _shuffle(uint32_t k)
50 return ((k & 0xff) << 24 |
52 (k & 0xff0000) >> 8 | (k & 0xff000000) >> 24);
58 static struct node **_lookup(struct node *const *c, uint32_t key,
74 return (struct node **)c;
77 void *btree_lookup(const struct btree *t, uint32_t k)
79 uint32_t key = _shuffle(k);
80 struct node *p, **c = _lookup(&t->root, key, &p);
81 return (*c) ? (*c)->data : NULL;
84 int btree_insert(struct btree *t, uint32_t k, void *data)
86 uint32_t key = _shuffle(k);
87 struct node *p, **c = _lookup(&t->root, key, &p), *n;
90 if (!(n = dm_pool_alloc(t->mem, sizeof(*n))))
104 void *btree_get_data(const struct btree_iter *it)
106 return ((const struct node *) it)->data;
109 static struct node *_left(struct node *n)
116 struct btree_iter *btree_first(const struct btree *t)
121 return (struct btree_iter *) _left(t->root);
124 struct btree_iter *btree_next(const struct btree_iter *it)
126 struct node *n = (struct node *) it;
130 return (struct btree_iter *) _left(n->r);
134 while (n && k > n->key);
136 return (struct btree_iter *) n;