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