Date:2010-11-20 00:48:49 (9 years 21 days ago)
Author:Werner Almesberger
Commit:47abebe364ef42ed3fc3f5fad306477079054c69
Message:qpkg: renamed "struct jrb_node" to "struct jrb" replaced JRB with "struct jrb *"

Files: qpkg/jrb.c (17 diffs)
qpkg/jrb.h (2 diffs)
qpkg/rbtest.c (1 diff)

Change Details

qpkg/jrb.c
5555#define isext(n) (!isint(n))
5656#define ishead(n) (n->roothead & 2)
5757#define isroot(n) (n->roothead & 1)
58#define getlext(n) ((struct jrb_node *) (n->key))
58#define getlext(n) ((struct jrb *) (n->key))
5959#define setlext(node, val) node->key = (void *) (val)
60#define getrext(n) ((struct jrb_node *) (n->val))
60#define getrext(n) ((struct jrb *) (n->val))
6161#define setrext(node, value) node->val = (void *) (value)
6262#define setred(n) n->red = 1
6363#define setblack(n) n->red = 0
...... 
7171#define sibling(n) (isleft(n) ? n->parent->blink : n->parent->flink)
7272
7373
74static void insert(JRB item, JRB list) /* Inserts to the end of a list */
74static void insert(struct jrb *item, struct jrb *list)
75    /* Inserts to the end of a list */
7576{
76    JRB last_node;
77    struct jrb *last_node;
7778
7879    last_node = list->blink;
7980
...... 
8485}
8586
8687
87static void delete_item(JRB item) /* Deletes an arbitrary iterm */
88static void delete_item(struct jrb *item) /* Deletes an arbitrary iterm */
8889{
8990    item->flink->blink = item->blink;
9091    item->blink->flink = item->flink;
9192}
9293
9394
94static void single_rotate(JRB y, int l)
95static void single_rotate(struct jrb *y, int l)
9596{
9697    int rl = 0 /* for gcc */, ir;
97    JRB x, yp;
98    struct jrb *x, *yp;
9899
99100    ir = isroot(y);
100101    yp = y->parent;
...... 
135136}
136137
137138
138static void recolor(JRB n)
139static void recolor(struct jrb *n)
139140{
140    JRB p, gp, s;
141    struct jrb *p, *gp, *s;
141142    int done = 0;
142143
143144    while (!done) {
...... 
182183}
183184
184185
185static JRB mk_new_ext(void *key, void *val)
186static struct jrb *mk_new_ext(void *key, void *val)
186187{
187    JRB new;
188    struct jrb *new;
188189
189    new = (JRB) malloc(sizeof(struct jrb_node));
190    new = (struct jrb *) malloc(sizeof(struct jrb));
190191    new->val = val;
191192    new->key = key;
192193    setext(new);
...... 
196197    return new;
197198}
198199
199static void mk_new_int(JRB l, JRB r, JRB p, int il)
200static void mk_new_int(struct jrb *l, struct jrb *r, struct jrb *p, int il)
200201{
201    JRB newnode;
202    struct jrb *newnode;
202203
203    newnode = (JRB) malloc(sizeof(struct jrb_node));
204    newnode = (struct jrb *) malloc(sizeof(struct jrb));
204205    setint(newnode);
205206    setred(newnode);
206207    setnormal(newnode);
...... 
227228}
228229
229230
230JRB lprev(JRB n)
231struct jrb *lprev(struct jrb *n)
231232{
232233    if (ishead(n))
233234        return n;
...... 
240241}
241242
242243
243JRB rprev(JRB n)
244struct jrb *rprev(struct jrb *n)
244245{
245246    if (ishead(n))
246247        return n;
...... 
253254}
254255
255256
256JRB make_jrb(void)
257struct jrb *make_jrb(void)
257258{
258    JRB head;
259    struct jrb *head;
259260
260    head = (JRB) malloc(sizeof(struct jrb_node));
261    head = (struct jrb *) malloc(sizeof(struct jrb));
261262    head->flink = head;
262263    head->blink = head;
263264    head->parent = head;
...... 
267268}
268269
269270
270JRB jrb_find_gte(JRB n, const void *key,
271struct jrb *jrb_find_gte(struct jrb *n, const void *key,
271272    int (*fxn)(const void *, const void *), int *fnd)
272273{
273274    int cmp;
...... 
304305}
305306
306307
307JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
308struct jrb *jrb_find(struct jrb *n, const void *key,
309    int (*fxn)(const void *a, const void *b))
308310{
309311    int fnd;
310    JRB j;
312    struct jrb *j;
311313
312314    j = jrb_find_gte(n, key, fxn, &fnd);
313315    if (fnd)
...... 
317319}
318320
319321
320static JRB jrb_insert_b(JRB n, void *key, void *val)
322static struct jrb *jrb_insert_b(struct jrb *n, void *key, void *val)
321323{
322    JRB newleft, newright, newnode, p;
324    struct jrb *newleft, *newright, *newnode, *p;
323325
324326    if (ishead(n)) {
325327        if (n->parent == n) { /* Tree is empty */
...... 
354356}
355357
356358
357void jrb_delete_node(JRB n)
359void jrb_delete_node(struct jrb *n)
358360{
359    JRB s, p, gp, x, z;
361    struct jrb *s, *p, *gp, *x, *z;
360362    char ir, il;
361363
362364    if (isint(n)) {
...... 
486488}
487489
488490
489int jrb_nblack(JRB n)
491int jrb_nblack(struct jrb *n)
490492{
491493    int nb;
492494
...... 
504506}
505507
506508
507int jrb_plength(JRB n)
509int jrb_plength(struct jrb *n)
508510{
509511    int pl;
510512
...... 
522524}
523525
524526
525void jrb_free_tree(JRB n)
527void jrb_free_tree(struct jrb *n)
526528{
527529    if (!ishead(n)) {
528530        fprintf(stderr,
...... 
537539}
538540
539541
540void *jrb_val(JRB n)
542void *jrb_val(struct jrb *n)
541543{
542544    return n->val;
543545}
544546
545547
546JRB jrb_insert(JRB tree, void *key, void *val,
548struct jrb *jrb_insert(struct jrb *tree, void *key, void *val,
547549                          int (*func)(const void *a, const void *b))
548550{
549551    int fnd;
qpkg/jrb.h
4949*/
5050
5151
52typedef struct jrb_node {
52struct jrb {
5353    unsigned char red;
5454    unsigned char internal;
5555    unsigned char left;
5656    unsigned char roothead; /* (bit 1 is root, bit 2 is head) */
57    struct jrb_node *flink;
58    struct jrb_node *blink;
59    struct jrb_node *parent;
57    struct jrb *flink;
58    struct jrb *blink;
59    struct jrb *parent;
6060    void *key;
6161    void *val;
62} *JRB;
62};
6363
6464
65JRB make_jrb(void); /* Creates a new rb-tree */
65struct jrb *make_jrb(void); /* Creates a new rb-tree */
6666
6767
6868/* Creates a node with key key and val val and inserts it into the tree.
6969   jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=,
7070   jrb_insertg uses func() */
7171
72JRB jrb_insert(JRB tree, void *key, void *val,
72struct jrb *jrb_insert(struct jrb *tree, void *key, void *val,
7373    int (*func)(const void *a, const void *b));
7474
7575/* returns an external node in t whose value is equal k. Returns NULL if
7676   there is no such node in the tree */
7777
78JRB jrb_find(JRB root, const void *key,
78struct jrb *jrb_find(struct jrb *root, const void *key,
7979    int (*func)(const void *a, const void *b));
8080
8181/* returns an external node in t whose value is equal
8282  k or whose value is the smallest value greater than k. Sets found to
8383  1 if the key was found, and 0 otherwise. */
8484
85JRB jrb_find_gte(JRB root, const void *key,
85struct jrb *jrb_find_gte(struct jrb *root, const void *key,
8686    int (*func)(const void *a, const void *b), int *found);
8787
8888
...... 
9090   tree before/after node nd. Does not check to ensure that you are
9191   keeping the correct order */
9292
93void jrb_delete_node(JRB node); /* Deletes and frees a node (but
94                                    not the key or val) */
95void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */
93void jrb_delete_node(struct jrb *node); /* Deletes and frees a node (but
94                                            not the key or val) */
95void jrb_free_tree(struct jrb *root); /* Deletes and frees an entire tree */
9696
97void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut
98                             lint up */
97void *jrb_val(struct jrb *node); /* Returns node->v.val -- this is to shut
98                                     lint up */
9999
100int jrb_nblack(JRB n); /* returns # of black nodes in path from
101                          n to the root */
102int jrb_plength(JRB n); /* returns the # of nodes in path from
103                 n to the root */
100int jrb_nblack(struct jrb *n); /* returns # of black nodes in path from
101                                  n to the root */
102int jrb_plength(struct jrb *n); /* returns the # of nodes in path from
103                   n to the root */
104104
105105#define jrb_first(n) ((n)->flink)
106106#define jrb_last(n) ((n)->blink)
qpkg/rbtest.c
1717
1818int main(void)
1919{
20    JRB tree = make_jrb();
21    JRB p;
20    struct jrb *tree = make_jrb();
21    struct jrb *p;
2222
2323    INSERT("ab", "have");
2424    INSERT("ac", "NOT");

Archive Download the corresponding diff file

Branches:
master



interactive