mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-25 19:05:56 +02:00
qpkg/jrb.c: major whitespace readjustment (converted from GNU to K&R style)
This commit is contained in:
parent
dec07f359a
commit
009f56c927
118
qpkg/jrb.c
118
qpkg/jrb.c
@ -72,7 +72,8 @@ static void single_rotate(JRB y, int l);
|
||||
#define setint(n) n->internal = 1
|
||||
#define setext(n) n->internal = 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 */
|
||||
{
|
||||
@ -86,6 +87,7 @@ static void insert(JRB item, JRB list) /* Inserts to the end of a list */
|
||||
item->flink = list;
|
||||
}
|
||||
|
||||
|
||||
static void delete_item(JRB item) /* Deletes an arbitrary iterm */
|
||||
{
|
||||
item->flink->blink = item->blink;
|
||||
@ -134,24 +136,30 @@ static void mk_new_int(JRB l, JRB r, JRB p, int il)
|
||||
|
||||
JRB lprev(JRB n)
|
||||
{
|
||||
if (ishead(n)) return n;
|
||||
if (ishead(n))
|
||||
return n;
|
||||
while (!isroot(n)) {
|
||||
if (isright(n)) return n->parent;
|
||||
if (isright(n))
|
||||
return n->parent;
|
||||
n = n->parent;
|
||||
}
|
||||
return n->parent;
|
||||
}
|
||||
|
||||
|
||||
JRB rprev(JRB n)
|
||||
{
|
||||
if (ishead(n)) return n;
|
||||
if (ishead(n))
|
||||
return n;
|
||||
while (!isroot(n)) {
|
||||
if (isleft(n)) return n->parent;
|
||||
if (isleft(n))
|
||||
return n->parent;
|
||||
n = n->parent;
|
||||
}
|
||||
return n->parent;
|
||||
}
|
||||
|
||||
|
||||
JRB make_jrb(void)
|
||||
{
|
||||
JRB head;
|
||||
@ -165,6 +173,7 @@ JRB make_jrb(void)
|
||||
return head;
|
||||
}
|
||||
|
||||
|
||||
JRB jrb_find_gte(JRB n, const void *key,
|
||||
int (*fxn)(const void *, const void *), int *fnd)
|
||||
{
|
||||
@ -175,34 +184,46 @@ JRB jrb_find_gte(JRB n, const void *key,
|
||||
fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n);
|
||||
exit(1);
|
||||
}
|
||||
if (n->parent == n) return n;
|
||||
if (n->parent == n)
|
||||
return n;
|
||||
cmp = (*fxn)(key, n->blink->key);
|
||||
if (cmp == 0) {
|
||||
*fnd = 1;
|
||||
return n->blink;
|
||||
}
|
||||
if (cmp > 0) return n;
|
||||
else n = n->parent;
|
||||
if (cmp > 0)
|
||||
return n;
|
||||
else
|
||||
n = n->parent;
|
||||
while (1) {
|
||||
if (isext(n)) return n;
|
||||
if (isext(n))
|
||||
return n;
|
||||
cmp = (*fxn)(key, getlext(n)->key);
|
||||
if (cmp == 0) {
|
||||
*fnd = 1;
|
||||
return getlext(n);
|
||||
}
|
||||
if (cmp < 0) n = n->flink ; else n = n->blink;
|
||||
if (cmp < 0)
|
||||
n = n->flink;
|
||||
else
|
||||
n = n->blink;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
|
||||
{
|
||||
int fnd;
|
||||
JRB j;
|
||||
|
||||
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)
|
||||
{
|
||||
JRB newleft, newright, newnode, p;
|
||||
@ -220,9 +241,11 @@ static JRB jrb_insert_b(JRB n, void *key, void *val)
|
||||
insert(newright, n);
|
||||
newleft = newright->blink;
|
||||
setnormal(newleft);
|
||||
mk_new_int(newleft, newright, newleft->parent, isleft(newleft));
|
||||
mk_new_int(newleft, newright, newleft->parent,
|
||||
isleft(newleft));
|
||||
p = rprev(newright);
|
||||
if (!ishead(p)) setlext(p, newright);
|
||||
if (!ishead(p))
|
||||
setlext(p, newright);
|
||||
return newright;
|
||||
}
|
||||
} else {
|
||||
@ -231,11 +254,13 @@ static JRB jrb_insert_b(JRB n, void *key, void *val)
|
||||
setnormal(n);
|
||||
mk_new_int(newleft, n, n->parent, isleft(n));
|
||||
p = lprev(newleft);
|
||||
if (!ishead(p)) setrext(p, newleft);
|
||||
if (!ishead(p))
|
||||
setrext(p, newleft);
|
||||
return newleft;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void recolor(JRB n)
|
||||
{
|
||||
JRB p, gp, s;
|
||||
@ -249,7 +274,8 @@ static void recolor(JRB n)
|
||||
|
||||
p = n->parent;
|
||||
|
||||
if (isblack(p)) return;
|
||||
if (isblack(p))
|
||||
return;
|
||||
|
||||
if (isroot(p)) {
|
||||
setblack(p);
|
||||
@ -281,6 +307,7 @@ static void recolor(JRB n)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void single_rotate(JRB y, int l)
|
||||
{
|
||||
int rl = 0 /* for gcc */, ir;
|
||||
@ -288,9 +315,8 @@ static void single_rotate(JRB y, int l)
|
||||
|
||||
ir = isroot(y);
|
||||
yp = y->parent;
|
||||
if (!ir) {
|
||||
if (!ir)
|
||||
rl = isleft(y);
|
||||
}
|
||||
|
||||
if (l) {
|
||||
x = y->flink;
|
||||
@ -325,6 +351,7 @@ static void single_rotate(JRB y, int l)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void jrb_delete_node(JRB n)
|
||||
{
|
||||
JRB s, p, gp;
|
||||
@ -335,7 +362,8 @@ void jrb_delete_node(JRB n)
|
||||
exit(1);
|
||||
}
|
||||
if (ishead(n)) {
|
||||
fprintf(stderr, "Cannot delete the head of an jrb_tree: %p\n", n);
|
||||
fprintf(stderr,
|
||||
"Cannot delete the head of an jrb_tree: %p\n", n);
|
||||
exit(1);
|
||||
}
|
||||
delete_item(n); /* Delete it from the list */
|
||||
@ -369,22 +397,27 @@ void jrb_delete_node(JRB n)
|
||||
|
||||
if (isext(s)) { /* Update proper rext and lext values */
|
||||
p = lprev(s);
|
||||
if (!ishead(p)) setrext(p, s);
|
||||
if (!ishead(p))
|
||||
setrext(p, s);
|
||||
p = rprev(s);
|
||||
if (!ishead(p)) setlext(p, s);
|
||||
if (!ishead(p))
|
||||
setlext(p, s);
|
||||
} else if (isblack(s)) {
|
||||
fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
|
||||
exit(1);
|
||||
} else {
|
||||
p = lprev(s);
|
||||
if (!ishead(p)) setrext(p, s->flink);
|
||||
if (!ishead(p))
|
||||
setrext(p, s->flink);
|
||||
p = rprev(s);
|
||||
if (!ishead(p)) setlext(p, s->blink);
|
||||
if (!ishead(p))
|
||||
setlext(p, s->blink);
|
||||
setblack(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ir) return;
|
||||
if (ir)
|
||||
return;
|
||||
|
||||
/* Recolor */
|
||||
|
||||
@ -395,7 +428,8 @@ void jrb_delete_node(JRB n)
|
||||
isblack(s->flink) && isblack(s->blink)) {
|
||||
setred(s);
|
||||
n = p;
|
||||
if (isroot(n)) return;
|
||||
if (isroot(n))
|
||||
return;
|
||||
p = n->parent;
|
||||
s = sibling(n);
|
||||
}
|
||||
@ -407,10 +441,12 @@ void jrb_delete_node(JRB n)
|
||||
s = sibling(n);
|
||||
}
|
||||
|
||||
{ JRB x, z; char il;
|
||||
{
|
||||
JRB x, z; char il;
|
||||
|
||||
if (isext(s)) {
|
||||
fprintf(stderr, "DELETION ERROR: sibling not internal\n");
|
||||
fprintf(stderr,
|
||||
"DELETION ERROR: sibling not internal\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -421,11 +457,15 @@ void jrb_delete_node(JRB n)
|
||||
if (isred(z)) { /* Rotation 2.3f */
|
||||
single_rotate(p, !il);
|
||||
setblack(z);
|
||||
if (isred(p)) setred(s); else setblack(s);
|
||||
if (isred(p))
|
||||
setred(s);
|
||||
else
|
||||
setblack(s);
|
||||
setblack(p);
|
||||
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
||||
if (isred(s) || isblack(p)) {
|
||||
fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n");
|
||||
fprintf(stderr,
|
||||
"DELETION ERROR: 2.3c not quite right\n");
|
||||
exit(1);
|
||||
}
|
||||
setblack(p);
|
||||
@ -446,11 +486,14 @@ void jrb_delete_node(JRB n)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int jrb_nblack(JRB n)
|
||||
{
|
||||
int nb;
|
||||
|
||||
if (ishead(n) || isint(n)) {
|
||||
fprintf(stderr, "ERROR: jrb_nblack called on a non-external node %p\n", n);
|
||||
fprintf(stderr,
|
||||
"ERROR: jrb_nblack called on a non-external node %p\n", n);
|
||||
exit(1);
|
||||
}
|
||||
nb = 0;
|
||||
@ -461,11 +504,14 @@ int jrb_nblack(JRB n)
|
||||
return nb;
|
||||
}
|
||||
|
||||
|
||||
int jrb_plength(JRB n)
|
||||
{
|
||||
int pl;
|
||||
|
||||
if (ishead(n) || isint(n)) {
|
||||
fprintf(stderr, "ERROR: jrb_plength called on a non-external node %p\n", n);
|
||||
fprintf(stderr,
|
||||
"ERROR: jrb_plength called on a non-external node %p\n", n);
|
||||
exit(1);
|
||||
}
|
||||
pl = 0;
|
||||
@ -476,26 +522,30 @@ int jrb_plength(JRB n)
|
||||
return pl;
|
||||
}
|
||||
|
||||
|
||||
void jrb_free_tree(JRB n)
|
||||
{
|
||||
if (!ishead(n)) {
|
||||
fprintf(stderr, "ERROR: Rb_free_tree called on a non-head node\n");
|
||||
fprintf(stderr,
|
||||
"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));
|
||||
}
|
||||
|
||||
free(n);
|
||||
}
|
||||
|
||||
|
||||
void *jrb_val(JRB n)
|
||||
{
|
||||
return n->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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user