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