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