1*86d7f5d3SJohn Marino /* $NetBSD: btree.c,v 1.1.1.1 2008/12/22 00:17:54 haad Exp $ */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5*86d7f5d3SJohn Marino * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6*86d7f5d3SJohn Marino *
7*86d7f5d3SJohn Marino * This file is part of LVM2.
8*86d7f5d3SJohn Marino *
9*86d7f5d3SJohn Marino * This copyrighted material is made available to anyone wishing to use,
10*86d7f5d3SJohn Marino * modify, copy, or redistribute it subject to the terms and conditions
11*86d7f5d3SJohn Marino * of the GNU Lesser General Public License v.2.1.
12*86d7f5d3SJohn Marino *
13*86d7f5d3SJohn Marino * You should have received a copy of the GNU Lesser General Public License
14*86d7f5d3SJohn Marino * along with this program; if not, write to the Free Software Foundation,
15*86d7f5d3SJohn Marino * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16*86d7f5d3SJohn Marino */
17*86d7f5d3SJohn Marino
18*86d7f5d3SJohn Marino #include "lib.h"
19*86d7f5d3SJohn Marino #include "btree.h"
20*86d7f5d3SJohn Marino
21*86d7f5d3SJohn Marino struct node {
22*86d7f5d3SJohn Marino uint32_t key;
23*86d7f5d3SJohn Marino struct node *l, *r, *p;
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino void *data;
26*86d7f5d3SJohn Marino };
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino struct btree {
29*86d7f5d3SJohn Marino struct dm_pool *mem;
30*86d7f5d3SJohn Marino struct node *root;
31*86d7f5d3SJohn Marino };
32*86d7f5d3SJohn Marino
btree_create(struct dm_pool * mem)33*86d7f5d3SJohn Marino struct btree *btree_create(struct dm_pool *mem)
34*86d7f5d3SJohn Marino {
35*86d7f5d3SJohn Marino struct btree *t = dm_pool_alloc(mem, sizeof(*t));
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino if (t) {
38*86d7f5d3SJohn Marino t->mem = mem;
39*86d7f5d3SJohn Marino t->root = NULL;
40*86d7f5d3SJohn Marino }
41*86d7f5d3SJohn Marino
42*86d7f5d3SJohn Marino return t;
43*86d7f5d3SJohn Marino }
44*86d7f5d3SJohn Marino
45*86d7f5d3SJohn Marino /*
46*86d7f5d3SJohn Marino * Shuffle the bits in a key, to try and remove
47*86d7f5d3SJohn Marino * any ordering.
48*86d7f5d3SJohn Marino */
_shuffle(uint32_t k)49*86d7f5d3SJohn Marino static uint32_t _shuffle(uint32_t k)
50*86d7f5d3SJohn Marino {
51*86d7f5d3SJohn Marino #if 1
52*86d7f5d3SJohn Marino return ((k & 0xff) << 24 |
53*86d7f5d3SJohn Marino (k & 0xff00) << 8 |
54*86d7f5d3SJohn Marino (k & 0xff0000) >> 8 | (k & 0xff000000) >> 24);
55*86d7f5d3SJohn Marino #else
56*86d7f5d3SJohn Marino return k;
57*86d7f5d3SJohn Marino #endif
58*86d7f5d3SJohn Marino }
59*86d7f5d3SJohn Marino
_lookup(struct node * const * c,uint32_t key,struct node ** p)60*86d7f5d3SJohn Marino static struct node **_lookup(struct node *const *c, uint32_t key,
61*86d7f5d3SJohn Marino struct node **p)
62*86d7f5d3SJohn Marino {
63*86d7f5d3SJohn Marino *p = NULL;
64*86d7f5d3SJohn Marino while (*c) {
65*86d7f5d3SJohn Marino *p = *c;
66*86d7f5d3SJohn Marino if ((*c)->key == key)
67*86d7f5d3SJohn Marino break;
68*86d7f5d3SJohn Marino
69*86d7f5d3SJohn Marino if (key < (*c)->key)
70*86d7f5d3SJohn Marino c = &(*c)->l;
71*86d7f5d3SJohn Marino
72*86d7f5d3SJohn Marino else
73*86d7f5d3SJohn Marino c = &(*c)->r;
74*86d7f5d3SJohn Marino }
75*86d7f5d3SJohn Marino
76*86d7f5d3SJohn Marino return (struct node **)c;
77*86d7f5d3SJohn Marino }
78*86d7f5d3SJohn Marino
btree_lookup(const struct btree * t,uint32_t k)79*86d7f5d3SJohn Marino void *btree_lookup(const struct btree *t, uint32_t k)
80*86d7f5d3SJohn Marino {
81*86d7f5d3SJohn Marino uint32_t key = _shuffle(k);
82*86d7f5d3SJohn Marino struct node *p, **c = _lookup(&t->root, key, &p);
83*86d7f5d3SJohn Marino return (*c) ? (*c)->data : NULL;
84*86d7f5d3SJohn Marino }
85*86d7f5d3SJohn Marino
btree_insert(struct btree * t,uint32_t k,void * data)86*86d7f5d3SJohn Marino int btree_insert(struct btree *t, uint32_t k, void *data)
87*86d7f5d3SJohn Marino {
88*86d7f5d3SJohn Marino uint32_t key = _shuffle(k);
89*86d7f5d3SJohn Marino struct node *p, **c = _lookup(&t->root, key, &p), *n;
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino if (!*c) {
92*86d7f5d3SJohn Marino if (!(n = dm_pool_alloc(t->mem, sizeof(*n))))
93*86d7f5d3SJohn Marino return_0;
94*86d7f5d3SJohn Marino
95*86d7f5d3SJohn Marino n->key = key;
96*86d7f5d3SJohn Marino n->data = data;
97*86d7f5d3SJohn Marino n->l = n->r = NULL;
98*86d7f5d3SJohn Marino n->p = p;
99*86d7f5d3SJohn Marino
100*86d7f5d3SJohn Marino *c = n;
101*86d7f5d3SJohn Marino }
102*86d7f5d3SJohn Marino
103*86d7f5d3SJohn Marino return 1;
104*86d7f5d3SJohn Marino }
105*86d7f5d3SJohn Marino
btree_get_data(const struct btree_iter * it)106*86d7f5d3SJohn Marino void *btree_get_data(const struct btree_iter *it)
107*86d7f5d3SJohn Marino {
108*86d7f5d3SJohn Marino return ((struct node *) it)->data;
109*86d7f5d3SJohn Marino }
110*86d7f5d3SJohn Marino
_left(struct node * n)111*86d7f5d3SJohn Marino static struct node *_left(struct node *n)
112*86d7f5d3SJohn Marino {
113*86d7f5d3SJohn Marino while (n->l)
114*86d7f5d3SJohn Marino n = n->l;
115*86d7f5d3SJohn Marino return n;
116*86d7f5d3SJohn Marino }
117*86d7f5d3SJohn Marino
btree_first(const struct btree * t)118*86d7f5d3SJohn Marino struct btree_iter *btree_first(const struct btree *t)
119*86d7f5d3SJohn Marino {
120*86d7f5d3SJohn Marino if (!t->root)
121*86d7f5d3SJohn Marino return NULL;
122*86d7f5d3SJohn Marino
123*86d7f5d3SJohn Marino return (struct btree_iter *) _left(t->root);
124*86d7f5d3SJohn Marino }
125*86d7f5d3SJohn Marino
btree_next(const struct btree_iter * it)126*86d7f5d3SJohn Marino struct btree_iter *btree_next(const struct btree_iter *it)
127*86d7f5d3SJohn Marino {
128*86d7f5d3SJohn Marino struct node *n = (struct node *) it;
129*86d7f5d3SJohn Marino uint32_t k = n->key;
130*86d7f5d3SJohn Marino
131*86d7f5d3SJohn Marino if (n->r)
132*86d7f5d3SJohn Marino return (struct btree_iter *) _left(n->r);
133*86d7f5d3SJohn Marino
134*86d7f5d3SJohn Marino do
135*86d7f5d3SJohn Marino n = n->p;
136*86d7f5d3SJohn Marino while (n && k > n->key);
137*86d7f5d3SJohn Marino
138*86d7f5d3SJohn Marino return (struct btree_iter *) n;
139*86d7f5d3SJohn Marino }
140