annotate src/parallel_execution/RedBlackTree.cbc @ 418:a74bec89c198

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