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