mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-22 19:37:11 +02:00
qpkg/jrb.c: major whitespace readjustment (converted from GNU to K&R style)
This commit is contained in:
parent
dec07f359a
commit
009f56c927
746
qpkg/jrb.c
746
qpkg/jrb.c
@ -51,453 +51,503 @@ static JRB rprev(JRB n);
|
|||||||
static void recolor(JRB n);
|
static void recolor(JRB n);
|
||||||
static void single_rotate(JRB y, int l);
|
static void single_rotate(JRB y, int l);
|
||||||
|
|
||||||
#define isred(n) (n->red)
|
#define isred(n) (n->red)
|
||||||
#define isblack(n) (!isred(n))
|
#define isblack(n) (!isred(n))
|
||||||
#define isleft(n) (n->left)
|
#define isleft(n) (n->left)
|
||||||
#define isright(n) (!isleft(n))
|
#define isright(n) (!isleft(n))
|
||||||
#define isint(n) (n->internal)
|
#define isint(n) (n->internal)
|
||||||
#define isext(n) (!isint(n))
|
#define isext(n) (!isint(n))
|
||||||
#define ishead(n) (n->roothead & 2)
|
#define ishead(n) (n->roothead & 2)
|
||||||
#define isroot(n) (n->roothead & 1)
|
#define isroot(n) (n->roothead & 1)
|
||||||
#define getlext(n) ((struct jrb_node *)(n->key))
|
#define getlext(n) ((struct jrb_node *) (n->key))
|
||||||
#define setlext(node, val) node->key = (void *) (val)
|
#define setlext(node, val) node->key = (void *) (val)
|
||||||
#define getrext(n) ((struct jrb_node *)(n->val))
|
#define getrext(n) ((struct jrb_node *) (n->val))
|
||||||
#define setrext(node, value) node->val = (void *) (value)
|
#define setrext(node, value) node->val = (void *) (value)
|
||||||
#define setred(n) n->red = 1
|
#define setred(n) n->red = 1
|
||||||
#define setblack(n) n->red = 0
|
#define setblack(n) n->red = 0
|
||||||
#define setleft(n) n->left = 1
|
#define setleft(n) n->left = 1
|
||||||
#define setright(n) n->left = 0
|
#define setright(n) n->left = 0
|
||||||
#define sethead(n) (n->roothead |= 2)
|
#define sethead(n) (n->roothead |= 2)
|
||||||
#define setroot(n) (n->roothead |= 1)
|
#define setroot(n) (n->roothead |= 1)
|
||||||
#define setint(n) n->internal = 1
|
#define setint(n) n->internal = 1
|
||||||
#define setext(n) n->internal = 0
|
#define setext(n) n->internal = 0
|
||||||
#define setnormal(n) n->roothead = 0
|
#define setnormal(n) n->roothead = 0
|
||||||
#define sibling(n) ((isleft(n)) ? n->parent->blink : n->parent->flink)
|
#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 */
|
static void insert(JRB item, JRB list) /* Inserts to the end of a list */
|
||||||
{
|
{
|
||||||
JRB last_node;
|
JRB last_node;
|
||||||
|
|
||||||
last_node = list->blink;
|
last_node = list->blink;
|
||||||
|
|
||||||
list->blink = item;
|
list->blink = item;
|
||||||
last_node->flink = item;
|
last_node->flink = item;
|
||||||
item->blink = last_node;
|
item->blink = last_node;
|
||||||
item->flink = list;
|
item->flink = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void delete_item(JRB item) /* Deletes an arbitrary iterm */
|
static void delete_item(JRB item) /* Deletes an arbitrary iterm */
|
||||||
{
|
{
|
||||||
item->flink->blink = item->blink;
|
item->flink->blink = item->blink;
|
||||||
item->blink->flink = item->flink;
|
item->blink->flink = item->flink;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define mk_new_ext(new, kkkey, vvval) {\
|
#define mk_new_ext(new, kkkey, vvval) { \
|
||||||
new = (JRB) malloc(sizeof(struct jrb_node));\
|
new = (JRB) malloc(sizeof(struct jrb_node)); \
|
||||||
new->val = vvval;\
|
new->val = vvval; \
|
||||||
new->key = kkkey;\
|
new->key = kkkey; \
|
||||||
setext(new);\
|
setext(new); \
|
||||||
setblack(new);\
|
setblack(new); \
|
||||||
setnormal(new);\
|
setnormal(new); \
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mk_new_int(JRB l, JRB r, JRB p, int il)
|
static void mk_new_int(JRB l, JRB r, JRB p, int il)
|
||||||
{
|
{
|
||||||
JRB newnode;
|
JRB newnode;
|
||||||
|
|
||||||
newnode = (JRB) malloc(sizeof(struct jrb_node));
|
newnode = (JRB) malloc(sizeof(struct jrb_node));
|
||||||
setint(newnode);
|
setint(newnode);
|
||||||
setred(newnode);
|
setred(newnode);
|
||||||
setnormal(newnode);
|
setnormal(newnode);
|
||||||
newnode->flink = l;
|
newnode->flink = l;
|
||||||
newnode->blink = r;
|
newnode->blink = r;
|
||||||
newnode->parent = p;
|
newnode->parent = p;
|
||||||
setlext(newnode, l);
|
setlext(newnode, l);
|
||||||
setrext(newnode, r);
|
setrext(newnode, r);
|
||||||
l->parent = newnode;
|
l->parent = newnode;
|
||||||
r->parent = newnode;
|
r->parent = newnode;
|
||||||
setleft(l);
|
setleft(l);
|
||||||
setright(r);
|
setright(r);
|
||||||
if (ishead(p)) {
|
if (ishead(p)) {
|
||||||
p->parent = newnode;
|
p->parent = newnode;
|
||||||
setroot(newnode);
|
setroot(newnode);
|
||||||
} else if (il) {
|
} else if (il) {
|
||||||
setleft(newnode);
|
setleft(newnode);
|
||||||
p->flink = newnode;
|
p->flink = newnode;
|
||||||
} else {
|
} else {
|
||||||
setright(newnode);
|
setright(newnode);
|
||||||
p->blink = newnode;
|
p->blink = newnode;
|
||||||
}
|
}
|
||||||
recolor(newnode);
|
recolor(newnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB lprev(JRB n)
|
JRB lprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n)) return n;
|
if (ishead(n))
|
||||||
while (!isroot(n)) {
|
return n;
|
||||||
if (isright(n)) return n->parent;
|
while (!isroot(n)) {
|
||||||
n = n->parent;
|
if (isright(n))
|
||||||
}
|
return n->parent;
|
||||||
return n->parent;
|
n = n->parent;
|
||||||
|
}
|
||||||
|
return n->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB rprev(JRB n)
|
JRB rprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n)) return n;
|
if (ishead(n))
|
||||||
while (!isroot(n)) {
|
return n;
|
||||||
if (isleft(n)) return n->parent;
|
while (!isroot(n)) {
|
||||||
n = n->parent;
|
if (isleft(n))
|
||||||
}
|
return n->parent;
|
||||||
return n->parent;
|
n = n->parent;
|
||||||
|
}
|
||||||
|
return n->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB make_jrb(void)
|
JRB make_jrb(void)
|
||||||
{
|
{
|
||||||
JRB head;
|
JRB head;
|
||||||
|
|
||||||
head = (JRB) malloc (sizeof(struct jrb_node));
|
head = (JRB) malloc(sizeof(struct jrb_node));
|
||||||
head->flink = head;
|
head->flink = head;
|
||||||
head->blink = head;
|
head->blink = head;
|
||||||
head->parent = head;
|
head->parent = head;
|
||||||
head->key = NULL;
|
head->key = NULL;
|
||||||
sethead(head);
|
sethead(head);
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB jrb_find_gte(JRB n, const void *key,
|
JRB jrb_find_gte(JRB n, const void *key,
|
||||||
int (*fxn)(const void *, const void *), int *fnd)
|
int (*fxn)(const void *, const void *), int *fnd)
|
||||||
{
|
{
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
*fnd = 0;
|
*fnd = 0;
|
||||||
if (!ishead(n)) {
|
if (!ishead(n)) {
|
||||||
fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n);
|
fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (n->parent == n) return n;
|
if (n->parent == n)
|
||||||
cmp = (*fxn)(key, n->blink->key);
|
return n;
|
||||||
if (cmp == 0) {
|
cmp = (*fxn)(key, n->blink->key);
|
||||||
*fnd = 1;
|
if (cmp == 0) {
|
||||||
return n->blink;
|
*fnd = 1;
|
||||||
}
|
return n->blink;
|
||||||
if (cmp > 0) return n;
|
}
|
||||||
else n = n->parent;
|
if (cmp > 0)
|
||||||
while (1) {
|
return n;
|
||||||
if (isext(n)) return n;
|
else
|
||||||
cmp = (*fxn)(key, getlext(n)->key);
|
n = n->parent;
|
||||||
if (cmp == 0) {
|
while (1) {
|
||||||
*fnd = 1;
|
if (isext(n))
|
||||||
return getlext(n);
|
return n;
|
||||||
}
|
cmp = (*fxn)(key, getlext(n)->key);
|
||||||
if (cmp < 0) n = n->flink ; else n = n->blink;
|
if (cmp == 0) {
|
||||||
}
|
*fnd = 1;
|
||||||
|
return getlext(n);
|
||||||
|
}
|
||||||
|
if (cmp < 0)
|
||||||
|
n = n->flink;
|
||||||
|
else
|
||||||
|
n = n->blink;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
|
JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
|
||||||
{
|
{
|
||||||
int fnd;
|
int fnd;
|
||||||
JRB j;
|
JRB j;
|
||||||
|
|
||||||
j = jrb_find_gte(n, key, fxn, &fnd);
|
j = jrb_find_gte(n, key, fxn, &fnd);
|
||||||
if (fnd) return j; else return NULL;
|
if (fnd)
|
||||||
|
return j;
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static JRB jrb_insert_b(JRB n, void *key, void *val)
|
static JRB jrb_insert_b(JRB n, void *key, void *val)
|
||||||
{
|
{
|
||||||
JRB newleft, newright, newnode, p;
|
JRB newleft, newright, newnode, p;
|
||||||
|
|
||||||
if (ishead(n)) {
|
if (ishead(n)) {
|
||||||
if (n->parent == n) { /* Tree is empty */
|
if (n->parent == n) { /* Tree is empty */
|
||||||
mk_new_ext(newnode, key, val);
|
mk_new_ext(newnode, key, val);
|
||||||
insert(newnode, n);
|
insert(newnode, n);
|
||||||
n->parent = newnode;
|
n->parent = newnode;
|
||||||
newnode->parent = n;
|
newnode->parent = n;
|
||||||
setroot(newnode);
|
setroot(newnode);
|
||||||
return newnode;
|
return newnode;
|
||||||
} else {
|
} else {
|
||||||
mk_new_ext(newright, key, val);
|
mk_new_ext(newright, key, val);
|
||||||
insert(newright, n);
|
insert(newright, n);
|
||||||
newleft = newright->blink;
|
newleft = newright->blink;
|
||||||
setnormal(newleft);
|
setnormal(newleft);
|
||||||
mk_new_int(newleft, newright, newleft->parent, isleft(newleft));
|
mk_new_int(newleft, newright, newleft->parent,
|
||||||
p = rprev(newright);
|
isleft(newleft));
|
||||||
if (!ishead(p)) setlext(p, newright);
|
p = rprev(newright);
|
||||||
return newright;
|
if (!ishead(p))
|
||||||
}
|
setlext(p, newright);
|
||||||
} else {
|
return newright;
|
||||||
mk_new_ext(newleft, key, val);
|
}
|
||||||
insert(newleft, n);
|
} else {
|
||||||
setnormal(n);
|
mk_new_ext(newleft, key, val);
|
||||||
mk_new_int(newleft, n, n->parent, isleft(n));
|
insert(newleft, n);
|
||||||
p = lprev(newleft);
|
setnormal(n);
|
||||||
if (!ishead(p)) setrext(p, newleft);
|
mk_new_int(newleft, n, n->parent, isleft(n));
|
||||||
return newleft;
|
p = lprev(newleft);
|
||||||
}
|
if (!ishead(p))
|
||||||
|
setrext(p, newleft);
|
||||||
|
return newleft;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void recolor(JRB n)
|
static void recolor(JRB n)
|
||||||
{
|
{
|
||||||
JRB p, gp, s;
|
JRB p, gp, s;
|
||||||
int done = 0;
|
int done = 0;
|
||||||
|
|
||||||
while(!done) {
|
while(!done) {
|
||||||
if (isroot(n)) {
|
if (isroot(n)) {
|
||||||
setblack(n);
|
setblack(n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = n->parent;
|
p = n->parent;
|
||||||
|
|
||||||
if (isblack(p)) return;
|
if (isblack(p))
|
||||||
|
return;
|
||||||
|
|
||||||
if (isroot(p)) {
|
if (isroot(p)) {
|
||||||
setblack(p);
|
setblack(p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gp = p->parent;
|
gp = p->parent;
|
||||||
s = sibling(p);
|
s = sibling(p);
|
||||||
if (isred(s)) {
|
if (isred(s)) {
|
||||||
setblack(p);
|
setblack(p);
|
||||||
setred(gp);
|
setred(gp);
|
||||||
setblack(s);
|
setblack(s);
|
||||||
n = gp;
|
n = gp;
|
||||||
} else {
|
} else {
|
||||||
done = 1;
|
done = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* p's sibling is black, p is red, gp is black */
|
/* p's sibling is black, p is red, gp is black */
|
||||||
|
|
||||||
if ((isleft(n) == 0) == (isleft(p) == 0)) {
|
if ((isleft(n) == 0) == (isleft(p) == 0)) {
|
||||||
single_rotate(gp, isleft(n));
|
single_rotate(gp, isleft(n));
|
||||||
setblack(p);
|
setblack(p);
|
||||||
setred(gp);
|
setred(gp);
|
||||||
} else {
|
} else {
|
||||||
single_rotate(p, isleft(n));
|
single_rotate(p, isleft(n));
|
||||||
single_rotate(gp, isleft(n));
|
single_rotate(gp, isleft(n));
|
||||||
setblack(n);
|
setblack(n);
|
||||||
setred(gp);
|
setred(gp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void single_rotate(JRB y, int l)
|
static void single_rotate(JRB y, int l)
|
||||||
{
|
{
|
||||||
int rl = 0 /* for gcc */, ir;
|
int rl = 0 /* for gcc */, ir;
|
||||||
JRB x, yp;
|
JRB x, yp;
|
||||||
|
|
||||||
ir = isroot(y);
|
ir = isroot(y);
|
||||||
yp = y->parent;
|
yp = y->parent;
|
||||||
if (!ir) {
|
if (!ir)
|
||||||
rl = isleft(y);
|
rl = isleft(y);
|
||||||
}
|
|
||||||
|
|
||||||
if (l) {
|
if (l) {
|
||||||
x = y->flink;
|
x = y->flink;
|
||||||
y->flink = x->blink;
|
y->flink = x->blink;
|
||||||
setleft(y->flink);
|
setleft(y->flink);
|
||||||
y->flink->parent = y;
|
y->flink->parent = y;
|
||||||
x->blink = y;
|
x->blink = y;
|
||||||
setright(y);
|
setright(y);
|
||||||
} else {
|
} else {
|
||||||
x = y->blink;
|
x = y->blink;
|
||||||
y->blink = x->flink;
|
y->blink = x->flink;
|
||||||
setright(y->blink);
|
setright(y->blink);
|
||||||
y->blink->parent = y;
|
y->blink->parent = y;
|
||||||
x->flink = y;
|
x->flink = y;
|
||||||
setleft(y);
|
setleft(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
x->parent = yp;
|
x->parent = yp;
|
||||||
y->parent = x;
|
y->parent = x;
|
||||||
if (ir) {
|
if (ir) {
|
||||||
yp->parent = x;
|
yp->parent = x;
|
||||||
setnormal(y);
|
setnormal(y);
|
||||||
setroot(x);
|
setroot(x);
|
||||||
} else {
|
} else {
|
||||||
if (rl) {
|
if (rl) {
|
||||||
yp->flink = x;
|
yp->flink = x;
|
||||||
setleft(x);
|
setleft(x);
|
||||||
} else {
|
} else {
|
||||||
yp->blink = x;
|
yp->blink = x;
|
||||||
setright(x);
|
setright(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void jrb_delete_node(JRB n)
|
void jrb_delete_node(JRB n)
|
||||||
{
|
{
|
||||||
JRB s, p, gp;
|
JRB s, p, gp;
|
||||||
char ir;
|
char ir;
|
||||||
|
|
||||||
if (isint(n)) {
|
if (isint(n)) {
|
||||||
fprintf(stderr, "Cannot delete an internal node: %p\n", n);
|
fprintf(stderr, "Cannot delete an internal node: %p\n", n);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (ishead(n)) {
|
if (ishead(n)) {
|
||||||
fprintf(stderr, "Cannot delete the head of an jrb_tree: %p\n", n);
|
fprintf(stderr,
|
||||||
exit(1);
|
"Cannot delete the head of an jrb_tree: %p\n", n);
|
||||||
}
|
exit(1);
|
||||||
delete_item(n); /* Delete it from the list */
|
}
|
||||||
p = n->parent; /* The only node */
|
delete_item(n); /* Delete it from the list */
|
||||||
if (isroot(n)) {
|
p = n->parent; /* The only node */
|
||||||
p->parent = p;
|
if (isroot(n)) {
|
||||||
free(n);
|
p->parent = p;
|
||||||
return;
|
free(n);
|
||||||
}
|
return;
|
||||||
s = sibling(n); /* The only node after deletion */
|
}
|
||||||
if (isroot(p)) {
|
s = sibling(n); /* The only node after deletion */
|
||||||
s->parent = p->parent;
|
if (isroot(p)) {
|
||||||
s->parent->parent = s;
|
s->parent = p->parent;
|
||||||
setroot(s);
|
s->parent->parent = s;
|
||||||
free(p);
|
setroot(s);
|
||||||
free(n);
|
free(p);
|
||||||
return;
|
free(n);
|
||||||
}
|
return;
|
||||||
gp = p->parent; /* Set parent to sibling */
|
}
|
||||||
s->parent = gp;
|
gp = p->parent; /* Set parent to sibling */
|
||||||
if (isleft(p)) {
|
s->parent = gp;
|
||||||
gp->flink = s;
|
if (isleft(p)) {
|
||||||
setleft(s);
|
gp->flink = s;
|
||||||
} else {
|
setleft(s);
|
||||||
gp->blink = s;
|
} else {
|
||||||
setright(s);
|
gp->blink = s;
|
||||||
}
|
setright(s);
|
||||||
ir = isred(p);
|
}
|
||||||
free(p);
|
ir = isred(p);
|
||||||
free(n);
|
free(p);
|
||||||
|
free(n);
|
||||||
|
|
||||||
if (isext(s)) { /* Update proper rext and lext values */
|
if (isext(s)) { /* Update proper rext and lext values */
|
||||||
p = lprev(s);
|
p = lprev(s);
|
||||||
if (!ishead(p)) setrext(p, s);
|
if (!ishead(p))
|
||||||
p = rprev(s);
|
setrext(p, s);
|
||||||
if (!ishead(p)) setlext(p, s);
|
p = rprev(s);
|
||||||
} else if (isblack(s)) {
|
if (!ishead(p))
|
||||||
fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
|
setlext(p, s);
|
||||||
exit(1);
|
} else if (isblack(s)) {
|
||||||
} else {
|
fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
|
||||||
p = lprev(s);
|
exit(1);
|
||||||
if (!ishead(p)) setrext(p, s->flink);
|
} else {
|
||||||
p = rprev(s);
|
p = lprev(s);
|
||||||
if (!ishead(p)) setlext(p, s->blink);
|
if (!ishead(p))
|
||||||
setblack(s);
|
setrext(p, s->flink);
|
||||||
return;
|
p = rprev(s);
|
||||||
}
|
if (!ishead(p))
|
||||||
|
setlext(p, s->blink);
|
||||||
|
setblack(s);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (ir) return;
|
if (ir)
|
||||||
|
return;
|
||||||
|
|
||||||
/* Recolor */
|
/* Recolor */
|
||||||
|
|
||||||
n = s;
|
n = s;
|
||||||
p = n->parent;
|
p = n->parent;
|
||||||
s = sibling(n);
|
s = sibling(n);
|
||||||
while(isblack(p) && isblack(s) && isint(s) &&
|
while(isblack(p) && isblack(s) && isint(s) &&
|
||||||
isblack(s->flink) && isblack(s->blink)) {
|
isblack(s->flink) && isblack(s->blink)) {
|
||||||
setred(s);
|
setred(s);
|
||||||
n = p;
|
n = p;
|
||||||
if (isroot(n)) return;
|
if (isroot(n))
|
||||||
p = n->parent;
|
return;
|
||||||
s = sibling(n);
|
p = n->parent;
|
||||||
}
|
s = sibling(n);
|
||||||
|
}
|
||||||
|
|
||||||
if (isblack(p) && isred(s)) { /* Rotation 2.3b */
|
if (isblack(p) && isred(s)) { /* Rotation 2.3b */
|
||||||
single_rotate(p, isright(n));
|
single_rotate(p, isright(n));
|
||||||
setred(p);
|
setred(p);
|
||||||
setblack(s);
|
setblack(s);
|
||||||
s = sibling(n);
|
s = sibling(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
{ JRB x, z; char il;
|
{
|
||||||
|
JRB x, z; char il;
|
||||||
|
|
||||||
if (isext(s)) {
|
if (isext(s)) {
|
||||||
fprintf(stderr, "DELETION ERROR: sibling not internal\n");
|
fprintf(stderr,
|
||||||
exit(1);
|
"DELETION ERROR: sibling not internal\n");
|
||||||
}
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
il = isleft(n);
|
il = isleft(n);
|
||||||
x = il ? s->flink : s->blink ;
|
x = il ? s->flink : s->blink;
|
||||||
z = sibling(x);
|
z = sibling(x);
|
||||||
|
|
||||||
if (isred(z)) { /* Rotation 2.3f */
|
if (isred(z)) { /* Rotation 2.3f */
|
||||||
single_rotate(p, !il);
|
single_rotate(p, !il);
|
||||||
setblack(z);
|
setblack(z);
|
||||||
if (isred(p)) setred(s); else setblack(s);
|
if (isred(p))
|
||||||
setblack(p);
|
setred(s);
|
||||||
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
else
|
||||||
if (isred(s) || isblack(p)) {
|
setblack(s);
|
||||||
fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n");
|
setblack(p);
|
||||||
exit(1);
|
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
||||||
}
|
if (isred(s) || isblack(p)) {
|
||||||
setblack(p);
|
fprintf(stderr,
|
||||||
setred(s);
|
"DELETION ERROR: 2.3c not quite right\n");
|
||||||
return;
|
exit(1);
|
||||||
} else if (isred(p)) { /* 2.3d */
|
}
|
||||||
single_rotate(s, il);
|
setblack(p);
|
||||||
single_rotate(p, !il);
|
setred(s);
|
||||||
setblack(x);
|
return;
|
||||||
setred(s);
|
} else if (isred(p)) { /* 2.3d */
|
||||||
return;
|
single_rotate(s, il);
|
||||||
} else { /* 2.3e */
|
single_rotate(p, !il);
|
||||||
single_rotate(s, il);
|
setblack(x);
|
||||||
single_rotate(p, !il);
|
setred(s);
|
||||||
setblack(x);
|
return;
|
||||||
return;
|
} else { /* 2.3e */
|
||||||
}
|
single_rotate(s, il);
|
||||||
}
|
single_rotate(p, !il);
|
||||||
|
setblack(x);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_nblack(JRB n)
|
int jrb_nblack(JRB n)
|
||||||
{
|
{
|
||||||
int nb;
|
int nb;
|
||||||
if (ishead(n) || isint(n)) {
|
|
||||||
fprintf(stderr, "ERROR: jrb_nblack called on a non-external node %p\n", n);
|
if (ishead(n) || isint(n)) {
|
||||||
exit(1);
|
fprintf(stderr,
|
||||||
}
|
"ERROR: jrb_nblack called on a non-external node %p\n", n);
|
||||||
nb = 0;
|
exit(1);
|
||||||
while(!ishead(n)) {
|
}
|
||||||
if (isblack(n)) nb++;
|
nb = 0;
|
||||||
n = n->parent;
|
while(!ishead(n)) {
|
||||||
}
|
if (isblack(n)) nb++;
|
||||||
return nb;
|
n = n->parent;
|
||||||
|
}
|
||||||
|
return nb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_plength(JRB n)
|
int jrb_plength(JRB n)
|
||||||
{
|
{
|
||||||
int pl;
|
int pl;
|
||||||
if (ishead(n) || isint(n)) {
|
|
||||||
fprintf(stderr, "ERROR: jrb_plength called on a non-external node %p\n", n);
|
if (ishead(n) || isint(n)) {
|
||||||
exit(1);
|
fprintf(stderr,
|
||||||
}
|
"ERROR: jrb_plength called on a non-external node %p\n", n);
|
||||||
pl = 0;
|
exit(1);
|
||||||
while(!ishead(n)) {
|
}
|
||||||
pl++;
|
pl = 0;
|
||||||
n = n->parent;
|
while (!ishead(n)) {
|
||||||
}
|
pl++;
|
||||||
return pl;
|
n = n->parent;
|
||||||
|
}
|
||||||
|
return pl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void jrb_free_tree(JRB n)
|
void jrb_free_tree(JRB n)
|
||||||
{
|
{
|
||||||
if (!ishead(n)) {
|
if (!ishead(n)) {
|
||||||
fprintf(stderr, "ERROR: Rb_free_tree called on a non-head node\n");
|
fprintf(stderr,
|
||||||
exit(1);
|
"ERROR: Rb_free_tree called on a non-head node\n");
|
||||||
}
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
while(jrb_first(n) != jrb_nil(n)) {
|
while(jrb_first(n) != jrb_nil(n))
|
||||||
jrb_delete_node(jrb_first(n));
|
jrb_delete_node(jrb_first(n));
|
||||||
}
|
|
||||||
free(n);
|
free(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *jrb_val(JRB n)
|
void *jrb_val(JRB n)
|
||||||
{
|
{
|
||||||
return n->val;
|
return n->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB jrb_insert(JRB tree, void *key, void *val,
|
JRB jrb_insert(JRB tree, void *key, void *val,
|
||||||
int (*func)(const void *, const void *))
|
int (*func)(const void *a, const void *b))
|
||||||
{
|
{
|
||||||
int fnd;
|
int fnd;
|
||||||
|
|
||||||
return jrb_insert_b(jrb_find_gte(tree, key, func, &fnd), key, val);
|
return jrb_insert_b(jrb_find_gte(tree, key, func, &fnd), key, val);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user