Date:2010-11-20 00:39:41 (8 years 7 months ago)
Author:Werner Almesberger
Commit:da7a6feb4b5343b52ac15171f9a9dcaff4d0cb96
Message:qpkg/jrb.c: reordered recolor and single_rotate and removed prototypes

Files: qpkg/jrb.c (3 diffs)

Change Details

qpkg/jrb.c
4646#include <ctype.h>
4747#include "jrb.h"
4848
49static void mk_new_int(JRB l, JRB r, JRB p, int il);
50static JRB lprev(JRB n);
51static JRB rprev(JRB n);
52static void recolor(JRB n);
53static void single_rotate(JRB y, int l);
5449
5550#define isred(n) (n->red)
5651#define isblack(n) (!isred(n))
...... 
9691}
9792
9893
94static void single_rotate(JRB y, int l)
95{
96    int rl = 0 /* for gcc */, ir;
97    JRB x, yp;
98
99    ir = isroot(y);
100    yp = y->parent;
101    if (!ir)
102        rl = isleft(y);
103
104     if (l) {
105        x = y->flink;
106        y->flink = x->blink;
107        setleft(y->flink);
108        y->flink->parent = y;
109        x->blink = y;
110        setright(y);
111    } else {
112        x = y->blink;
113        y->blink = x->flink;
114        setright(y->blink);
115        y->blink->parent = y;
116        x->flink = y;
117        setleft(y);
118    }
119
120    x->parent = yp;
121    y->parent = x;
122    if (ir) {
123        yp->parent = x;
124        setnormal(y);
125        setroot(x);
126    } else {
127        if (rl) {
128            yp->flink = x;
129            setleft(x);
130        } else {
131            yp->blink = x;
132            setright(x);
133        }
134    }
135}
136
137
138static void recolor(JRB n)
139{
140    JRB p, gp, s;
141    int done = 0;
142
143    while (!done) {
144        if (isroot(n)) {
145            setblack(n);
146            return;
147        }
148
149        p = n->parent;
150
151        if (isblack(p))
152            return;
153
154        if (isroot(p)) {
155            setblack(p);
156            return;
157        }
158
159        gp = p->parent;
160        s = sibling(p);
161        if (isred(s)) {
162            setblack(p);
163            setred(gp);
164            setblack(s);
165            n = gp;
166        } else {
167            done = 1;
168        }
169    }
170    /* p's sibling is black, p is red, gp is black */
171
172    if ((isleft(n) == 0) == (isleft(p) == 0)) {
173        single_rotate(gp, isleft(n));
174        setblack(p);
175        setred(gp);
176    } else {
177        single_rotate(p, isleft(n));
178        single_rotate(gp, isleft(n));
179        setblack(n);
180        setred(gp);
181    }
182}
183
184
99185static JRB mk_new_ext(void *key, void *val)
100186{
101187    JRB new;
...... 
268354}
269355
270356
271static void recolor(JRB n)
272{
273    JRB p, gp, s;
274    int done = 0;
275
276    while (!done) {
277        if (isroot(n)) {
278            setblack(n);
279            return;
280        }
281
282        p = n->parent;
283
284        if (isblack(p))
285            return;
286
287        if (isroot(p)) {
288            setblack(p);
289            return;
290        }
291
292        gp = p->parent;
293        s = sibling(p);
294        if (isred(s)) {
295            setblack(p);
296            setred(gp);
297            setblack(s);
298            n = gp;
299        } else {
300            done = 1;
301        }
302    }
303    /* p's sibling is black, p is red, gp is black */
304
305    if ((isleft(n) == 0) == (isleft(p) == 0)) {
306        single_rotate(gp, isleft(n));
307        setblack(p);
308        setred(gp);
309    } else {
310        single_rotate(p, isleft(n));
311        single_rotate(gp, isleft(n));
312        setblack(n);
313        setred(gp);
314    }
315}
316
317
318static void single_rotate(JRB y, int l)
319{
320    int rl = 0 /* for gcc */, ir;
321    JRB x, yp;
322
323    ir = isroot(y);
324    yp = y->parent;
325    if (!ir)
326        rl = isleft(y);
327
328     if (l) {
329        x = y->flink;
330        y->flink = x->blink;
331        setleft(y->flink);
332        y->flink->parent = y;
333        x->blink = y;
334        setright(y);
335    } else {
336        x = y->blink;
337        y->blink = x->flink;
338        setright(y->blink);
339        y->blink->parent = y;
340        x->flink = y;
341        setleft(y);
342    }
343
344    x->parent = yp;
345    y->parent = x;
346    if (ir) {
347        yp->parent = x;
348        setnormal(y);
349        setroot(x);
350    } else {
351        if (rl) {
352            yp->flink = x;
353            setleft(x);
354        } else {
355            yp->blink = x;
356            setright(x);
357        }
358    }
359}
360
361
362357void jrb_delete_node(JRB n)
363358{
364359    JRB s, p, gp, x, z;

Archive Download the corresponding diff file

Branches:
master



interactive