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