mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-23 00:46:15 +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 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 */
|
||||||
{
|
{
|
||||||
@ -86,6 +87,7 @@ static void insert(JRB item, JRB list) /* Inserts to the end of a list */
|
|||||||
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;
|
||||||
@ -134,24 +136,30 @@ static void mk_new_int(JRB l, JRB r, JRB p, int il)
|
|||||||
|
|
||||||
JRB lprev(JRB n)
|
JRB lprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n)) return n;
|
if (ishead(n))
|
||||||
|
return n;
|
||||||
while (!isroot(n)) {
|
while (!isroot(n)) {
|
||||||
if (isright(n)) return n->parent;
|
if (isright(n))
|
||||||
|
return n->parent;
|
||||||
n = n->parent;
|
n = n->parent;
|
||||||
}
|
}
|
||||||
return n->parent;
|
return n->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB rprev(JRB n)
|
JRB rprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n)) return n;
|
if (ishead(n))
|
||||||
|
return n;
|
||||||
while (!isroot(n)) {
|
while (!isroot(n)) {
|
||||||
if (isleft(n)) return n->parent;
|
if (isleft(n))
|
||||||
|
return n->parent;
|
||||||
n = n->parent;
|
n = n->parent;
|
||||||
}
|
}
|
||||||
return n->parent;
|
return n->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB make_jrb(void)
|
JRB make_jrb(void)
|
||||||
{
|
{
|
||||||
JRB head;
|
JRB head;
|
||||||
@ -165,6 +173,7 @@ JRB make_jrb(void)
|
|||||||
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)
|
||||||
{
|
{
|
||||||
@ -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);
|
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)
|
||||||
|
return n;
|
||||||
cmp = (*fxn)(key, n->blink->key);
|
cmp = (*fxn)(key, n->blink->key);
|
||||||
if (cmp == 0) {
|
if (cmp == 0) {
|
||||||
*fnd = 1;
|
*fnd = 1;
|
||||||
return n->blink;
|
return n->blink;
|
||||||
}
|
}
|
||||||
if (cmp > 0) return n;
|
if (cmp > 0)
|
||||||
else n = n->parent;
|
return n;
|
||||||
|
else
|
||||||
|
n = n->parent;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (isext(n)) return n;
|
if (isext(n))
|
||||||
|
return n;
|
||||||
cmp = (*fxn)(key, getlext(n)->key);
|
cmp = (*fxn)(key, getlext(n)->key);
|
||||||
if (cmp == 0) {
|
if (cmp == 0) {
|
||||||
*fnd = 1;
|
*fnd = 1;
|
||||||
return getlext(n);
|
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 *))
|
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;
|
||||||
@ -220,9 +241,11 @@ static JRB jrb_insert_b(JRB n, void *key, void *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,
|
||||||
|
isleft(newleft));
|
||||||
p = rprev(newright);
|
p = rprev(newright);
|
||||||
if (!ishead(p)) setlext(p, newright);
|
if (!ishead(p))
|
||||||
|
setlext(p, newright);
|
||||||
return newright;
|
return newright;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -231,11 +254,13 @@ static JRB jrb_insert_b(JRB n, void *key, void *val)
|
|||||||
setnormal(n);
|
setnormal(n);
|
||||||
mk_new_int(newleft, n, n->parent, isleft(n));
|
mk_new_int(newleft, n, n->parent, isleft(n));
|
||||||
p = lprev(newleft);
|
p = lprev(newleft);
|
||||||
if (!ishead(p)) setrext(p, newleft);
|
if (!ishead(p))
|
||||||
|
setrext(p, newleft);
|
||||||
return newleft;
|
return newleft;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void recolor(JRB n)
|
static void recolor(JRB n)
|
||||||
{
|
{
|
||||||
JRB p, gp, s;
|
JRB p, gp, s;
|
||||||
@ -249,7 +274,8 @@ static void recolor(JRB n)
|
|||||||
|
|
||||||
p = n->parent;
|
p = n->parent;
|
||||||
|
|
||||||
if (isblack(p)) return;
|
if (isblack(p))
|
||||||
|
return;
|
||||||
|
|
||||||
if (isroot(p)) {
|
if (isroot(p)) {
|
||||||
setblack(p);
|
setblack(p);
|
||||||
@ -281,6 +307,7 @@ static void recolor(JRB n)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
@ -288,9 +315,8 @@ static void single_rotate(JRB y, int l)
|
|||||||
|
|
||||||
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;
|
||||||
@ -325,6 +351,7 @@ static void single_rotate(JRB y, int l)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void jrb_delete_node(JRB n)
|
void jrb_delete_node(JRB n)
|
||||||
{
|
{
|
||||||
JRB s, p, gp;
|
JRB s, p, gp;
|
||||||
@ -335,7 +362,8 @@ void jrb_delete_node(JRB 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,
|
||||||
|
"Cannot delete the head of an jrb_tree: %p\n", n);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
delete_item(n); /* Delete it from the list */
|
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 */
|
if (isext(s)) { /* Update proper rext and lext values */
|
||||||
p = lprev(s);
|
p = lprev(s);
|
||||||
if (!ishead(p)) setrext(p, s);
|
if (!ishead(p))
|
||||||
|
setrext(p, s);
|
||||||
p = rprev(s);
|
p = rprev(s);
|
||||||
if (!ishead(p)) setlext(p, s);
|
if (!ishead(p))
|
||||||
|
setlext(p, s);
|
||||||
} else if (isblack(s)) {
|
} else if (isblack(s)) {
|
||||||
fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
|
fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
} else {
|
||||||
p = lprev(s);
|
p = lprev(s);
|
||||||
if (!ishead(p)) setrext(p, s->flink);
|
if (!ishead(p))
|
||||||
|
setrext(p, s->flink);
|
||||||
p = rprev(s);
|
p = rprev(s);
|
||||||
if (!ishead(p)) setlext(p, s->blink);
|
if (!ishead(p))
|
||||||
|
setlext(p, s->blink);
|
||||||
setblack(s);
|
setblack(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ir) return;
|
if (ir)
|
||||||
|
return;
|
||||||
|
|
||||||
/* Recolor */
|
/* Recolor */
|
||||||
|
|
||||||
@ -395,7 +428,8 @@ void jrb_delete_node(JRB n)
|
|||||||
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))
|
||||||
|
return;
|
||||||
p = n->parent;
|
p = n->parent;
|
||||||
s = sibling(n);
|
s = sibling(n);
|
||||||
}
|
}
|
||||||
@ -407,10 +441,12 @@ void jrb_delete_node(JRB n)
|
|||||||
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,
|
||||||
|
"DELETION ERROR: sibling not internal\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -421,11 +457,15 @@ void jrb_delete_node(JRB n)
|
|||||||
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))
|
||||||
|
setred(s);
|
||||||
|
else
|
||||||
|
setblack(s);
|
||||||
setblack(p);
|
setblack(p);
|
||||||
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
} else if (isblack(x)) { /* Recoloring only (2.3c) */
|
||||||
if (isred(s) || isblack(p)) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
setblack(p);
|
setblack(p);
|
||||||
@ -446,11 +486,14 @@ void jrb_delete_node(JRB n)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_nblack(JRB n)
|
int jrb_nblack(JRB n)
|
||||||
{
|
{
|
||||||
int nb;
|
int nb;
|
||||||
|
|
||||||
if (ishead(n) || isint(n)) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
nb = 0;
|
nb = 0;
|
||||||
@ -461,11 +504,14 @@ int jrb_nblack(JRB n)
|
|||||||
return nb;
|
return nb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_plength(JRB n)
|
int jrb_plength(JRB n)
|
||||||
{
|
{
|
||||||
int pl;
|
int pl;
|
||||||
|
|
||||||
if (ishead(n) || isint(n)) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
pl = 0;
|
pl = 0;
|
||||||
@ -476,26 +522,30 @@ int jrb_plength(JRB n)
|
|||||||
return pl;
|
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,
|
||||||
|
"ERROR: Rb_free_tree called on a non-head node\n");
|
||||||
exit(1);
|
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;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user