diff --git a/qpkg/jrb.c b/qpkg/jrb.c index 2046cef..2e201a6 100644 --- a/qpkg/jrb.c +++ b/qpkg/jrb.c @@ -38,19 +38,19 @@ Fax: 865-974-4404 /* Original code by Jim Plank (plank@cs.utk.edu) */ /* modified for THINK C 6.0 for Macintosh by Chris Bartley */ - + #include #include #include #include #include "jrb.h" - + static void mk_new_int(JRB l, JRB r, JRB p, int il); static JRB lprev(JRB n); static JRB rprev(JRB n); static void recolor(JRB n); static void single_rotate(JRB y, int l); - + #define isred(n) (n->red) #define isblack(n) (!isred(n)) #define isleft(n) (n->left) @@ -73,21 +73,21 @@ static void single_rotate(JRB y, int l); #define setext(n) n->internal = 0 #define setnormal(n) n->roothead = 0 #define sibling(n) (isleft(n) ? n->parent->blink : n->parent->flink) - + static void insert(JRB item, JRB list) /* Inserts to the end of a list */ { JRB last_node; - + last_node = list->blink; - + list->blink = item; last_node->flink = item; item->blink = last_node; item->flink = list; } - + static void delete_item(JRB item) /* Deletes an arbitrary iterm */ { item->flink->blink = item->blink; @@ -108,11 +108,11 @@ static JRB mk_new_ext(void *key, void *val) return new; } - + static void mk_new_int(JRB l, JRB r, JRB p, int il) { JRB newnode; - + newnode = (JRB) malloc(sizeof(struct jrb_node)); setint(newnode); setred(newnode); @@ -137,9 +137,9 @@ static void mk_new_int(JRB l, JRB r, JRB p, int il) p->blink = newnode; } recolor(newnode); -} - - +} + + JRB lprev(JRB n) { if (ishead(n)) @@ -152,7 +152,7 @@ JRB lprev(JRB n) return n->parent; } - + JRB rprev(JRB n) { if (ishead(n)) @@ -164,12 +164,12 @@ JRB rprev(JRB n) } return n->parent; } - + JRB make_jrb(void) { JRB head; - + head = (JRB) malloc(sizeof(struct jrb_node)); head->flink = head; head->blink = head; @@ -178,13 +178,13 @@ JRB make_jrb(void) sethead(head); return head; } - + JRB jrb_find_gte(JRB n, const void *key, int (*fxn)(const void *, const void *), int *fnd) { int cmp; - + *fnd = 0; if (!ishead(n)) { fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n); @@ -195,10 +195,10 @@ JRB jrb_find_gte(JRB n, const void *key, cmp = (*fxn)(key, n->blink->key); if (cmp == 0) { *fnd = 1; - return n->blink; + return n->blink; } if (cmp > 0) - return n; + return n; else n = n->parent; while (1) { @@ -216,7 +216,7 @@ JRB jrb_find_gte(JRB n, const void *key, } } - + JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *)) { int fnd; @@ -229,11 +229,11 @@ JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *)) return NULL; } - + static JRB jrb_insert_b(JRB n, void *key, void *val) { JRB newleft, newright, newnode, p; - + if (ishead(n)) { if (n->parent == n) { /* Tree is empty */ newnode = mk_new_ext(key, val); @@ -262,32 +262,32 @@ static JRB jrb_insert_b(JRB n, void *key, void *val) p = lprev(newleft); if (!ishead(p)) setrext(p, newleft); - return newleft; + return newleft; } } - + static void recolor(JRB n) -{ +{ JRB p, gp, s; int done = 0; - + while (!done) { if (isroot(n)) { setblack(n); return; } - + p = n->parent; - + if (isblack(p)) return; - + if (isroot(p)) { setblack(p); return; } - + gp = p->parent; s = sibling(p); if (isred(s)) { @@ -300,7 +300,7 @@ static void recolor(JRB n) } } /* p's sibling is black, p is red, gp is black */ - + if ((isleft(n) == 0) == (isleft(p) == 0)) { single_rotate(gp, isleft(n)); setblack(p); @@ -313,33 +313,33 @@ static void recolor(JRB n) } } - + static void single_rotate(JRB y, int l) { int rl = 0 /* for gcc */, ir; JRB x, yp; - + ir = isroot(y); yp = y->parent; if (!ir) rl = isleft(y); - + if (l) { x = y->flink; y->flink = x->blink; setleft(y->flink); y->flink->parent = y; x->blink = y; - setright(y); + setright(y); } else { x = y->blink; y->blink = x->flink; setright(y->blink); y->blink->parent = y; x->flink = y; - setleft(y); + setleft(y); } - + x->parent = yp; y->parent = x; if (ir) { @@ -362,7 +362,7 @@ void jrb_delete_node(JRB n) { JRB s, p, gp, x, z; char ir, il; - + if (isint(n)) { fprintf(stderr, "Cannot delete an internal node: %p\n", n); exit(1); @@ -378,7 +378,7 @@ void jrb_delete_node(JRB n) p->parent = p; free(n); return; - } + } s = sibling(n); /* The only node after deletion */ if (isroot(p)) { s->parent = p->parent; @@ -400,9 +400,9 @@ void jrb_delete_node(JRB n) ir = isred(p); free(p); free(n); - + if (isext(s)) { /* Update proper rext and lext values */ - p = lprev(s); + p = lprev(s); if (!ishead(p)) setrext(p, s); p = rprev(s); @@ -421,16 +421,16 @@ void jrb_delete_node(JRB n) setblack(s); return; } - + if (ir) return; - + /* Recolor */ - + n = s; p = n->parent; s = sibling(n); - while (isblack(p) && isblack(s) && isint(s) && + while (isblack(p) && isblack(s) && isint(s) && isblack(s->flink) && isblack(s->blink)) { setred(s); n = p; @@ -439,23 +439,23 @@ void jrb_delete_node(JRB n) p = n->parent; s = sibling(n); } - + if (isblack(p) && isred(s)) { /* Rotation 2.3b */ single_rotate(p, isright(n)); setred(p); setblack(s); s = sibling(n); } - + if (isext(s)) { fprintf(stderr, "DELETION ERROR: sibling not internal\n"); exit(1); } - + il = isleft(n); x = il ? s->flink : s->blink; z = sibling(x); - + if (isred(z)) { /* Rotation 2.3f */ single_rotate(p, !il); setblack(z); @@ -489,7 +489,7 @@ void jrb_delete_node(JRB n) setblack(x); } - + int jrb_nblack(JRB n) { int nb; @@ -507,7 +507,7 @@ int jrb_nblack(JRB n) return nb; } - + int jrb_plength(JRB n) { int pl; @@ -525,7 +525,7 @@ int jrb_plength(JRB n) return pl; } - + void jrb_free_tree(JRB n) { if (!ishead(n)) { @@ -533,23 +533,23 @@ void jrb_free_tree(JRB n) "ERROR: Rb_free_tree called on a non-head node\n"); exit(1); } - + while (jrb_first(n) != jrb_nil(n)) jrb_delete_node(jrb_first(n)); free(n); } - + void *jrb_val(JRB n) { return n->val; } - + JRB jrb_insert(JRB tree, void *key, void *val, int (*func)(const void *a, const void *b)) -{ +{ int fnd; return jrb_insert_b(jrb_find_gte(tree, key, func, &fnd), key, val); diff --git a/qpkg/jrb.h b/qpkg/jrb.h index b06e7f4..dfaca49 100644 --- a/qpkg/jrb.h +++ b/qpkg/jrb.h @@ -78,15 +78,15 @@ JRB jrb_find(JRB root, const void *key, k or whose value is the smallest value greater than k. Sets found to 1 if the key was found, and 0 otherwise. */ -JRB jrb_find_gte(JRB root, const void *key, +JRB jrb_find_gte(JRB root, const void *key, int (*func)(const void *a, const void *b), int *found); -/* Creates a node with key key and val val and inserts it into the - tree before/after node nd. Does not check to ensure that you are +/* Creates a node with key key and val val and inserts it into the + tree before/after node nd. Does not check to ensure that you are keeping the correct order */ -void jrb_delete_node(JRB node); /* Deletes and frees a node (but +void jrb_delete_node(JRB node); /* Deletes and frees a node (but not the key or val) */ void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */ @@ -97,7 +97,7 @@ int jrb_nblack(JRB n); /* returns # of black nodes in path from n to the root */ int jrb_plength(JRB n); /* returns the # of nodes in path from n to the root */ - + #define jrb_first(n) ((n)->flink) #define jrb_last(n) ((n)->blink) #define jrb_next(n) ((n)->flink) @@ -106,11 +106,11 @@ int jrb_plength(JRB n); /* returns the # of nodes in path from #ifndef jrb_nil #define jrb_nil(t) (t) #endif - + #define jrb_traverse(ptr, lst) \ for (ptr = jrb_first(lst); ptr != jrb_nil(lst); ptr = jrb_next(ptr)) - + #define jrb_rtraverse(ptr, lst) \ for (ptr = jrb_last(lst); ptr != jrb_nil(lst); ptr = jrb_prev(ptr)) - + #endif