39static void *rbtree_first(
struct RB_TRAV *);
41static void *rbtree_next(
struct RB_TRAV *);
42static void *rbtree_previous(
struct RB_TRAV *);
43static struct RB_NODE *rbtree_make_node(
size_t,
void *);
44static int is_red(
struct RB_NODE *);
84 struct RB_NODE head = {0, 0, {0, 0}};
87 int dir = 0, last = 0;
92 q =
t->link[1] = tree->
root;
102 else if (is_red(q->
link[0]) && is_red(q->
link[1])) {
110 if (is_red(q) && is_red(p)) {
111 int dir2 =
t->link[1] ==
g;
113 if (q == p->
link[last])
114 t->link[
dir2] = rbtree_single(
g, !last);
116 t->link[
dir2] = rbtree_double(
g, !last);
156 struct RB_NODE head = {0, 0, {0, 0}};
188 if (!is_red(q) && !is_red(q->
link[dir])) {
189 if (is_red(q->
link[!dir]))
190 p = p->link[last] = rbtree_single(q, dir);
191 else if (!is_red(q->
link[!dir])) {
195 if (!is_red(s->
link[!last]) && !is_red(s->
link[last])) {
202 int dir2 =
g->link[1] == p;
204 if (is_red(s->
link[last]))
205 g->link[
dir2] = rbtree_double(p, last);
206 else if (is_red(s->
link[!last]))
207 g->link[
dir2] = rbtree_single(p, last);
211 g->link[
dir2]->link[0]->red = 0;
212 g->link[
dir2]->link[1]->red = 0;
230 G_debug(2,
"RB tree: data not found in search tree");
250 while (curr_node !=
NULL) {
253 return curr_node->
data;
255 curr_node = curr_node->
link[cmp < 0];
287 G_debug(1,
"RB tree: empty tree");
289 G_debug(1,
"RB tree: finished traversing");
295 return rbtree_next(
trav);
298 return rbtree_first(
trav);
313 G_debug(1,
"RB tree: empty tree");
315 G_debug(1,
"RB tree: finished traversing");
321 return rbtree_previous(
trav);
324 return rbtree_last(
trav);
345 G_warning(
"RB tree: finished traversing");
351 return rbtree_next(
trav);
359 dir =
trav->tree->rb_compare(
trav->curr_node->data,
data);
362 return trav->curr_node->data;
368 if (
trav->curr_node->link[dir] ==
NULL)
369 return trav->curr_node->data;
372 trav->curr_node =
trav->curr_node->link[dir];
393 while (
trav->curr_node->link[0] !=
NULL) {
395 trav->curr_node =
trav->curr_node->link[0];
398 return trav->curr_node->data;
407 while (
trav->curr_node->link[1] !=
NULL) {
409 trav->curr_node =
trav->curr_node->link[1];
412 return trav->curr_node->data;
420 if (
trav->curr_node->link[1] !=
NULL) {
423 trav->curr_node =
trav->curr_node->link[1];
426 while (
trav->curr_node->link[0] !=
NULL) {
428 trav->curr_node =
trav->curr_node->link[0];
436 if (
trav->top == 0) {
440 last =
trav->curr_node;
442 }
while (last ==
trav->curr_node->link[1]);
446 return trav->curr_node->data;
457 if (
trav->curr_node->link[0] !=
NULL) {
460 trav->curr_node =
trav->curr_node->link[0];
463 while (
trav->curr_node->link[1] !=
NULL) {
465 trav->curr_node =
trav->curr_node->link[1];
473 if (
trav->top == 0) {
477 last =
trav->curr_node;
479 }
while (last ==
trav->curr_node->link[0]);
483 return trav->curr_node->data;
501 if (
it->link[0] ==
NULL) {
512 it->link[0] =
save->link[1];
543 if (is_red(
ln) || is_red(
rn)) {
544 G_warning(
"Red Black Tree debugging: Red violation");
563 G_warning(
"Red Black Tree debugging: Binary tree violation");
568 if (
lh != 0 &&
rh != 0 &&
lh !=
rh) {
569 G_warning(
"Red Black Tree debugging: Black violation");
574 if (
lh != 0 &&
rh != 0)
575 return is_red(root) ?
lh :
lh + 1;
588static struct RB_NODE *rbtree_make_node(
size_t datasize,
void *
data)
608static int is_red(
struct RB_NODE *root)
611 return root->
red == 1;
617static struct RB_NODE *rbtree_single(
struct RB_NODE *root,
int dir)
631static struct RB_NODE *rbtree_double(
struct RB_NODE *root,
int dir)
633 root->
link[!dir] = rbtree_single(root->
link[!dir], !dir);
634 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