comparison src/llrb/llrb.c @ 80:099d85f9d371

refactoring
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Mon, 30 Nov 2015 21:40:50 +0900
parents 618c03f25108
children dc6f665bb753
comparison
equal deleted inserted replaced
77:618c03f25108 80:099d85f9d371
14 14
15 stack_push(context->code_stack, &context->next); 15 stack_push(context->code_stack, &context->next);
16 16
17 if (tree->root) { 17 if (tree->root) {
18 tree->current = tree->root; 18 tree->current = tree->root;
19 tree->root = &context->data[context->dataNum]->node;
19 compare(context, tree, tree->current->key, context->data[Node]->node.key); 20 compare(context, tree, tree->current->key, context->data[Node]->node.key);
20 21
21 goto meta(context, Replace); 22 goto meta(context, Replace);
22 } 23 }
23 24
32 *newNode = *oldNode; 33 *newNode = *oldNode;
33 34
34 if (result == 0) { 35 if (result == 0) {
35 newNode->value = context->data[Node]->node.value; 36 newNode->value = context->data[Node]->node.value;
36 37
37 goto meta(context, SetRoot); 38 stack_pop(context->code_stack, &context->next);
39 goto meta(context, context->next);
38 } else if (result == 1) { 40 } else if (result == 1) {
39 tree->current = oldNode->right; 41 tree->current = oldNode->right;
40 newNode->right = context->heap; 42 newNode->right = context->heap;
41 } else { 43 } else {
42 tree->current = oldNode->left; 44 tree->current = oldNode->left;
57 59
58 __code replaceNode_stub(struct Context* context) { 60 __code replaceNode_stub(struct Context* context) {
59 goto replaceNode(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); 61 goto replaceNode(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result);
60 } 62 }
61 63
64 __code insertNode(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) {
65 node->color = Red;
66 *newNode = *node;
67 newNode->parent = tree->current;
68
69 tree->current = newNode;
70
71 goto meta(context, Balance1);
72 }
73
74 __code insertNode_stub(struct Context* context) {
75 goto insertNode(context, &context->data[Tree]->tree, &context->data[Node]->node, &context->data[context->dataNum]->node);
76 }
77
62 __code balance1(struct Context* context, struct Node* current) { 78 __code balance1(struct Context* context, struct Node* current) {
63 if (current->parent == 0) 79 if (current->parent == 0) {
64 goto meta(context, SetRoot); 80 current->color = Black;
65 else 81
82 stack_pop(context->code_stack, &context->next);
83 goto meta(context, context->next);
84 } else {
66 goto meta(context, Balance2); 85 goto meta(context, Balance2);
86 }
67 } 87 }
68 88
69 __code balance1_stub(struct Context* context) { 89 __code balance1_stub(struct Context* context) {
70 goto balance1(context, context->data[Tree]->tree.current); 90 goto balance1(context, context->data[Tree]->tree.current);
71 } 91 }
103 goto meta(context, Balance4); 123 goto meta(context, Balance4);
104 } 124 }
105 } 125 }
106 126
107 __code balance3_stub(struct Context* context) { 127 __code balance3_stub(struct Context* context) {
108 goto balance3(context, context->data[Tree], context->data[Tree]->tree.current); 128 goto balance3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current);
109 } 129 }
110 130
111 __code balance4(struct Context* context, struct Node* current) { 131 __code balance4(struct Context* context, struct Node* current) {
112 struct Node* grandparent = current->parent->parent; 132 struct Node* grandparent = current->parent->parent;
113 133
134 tree->current = tree->current->left; 154 tree->current = tree->current->left;
135 goto meta(context, Balance5); 155 goto meta(context, Balance5);
136 } 156 }
137 157
138 __code balance4_1_stub(struct Context* context) { 158 __code balance4_1_stub(struct Context* context) {
139 goto balance4_1(context, context->data[Tree]); 159 goto balance4_1(context, &context->data[Tree]->tree);
140 } 160 }
141 161
142 __code balance4_2(struct Context* context, struct Tree* tree) { 162 __code balance4_2(struct Context* context, struct Tree* tree) {
143 tree->current = tree->current->right; 163 tree->current = tree->current->right;
144 goto meta(context, Balance5); 164 goto meta(context, Balance5);
145 } 165 }
146 166
147 __code balance4_2_stub(struct Context* context) { 167 __code balance4_2_stub(struct Context* context) {
148 goto balance4_2(context, context->data[Tree]); 168 goto balance4_2(context, &context->data[Tree]->tree);
149 } 169 }
150 170
151 __code balance5(struct Context* context, struct Tree* tree, struct Node* current) { 171 __code balance5(struct Context* context, struct Tree* tree, struct Node* current) {
152 current->parent->color = Black; 172 current->parent->color = Black;
153 current->parent->parent->color = Red; 173 current->parent->parent->color = Red;
154 174
155 context->next = SetRoot;
156
157 stack_push(context->code_stack, &context->next);
158 tree->current = current->parent->parent; 175 tree->current = current->parent->parent;
159 176
160 if ((current == current->parent->left) && (current->parent == current->parent->parent->left)) 177 if ((current == current->parent->left) && (current->parent == current->parent->parent->left))
161 goto meta(context, RotateR); 178 goto meta(context, RotateR);
162 else 179 else
163 goto meta(context, RotateL); 180 goto meta(context, RotateL);
164 } 181 }
165 182
166 __code balance5_stub(struct Context* context) { 183 __code balance5_stub(struct Context* context) {
167 goto balance5(context, context->data[Tree], context->data[Tree]->tree.current); 184 goto balance5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current);
168 }
169
170 __code insertNode(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) {
171 node->color = Red;
172 *newNode = *node;
173 newNode->parent = tree->current;
174
175 tree->current = newNode;
176
177 goto meta(context, Balance1);
178 }
179
180 __code insertNode_stub(struct Context* context) {
181 goto insertNode(context, &context->data[Tree]->tree, &context->data[Node]->node, &context->data[context->dataNum]->node);
182 } 185 }
183 186
184 __code rotateLeft(struct Context* context, struct Node* node, struct Tree* tree) { 187 __code rotateLeft(struct Context* context, struct Node* node, struct Tree* tree) {
185 struct Node* tmp = node->right; 188 struct Node* tmp = node->right;
186 189