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