mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-22 03:20:37 +02:00
qpkg/jrb.[ch]: remove trailing spaces
This commit is contained in:
parent
a463773c12
commit
7e641d2655
112
qpkg/jrb.c
112
qpkg/jrb.c
@ -38,19 +38,19 @@ Fax: 865-974-4404
|
|||||||
|
|
||||||
/* Original code by Jim Plank (plank@cs.utk.edu) */
|
/* Original code by Jim Plank (plank@cs.utk.edu) */
|
||||||
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
|
/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include "jrb.h"
|
#include "jrb.h"
|
||||||
|
|
||||||
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);
|
||||||
static JRB lprev(JRB n);
|
static JRB lprev(JRB n);
|
||||||
static JRB rprev(JRB n);
|
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)
|
||||||
@ -73,21 +73,21 @@ static void single_rotate(JRB y, int l);
|
|||||||
#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;
|
||||||
@ -108,11 +108,11 @@ static JRB mk_new_ext(void *key, void *val)
|
|||||||
|
|
||||||
return new;
|
return 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);
|
||||||
@ -137,9 +137,9 @@ static void mk_new_int(JRB l, JRB r, JRB p, int il)
|
|||||||
p->blink = newnode;
|
p->blink = newnode;
|
||||||
}
|
}
|
||||||
recolor(newnode);
|
recolor(newnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB lprev(JRB n)
|
JRB lprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n))
|
if (ishead(n))
|
||||||
@ -152,7 +152,7 @@ JRB lprev(JRB n)
|
|||||||
return n->parent;
|
return n->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JRB rprev(JRB n)
|
JRB rprev(JRB n)
|
||||||
{
|
{
|
||||||
if (ishead(n))
|
if (ishead(n))
|
||||||
@ -164,12 +164,12 @@ JRB rprev(JRB n)
|
|||||||
}
|
}
|
||||||
return 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;
|
||||||
@ -178,13 +178,13 @@ JRB make_jrb(void)
|
|||||||
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);
|
||||||
@ -195,10 +195,10 @@ JRB jrb_find_gte(JRB n, const void *key,
|
|||||||
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)
|
if (cmp > 0)
|
||||||
return n;
|
return n;
|
||||||
else
|
else
|
||||||
n = n->parent;
|
n = n->parent;
|
||||||
while (1) {
|
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 *))
|
JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
|
||||||
{
|
{
|
||||||
int fnd;
|
int fnd;
|
||||||
@ -229,11 +229,11 @@ JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
|
|||||||
return NULL;
|
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 */
|
||||||
newnode = mk_new_ext(key, val);
|
newnode = mk_new_ext(key, val);
|
||||||
@ -262,32 +262,32 @@ static JRB jrb_insert_b(JRB n, void *key, void *val)
|
|||||||
p = lprev(newleft);
|
p = lprev(newleft);
|
||||||
if (!ishead(p))
|
if (!ishead(p))
|
||||||
setrext(p, newleft);
|
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;
|
||||||
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))
|
if (isblack(p))
|
||||||
return;
|
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)) {
|
||||||
@ -300,7 +300,7 @@ static void recolor(JRB n)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* 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);
|
||||||
@ -313,33 +313,33 @@ 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;
|
||||||
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) {
|
||||||
@ -362,7 +362,7 @@ void jrb_delete_node(JRB n)
|
|||||||
{
|
{
|
||||||
JRB s, p, gp, x, z;
|
JRB s, p, gp, x, z;
|
||||||
char ir, il;
|
char ir, il;
|
||||||
|
|
||||||
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);
|
||||||
@ -378,7 +378,7 @@ void jrb_delete_node(JRB n)
|
|||||||
p->parent = p;
|
p->parent = p;
|
||||||
free(n);
|
free(n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
s = sibling(n); /* The only node after deletion */
|
s = sibling(n); /* The only node after deletion */
|
||||||
if (isroot(p)) {
|
if (isroot(p)) {
|
||||||
s->parent = p->parent;
|
s->parent = p->parent;
|
||||||
@ -400,9 +400,9 @@ void jrb_delete_node(JRB n)
|
|||||||
ir = isred(p);
|
ir = isred(p);
|
||||||
free(p);
|
free(p);
|
||||||
free(n);
|
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))
|
if (!ishead(p))
|
||||||
setrext(p, s);
|
setrext(p, s);
|
||||||
p = rprev(s);
|
p = rprev(s);
|
||||||
@ -421,16 +421,16 @@ void jrb_delete_node(JRB n)
|
|||||||
setblack(s);
|
setblack(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ir)
|
if (ir)
|
||||||
return;
|
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;
|
||||||
@ -439,23 +439,23 @@ void jrb_delete_node(JRB n)
|
|||||||
p = n->parent;
|
p = n->parent;
|
||||||
s = sibling(n);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -489,7 +489,7 @@ void jrb_delete_node(JRB n)
|
|||||||
setblack(x);
|
setblack(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_nblack(JRB n)
|
int jrb_nblack(JRB n)
|
||||||
{
|
{
|
||||||
int nb;
|
int nb;
|
||||||
@ -507,7 +507,7 @@ int jrb_nblack(JRB n)
|
|||||||
return nb;
|
return nb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int jrb_plength(JRB n)
|
int jrb_plength(JRB n)
|
||||||
{
|
{
|
||||||
int pl;
|
int pl;
|
||||||
@ -525,7 +525,7 @@ 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)) {
|
||||||
@ -533,23 +533,23 @@ void jrb_free_tree(JRB n)
|
|||||||
"ERROR: Rb_free_tree called on a non-head node\n");
|
"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 *a, const void *b))
|
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);
|
||||||
|
16
qpkg/jrb.h
16
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
|
k or whose value is the smallest value greater than k. Sets found to
|
||||||
1 if the key was found, and 0 otherwise. */
|
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);
|
int (*func)(const void *a, const void *b), int *found);
|
||||||
|
|
||||||
|
|
||||||
/* Creates a node with key key and val val and inserts it into the
|
/* 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
|
tree before/after node nd. Does not check to ensure that you are
|
||||||
keeping the correct order */
|
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) */
|
not the key or val) */
|
||||||
void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */
|
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 */
|
n to the root */
|
||||||
int jrb_plength(JRB n); /* returns the # of nodes in path from
|
int jrb_plength(JRB n); /* returns the # of nodes in path from
|
||||||
n to the root */
|
n to the root */
|
||||||
|
|
||||||
#define jrb_first(n) ((n)->flink)
|
#define jrb_first(n) ((n)->flink)
|
||||||
#define jrb_last(n) ((n)->blink)
|
#define jrb_last(n) ((n)->blink)
|
||||||
#define jrb_next(n) ((n)->flink)
|
#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
|
#ifndef jrb_nil
|
||||||
#define jrb_nil(t) (t)
|
#define jrb_nil(t) (t)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define jrb_traverse(ptr, lst) \
|
#define jrb_traverse(ptr, lst) \
|
||||||
for (ptr = jrb_first(lst); ptr != jrb_nil(lst); ptr = jrb_next(ptr))
|
for (ptr = jrb_first(lst); ptr != jrb_nil(lst); ptr = jrb_next(ptr))
|
||||||
|
|
||||||
#define jrb_rtraverse(ptr, lst) \
|
#define jrb_rtraverse(ptr, lst) \
|
||||||
for (ptr = jrb_last(lst); ptr != jrb_nil(lst); ptr = jrb_prev(ptr))
|
for (ptr = jrb_last(lst); ptr != jrb_nil(lst); ptr = jrb_prev(ptr))
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user