1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-22 06:53:08 +02:00

qpkg/jrb.c: reordered recolor and single_rotate and removed prototypes

This commit is contained in:
Werner Almesberger 2010-11-19 20:39:41 -03:00
parent c060b883a9
commit da7a6feb4b

View File

@ -46,11 +46,6 @@ Fax: 865-974-4404
#include <ctype.h>
#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))
@ -96,6 +91,97 @@ static void delete_item(JRB item) /* Deletes an arbitrary iterm */
}
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);
} else {
x = y->blink;
y->blink = x->flink;
setright(y->blink);
y->blink->parent = y;
x->flink = y;
setleft(y);
}
x->parent = yp;
y->parent = x;
if (ir) {
yp->parent = x;
setnormal(y);
setroot(x);
} else {
if (rl) {
yp->flink = x;
setleft(x);
} else {
yp->blink = x;
setright(x);
}
}
}
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)) {
setblack(p);
setred(gp);
setblack(s);
n = gp;
} else {
done = 1;
}
}
/* 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);
setred(gp);
} else {
single_rotate(p, isleft(n));
single_rotate(gp, isleft(n));
setblack(n);
setred(gp);
}
}
static JRB mk_new_ext(void *key, void *val)
{
JRB new;
@ -268,97 +354,6 @@ static JRB jrb_insert_b(JRB n, void *key, void *val)
}
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)) {
setblack(p);
setred(gp);
setblack(s);
n = gp;
} else {
done = 1;
}
}
/* 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);
setred(gp);
} else {
single_rotate(p, isleft(n));
single_rotate(gp, isleft(n));
setblack(n);
setred(gp);
}
}
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);
} else {
x = y->blink;
y->blink = x->flink;
setright(y->blink);
y->blink->parent = y;
x->flink = y;
setleft(y);
}
x->parent = yp;
y->parent = x;
if (ir) {
yp->parent = x;
setnormal(y);
setroot(x);
} else {
if (rl) {
yp->flink = x;
setleft(x);
} else {
yp->blink = x;
setright(x);
}
}
}
void jrb_delete_node(JRB n)
{
JRB s, p, gp, x, z;