Date:2010-11-20 00:35:43 (8 years 9 months ago)
Author:Werner Almesberger
Commit:7e641d2655b767e7282a05959a19ac0d54542323
Message:qpkg/jrb.[ch]: remove trailing spaces

Files: qpkg/jrb.c (22 diffs)
qpkg/jrb.h (3 diffs)

Change Details

qpkg/jrb.c
3838
3939/* Original code by Jim Plank (plank@cs.utk.edu) */
4040/* modified for THINK C 6.0 for Macintosh by Chris Bartley */
41
41
4242#include <string.h>
4343#include <stdio.h>
4444#include <stdlib.h>
4545#include <ctype.h>
4646#include "jrb.h"
47
47
4848static void mk_new_int(JRB l, JRB r, JRB p, int il);
4949static JRB lprev(JRB n);
5050static JRB rprev(JRB n);
5151static void recolor(JRB n);
5252static void single_rotate(JRB y, int l);
53
53
5454#define isred(n) (n->red)
5555#define isblack(n) (!isred(n))
5656#define isleft(n) (n->left)
...... 
7373#define setext(n) n->internal = 0
7474#define setnormal(n) n->roothead = 0
7575#define sibling(n) (isleft(n) ? n->parent->blink : n->parent->flink)
76
76
7777
7878static void insert(JRB item, JRB list) /* Inserts to the end of a list */
7979{
8080    JRB last_node;
81
81
8282    last_node = list->blink;
83
83
8484    list->blink = item;
8585    last_node->flink = item;
8686    item->blink = last_node;
8787    item->flink = list;
8888}
8989
90
90
9191static void delete_item(JRB item) /* Deletes an arbitrary iterm */
9292{
9393    item->flink->blink = item->blink;
...... 
108108
109109    return new;
110110}
111
111
112112static void mk_new_int(JRB l, JRB r, JRB p, int il)
113113{
114114    JRB newnode;
115
115
116116    newnode = (JRB) malloc(sizeof(struct jrb_node));
117117    setint(newnode);
118118    setred(newnode);
...... 
137137        p->blink = newnode;
138138    }
139139    recolor(newnode);
140}
141
142
140}
141
142
143143JRB lprev(JRB n)
144144{
145145    if (ishead(n))
...... 
152152    return n->parent;
153153}
154154
155
155
156156JRB rprev(JRB n)
157157{
158158    if (ishead(n))
...... 
164164    }
165165    return n->parent;
166166}
167
167
168168
169169JRB make_jrb(void)
170170{
171171    JRB head;
172
172
173173    head = (JRB) malloc(sizeof(struct jrb_node));
174174    head->flink = head;
175175    head->blink = head;
...... 
178178    sethead(head);
179179    return head;
180180}
181
181
182182
183183JRB jrb_find_gte(JRB n, const void *key,
184184    int (*fxn)(const void *, const void *), int *fnd)
185185{
186186    int cmp;
187
187
188188    *fnd = 0;
189189    if (!ishead(n)) {
190190        fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n);
...... 
195195    cmp = (*fxn)(key, n->blink->key);
196196    if (cmp == 0) {
197197        *fnd = 1;
198        return n->blink;
198        return n->blink;
199199    }
200200    if (cmp > 0)
201        return n;
201        return n;
202202    else
203203        n = n->parent;
204204    while (1) {
...... 
216216    }
217217}
218218
219
219
220220JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *))
221221{
222222    int fnd;
...... 
229229        return NULL;
230230}
231231
232
232
233233static JRB jrb_insert_b(JRB n, void *key, void *val)
234234{
235235    JRB newleft, newright, newnode, p;
236
236
237237    if (ishead(n)) {
238238        if (n->parent == n) { /* Tree is empty */
239239            newnode = mk_new_ext(key, val);
...... 
262262        p = lprev(newleft);
263263        if (!ishead(p))
264264            setrext(p, newleft);
265        return newleft;
265        return newleft;
266266    }
267267}
268268
269
269
270270static void recolor(JRB n)
271{
271{
272272    JRB p, gp, s;
273273    int done = 0;
274
274
275275    while (!done) {
276276        if (isroot(n)) {
277277            setblack(n);
278278            return;
279279        }
280
280
281281        p = n->parent;
282
282
283283        if (isblack(p))
284284            return;
285
285
286286        if (isroot(p)) {
287287            setblack(p);
288288            return;
289289        }
290
290
291291        gp = p->parent;
292292        s = sibling(p);
293293        if (isred(s)) {
...... 
300300        }
301301    }
302302    /* p's sibling is black, p is red, gp is black */
303
303
304304    if ((isleft(n) == 0) == (isleft(p) == 0)) {
305305        single_rotate(gp, isleft(n));
306306        setblack(p);
...... 
313313    }
314314}
315315
316
316
317317static void single_rotate(JRB y, int l)
318318{
319319    int rl = 0 /* for gcc */, ir;
320320    JRB x, yp;
321
321
322322    ir = isroot(y);
323323    yp = y->parent;
324324    if (!ir)
325325        rl = isleft(y);
326
326
327327     if (l) {
328328        x = y->flink;
329329        y->flink = x->blink;
330330        setleft(y->flink);
331331        y->flink->parent = y;
332332        x->blink = y;
333        setright(y);
333        setright(y);
334334    } else {
335335        x = y->blink;
336336        y->blink = x->flink;
337337        setright(y->blink);
338338        y->blink->parent = y;
339339        x->flink = y;
340        setleft(y);
340        setleft(y);
341341    }
342
342
343343    x->parent = yp;
344344    y->parent = x;
345345    if (ir) {
...... 
362362{
363363    JRB s, p, gp, x, z;
364364    char ir, il;
365
365
366366    if (isint(n)) {
367367        fprintf(stderr, "Cannot delete an internal node: %p\n", n);
368368        exit(1);
...... 
378378        p->parent = p;
379379        free(n);
380380        return;
381    }
381    }
382382    s = sibling(n); /* The only node after deletion */
383383    if (isroot(p)) {
384384        s->parent = p->parent;
...... 
400400    ir = isred(p);
401401    free(p);
402402    free(n);
403
403
404404    if (isext(s)) { /* Update proper rext and lext values */
405        p = lprev(s);
405        p = lprev(s);
406406        if (!ishead(p))
407407            setrext(p, s);
408408        p = rprev(s);
...... 
421421        setblack(s);
422422        return;
423423    }
424
424
425425    if (ir)
426426        return;
427
427
428428    /* Recolor */
429
429
430430    n = s;
431431    p = n->parent;
432432    s = sibling(n);
433    while (isblack(p) && isblack(s) && isint(s) &&
433    while (isblack(p) && isblack(s) && isint(s) &&
434434        isblack(s->flink) && isblack(s->blink)) {
435435        setred(s);
436436        n = p;
...... 
439439        p = n->parent;
440440        s = sibling(n);
441441    }
442
442
443443    if (isblack(p) && isred(s)) { /* Rotation 2.3b */
444444        single_rotate(p, isright(n));
445445        setred(p);
446446        setblack(s);
447447        s = sibling(n);
448448    }
449
449
450450    if (isext(s)) {
451451        fprintf(stderr, "DELETION ERROR: sibling not internal\n");
452452        exit(1);
453453    }
454
454
455455    il = isleft(n);
456456    x = il ? s->flink : s->blink;
457457    z = sibling(x);
458
458
459459    if (isred(z)) { /* Rotation 2.3f */
460460        single_rotate(p, !il);
461461        setblack(z);
...... 
489489    setblack(x);
490490}
491491
492
492
493493int jrb_nblack(JRB n)
494494{
495495    int nb;
...... 
507507    return nb;
508508}
509509
510
510
511511int jrb_plength(JRB n)
512512{
513513    int pl;
...... 
525525    return pl;
526526}
527527
528
528
529529void jrb_free_tree(JRB n)
530530{
531531    if (!ishead(n)) {
...... 
533533            "ERROR: Rb_free_tree called on a non-head node\n");
534534        exit(1);
535535    }
536
536
537537    while (jrb_first(n) != jrb_nil(n))
538538        jrb_delete_node(jrb_first(n));
539539
540540    free(n);
541541}
542542
543
543
544544void *jrb_val(JRB n)
545545{
546546    return n->val;
547547}
548548
549
549
550550JRB jrb_insert(JRB tree, void *key, void *val,
551551                          int (*func)(const void *a, const void *b))
552{
552{
553553    int fnd;
554554
555555    return jrb_insert_b(jrb_find_gte(tree, key, func, &fnd), key, val);
qpkg/jrb.h
7878  k or whose value is the smallest value greater than k. Sets found to
7979  1 if the key was found, and 0 otherwise. */
8080
81JRB jrb_find_gte(JRB root, const void *key,
81JRB jrb_find_gte(JRB root, const void *key,
8282    int (*func)(const void *a, const void *b), int *found);
8383
8484
85/* Creates a node with key key and val val and inserts it into the
86   tree before/after node nd. Does not check to ensure that you are
85/* Creates a node with key key and val val and inserts it into the
86   tree before/after node nd. Does not check to ensure that you are
8787   keeping the correct order */
8888
89void jrb_delete_node(JRB node); /* Deletes and frees a node (but
89void jrb_delete_node(JRB node); /* Deletes and frees a node (but
9090                                    not the key or val) */
9191void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */
9292
...... 
9797                          n to the root */
9898int jrb_plength(JRB n); /* returns the # of nodes in path from
9999                 n to the root */
100
100
101101#define jrb_first(n) ((n)->flink)
102102#define jrb_last(n) ((n)->blink)
103103#define jrb_next(n) ((n)->flink)
...... 
106106#ifndef jrb_nil
107107#define jrb_nil(t) (t)
108108#endif
109
109
110110#define jrb_traverse(ptr, lst) \
111111    for (ptr = jrb_first(lst); ptr != jrb_nil(lst); ptr = jrb_next(ptr))
112
112
113113#define jrb_rtraverse(ptr, lst) \
114114    for (ptr = jrb_last(lst); ptr != jrb_nil(lst); ptr = jrb_prev(ptr))
115
115
116116#endif

Archive Download the corresponding diff file

Branches:
master



interactive