Change indentation to follow the updated standard.
Alot of mixed indentation. use 4 chars wide soft tabs.
This commit is contained in:
parent
9cc5420447
commit
f46ae1970b
51 changed files with 634 additions and 639 deletions
376
src/rbtree.c
376
src/rbtree.c
|
|
@ -1,5 +1,5 @@
|
|||
/* rbtree.c - red black tree implementation
|
||||
*
|
||||
*
|
||||
* Copyright (C) 2010-2011 Henrik Hautakoski <henrik@fiktivkod.org>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
@ -24,84 +24,84 @@
|
|||
/* node definition */
|
||||
typedef struct _rbn {
|
||||
const void *key;
|
||||
struct _rbn *child[2];
|
||||
unsigned char color;
|
||||
struct _rbn *child[2];
|
||||
unsigned char color;
|
||||
} rbnode;
|
||||
|
||||
#define is_red(n) ((n) != NULL && (n)->color == RB_RED)
|
||||
#define swap(n,d,q) ((n)->child[(n)->child[d] == (q)])
|
||||
|
||||
static rbnode* node_alloc(const void *key) {
|
||||
|
||||
rbnode *n = xmalloc(sizeof(rbnode));
|
||||
|
||||
n->key = key;
|
||||
n->color = RB_RED;
|
||||
n->child[0] = NULL;
|
||||
n->child[1] = NULL;
|
||||
|
||||
return n;
|
||||
|
||||
rbnode *n = xmalloc(sizeof(rbnode));
|
||||
|
||||
n->key = key;
|
||||
n->color = RB_RED;
|
||||
n->child[0] = NULL;
|
||||
n->child[1] = NULL;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Recursivly deallocate a tree.
|
||||
*/
|
||||
static void node_dealloc(rbnode *n, void (*dealloc)(void *)) {
|
||||
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
if (dealloc)
|
||||
dealloc((void *)n->key);
|
||||
|
||||
node_dealloc(n->child[0], dealloc);
|
||||
node_dealloc(n->child[1], dealloc);
|
||||
node_dealloc(n->child[0], dealloc);
|
||||
node_dealloc(n->child[1], dealloc);
|
||||
|
||||
free(n);
|
||||
free(n);
|
||||
}
|
||||
|
||||
#ifdef __DEBUG__
|
||||
#define rb_err(msg) fputs("rbtree error: " msg, stderr)
|
||||
|
||||
|
||||
static int rb_assert_r(rbnode *node, int (*cmp)(const void *, const void *)) {
|
||||
|
||||
int rh, lh;
|
||||
rbnode *ln, *rn;
|
||||
|
||||
if (node == NULL)
|
||||
return 1;
|
||||
|
||||
ln = node->child[0];
|
||||
rn = node->child[1];
|
||||
|
||||
if (is_red(node)) {
|
||||
|
||||
int rh, lh;
|
||||
rbnode *ln, *rn;
|
||||
|
||||
if (node == NULL)
|
||||
return 1;
|
||||
|
||||
ln = node->child[0];
|
||||
rn = node->child[1];
|
||||
|
||||
if (is_red(node)) {
|
||||
/* double red violation */
|
||||
if (is_red(ln) || is_red(rn)) {
|
||||
rb_err("Double red violation");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
lh = rb_assert_r(ln, cmp);
|
||||
rh = rb_assert_r(rn, cmp);
|
||||
if (is_red(ln) || is_red(rn)) {
|
||||
rb_err("Double red violation");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
lh = rb_assert_r(ln, cmp);
|
||||
rh = rb_assert_r(rn, cmp);
|
||||
|
||||
if ( (ln && cmp(ln->key, node->key) >= 0) &&
|
||||
(rn && cmp(rn->key, node->key) <= 0) ) {
|
||||
rb_err("Binary tree violation");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (rh != 0 && lh != 0) {
|
||||
|
||||
if (rh != lh) {
|
||||
rb_err("Binary tree violation");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (rh != 0 && lh != 0) {
|
||||
|
||||
if (rh != lh) {
|
||||
rb_err("Black height violation");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return is_red(node) ? lh : lh+1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return is_red(node) ? lh : lh+1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#undef rb_err
|
||||
|
||||
|
|
@ -118,67 +118,67 @@ static void rb_assert(rbtree *tree) {
|
|||
* Recursivly walks a tree, applying action function on every node
|
||||
*/
|
||||
static void rbwalk(rbnode *n, void (*action)(const void *)) {
|
||||
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
rbwalk(n->child[0], action);
|
||||
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
rbwalk(n->child[0], action);
|
||||
action(n->key);
|
||||
rbwalk(n->child[1], action);
|
||||
rbwalk(n->child[1], action);
|
||||
}
|
||||
|
||||
static rbnode* rotate_single(rbnode *root, int dir) {
|
||||
|
||||
rbnode *save = root->child[!dir];
|
||||
|
||||
root->child[!dir] = save->child[dir];
|
||||
save->child[dir] = root;
|
||||
|
||||
root->color = RB_RED;
|
||||
save->color = RB_BLACK;
|
||||
|
||||
return save;
|
||||
|
||||
rbnode *save = root->child[!dir];
|
||||
|
||||
root->child[!dir] = save->child[dir];
|
||||
save->child[dir] = root;
|
||||
|
||||
root->color = RB_RED;
|
||||
save->color = RB_BLACK;
|
||||
|
||||
return save;
|
||||
}
|
||||
|
||||
static rbnode* rotate_double(rbnode *root, int dir) {
|
||||
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
||||
return rotate_single(root, dir);
|
||||
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
||||
return rotate_single(root, dir);
|
||||
}
|
||||
|
||||
int rbtree_is_empty(rbtree *tree) {
|
||||
|
||||
return tree == NULL || tree->root == NULL;
|
||||
return tree == NULL || tree->root == NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Searches a tree by key.
|
||||
*/
|
||||
void* rbtree_search(rbtree *tree, const void *key) {
|
||||
|
||||
rbnode *n;
|
||||
|
||||
if (!tree || !tree->cmp_fn)
|
||||
return NULL;
|
||||
|
||||
n = tree->root;
|
||||
|
||||
while(n) {
|
||||
|
||||
rbnode *n;
|
||||
|
||||
if (!tree || !tree->cmp_fn)
|
||||
return NULL;
|
||||
|
||||
n = tree->root;
|
||||
|
||||
while(n) {
|
||||
int cmp = tree->cmp_fn(n->key, key);
|
||||
|
||||
if (cmp == 0)
|
||||
return (void *) n->key;
|
||||
|
||||
if (cmp == 0)
|
||||
return (void *) n->key;
|
||||
|
||||
n = n->child[cmp < 0];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
||||
|
||||
if (!tree || !action)
|
||||
return;
|
||||
|
||||
rbwalk(tree->root, action);
|
||||
|
||||
if (!tree || !action)
|
||||
return;
|
||||
|
||||
rbwalk(tree->root, action);
|
||||
|
||||
#ifdef __DEBUG__
|
||||
rb_assert(tree);
|
||||
|
|
@ -186,12 +186,12 @@ void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
|||
}
|
||||
|
||||
void rbtree_free(rbtree *tree, void (*free_fn)(void *)) {
|
||||
|
||||
if (!tree)
|
||||
return;
|
||||
|
||||
node_dealloc(tree->root, free_fn);
|
||||
tree->root = NULL;
|
||||
|
||||
if (!tree)
|
||||
return;
|
||||
|
||||
node_dealloc(tree->root, free_fn);
|
||||
tree->root = NULL;
|
||||
|
||||
#ifdef __DEBUG__
|
||||
rb_assert(tree);
|
||||
|
|
@ -199,71 +199,71 @@ void rbtree_free(rbtree *tree, void (*free_fn)(void *)) {
|
|||
}
|
||||
|
||||
int rbtree_insert(rbtree *tree, const void *key) {
|
||||
|
||||
rbnode head = {0};
|
||||
|
||||
/* grandparent and parent */
|
||||
rbnode *g, *t;
|
||||
|
||||
/* iterator and parent */
|
||||
rbnode *p, *q;
|
||||
|
||||
int dir = 0, last = 0;
|
||||
|
||||
rbnode head = {0};
|
||||
|
||||
/* grandparent and parent */
|
||||
rbnode *g, *t;
|
||||
|
||||
/* iterator and parent */
|
||||
rbnode *p, *q;
|
||||
|
||||
int dir = 0, last = 0;
|
||||
|
||||
if (!tree || !tree->cmp_fn)
|
||||
return -1;
|
||||
|
||||
if (!tree->root) {
|
||||
tree->root = q = node_alloc(key);
|
||||
if (!tree->root) {
|
||||
tree->root = q = node_alloc(key);
|
||||
goto done;
|
||||
}
|
||||
|
||||
t = &head;
|
||||
g = p = NULL;
|
||||
q = t->child[1] = tree->root;
|
||||
}
|
||||
|
||||
t = &head;
|
||||
g = p = NULL;
|
||||
q = t->child[1] = tree->root;
|
||||
|
||||
/* somewhere in here, there should be dragons */
|
||||
|
||||
for(;;) {
|
||||
|
||||
for(;;) {
|
||||
int cmp;
|
||||
|
||||
if (q == NULL) {
|
||||
p->child[dir] = q = node_alloc(key);
|
||||
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
||||
/* color flip case */
|
||||
q->color = RB_RED;
|
||||
q->child[0]->color = RB_BLACK;
|
||||
q->child[1]->color = RB_BLACK;
|
||||
}
|
||||
|
||||
/* fix red validation */
|
||||
if (is_red(q) && is_red(p)) {
|
||||
int dir2 = (t->child[1] == g);
|
||||
if (q == p->child[last])
|
||||
t->child[dir2] = rotate_single(g, !last);
|
||||
else
|
||||
t->child[dir2] = rotate_double(g, !last);
|
||||
}
|
||||
|
||||
if (q == NULL) {
|
||||
p->child[dir] = q = node_alloc(key);
|
||||
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
||||
/* color flip case */
|
||||
q->color = RB_RED;
|
||||
q->child[0]->color = RB_BLACK;
|
||||
q->child[1]->color = RB_BLACK;
|
||||
}
|
||||
|
||||
/* fix red validation */
|
||||
if (is_red(q) && is_red(p)) {
|
||||
int dir2 = (t->child[1] == g);
|
||||
if (q == p->child[last])
|
||||
t->child[dir2] = rotate_single(g, !last);
|
||||
else
|
||||
t->child[dir2] = rotate_double(g, !last);
|
||||
}
|
||||
|
||||
cmp = tree->cmp_fn(q->key, key);
|
||||
|
||||
if (cmp == 0)
|
||||
break;
|
||||
|
||||
last = dir;
|
||||
dir = cmp < 0;
|
||||
|
||||
if (g)
|
||||
t = g;
|
||||
g = p, p = q;
|
||||
q = q->child[dir];
|
||||
}
|
||||
|
||||
tree->root = head.child[1];
|
||||
if (cmp == 0)
|
||||
break;
|
||||
|
||||
last = dir;
|
||||
dir = cmp < 0;
|
||||
|
||||
if (g)
|
||||
t = g;
|
||||
g = p, p = q;
|
||||
q = q->child[dir];
|
||||
}
|
||||
|
||||
tree->root = head.child[1];
|
||||
|
||||
done:
|
||||
/* root should be black */
|
||||
tree->root->color = RB_BLACK;
|
||||
/* root should be black */
|
||||
tree->root->color = RB_BLACK;
|
||||
|
||||
#ifdef __DEBUG__
|
||||
rb_assert(tree);
|
||||
|
|
@ -273,46 +273,46 @@ done:
|
|||
}
|
||||
|
||||
void* rbtree_delete(rbtree *tree, const void *key) {
|
||||
|
||||
rbnode head = {0};
|
||||
|
||||
/* helpers*/
|
||||
rbnode *q, *p, *g, *s;
|
||||
|
||||
/* found item */
|
||||
rbnode *f = NULL, *ret = NULL;
|
||||
|
||||
int dir = 1, dir2, last;
|
||||
|
||||
if (rbtree_is_empty(tree) || !tree->cmp_fn)
|
||||
return 0;
|
||||
|
||||
q = &head;
|
||||
g = p = NULL;
|
||||
q->child[1] = tree->root;
|
||||
|
||||
/* more dragons (killed some of them though) */
|
||||
|
||||
while(q->child[dir]) {
|
||||
rbnode head = {0};
|
||||
|
||||
/* helpers*/
|
||||
rbnode *q, *p, *g, *s;
|
||||
|
||||
/* found item */
|
||||
rbnode *f = NULL, *ret = NULL;
|
||||
|
||||
int dir = 1, dir2, last;
|
||||
|
||||
if (rbtree_is_empty(tree) || !tree->cmp_fn)
|
||||
return 0;
|
||||
|
||||
q = &head;
|
||||
g = p = NULL;
|
||||
q->child[1] = tree->root;
|
||||
|
||||
/* more dragons (killed some of them though) */
|
||||
|
||||
while(q->child[dir]) {
|
||||
int cmp;
|
||||
|
||||
g = p, p = q;
|
||||
|
||||
g = p, p = q;
|
||||
q = q->child[dir];
|
||||
last = dir;
|
||||
|
||||
|
||||
cmp = tree->cmp_fn(q->key, key);
|
||||
dir = cmp < 0;
|
||||
if (cmp == 0)
|
||||
f = q;
|
||||
|
||||
if (is_red(q) || is_red(q->child[dir]))
|
||||
dir = cmp < 0;
|
||||
if (cmp == 0)
|
||||
f = q;
|
||||
|
||||
if (is_red(q) || is_red(q->child[dir]))
|
||||
continue;
|
||||
|
||||
|
||||
if (is_red(q->child[!dir])) {
|
||||
p = p->child[last] = rotate_single(q, dir);
|
||||
} else {
|
||||
s = p->child[!last];
|
||||
|
||||
|
||||
if (s == NULL)
|
||||
continue;
|
||||
|
||||
|
|
@ -321,35 +321,35 @@ void* rbtree_delete(rbtree *tree, const void *key) {
|
|||
s->color = q->color = RB_RED;
|
||||
} else {
|
||||
dir2 = (g->child[1] == p);
|
||||
|
||||
|
||||
if (is_red(s->child[last]))
|
||||
g->child[dir2] = rotate_double(p, last);
|
||||
else if (is_red(s->child[!last]))
|
||||
g->child[dir2] = rotate_single(p, last);
|
||||
|
||||
|
||||
q->color = g->child[dir2]->color = RB_RED;
|
||||
g->child[dir2]->child[0]->color = RB_BLACK;
|
||||
g->child[dir2]->child[1]->color = RB_BLACK;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* remove if found */
|
||||
if (f) {
|
||||
/* remove if found */
|
||||
if (f) {
|
||||
ret = (void*)f->key;
|
||||
if (f != q)
|
||||
f->key = q->key;
|
||||
swap(p, 1, q) = swap(q, 0, NULL);
|
||||
xfree(q);
|
||||
}
|
||||
}
|
||||
|
||||
tree->root = head.child[1];
|
||||
if (tree->root)
|
||||
tree->root->color = RB_BLACK;
|
||||
if (tree->root)
|
||||
tree->root->color = RB_BLACK;
|
||||
|
||||
#ifdef __DEBUG__
|
||||
rb_assert(tree);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue