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
23 struct node
*l
, *r
, *p
;
33 struct btree
*btree_create(struct dm_pool
*mem
)
35 struct btree
*t
= dm_pool_alloc(mem
, sizeof(*t
));
46 * Shuffle the bits in a key, to try and remove
49 static uint32_t _shuffle(uint32_t k
)
52 return ((k
& 0xff) << 24 |
54 (k
& 0xff0000) >> 8 | (k
& 0xff000000) >> 24);
60 static struct node
**_lookup(struct node
*const *c
, uint32_t key
,
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
;
92 if (!(n
= dm_pool_alloc(t
->mem
, sizeof(*n
))))
106 void *btree_get_data(const struct btree_iter
*it
)
108 return ((struct node
*) it
)->data
;
111 static struct node
*_left(struct node
*n
)
118 struct btree_iter
*btree_first(const struct btree
*t
)
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
;
132 return (struct btree_iter
*) _left(n
->r
);
136 while (n
&& k
> n
->key
);
138 return (struct btree_iter
*) n
;