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