Mercurial > hg > GearsTemplate
annotate src/parallel_execution/rb_tree.c @ 131:a4507906938c
Fix compile error but not work
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 25 Oct 2016 00:49:28 +0900 |
parents | 2bb5e4f0fd35 |
children | 7c309e1aea73 |
rev | line source |
---|---|
86 | 1 #include <stdio.h> |
2 | |
3 #include "context.h" | |
4 #include "origin_cs.h" | |
5 | |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
6 extern union Data* allocator(struct Context* context); |
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
7 extern enum Relational compare(struct Node* node1, struct Node* node2); |
86 | 8 |
126 | 9 void printTree1(union Data* data) { |
10 struct Node* node = (struct Node*)data; | |
11 if (node == NULL) { | |
12 printf("NULL"); | |
13 } else { | |
14 printf("key = %d (", node->key); | |
15 printTree1((union Data*)node->right); | |
16 printf("), ("); | |
17 printTree1((union Data*)node->left); | |
18 printf(")"); | |
19 } | |
20 } | |
21 | |
22 void printTree(union Data* data) { | |
23 printTree1(data); | |
24 printf("\n"); | |
25 } | |
86 | 26 |
130 | 27 __code put(struct Context* context, struct Stack* nodeStack, struct Tree* tree, struct Node* node, struct Traverse* traverse, struct Node* root, struct Node* newNode) { |
126 | 28 printTree((union Data*)tree->root); |
122 | 29 traverse->newNode = newNode; |
30 tree->root = newNode; // this should done at stackClear | |
86 | 31 if (root) { |
91 | 32 traverse->current = root; |
117 | 33 // traverse->result=compare(...) |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
34 // traverse->result = traverse->compare(traverse->current, node); |
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
35 traverse->result = compare(traverse->current, node); |
130 | 36 nodeStack->stack = traverse->nodeStack; |
37 nodeStack->data = (union Data*)newNode; | |
38 nodeStack->next = Replace1; | |
39 goto meta(context, nodeStack->push); | |
40 // goto traverse->nodeStack->push(newNode, replaceNode1, traverse, node); | |
86 | 41 } |
42 | |
43 goto meta(context, Insert); | |
44 } | |
45 | |
46 __code put_stub(struct Context* context) { | |
121 | 47 struct Allocate* allocate = &context->data[Allocate]->allocate; |
48 allocate->size = sizeof(struct Node); | |
49 allocator(context); | |
50 | |
91 | 51 goto put(context, |
130 | 52 &context->data[Stack]->stack, |
91 | 53 &context->data[Tree]->tree, |
124 | 54 &context->data[Node]->node, |
91 | 55 &context->data[Traverse]->traverse, |
56 context->data[Tree]->tree.root, | |
124 | 57 &context->data[context->dataNum]->node |
58 ); | |
121 | 59 } |
60 | |
61 __code replaceNode(struct Context* context, struct Traverse* traverse, struct Node* oldNode, struct Node* newNode, struct Element* element) { | |
62 *newNode = *oldNode; | |
124 | 63 element->next = traverse->nodeStack; |
64 element->data = (union Data* )newNode; | |
65 traverse->nodeStack = element; | |
122 | 66 // goto replaceNode1(struct Traverse* traverse, struct Node* node, struct Node* oldNode, struct Node* newNode, +struct Node* newnewNode, int result) |
121 | 67 goto meta(context, Replace1); |
86 | 68 } |
69 | |
121 | 70 __code replaceNode_stub(struct Context* context) { |
71 struct Allocate* allocate = &context->data[Allocate]->allocate; | |
72 allocate->size = sizeof(struct Element); | |
73 allocator(context); | |
124 | 74 struct Element* element = &context->data[context->dataNum]->element; |
121 | 75 goto replaceNode(context, |
76 &context->data[Traverse]->traverse, | |
77 context->data[Traverse]->traverse.current, | |
122 | 78 context->data[Traverse]->traverse.newNode, |
121 | 79 element); |
80 } | |
81 | |
82 __code replaceNode1(struct Context* context, struct Traverse* traverse, struct Node* node, struct Node* oldNode, struct Node* newNode, struct Node* newnewNode, int result) { | |
86 | 83 if (result == EQ) { |
121 | 84 newNode->value = node->value; |
117 | 85 // go to stack clear |
86 | 86 goto meta(context, context->next); |
87 } else if (result == GT) { | |
91 | 88 traverse->current = oldNode->right; |
121 | 89 newNode->right = newnewNode; |
86 | 90 } else { |
91 | 91 traverse->current = oldNode->left; |
121 | 92 newNode->left = newnewNode; |
86 | 93 } |
122 | 94 traverse->newNode = newnewNode; |
91 | 95 if (traverse->current) { |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
96 // compare(context, traverse, traverse->current->key, node->key); |
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
97 traverse->result = compare(traverse->current, node); |
86 | 98 goto meta(context, Replace); |
99 } | |
100 | |
101 goto meta(context, Insert); | |
121 | 102 |
86 | 103 } |
104 | |
121 | 105 __code replaceNode1_stub(struct Context* context) { |
106 struct Allocate* allocate = &context->data[Allocate]->allocate; | |
107 allocate->size = sizeof(struct Node); | |
108 allocator(context); | |
109 struct Node* newnewNode = &context->data[context->dataNum]->node; | |
110 goto replaceNode1(context, | |
91 | 111 &context->data[Traverse]->traverse, |
124 | 112 &context->data[Node]->node, |
91 | 113 context->data[Traverse]->traverse.current, |
124 | 114 &context->data[Traverse]->traverse.nodeStack->data->node, |
115 newnewNode, | |
91 | 116 context->data[Traverse]->traverse.result); |
86 | 117 } |
118 | |
127 | 119 __code insertNode(struct Context* context, struct Traverse* traverse, struct Node* node, struct Node* newNode) { |
86 | 120 *newNode = *node; |
122 | 121 newNode->color = Red; |
91 | 122 traverse->current = newNode; |
86 | 123 goto meta(context, InsertCase1); |
124 } | |
125 | |
126 __code insertNode_stub(struct Context* context) { | |
91 | 127 goto insertNode(context, |
128 &context->data[Traverse]->traverse, | |
129 &context->data[Node]->node, | |
124 | 130 context->data[Traverse]->traverse.newNode); |
86 | 131 } |
132 | |
127 | 133 __code insertCase1(struct Context* context, struct Tree* tree,struct Element* nodeStack) { |
125 | 134 if (nodeStack!=NULL) { |
86 | 135 goto meta(context, InsertCase2); |
122 | 136 } |
127 | 137 tree->root->color = Black; |
119 | 138 goto meta(context, StackClear); |
86 | 139 } |
140 | |
141 __code insert1_stub(struct Context* context) { | |
127 | 142 goto insertCase1(context, |
143 &context->data[Tree]->tree, | |
144 context->data[Traverse]->traverse.nodeStack); | |
86 | 145 } |
146 | |
122 | 147 __code insertCase2(struct Context* context, struct Node* parent) { |
86 | 148 if (parent->color == Black) { |
119 | 149 goto meta(context, StackClear); |
86 | 150 } |
151 goto meta(context, InsertCase3); | |
152 } | |
153 | |
154 __code insert2_stub(struct Context* context) { | |
124 | 155 goto insertCase2(context, (struct Node*)context->data[Traverse]->traverse.nodeStack->data); |
86 | 156 } |
157 | |
124 | 158 __code insertCase3(struct Context* context, struct Traverse* traverse, struct Node* parent, struct Node* grandparent) { |
86 | 159 struct Node* uncle; |
160 | |
161 if (grandparent->left == parent) | |
162 uncle = grandparent->right; | |
163 else | |
164 uncle = grandparent->left; | |
165 | |
166 if (uncle && (uncle->color == Red)) { | |
117 | 167 // do insertcase1 on grandparent, stack must be pop by two |
86 | 168 parent->color = Black; |
169 uncle->color = Black; | |
170 grandparent->color = Red; | |
91 | 171 traverse->current = grandparent; |
124 | 172 goto meta(context, InsertCase31); |
86 | 173 } |
174 goto meta(context, InsertCase4); | |
175 } | |
176 | |
177 __code insert3_stub(struct Context* context) { | |
122 | 178 goto insertCase3(context, &context->data[Traverse]->traverse, |
124 | 179 (struct Node*)context->data[Traverse]->traverse.nodeStack->data, |
180 (struct Node*)context->data[Traverse]->traverse.nodeStack->next->data | |
122 | 181 ); |
86 | 182 } |
122 | 183 __code insertCase31(struct Context* context, struct Traverse* traverse) { |
184 traverse->nodeStack = traverse->nodeStack->next->next; | |
120 | 185 goto meta(context, InsertCase1); |
186 } | |
122 | 187 __code insert31_stub(struct Context* context) { |
188 goto insertCase31(context, &context->data[Traverse]->traverse); | |
120 | 189 } |
86 | 190 |
122 | 191 __code insertCase4(struct Context* context, struct Traverse* traverse, struct Node* current,struct Node* parent, struct Node* grandparent) { |
91 | 192 traverse->current = parent; |
86 | 193 |
194 if ((current == parent->right) && (parent == grandparent->left)) { | |
119 | 195 traverse->rotateNext = InsertCase4_1; |
122 | 196 goto meta(context, InsertCase4_01); |
86 | 197 } else if ((current == parent->left) && (parent == grandparent->right)) { |
119 | 198 traverse->rotateNext = InsertCase4_2; |
122 | 199 goto meta(context, InsertCase4_02); |
86 | 200 } |
201 | |
91 | 202 traverse->current = current; |
86 | 203 goto meta(context, InsertCase5); |
204 } | |
205 | |
206 __code insert4_stub(struct Context* context) { | |
124 | 207 goto insertCase4(context, &context->data[Traverse]->traverse, context->data[Traverse]->traverse.current, |
208 &context->data[Traverse]->traverse.nodeStack->data->node, | |
209 &context->data[Traverse]->traverse.nodeStack->next->data->node); | |
86 | 210 } |
211 | |
122 | 212 __code insertCase4_01(struct Context* context, struct Traverse* traverse) { |
126 | 213 traverse->nodeStack = traverse->nodeStack -> next; |
214 traverse->rotateNext = InsertCase5; | |
122 | 215 goto meta(context, RotateL); |
216 } | |
217 | |
218 __code insert4_01_stub(struct Context* context) { | |
219 goto insertCase4_01(context, &context->data[Traverse]->traverse); | |
220 } | |
91 | 221 __code insertCase4_1(struct Context* context, struct Traverse* traverse) { |
222 traverse->current = traverse->current->left; | |
86 | 223 goto meta(context, InsertCase5); |
224 } | |
225 | |
226 __code insert4_1_stub(struct Context* context) { | |
122 | 227 struct Allocate* allocate = &context->data[Allocate]->allocate; |
228 allocate->size = sizeof(struct Element); | |
229 allocator(context); | |
124 | 230 struct Element* element = &context->data[context->dataNum]->element; |
231 struct Traverse* traverse = &context->data[Traverse]->traverse; | |
232 element->data = (union Data*)traverse->current; | |
91 | 233 goto insertCase4_1(context, &context->data[Traverse]->traverse); |
86 | 234 } |
122 | 235 __code insertCase4_02(struct Context* context, struct Traverse* traverse) { |
126 | 236 traverse->nodeStack = traverse->nodeStack -> next; |
237 traverse->rotateNext = InsertCase5; | |
122 | 238 goto meta(context, RotateR); |
239 } | |
240 | |
241 __code insert4_02_stub(struct Context* context) { | |
242 goto insertCase4_02(context, &context->data[Traverse]->traverse); | |
243 } | |
86 | 244 |
91 | 245 __code insertCase4_2(struct Context* context, struct Traverse* traverse) { |
246 traverse->current = traverse->current->right; | |
86 | 247 goto meta(context, InsertCase5); |
248 } | |
249 | |
123 | 250 __code insert4_2_stub(struct Context* context) { |
122 | 251 struct Allocate* allocate = &context->data[Allocate]->allocate; |
252 allocate->size = sizeof(struct Element); | |
253 allocator(context); | |
124 | 254 struct Element* element = &context->data[context->dataNum]->element; |
255 struct Traverse* traverse = &context->data[Traverse]->traverse; | |
256 element->data = (union Data*)traverse->current; | |
91 | 257 goto insertCase4_2(context, &context->data[Traverse]->traverse); |
86 | 258 } |
259 | |
124 | 260 __code insertCase5(struct Context* context, struct Traverse* traverse, struct Node* current, struct Node* parent, struct Node* grandparent) { |
86 | 261 parent->color = Black; |
262 grandparent->color = Red; | |
263 | |
91 | 264 traverse->current = grandparent; |
119 | 265 traverse->rotateNext = StackClear; |
86 | 266 if ((current == parent->left) && (parent == grandparent->left)) |
267 goto meta(context, RotateR); | |
268 else | |
269 goto meta(context, RotateL); | |
270 } | |
271 | |
272 __code insert5_stub(struct Context* context) { | |
124 | 273 struct Traverse* traverse = &context->data[Traverse]->traverse; |
122 | 274 struct Node* parent = (struct Node*)traverse->nodeStack->data; |
275 struct Node* grandparent = (struct Node*)traverse->nodeStack->next->data; | |
276 traverse->nodeStack = traverse->nodeStack->next->next; | |
124 | 277 goto insertCase5(context, &context->data[Traverse]->traverse, context->data[Traverse]->traverse.current, parent, grandparent); |
86 | 278 } |
279 | |
117 | 280 // put rotateLeft's continuation as argument |
126 | 281 __code rotateLeft(struct Context* context, struct Node* node, struct Tree* tree, struct Traverse* traverse, struct Node* parent) { |
86 | 282 struct Node* tmp = node->right; |
123 | 283 |
284 if (parent) { | |
285 if (node == parent->left) | |
286 parent->left = tmp; | |
287 else | |
288 parent->right = tmp; | |
289 } else { | |
290 tree->root = tmp; | |
291 } | |
117 | 292 |
123 | 293 node->right = tmp->left; |
294 tmp->left = node; | |
295 traverse->current = tmp; | |
296 | |
297 goto meta(context, traverse->rotateNext); | |
298 } | |
299 | |
300 __code rotateLeft_stub(struct Context* context) { | |
124 | 301 struct Traverse* traverse = &context->data[Traverse]->traverse; |
123 | 302 struct Node* parent = (traverse->nodeStack)?(struct Node*)traverse->nodeStack->data:NULL; |
303 goto rotateLeft(context, | |
125 | 304 context->data[Traverse]->traverse.current, |
123 | 305 &context->data[Tree]->tree, |
124 | 306 &context->data[Traverse]->traverse, |
126 | 307 parent); |
123 | 308 } |
86 | 309 |
126 | 310 __code rotateRight(struct Context* context, struct Node* node, struct Tree* tree, struct Traverse* traverse, struct Node* parent) { |
123 | 311 struct Node* tmp = node->left; |
312 | |
86 | 313 if (parent) { |
314 if (node == parent->left) | |
315 parent->left = tmp; | |
316 else | |
317 parent->right = tmp; | |
318 } else { | |
319 tree->root = tmp; | |
320 } | |
321 | |
322 node->left = tmp->right; | |
323 tmp->right = node; | |
91 | 324 traverse->current = tmp; |
86 | 325 |
119 | 326 goto meta(context, traverse->rotateNext); |
86 | 327 } |
328 | |
329 __code rotateRight_stub(struct Context* context) { | |
125 | 330 struct Traverse* traverse = &context->data[Traverse]->traverse; |
331 struct Node* parent = (traverse->nodeStack)?(struct Node*)traverse->nodeStack->data:NULL; | |
91 | 332 goto rotateRight(context, |
125 | 333 context->data[Traverse]->traverse.current, |
124 | 334 &context->data[Tree]->tree, |
335 &context->data[Traverse]->traverse, | |
126 | 336 parent); |
86 | 337 } |
338 | |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
339 __code stackClear(struct Context* context, struct Traverse* traverse) { |
91 | 340 traverse->current = 0; |
127 | 341 traverse->nodeStack = NULL; |
86 | 342 |
343 goto meta(context, context->next); | |
344 } | |
345 | |
346 __code stackClear_stub(struct Context* context) { | |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
347 goto stackClear(context, &context->data[Traverse]->traverse); |
86 | 348 } |
349 | |
350 | |
92 | 351 __code get(struct Context* context, struct Tree* tree, struct Traverse* traverse) { |
352 if (tree->root) { | |
353 traverse->current = tree->root; | |
86 | 354 |
92 | 355 goto meta(context, Search); |
356 } | |
86 | 357 |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
358 goto meta(context, traverse->next); |
92 | 359 } |
86 | 360 |
92 | 361 __code get_stub(struct Context* context) { |
362 goto get(context, &context->data[Tree]->tree, &context->data[Traverse]->traverse); | |
363 } | |
86 | 364 |
91 | 365 __code search(struct Context* context, struct Traverse* traverse, struct Node* node) { |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
366 // compare(context, traverse, traverse->current->key, node->key); |
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
367 traverse->result = compare(traverse->current, node); |
91 | 368 if (traverse->result == EQ) { |
369 *node = *traverse->current; | |
86 | 370 |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
371 goto meta(context, traverse->next); |
91 | 372 } else if (traverse->result == GT) { |
373 traverse->current = traverse->current->right; | |
374 } else { | |
375 traverse->current = traverse->current->left; | |
376 } | |
86 | 377 |
91 | 378 if (traverse->current) |
379 goto meta(context, Search); | |
86 | 380 |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
381 goto meta(context, traverse->next); |
91 | 382 } |
86 | 383 |
91 | 384 __code search_stub(struct Context* context) { |
385 goto search(context, &context->data[Traverse]->traverse, &context->data[Node]->node); | |
386 } | |
86 | 387 |
388 /* /\* __code delete(struct Context* context, struct Tree* tree) { *\/ */ | |
389 /* /\* if (tree->root) { *\/ */ | |
390 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
391 /* /\* context->next = Delete1; *\/ */ | |
392 /* /\* goto meta(context, Get); *\/ */ | |
393 /* /\* } *\/ */ | |
394 | |
395 /* /\* goto meta(context, context->next); *\/ */ | |
396 /* /\* } *\/ */ | |
397 | |
398 /* /\* __code delete_stub(struct Context* context) { *\/ */ | |
399 /* /\* goto delete(context, &context->data[Tree]->tree); *\/ */ | |
400 /* /\* } *\/ */ | |
401 | |
402 /* /\* __code delete1(struct Context* context, struct Tree* tree, struct Allocate* allocate) { *\/ */ | |
403 /* /\* allocate->size = sizeof(struct Node); *\/ */ | |
404 /* /\* allocator(context); *\/ */ | |
405 | |
406 /* /\* struct Node* root = tree->root; *\/ */ | |
407 | |
408 /* /\* tree->root = &context->data[context->dataNum]->node; *\/ */ | |
409 /* /\* tree->current = root; *\/ */ | |
410 | |
411 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ | |
412 | |
413 /* /\* goto meta(context, Replace_d1); *\/ */ | |
414 /* /\* } *\/ */ | |
415 | |
416 /* /\* __code delete1_stub(struct Context* context) { *\/ */ | |
417 /* /\* goto delete1(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate); *\/ */ | |
418 /* /\* } *\/ */ | |
419 | |
420 /* /\* __code delete2(struct Context* context, struct Node* current) { *\/ */ | |
421 /* /\* if (current->color == Black) { *\/ */ | |
422 /* /\* struct Node* child = current->right == NULL ? current->left : current->right; *\/ */ | |
423 /* /\* current->color = child == NULL ? Black : child->color; *\/ */ | |
424 | |
425 /* /\* goto meta(context, DeleteCase1); *\/ */ | |
426 /* /\* } *\/ */ | |
427 | |
428 /* /\* goto meta(context, Delete3); *\/ */ | |
429 /* /\* } *\/ */ | |
430 | |
431 /* /\* __code delete2_stub(struct Context* context) { *\/ */ | |
432 /* /\* goto delete2(context, context->data[Tree]->tree.current); *\/ */ | |
433 /* /\* } *\/ */ | |
434 | |
435 /* /\* __code delete3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
436 /* /\* struct Node* tmp = current->right == NULL ? current->left : current->right; *\/ */ | |
437 | |
438 /* /\* if (current->parent) { *\/ */ | |
439 /* /\* if (current == current->parent->left) *\/ */ | |
440 /* /\* current->parent->left = tmp; *\/ */ | |
441 /* /\* else *\/ */ | |
442 /* /\* current->parent->right = tmp; *\/ */ | |
443 /* /\* } else { *\/ */ | |
444 /* /\* tree->root = tmp; *\/ */ | |
445 /* /\* } *\/ */ | |
446 | |
447 /* /\* if (tmp) *\/ */ | |
448 /* /\* tmp->parent = current->parent; *\/ */ | |
449 | |
450 /* /\* if (current->parent == NULL && tmp) *\/ */ | |
451 /* /\* tmp->color = Black; *\/ */ | |
452 | |
453 /* /\* current == current->parent->left ? (current->parent->left = NULL) : (current->parent->right = NULL); *\/ */ | |
454 | |
455 /* /\* stack_pop(context->code_stack, &context->next); *\/ */ | |
456 /* /\* goto meta(context, context->next); *\/ */ | |
457 /* /\* } *\/ */ | |
458 | |
459 /* /\* __code delete3_stub(struct Context* context) { *\/ */ | |
460 /* /\* goto delete3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
461 /* /\* } *\/ */ | |
462 | |
463 /* /\* __code replaceNodeForDelete1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { *\/ */ | |
464 /* /\* *newNode = *oldNode; *\/ */ | |
465 | |
466 /* /\* if (result == EQ) *\/ */ | |
467 /* /\* goto meta(context, Replace_d2); *\/ */ | |
468 /* /\* else if (result == GT) *\/ */ | |
469 /* /\* tree->current = newNode->right; *\/ */ | |
470 /* /\* else *\/ */ | |
471 /* /\* tree->current = newNode->left; *\/ */ | |
472 | |
473 /* /\* tree->current->parent = newNode; *\/ */ | |
474 | |
475 /* /\* if (tree->current->left == NULL && tree->current->right == NULL) *\/ */ | |
476 /* /\* goto meta(context, Delete2); *\/ */ | |
477 | |
478 /* /\* if (result == GT) *\/ */ | |
479 /* /\* newNode->right = context->heap; *\/ */ | |
480 /* /\* else if (result == LT) *\/ */ | |
481 /* /\* newNode->left = context->heap; *\/ */ | |
482 | |
483 /* /\* allocator(context); *\/ */ | |
484 | |
485 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ | |
486 | |
487 /* /\* goto meta(context, Replace_d1); *\/ */ | |
488 /* /\* } *\/ */ | |
489 | |
490 /* /\* __code replaceNodeForDelete1_stub(struct Context* context) { *\/ */ | |
491 /* /\* goto replaceNodeForDelete1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); *\/ */ | |
492 /* /\* } *\/ */ | |
493 | |
494 /* /\* __code replaceNodeForDelete2(struct Context* context, struct Tree* tree, struct Node* newNode) { *\/ */ | |
495 /* /\* if (tree->current->left && tree->current->right) { *\/ */ | |
496 /* /\* newNode->left->parent = newNode; *\/ */ | |
497 /* /\* tree->current = newNode->left; *\/ */ | |
498 /* /\* newNode->left = context->heap; *\/ */ | |
499 /* /\* tree->deleted = newNode; *\/ */ | |
500 | |
501 /* /\* allocator(context); *\/ */ | |
502 /* /\* tree->current->parent = newNode; *\/ */ | |
503 | |
504 /* /\* goto meta(context, FindMax1); *\/ */ | |
505 /* /\* } *\/ */ | |
506 | |
507 /* /\* goto meta(context, Delete2); *\/ */ | |
508 /* /\* } *\/ */ | |
509 | |
510 /* /\* __code replaceNodeForDelete2_stub(struct Context* context) { *\/ */ | |
511 /* /\* goto replaceNodeForDelete2(context, &context->data[Tree]->tree, &context->data[context->dataNum]->node); *\/ */ | |
512 /* /\* } *\/ */ | |
513 | |
514 /* /\* __code findMax1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ | |
515 /* /\* *newNode = *oldNode; *\/ */ | |
516 | |
517 /* /\* if (newNode->right) *\/ */ | |
518 /* /\* goto meta(context, FindMax2); *\/ */ | |
519 | |
520 /* /\* tree->deleted->key = newNode->key; *\/ */ | |
521 /* /\* tree->deleted->value = newNode->value; *\/ */ | |
522 | |
523 /* /\* tree->current = newNode; *\/ */ | |
524 | |
525 /* /\* goto meta(context, Delete2); *\/ */ | |
526 /* /\* } *\/ */ | |
527 | |
528 /* /\* __code findMax1_stub(struct Context* context) { *\/ */ | |
529 /* /\* goto findMax1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ | |
530 /* /\* } *\/ */ | |
531 | |
532 | |
533 /* /\* __code findMax2(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ | |
534 /* /\* *newNode = *oldNode; *\/ */ | |
535 | |
536 /* /\* if (newNode->right->right) { *\/ */ | |
537 /* /\* tree->current = newNode->right; *\/ */ | |
538 /* /\* newNode->right = context->heap; *\/ */ | |
539 | |
540 /* /\* allocator(context); *\/ */ | |
541 /* /\* tree->current->parent = newNode; *\/ */ | |
542 | |
543 /* /\* goto meta(context, FindMax2); *\/ */ | |
544 /* /\* } *\/ */ | |
545 | |
546 /* /\* tree->deleted->key = newNode->right->key; *\/ */ | |
547 /* /\* tree->deleted->value = newNode->right->value; *\/ */ | |
548 | |
549 /* /\* tree->current = newNode; *\/ */ | |
550 | |
551 /* /\* goto meta(context, Delete2); *\/ */ | |
552 /* /\* } *\/ */ | |
553 | |
554 /* /\* __code findMax2_stub(struct Context* context) { *\/ */ | |
555 /* /\* goto findMax2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ | |
556 /* /\* } *\/ */ | |
557 | |
558 /* /\* __code deleteCase1(struct Context* context, struct Node* current) { *\/ */ | |
559 /* /\* if (current->parent) *\/ */ | |
560 /* /\* goto meta(context, DeleteCase2); *\/ */ | |
561 | |
562 /* /\* goto meta(context, Delete3); *\/ */ | |
563 /* /\* } *\/ */ | |
564 | |
565 /* /\* __code deleteCase1_stub(struct Context* context) { *\/ */ | |
566 /* /\* goto deleteCase1(context, context->data[Tree]->tree.current); *\/ */ | |
567 /* /\* } *\/ */ | |
568 | |
569 /* /\* __code deleteCase2(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
570 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
571 | |
572 /* /\* if ((sibling == NULL ? Black : sibling->color) == Red) { *\/ */ | |
573 /* /\* current->parent->color = Red; *\/ */ | |
574 /* /\* sibling->color = Black; *\/ */ | |
575 | |
576 /* /\* current == current->parent->left ? (current->parent->left = context->heap) : (current->parent->right = context->heap); *\/ */ | |
577 /* /\* allocator(context); *\/ */ | |
578 /* /\* context->data[context->dataNum]->node = *sibling; *\/ */ | |
579 | |
580 /* /\* tree->current = current->parent; *\/ */ | |
581 | |
582 /* /\* context->next = DeleteCase3; *\/ */ | |
583 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
584 | |
585 /* /\* if (current == current->parent->left) *\/ */ | |
586 /* /\* goto meta(context, RotateL); *\/ */ | |
587 /* /\* else *\/ */ | |
588 /* /\* goto meta(context, RotateR); *\/ */ | |
589 /* /\* } *\/ */ | |
590 | |
591 /* /\* goto meta(context, DeleteCase3); *\/ */ | |
592 /* /\* } *\/ */ | |
593 | |
594 /* /\* __code deleteCase2_stub(struct Context* context) { *\/ */ | |
595 /* /\* goto deleteCase2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
596 /* /\* } *\/ */ | |
597 | |
598 /* /\* __code deleteCase3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
599 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
600 | |
601 /* /\* if (current->parent->color == Black && *\/ */ | |
602 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
603 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
604 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
605 /* /\* sibling->color = Red; *\/ */ | |
606 | |
607 /* /\* tree->current = current->parent; *\/ */ | |
608 /* /\* goto meta(context, DeleteCase1); *\/ */ | |
609 /* /\* } *\/ */ | |
610 | |
611 /* /\* goto meta(context, DeleteCase4); *\/ */ | |
612 /* /\* } *\/ */ | |
613 | |
614 /* /\* __code deleteCase3_stub(struct Context* context) { *\/ */ | |
615 /* /\* goto deleteCase3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
616 /* /\* } *\/ */ | |
617 | |
618 /* /\* __code deleteCase4(struct Context* context, struct Node* current) { *\/ */ | |
619 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
620 | |
621 /* /\* if (current->parent->color == Red && *\/ */ | |
622 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
623 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
624 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
625 /* /\* sibling->color = Red; *\/ */ | |
626 /* /\* current->parent->color = Black; *\/ */ | |
627 | |
628 /* /\* goto meta(context, Delete3); *\/ */ | |
629 /* /\* } *\/ */ | |
630 | |
631 /* /\* goto meta(context, DeleteCase5); *\/ */ | |
632 /* /\* } *\/ */ | |
633 | |
634 /* /\* __code deleteCase4_stub(struct Context* context) { *\/ */ | |
635 /* /\* goto deleteCase4(context, context->data[Tree]->tree.current); *\/ */ | |
636 /* /\* } *\/ */ | |
637 | |
638 /* /\* __code deleteCase5(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
639 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
640 /* /\* sibling->parent = current->parent; *\/ */ | |
641 | |
642 /* /\* if (current == current->parent->left && *\/ */ | |
643 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
644 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Red && *\/ */ | |
645 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
646 /* /\* sibling->color = Red; *\/ */ | |
647 /* /\* sibling->left->color = Black; *\/ */ | |
648 | |
649 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ | |
650 /* /\* allocator(context); *\/ */ | |
651 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ | |
652 /* /\* *tmp = *sibling; *\/ */ | |
653 /* /\* tmp->parent = current; *\/ */ | |
654 | |
655 /* /\* tmp->left = context->heap; *\/ */ | |
656 /* /\* allocator(context); *\/ */ | |
657 /* /\* context->data[context->dataNum]->node = *sibling->left; *\/ */ | |
658 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ | |
659 | |
660 /* /\* tree->current = tmp; *\/ */ | |
661 | |
662 /* /\* context->next = DeleteCase6; *\/ */ | |
663 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
664 | |
665 /* /\* goto meta(context, RotateR); *\/ */ | |
666 /* /\* } else if (current == current->parent->right && *\/ */ | |
667 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
668 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
669 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Red) { *\/ */ | |
670 /* /\* sibling->color = Red; *\/ */ | |
671 /* /\* sibling->right->color = Black; *\/ */ | |
672 | |
673 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ | |
674 /* /\* allocator(context); *\/ */ | |
675 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ | |
676 /* /\* *tmp = *sibling; *\/ */ | |
677 /* /\* tmp->parent = current; *\/ */ | |
678 | |
679 /* /\* tmp->right = context->heap; *\/ */ | |
680 /* /\* allocator(context); *\/ */ | |
681 /* /\* context->data[context->dataNum]->node = *sibling->right; *\/ */ | |
682 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ | |
683 | |
684 /* /\* tree->current = tmp; *\/ */ | |
685 | |
686 /* /\* context->next = DeleteCase6; *\/ */ | |
687 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
688 /* /\* goto meta(context, RotateL); *\/ */ | |
689 /* /\* } *\/ */ | |
690 | |
691 /* /\* goto meta(context, DeleteCase6); *\/ */ | |
692 /* /\* } *\/ */ | |
693 | |
694 /* /\* __code deleteCase5_stub(struct Context* context) { *\/ */ | |
695 /* /\* goto deleteCase5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
696 /* /\* } *\/ */ | |
697 | |
698 /* /\* __code deleteCase6(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
699 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
700 | |
701 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ | |
702 /* /\* allocator(context); *\/ */ | |
703 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ | |
704 /* /\* *tmp = *sibling; *\/ */ | |
705 /* /\* tmp->parent = current; *\/ */ | |
706 | |
707 /* /\* tmp->color = current->parent->color; *\/ */ | |
708 /* /\* current->parent->color = Black; *\/ */ | |
709 | |
710 /* /\* context->next = Delete3; *\/ */ | |
711 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
712 | |
713 /* /\* if (current == current->parent->left) { *\/ */ | |
714 /* /\* tmp->right->color = Black; *\/ */ | |
715 /* /\* tree->current = current->parent; *\/ */ | |
716 | |
717 /* /\* goto meta(context, RotateL); *\/ */ | |
718 /* /\* } else { *\/ */ | |
719 /* /\* tmp->left->color = Black; *\/ */ | |
720 /* /\* tree->current = current->parent; *\/ */ | |
721 | |
722 /* /\* goto meta(context, RotateR); *\/ */ | |
723 /* /\* } *\/ */ | |
724 /* /\* } *\/ */ | |
725 | |
726 /* /\* __code deleteCase6_stub(struct Context* context) { *\/ */ | |
727 /* /\* goto deleteCase6(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
728 /* /\* } *\/ */ |