diff --git a/qpkg/jrb.c b/qpkg/jrb.c index 103af12..ec212a5 100644 --- a/qpkg/jrb.c +++ b/qpkg/jrb.c @@ -55,9 +55,9 @@ Fax: 865-974-4404 #define isext(n) (!isint(n)) #define ishead(n) (n->roothead & 2) #define isroot(n) (n->roothead & 1) -#define getlext(n) ((struct jrb_node *) (n->key)) +#define getlext(n) ((struct jrb *) (n->key)) #define setlext(node, val) node->key = (void *) (val) -#define getrext(n) ((struct jrb_node *) (n->val)) +#define getrext(n) ((struct jrb *) (n->val)) #define setrext(node, value) node->val = (void *) (value) #define setred(n) n->red = 1 #define setblack(n) n->red = 0 @@ -71,9 +71,10 @@ Fax: 865-974-4404 #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(struct jrb *item, struct jrb *list) + /* Inserts to the end of a list */ { - JRB last_node; + struct jrb *last_node; last_node = list->blink; @@ -84,17 +85,17 @@ static void insert(JRB item, JRB list) /* Inserts to the end of a list */ } -static void delete_item(JRB item) /* Deletes an arbitrary iterm */ +static void delete_item(struct jrb *item) /* Deletes an arbitrary iterm */ { item->flink->blink = item->blink; item->blink->flink = item->flink; } -static void single_rotate(JRB y, int l) +static void single_rotate(struct jrb *y, int l) { int rl = 0 /* for gcc */, ir; - JRB x, yp; + struct jrb *x, *yp; ir = isroot(y); yp = y->parent; @@ -135,9 +136,9 @@ static void single_rotate(JRB y, int l) } -static void recolor(JRB n) +static void recolor(struct jrb *n) { - JRB p, gp, s; + struct jrb *p, *gp, *s; int done = 0; while (!done) { @@ -182,11 +183,11 @@ static void recolor(JRB n) } -static JRB mk_new_ext(void *key, void *val) +static struct jrb *mk_new_ext(void *key, void *val) { - JRB new; + struct jrb *new; - new = (JRB) malloc(sizeof(struct jrb_node)); + new = (struct jrb *) malloc(sizeof(struct jrb)); new->val = val; new->key = key; setext(new); @@ -196,11 +197,11 @@ static JRB mk_new_ext(void *key, void *val) return new; } -static void mk_new_int(JRB l, JRB r, JRB p, int il) +static void mk_new_int(struct jrb *l, struct jrb *r, struct jrb *p, int il) { - JRB newnode; + struct jrb *newnode; - newnode = (JRB) malloc(sizeof(struct jrb_node)); + newnode = (struct jrb *) malloc(sizeof(struct jrb)); setint(newnode); setred(newnode); setnormal(newnode); @@ -227,7 +228,7 @@ static void mk_new_int(JRB l, JRB r, JRB p, int il) } -JRB lprev(JRB n) +struct jrb *lprev(struct jrb *n) { if (ishead(n)) return n; @@ -240,7 +241,7 @@ JRB lprev(JRB n) } -JRB rprev(JRB n) +struct jrb *rprev(struct jrb *n) { if (ishead(n)) return n; @@ -253,11 +254,11 @@ JRB rprev(JRB n) } -JRB make_jrb(void) +struct jrb *make_jrb(void) { - JRB head; + struct jrb *head; - head = (JRB) malloc(sizeof(struct jrb_node)); + head = (struct jrb *) malloc(sizeof(struct jrb)); head->flink = head; head->blink = head; head->parent = head; @@ -267,7 +268,7 @@ JRB make_jrb(void) } -JRB jrb_find_gte(JRB n, const void *key, +struct jrb *jrb_find_gte(struct jrb *n, const void *key, int (*fxn)(const void *, const void *), int *fnd) { int cmp; @@ -304,10 +305,11 @@ JRB jrb_find_gte(JRB n, const void *key, } -JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *)) +struct jrb *jrb_find(struct jrb *n, const void *key, + int (*fxn)(const void *a, const void *b)) { int fnd; - JRB j; + struct jrb *j; j = jrb_find_gte(n, key, fxn, &fnd); if (fnd) @@ -317,9 +319,9 @@ JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *)) } -static JRB jrb_insert_b(JRB n, void *key, void *val) +static struct jrb *jrb_insert_b(struct jrb *n, void *key, void *val) { - JRB newleft, newright, newnode, p; + struct jrb *newleft, *newright, *newnode, *p; if (ishead(n)) { if (n->parent == n) { /* Tree is empty */ @@ -354,9 +356,9 @@ static JRB jrb_insert_b(JRB n, void *key, void *val) } -void jrb_delete_node(JRB n) +void jrb_delete_node(struct jrb *n) { - JRB s, p, gp, x, z; + struct jrb *s, *p, *gp, *x, *z; char ir, il; if (isint(n)) { @@ -486,7 +488,7 @@ void jrb_delete_node(JRB n) } -int jrb_nblack(JRB n) +int jrb_nblack(struct jrb *n) { int nb; @@ -504,7 +506,7 @@ int jrb_nblack(JRB n) } -int jrb_plength(JRB n) +int jrb_plength(struct jrb *n) { int pl; @@ -522,7 +524,7 @@ int jrb_plength(JRB n) } -void jrb_free_tree(JRB n) +void jrb_free_tree(struct jrb *n) { if (!ishead(n)) { fprintf(stderr, @@ -537,13 +539,13 @@ void jrb_free_tree(JRB n) } -void *jrb_val(JRB n) +void *jrb_val(struct jrb *n) { return n->val; } -JRB jrb_insert(JRB tree, void *key, void *val, +struct jrb *jrb_insert(struct jrb *tree, void *key, void *val, int (*func)(const void *a, const void *b)) { int fnd; diff --git a/qpkg/jrb.h b/qpkg/jrb.h index 23331e6..b5658e8 100644 --- a/qpkg/jrb.h +++ b/qpkg/jrb.h @@ -49,40 +49,40 @@ Fax: 865-974-4404 */ -typedef struct jrb_node { +struct jrb { unsigned char red; unsigned char internal; unsigned char left; unsigned char roothead; /* (bit 1 is root, bit 2 is head) */ - struct jrb_node *flink; - struct jrb_node *blink; - struct jrb_node *parent; + struct jrb *flink; + struct jrb *blink; + struct jrb *parent; void *key; void *val; -} *JRB; +}; -JRB make_jrb(void); /* Creates a new rb-tree */ +struct jrb *make_jrb(void); /* Creates a new rb-tree */ /* Creates a node with key key and val val and inserts it into the tree. jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=, jrb_insertg uses func() */ -JRB jrb_insert(JRB tree, void *key, void *val, +struct jrb *jrb_insert(struct jrb *tree, void *key, void *val, int (*func)(const void *a, const void *b)); /* returns an external node in t whose value is equal k. Returns NULL if there is no such node in the tree */ -JRB jrb_find(JRB root, const void *key, +struct jrb *jrb_find(struct jrb *root, const void *key, int (*func)(const void *a, const void *b)); /* returns an external node in t whose value is equal k or whose value is the smallest value greater than k. Sets found to 1 if the key was found, and 0 otherwise. */ -JRB jrb_find_gte(JRB root, const void *key, +struct jrb *jrb_find_gte(struct jrb *root, const void *key, int (*func)(const void *a, const void *b), int *found); @@ -90,17 +90,17 @@ JRB jrb_find_gte(JRB root, const void *key, tree before/after node nd. Does not check to ensure that you are keeping the correct order */ -void jrb_delete_node(JRB node); /* Deletes and frees a node (but - not the key or val) */ -void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */ +void jrb_delete_node(struct jrb *node); /* Deletes and frees a node (but + not the key or val) */ +void jrb_free_tree(struct jrb *root); /* Deletes and frees an entire tree */ -void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut - lint up */ +void *jrb_val(struct jrb *node); /* Returns node->v.val -- this is to shut + lint up */ -int jrb_nblack(JRB n); /* returns # of black nodes in path from - n to the root */ -int jrb_plength(JRB n); /* returns the # of nodes in path from - n to the root */ +int jrb_nblack(struct jrb *n); /* returns # of black nodes in path from + n to the root */ +int jrb_plength(struct jrb *n); /* returns the # of nodes in path from + n to the root */ #define jrb_first(n) ((n)->flink) #define jrb_last(n) ((n)->blink) diff --git a/qpkg/rbtest.c b/qpkg/rbtest.c index e0bed09..118832f 100644 --- a/qpkg/rbtest.c +++ b/qpkg/rbtest.c @@ -17,8 +17,8 @@ static int cmp(const void *a, const void *b) int main(void) { - JRB tree = make_jrb(); - JRB p; + struct jrb *tree = make_jrb(); + struct jrb *p; INSERT("ab", "have"); INSERT("ac", "NOT");