37static void *rbtree_first(
struct RB_TRAV *);
39static void *rbtree_next(
struct RB_TRAV *);
40static void *rbtree_previous(
struct RB_TRAV *);
41static struct RB_NODE *rbtree_make_node(
size_t,
void *);
42static int is_red(
struct RB_NODE *);
82 struct RB_NODE head = {0, 0, {0, 0}};
85 int dir = 0, last = 0;
90 q =
t->link[1] = tree->
root;
100 else if (is_red(q->
link[0]) && is_red(q->
link[1])) {
108 if (is_red(q) && is_red(p)) {
109 int dir2 =
t->link[1] ==
g;
111 if (q == p->
link[last])
112 t->link[
dir2] = rbtree_single(
g, !last);
114 t->link[
dir2] = rbtree_double(
g, !last);
154 struct RB_NODE head = {0, 0, {0, 0}};
186 if (!is_red(q) && !is_red(q->
link[dir])) {
187 if (is_red(q->
link[!dir]))
188 p = p->link[last] = rbtree_single(q, dir);
189 else if (!is_red(q->
link[!dir])) {
193 if (!is_red(s->
link[!last]) && !is_red(s->
link[last])) {
200 int dir2 =
g->link[1] == p;
202 if (is_red(s->
link[last]))
203 g->link[
dir2] = rbtree_double(p, last);
204 else if (is_red(s->
link[!last]))
205 g->link[
dir2] = rbtree_single(p, last);
209 g->link[
dir2]->link[0]->red = 0;
210 g->link[
dir2]->link[1]->red = 0;
228 G_debug(2,
"RB tree: data not found in search tree");
248 while (curr_node !=
NULL) {
251 return curr_node->
data;
253 curr_node = curr_node->
link[cmp < 0];
285 G_debug(1,
"RB tree: empty tree");
287 G_debug(1,
"RB tree: finished traversing");
293 return rbtree_next(
trav);
296 return rbtree_first(
trav);
311 G_debug(1,
"RB tree: empty tree");
313 G_debug(1,
"RB tree: finished traversing");
319 return rbtree_previous(
trav);
322 return rbtree_last(
trav);
343 G_warning(
"RB tree: finished traversing");
349 return rbtree_next(
trav);
357 dir =
trav->tree->rb_compare(
trav->curr_node->data,
data);
360 return trav->curr_node->data;
366 if (
trav->curr_node->link[dir] ==
NULL)
367 return trav->curr_node->data;
370 trav->curr_node =
trav->curr_node->link[dir];
391 while (
trav->curr_node->link[0] !=
NULL) {
393 trav->curr_node =
trav->curr_node->link[0];
396 return trav->curr_node->data;
405 while (
trav->curr_node->link[1] !=
NULL) {
407 trav->curr_node =
trav->curr_node->link[1];
410 return trav->curr_node->data;
418 if (
trav->curr_node->link[1] !=
NULL) {
421 trav->curr_node =
trav->curr_node->link[1];
424 while (
trav->curr_node->link[0] !=
NULL) {
426 trav->curr_node =
trav->curr_node->link[0];
434 if (
trav->top == 0) {
438 last =
trav->curr_node;
440 }
while (last ==
trav->curr_node->link[1]);
444 return trav->curr_node->data;
455 if (
trav->curr_node->link[0] !=
NULL) {
458 trav->curr_node =
trav->curr_node->link[0];
461 while (
trav->curr_node->link[1] !=
NULL) {
463 trav->curr_node =
trav->curr_node->link[1];
471 if (
trav->top == 0) {
475 last =
trav->curr_node;
477 }
while (last ==
trav->curr_node->link[0]);
481 return trav->curr_node->data;
499 if (
it->link[0] ==
NULL) {
510 it->link[0] =
save->link[1];
541 if (is_red(
ln) || is_red(
rn)) {
542 G_warning(
"Red Black Tree debugging: Red violation");
561 G_warning(
"Red Black Tree debugging: Binary tree violation");
566 if (
lh != 0 &&
rh != 0 &&
lh !=
rh) {
567 G_warning(
"Red Black Tree debugging: Black violation");
572 if (
lh != 0 &&
rh != 0)
573 return is_red(root) ?
lh :
lh + 1;
586static struct RB_NODE *rbtree_make_node(
size_t datasize,
void *
data)
606static int is_red(
struct RB_NODE *root)
609 return root->
red == 1;
615static struct RB_NODE *rbtree_single(
struct RB_NODE *root,
int dir)
629static struct RB_NODE *rbtree_double(
struct RB_NODE *root,
int dir)
631 root->
link[!dir] = rbtree_single(root->
link[!dir], !dir);
632 return rbtree_single(root, dir);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int compare(const void *a, const void *b)
#define assert(condition)
void * rbtree_find(struct RB_TREE *tree, const void *data)
void rbtree_destroy(struct RB_TREE *tree)
int rbtree_remove(struct RB_TREE *tree, const void *data)
int rbtree_debug(struct RB_TREE *tree, struct RB_NODE *root)
void * rbtree_traverse(struct RB_TRAV *trav)
int rbtree_init_trav(struct RB_TRAV *trav, struct RB_TREE *tree)
void * rbtree_traverse_start(struct RB_TRAV *trav, const void *data)
void rbtree_clear(struct RB_TREE *tree)
void * rbtree_traverse_backwd(struct RB_TRAV *trav)
struct RB_TREE * rbtree_create(rb_compare_fn *compare, size_t rb_datasize)
int rbtree_insert(struct RB_TREE *tree, void *data)
int rb_compare_fn(const void *rb_a, const void *rb_b)
rb_compare_fn * rb_compare