Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Commit Details
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 | ||
---|---|---|
55 | 55 | #define isext(n) (!isint(n)) |
56 | 56 | #define ishead(n) (n->roothead & 2) |
57 | 57 | #define isroot(n) (n->roothead & 1) |
58 | #define getlext(n) ((struct jrb_node *) (n->key)) | |
58 | #define getlext(n) ((struct jrb *) (n->key)) | |
59 | 59 | #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)) | |
61 | 61 | #define setrext(node, value) node->val = (void *) (value) |
62 | 62 | #define setred(n) n->red = 1 |
63 | 63 | #define setblack(n) n->red = 0 |
... | ... | |
71 | 71 | #define sibling(n) (isleft(n) ? n->parent->blink : n->parent->flink) |
72 | 72 | |
73 | 73 | |
74 | static void insert(JRB item, JRB list) /* Inserts to the end of a list */ | |
74 | static void insert(struct jrb *item, struct jrb *list) | |
75 | /* Inserts to the end of a list */ | |
75 | 76 | { |
76 | JRB last_node; | |
77 | struct jrb *last_node; | |
77 | 78 | |
78 | 79 | last_node = list->blink; |
79 | 80 | |
... | ... | |
84 | 85 | } |
85 | 86 | |
86 | 87 | |
87 | static void delete_item(JRB item) /* Deletes an arbitrary iterm */ | |
88 | static void delete_item(struct jrb *item) /* Deletes an arbitrary iterm */ | |
88 | 89 | { |
89 | 90 | item->flink->blink = item->blink; |
90 | 91 | item->blink->flink = item->flink; |
91 | 92 | } |
92 | 93 | |
93 | 94 | |
94 | static void single_rotate(JRB y, int l) | |
95 | static void single_rotate(struct jrb *y, int l) | |
95 | 96 | { |
96 | 97 | int rl = 0 /* for gcc */, ir; |
97 | JRB x, yp; | |
98 | struct jrb *x, *yp; | |
98 | 99 | |
99 | 100 | ir = isroot(y); |
100 | 101 | yp = y->parent; |
... | ... | |
135 | 136 | } |
136 | 137 | |
137 | 138 | |
138 | static void recolor(JRB n) | |
139 | static void recolor(struct jrb *n) | |
139 | 140 | { |
140 | JRB p, gp, s; | |
141 | struct jrb *p, *gp, *s; | |
141 | 142 | int done = 0; |
142 | 143 | |
143 | 144 | while (!done) { |
... | ... | |
182 | 183 | } |
183 | 184 | |
184 | 185 | |
185 | static JRB mk_new_ext(void *key, void *val) | |
186 | static struct jrb *mk_new_ext(void *key, void *val) | |
186 | 187 | { |
187 | JRB new; | |
188 | struct jrb *new; | |
188 | 189 | |
189 | new = (JRB) malloc(sizeof(struct jrb_node)); | |
190 | new = (struct jrb *) malloc(sizeof(struct jrb)); | |
190 | 191 | new->val = val; |
191 | 192 | new->key = key; |
192 | 193 | setext(new); |
... | ... | |
196 | 197 | return new; |
197 | 198 | } |
198 | 199 | |
199 | static void mk_new_int(JRB l, JRB r, JRB p, int il) | |
200 | static void mk_new_int(struct jrb *l, struct jrb *r, struct jrb *p, int il) | |
200 | 201 | { |
201 | JRB newnode; | |
202 | struct jrb *newnode; | |
202 | 203 | |
203 | newnode = (JRB) malloc(sizeof(struct jrb_node)); | |
204 | newnode = (struct jrb *) malloc(sizeof(struct jrb)); | |
204 | 205 | setint(newnode); |
205 | 206 | setred(newnode); |
206 | 207 | setnormal(newnode); |
... | ... | |
227 | 228 | } |
228 | 229 | |
229 | 230 | |
230 | JRB lprev(JRB n) | |
231 | struct jrb *lprev(struct jrb *n) | |
231 | 232 | { |
232 | 233 | if (ishead(n)) |
233 | 234 | return n; |
... | ... | |
240 | 241 | } |
241 | 242 | |
242 | 243 | |
243 | JRB rprev(JRB n) | |
244 | struct jrb *rprev(struct jrb *n) | |
244 | 245 | { |
245 | 246 | if (ishead(n)) |
246 | 247 | return n; |
... | ... | |
253 | 254 | } |
254 | 255 | |
255 | 256 | |
256 | JRB make_jrb(void) | |
257 | struct jrb *make_jrb(void) | |
257 | 258 | { |
258 | JRB head; | |
259 | struct jrb *head; | |
259 | 260 | |
260 | head = (JRB) malloc(sizeof(struct jrb_node)); | |
261 | head = (struct jrb *) malloc(sizeof(struct jrb)); | |
261 | 262 | head->flink = head; |
262 | 263 | head->blink = head; |
263 | 264 | head->parent = head; |
... | ... | |
267 | 268 | } |
268 | 269 | |
269 | 270 | |
270 | JRB jrb_find_gte(JRB n, const void *key, | |
271 | struct jrb *jrb_find_gte(struct jrb *n, const void *key, | |
271 | 272 | int (*fxn)(const void *, const void *), int *fnd) |
272 | 273 | { |
273 | 274 | int cmp; |
... | ... | |
304 | 305 | } |
305 | 306 | |
306 | 307 | |
307 | JRB jrb_find(JRB n, const void *key, int (*fxn)(const void *, const void *)) | |
308 | struct jrb *jrb_find(struct jrb *n, const void *key, | |
309 | int (*fxn)(const void *a, const void *b)) | |
308 | 310 | { |
309 | 311 | int fnd; |
310 | JRB j; | |
312 | struct jrb *j; | |
311 | 313 | |
312 | 314 | j = jrb_find_gte(n, key, fxn, &fnd); |
313 | 315 | if (fnd) |
... | ... | |
317 | 319 | } |
318 | 320 | |
319 | 321 | |
320 | static JRB jrb_insert_b(JRB n, void *key, void *val) | |
322 | static struct jrb *jrb_insert_b(struct jrb *n, void *key, void *val) | |
321 | 323 | { |
322 | JRB newleft, newright, newnode, p; | |
324 | struct jrb *newleft, *newright, *newnode, *p; | |
323 | 325 | |
324 | 326 | if (ishead(n)) { |
325 | 327 | if (n->parent == n) { /* Tree is empty */ |
... | ... | |
354 | 356 | } |
355 | 357 | |
356 | 358 | |
357 | void jrb_delete_node(JRB n) | |
359 | void jrb_delete_node(struct jrb *n) | |
358 | 360 | { |
359 | JRB s, p, gp, x, z; | |
361 | struct jrb *s, *p, *gp, *x, *z; | |
360 | 362 | char ir, il; |
361 | 363 | |
362 | 364 | if (isint(n)) { |
... | ... | |
486 | 488 | } |
487 | 489 | |
488 | 490 | |
489 | int jrb_nblack(JRB n) | |
491 | int jrb_nblack(struct jrb *n) | |
490 | 492 | { |
491 | 493 | int nb; |
492 | 494 | |
... | ... | |
504 | 506 | } |
505 | 507 | |
506 | 508 | |
507 | int jrb_plength(JRB n) | |
509 | int jrb_plength(struct jrb *n) | |
508 | 510 | { |
509 | 511 | int pl; |
510 | 512 | |
... | ... | |
522 | 524 | } |
523 | 525 | |
524 | 526 | |
525 | void jrb_free_tree(JRB n) | |
527 | void jrb_free_tree(struct jrb *n) | |
526 | 528 | { |
527 | 529 | if (!ishead(n)) { |
528 | 530 | fprintf(stderr, |
... | ... | |
537 | 539 | } |
538 | 540 | |
539 | 541 | |
540 | void *jrb_val(JRB n) | |
542 | void *jrb_val(struct jrb *n) | |
541 | 543 | { |
542 | 544 | return n->val; |
543 | 545 | } |
544 | 546 | |
545 | 547 | |
546 | JRB jrb_insert(JRB tree, void *key, void *val, | |
548 | struct jrb *jrb_insert(struct jrb *tree, void *key, void *val, | |
547 | 549 | int (*func)(const void *a, const void *b)) |
548 | 550 | { |
549 | 551 | int fnd; |
qpkg/jrb.h | ||
---|---|---|
49 | 49 | */ |
50 | 50 | |
51 | 51 | |
52 | typedef struct jrb_node { | |
52 | struct jrb { | |
53 | 53 | unsigned char red; |
54 | 54 | unsigned char internal; |
55 | 55 | unsigned char left; |
56 | 56 | 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; | |
60 | 60 | void *key; |
61 | 61 | void *val; |
62 | } *JRB; | |
62 | }; | |
63 | 63 | |
64 | 64 | |
65 | JRB make_jrb(void); /* Creates a new rb-tree */ | |
65 | struct jrb *make_jrb(void); /* Creates a new rb-tree */ | |
66 | 66 | |
67 | 67 | |
68 | 68 | /* Creates a node with key key and val val and inserts it into the tree. |
69 | 69 | jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=, |
70 | 70 | jrb_insertg uses func() */ |
71 | 71 | |
72 | JRB jrb_insert(JRB tree, void *key, void *val, | |
72 | struct jrb *jrb_insert(struct jrb *tree, void *key, void *val, | |
73 | 73 | int (*func)(const void *a, const void *b)); |
74 | 74 | |
75 | 75 | /* returns an external node in t whose value is equal k. Returns NULL if |
76 | 76 | there is no such node in the tree */ |
77 | 77 | |
78 | JRB jrb_find(JRB root, const void *key, | |
78 | struct jrb *jrb_find(struct jrb *root, const void *key, | |
79 | 79 | int (*func)(const void *a, const void *b)); |
80 | 80 | |
81 | 81 | /* returns an external node in t whose value is equal |
82 | 82 | k or whose value is the smallest value greater than k. Sets found to |
83 | 83 | 1 if the key was found, and 0 otherwise. */ |
84 | 84 | |
85 | JRB jrb_find_gte(JRB root, const void *key, | |
85 | struct jrb *jrb_find_gte(struct jrb *root, const void *key, | |
86 | 86 | int (*func)(const void *a, const void *b), int *found); |
87 | 87 | |
88 | 88 | |
... | ... | |
90 | 90 | tree before/after node nd. Does not check to ensure that you are |
91 | 91 | keeping the correct order */ |
92 | 92 | |
93 | void jrb_delete_node(JRB node); /* Deletes and frees a node (but | |
94 | not the key or val) */ | |
95 | void jrb_free_tree(JRB root); /* Deletes and frees an entire tree */ | |
93 | void jrb_delete_node(struct jrb *node); /* Deletes and frees a node (but | |
94 | not the key or val) */ | |
95 | void jrb_free_tree(struct jrb *root); /* Deletes and frees an entire tree */ | |
96 | 96 | |
97 | void *jrb_val(JRB node); /* Returns node->v.val -- this is to shut | |
98 | lint up */ | |
97 | void *jrb_val(struct jrb *node); /* Returns node->v.val -- this is to shut | |
98 | lint up */ | |
99 | 99 | |
100 | int jrb_nblack(JRB n); /* returns # of black nodes in path from | |
101 | n to the root */ | |
102 | int jrb_plength(JRB n); /* returns the # of nodes in path from | |
103 | n to the root */ | |
100 | int jrb_nblack(struct jrb *n); /* returns # of black nodes in path from | |
101 | n to the root */ | |
102 | int jrb_plength(struct jrb *n); /* returns the # of nodes in path from | |
103 | n to the root */ | |
104 | 104 | |
105 | 105 | #define jrb_first(n) ((n)->flink) |
106 | 106 | #define jrb_last(n) ((n)->blink) |
qpkg/rbtest.c | ||
---|---|---|
17 | 17 | |
18 | 18 | int main(void) |
19 | 19 | { |
20 | JRB tree = make_jrb(); | |
21 | JRB p; | |
20 | struct jrb *tree = make_jrb(); | |
21 | struct jrb *p; | |
22 | 22 | |
23 | 23 | INSERT("ab", "have"); |
24 | 24 | INSERT("ac", "NOT"); |
Branches:
master