Mercurial > hg > GearsTemplate
annotate src/llrb/llrb.c @ 101:8987cf13d5bb
Add Allocate macro
author | innparusu |
---|---|
date | Wed, 02 Mar 2016 19:23:00 +0900 |
parents | c13575c3dbe9 |
children | 4db311ba1289 |
rev | line source |
---|---|
19 | 1 #include <stdio.h> |
2 | |
3 #include "llrbContext.h" | |
4 #include "origin_cs.h" | |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
5 |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
6 extern void allocator(struct Context* context); |
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
7 extern void compare(struct Context* context, struct Tree* tree, int key1, int key2); |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
8 |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
9 extern int num; |
19 | 10 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
11 __code put(struct Context* context, struct Tree* tree, struct Node* root, struct Allocate* allocate) { |
56 | 12 allocate->size = sizeof(struct Node); |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
13 allocator(context); |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
14 |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
15 stack_push(context->code_stack, &context->next); |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
16 |
83 | 17 context->next = StackClear; |
18 stack_push(context->code_stack, &context->next); | |
19 | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
20 tree->root = &context->data[context->dataNum]->node; |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
21 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
22 if (root) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
23 tree->current = root; |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
24 compare(context, tree, tree->current->key, context->data[Node]->node.key); |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
25 |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
26 goto meta(context, Replace); |
56 | 27 } |
76 | 28 |
29 goto meta(context, Insert); | |
56 | 30 } |
31 | |
54 | 32 __code put_stub(struct Context* context) { |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
33 goto put(context, &context->data[Tree]->tree, context->data[Tree]->tree.root, &context->data[Allocate]->allocate); |
54 | 34 } |
35 | |
56 | 36 __code replaceNode(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { |
37 *newNode = *oldNode; | |
83 | 38 stack_push(context->node_stack, &newNode); |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
39 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
40 if (result == EQ) { |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
41 newNode->value = context->data[Node]->node.value; |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
42 |
80 | 43 stack_pop(context->code_stack, &context->next); |
44 goto meta(context, context->next); | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
45 } else if (result == GT) { |
56 | 46 tree->current = oldNode->right; |
44 | 47 newNode->right = context->heap; |
31
dbbafae822f8
modify Non-Destructive Red Black Tree
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
27
diff
changeset
|
48 } else { |
56 | 49 tree->current = oldNode->left; |
44 | 50 newNode->left = context->heap; |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
51 } |
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
52 |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
53 allocator(context); |
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
54 |
76 | 55 if (tree->current) { |
65
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
56 compare(context, tree, tree->current->key, context->data[Node]->node.key); |
025fd6e90597
to the function call(allocate and compare)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
57 goto meta(context, Replace); |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
58 } |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
59 |
76 | 60 goto meta(context, Insert); |
19 | 61 } |
62 | |
56 | 63 __code replaceNode_stub(struct Context* context) { |
64 goto replaceNode(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); | |
65 } | |
23
868c2918b634
Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
66 |
80 | 67 __code insertNode(struct Context* context, struct Tree* tree, struct Node* node, struct Node* newNode) { |
68 node->color = Red; | |
69 *newNode = *node; | |
70 | |
71 tree->current = newNode; | |
72 | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
73 goto meta(context, InsertCase1); |
80 | 74 } |
75 | |
76 __code insertNode_stub(struct Context* context) { | |
77 goto insertNode(context, &context->data[Tree]->tree, &context->data[Node]->node, &context->data[context->dataNum]->node); | |
78 } | |
79 | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
80 __code insertCase1(struct Context* context, struct Tree* tree, struct Node* current) { |
83 | 81 if (!isEmpty(context->node_stack)) |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
82 goto meta(context, InsertCase2); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
83 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
84 tree->root->color = Black; |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
85 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
86 stack_pop(context->code_stack, &context->next); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
87 goto meta(context, context->next); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
88 } |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
89 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
90 __code insert1_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
91 goto insertCase1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
92 } |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
93 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
94 __code insertCase2(struct Context* context, struct Node* current) { |
83 | 95 struct Node* parent; |
96 stack_pop(context->node_stack, &parent); | |
97 | |
98 if (parent->color == Black) { | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
99 stack_pop(context->code_stack, &context->next); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
100 goto meta(context, context->next); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
101 } |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
102 |
83 | 103 stack_push(context->node_stack, &parent); |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
104 goto meta(context, InsertCase3); |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
105 } |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
106 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
107 __code insert2_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
108 goto insertCase2(context, context->data[Tree]->tree.current); |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
109 } |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
110 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
111 __code insertCase3(struct Context* context, struct Tree* tree, struct Node* current) { |
83 | 112 struct Node* parent; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
113 struct Node* uncle; |
83 | 114 struct Node* grandparent; |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
115 |
83 | 116 stack_pop(context->node_stack, &parent); |
117 stack_pop(context->node_stack, &grandparent); | |
118 | |
119 if (grandparent->left == parent) | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
120 uncle = grandparent->right; |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
121 else |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
122 uncle = grandparent->left; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
123 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
124 if (uncle && (uncle->color == Red)) { |
83 | 125 parent->color = Black; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
126 uncle->color = Black; |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
127 grandparent->color = Red; |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
128 tree->current = grandparent; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
129 goto meta(context, InsertCase1); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
130 } |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
131 |
83 | 132 stack_push(context->node_stack, &grandparent); |
133 stack_push(context->node_stack, &parent); | |
134 | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
135 goto meta(context, InsertCase4); |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
136 } |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
137 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
138 __code insert3_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
139 goto insertCase3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
140 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
141 |
83 | 142 __code insertCase4(struct Context* context, struct Tree* tree, struct Node* current) { |
143 struct Node* parent; | |
144 struct Node* grandparent; | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
145 |
83 | 146 stack_pop(context->node_stack, &parent); |
147 stack_pop(context->node_stack, &grandparent); | |
148 | |
149 stack_push(context->node_stack, &grandparent); | |
150 | |
151 tree->current = parent; | |
152 | |
153 if ((current == parent->right) && (parent == grandparent->left)) { | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
154 context->next = InsertCase4_1; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
155 |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
156 stack_push(context->code_stack, &context->next); |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
157 goto meta(context, RotateL); |
83 | 158 } else if ((current == parent->left) && (parent == grandparent->right)) { |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
159 context->next = InsertCase4_2; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
160 |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
161 stack_push(context->code_stack, &context->next); |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
162 goto meta(context, RotateR); |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
163 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
164 |
83 | 165 stack_push(context->node_stack, &parent); |
166 tree->current = current; | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
167 goto meta(context, InsertCase5); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
168 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
169 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
170 __code insert4_stub(struct Context* context) { |
83 | 171 goto insertCase4(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
172 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
173 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
174 __code insertCase4_1(struct Context* context, struct Tree* tree) { |
83 | 175 stack_push(context->node_stack, &tree->current); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
176 tree->current = tree->current->left; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
177 goto meta(context, InsertCase5); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
178 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
179 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
180 __code insert4_1_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
181 goto insertCase4_1(context, &context->data[Tree]->tree); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
182 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
183 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
184 __code insertCase4_2(struct Context* context, struct Tree* tree) { |
83 | 185 stack_push(context->node_stack, &tree->current); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
186 tree->current = tree->current->right; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
187 goto meta(context, InsertCase5); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
188 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
189 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
190 __code insert4_2_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
191 goto insertCase4_2(context, &context->data[Tree]->tree); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
192 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
193 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
194 __code insertCase5(struct Context* context, struct Tree* tree, struct Node* current) { |
83 | 195 struct Node* parent; |
196 struct Node* grandparent; | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
197 |
83 | 198 stack_pop(context->node_stack, &parent); |
199 stack_pop(context->node_stack, &grandparent); | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
200 |
83 | 201 parent->color = Black; |
202 grandparent->color = Red; | |
203 | |
204 tree->current = grandparent; | |
205 | |
206 if ((current == parent->left) && (parent == grandparent->left)) | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
207 goto meta(context, RotateR); |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
208 else |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
209 goto meta(context, RotateL); |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
210 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
211 |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
212 __code insert5_stub(struct Context* context) { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
213 goto insertCase5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
214 } |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
215 |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
216 __code rotateLeft(struct Context* context, struct Node* node, struct Tree* tree) { |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
217 struct Node* tmp = node->right; |
83 | 218 struct Node* parent = 0; |
219 | |
220 stack_pop(context->node_stack, &parent); | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
221 |
83 | 222 if (parent) { |
223 if (node == parent->left) | |
224 parent->left = tmp; | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
225 else |
83 | 226 parent->right = tmp; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
227 } else { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
228 tree->root = tmp; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
229 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
230 |
83 | 231 stack_push(context->node_stack, &parent); |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
232 |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
233 node->right = tmp->left; |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
234 tmp->left = node; |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
235 tree->current = tmp; |
20 | 236 |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
237 stack_pop(context->code_stack, &context->next); |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
238 goto meta(context, context->next); |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
239 } |
21 | 240 |
56 | 241 __code rotateLeft_stub(struct Context* context) { |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
242 goto rotateLeft(context, context->data[Tree]->tree.current, &context->data[Tree]->tree); |
56 | 243 } |
23
868c2918b634
Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
22
diff
changeset
|
244 |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
245 __code rotateRight(struct Context* context, struct Node* node, struct Tree* tree) { |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
246 struct Node* tmp = node->left; |
83 | 247 struct Node* parent = 0; |
248 | |
249 stack_pop(context->node_stack, &parent); | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
250 |
83 | 251 if (parent) { |
252 if (node == parent->left) | |
253 parent->left = tmp; | |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
254 else |
83 | 255 parent->right = tmp; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
256 } else { |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
257 tree->root = tmp; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
258 } |
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
259 |
83 | 260 stack_push(context->node_stack, &parent); |
261 | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
262 node->left = tmp->right; |
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
263 tmp->right = node; |
77
618c03f25108
implement insert(tail recursion)
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
76
diff
changeset
|
264 tree->current = tmp; |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
265 |
72
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
266 stack_pop(context->code_stack, &context->next); |
5c4b9d116eda
use stack for code segment
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
267 goto meta(context, context->next); |
24
7494c0b87ec4
implement insert of Non Destructive llrb
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
268 } |
27 | 269 |
56 | 270 __code rotateRight_stub(struct Context* context) { |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
271 goto rotateRight(context, context->data[Tree]->tree.current, &context->data[Tree]->tree); |
56 | 272 } |
273 | |
83 | 274 __code stackClear(struct Context* context, stack_ptr node_stack, struct Tree* tree) { |
275 if (stack_pop(node_stack, &tree->current) == 0) | |
276 goto meta(context, StackClear); | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
277 |
83 | 278 tree->current = 0; |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
279 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
280 stack_pop(context->code_stack, &context->next); |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
281 goto meta(context, context->next); |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
282 } |
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
283 |
83 | 284 __code stackClear_stub(struct Context* context) { |
285 goto stackClear(context, context->node_stack, &context->data[Tree]->tree); | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
286 } |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
287 |
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
288 |
83 | 289 /* /\* __code get(struct Context* context, struct Tree* tree) { *\/ */ |
290 /* /\* if (tree->root) { *\/ */ | |
291 /* /\* tree->current = tree->root; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
292 |
83 | 293 /* /\* goto meta(context, Search); *\/ */ |
294 /* /\* } *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
295 |
83 | 296 /* /\* stack_pop(context->code_stack, &context->next); *\/ */ |
297 /* /\* goto meta(context, context->next); *\/ */ | |
298 /* /\* } *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
299 |
83 | 300 /* /\* __code get_stub(struct Context* context) { *\/ */ |
301 /* /\* goto get(context, &context->data[Tree]->tree); *\/ */ | |
302 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
303 |
83 | 304 /* /\* __code search(struct Context* context, struct Tree* tree, struct Node* node) { *\/ */ |
305 /* /\* compare(context, tree, tree->current->key, node->key); *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
306 |
83 | 307 /* /\* if (tree->result == EQ) { *\/ */ |
308 /* /\* *node = *tree->current; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
309 |
83 | 310 /* /\* goto meta(context, context->next); *\/ */ |
311 /* /\* } else if (tree->result == GT) { *\/ */ | |
312 /* /\* tree->current = tree->current->right; *\/ */ | |
313 /* /\* } else { *\/ */ | |
314 /* /\* tree->current = tree->current->left; *\/ */ | |
315 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
316 |
83 | 317 /* /\* if (tree->current) *\/ */ |
318 /* /\* goto meta(context, Search); *\/ */ | |
319 | |
320 /* /\* stack_pop(context->code_stack, &context->next); *\/ */ | |
321 /* /\* goto meta(context, context->next); *\/ */ | |
322 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
323 |
83 | 324 /* /\* __code search_stub(struct Context* context) { *\/ */ |
325 /* /\* goto search(context, &context->data[Tree]->tree, &context->data[Node]->node); *\/ */ | |
326 /* /\* } *\/ */ | |
327 | |
328 /* /\* __code delete(struct Context* context, struct Tree* tree) { *\/ */ | |
329 /* /\* if (tree->root) { *\/ */ | |
330 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
331 /* /\* context->next = Delete1; *\/ */ | |
332 /* /\* goto meta(context, Get); *\/ */ | |
333 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
334 |
83 | 335 /* /\* goto meta(context, context->next); *\/ */ |
336 /* /\* } *\/ */ | |
337 | |
338 /* /\* __code delete_stub(struct Context* context) { *\/ */ | |
339 /* /\* goto delete(context, &context->data[Tree]->tree); *\/ */ | |
340 /* /\* } *\/ */ | |
22
4c3c0ad4a75d
add benchmark method
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
21
diff
changeset
|
341 |
83 | 342 /* /\* __code delete1(struct Context* context, struct Tree* tree, struct Allocate* allocate) { *\/ */ |
343 /* /\* allocate->size = sizeof(struct Node); *\/ */ | |
344 /* /\* allocator(context); *\/ */ | |
345 | |
346 /* /\* struct Node* root = tree->root; *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
347 |
83 | 348 /* /\* tree->root = &context->data[context->dataNum]->node; *\/ */ |
349 /* /\* tree->current = root; *\/ */ | |
350 | |
351 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ | |
76 | 352 |
83 | 353 /* /\* goto meta(context, Replace_d1); *\/ */ |
354 /* /\* } *\/ */ | |
355 | |
356 /* /\* __code delete1_stub(struct Context* context) { *\/ */ | |
357 /* /\* goto delete1(context, &context->data[Tree]->tree, &context->data[Allocate]->allocate); *\/ */ | |
358 /* /\* } *\/ */ | |
359 | |
360 /* /\* __code delete2(struct Context* context, struct Node* current) { *\/ */ | |
361 /* /\* if (current->color == Black) { *\/ */ | |
362 /* /\* struct Node* child = current->right == NULL ? current->left : current->right; *\/ */ | |
363 /* /\* current->color = child == NULL ? Black : child->color; *\/ */ | |
364 | |
365 /* /\* goto meta(context, DeleteCase1); *\/ */ | |
366 /* /\* } *\/ */ | |
367 | |
368 /* /\* goto meta(context, Delete3); *\/ */ | |
369 /* /\* } *\/ */ | |
370 | |
371 /* /\* __code delete2_stub(struct Context* context) { *\/ */ | |
372 /* /\* goto delete2(context, context->data[Tree]->tree.current); *\/ */ | |
373 /* /\* } *\/ */ | |
374 | |
375 /* /\* __code delete3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
376 /* /\* struct Node* tmp = current->right == NULL ? current->left : current->right; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
377 |
83 | 378 /* /\* if (current->parent) { *\/ */ |
379 /* /\* if (current == current->parent->left) *\/ */ | |
380 /* /\* current->parent->left = tmp; *\/ */ | |
381 /* /\* else *\/ */ | |
382 /* /\* current->parent->right = tmp; *\/ */ | |
383 /* /\* } else { *\/ */ | |
384 /* /\* tree->root = tmp; *\/ */ | |
385 /* /\* } *\/ */ | |
386 | |
387 /* /\* if (tmp) *\/ */ | |
388 /* /\* tmp->parent = current->parent; *\/ */ | |
389 | |
390 /* /\* if (current->parent == NULL && tmp) *\/ */ | |
391 /* /\* tmp->color = Black; *\/ */ | |
392 | |
393 /* /\* current == current->parent->left ? (current->parent->left = NULL) : (current->parent->right = NULL); *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
394 |
83 | 395 /* /\* stack_pop(context->code_stack, &context->next); *\/ */ |
396 /* /\* goto meta(context, context->next); *\/ */ | |
397 /* /\* } *\/ */ | |
398 | |
399 /* /\* __code delete3_stub(struct Context* context) { *\/ */ | |
400 /* /\* goto delete3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
401 /* /\* } *\/ */ | |
402 | |
403 /* /\* __code replaceNodeForDelete1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode, int result) { *\/ */ | |
404 /* /\* *newNode = *oldNode; *\/ */ | |
405 | |
406 /* /\* if (result == EQ) *\/ */ | |
407 /* /\* goto meta(context, Replace_d2); *\/ */ | |
408 /* /\* else if (result == GT) *\/ */ | |
409 /* /\* tree->current = newNode->right; *\/ */ | |
410 /* /\* else *\/ */ | |
411 /* /\* tree->current = newNode->left; *\/ */ | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
412 |
83 | 413 /* /\* tree->current->parent = newNode; *\/ */ |
414 | |
415 /* /\* if (tree->current->left == NULL && tree->current->right == NULL) *\/ */ | |
416 /* /\* goto meta(context, Delete2); *\/ */ | |
417 | |
418 /* /\* if (result == GT) *\/ */ | |
419 /* /\* newNode->right = context->heap; *\/ */ | |
420 /* /\* else if (result == LT) *\/ */ | |
421 /* /\* newNode->left = context->heap; *\/ */ | |
422 | |
423 /* /\* allocator(context); *\/ */ | |
424 | |
425 /* /\* compare(context, tree, tree->current->key, context->data[Node]->node.key); *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
426 |
83 | 427 /* /\* goto meta(context, Replace_d1); *\/ */ |
428 /* /\* } *\/ */ | |
429 | |
430 /* /\* __code replaceNodeForDelete1_stub(struct Context* context) { *\/ */ | |
431 /* /\* goto replaceNodeForDelete1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node, context->data[Tree]->tree.result); *\/ */ | |
432 /* /\* } *\/ */ | |
433 | |
434 /* /\* __code replaceNodeForDelete2(struct Context* context, struct Tree* tree, struct Node* newNode) { *\/ */ | |
435 /* /\* if (tree->current->left && tree->current->right) { *\/ */ | |
436 /* /\* newNode->left->parent = newNode; *\/ */ | |
437 /* /\* tree->current = newNode->left; *\/ */ | |
438 /* /\* newNode->left = context->heap; *\/ */ | |
439 /* /\* tree->deleted = newNode; *\/ */ | |
440 | |
441 /* /\* allocator(context); *\/ */ | |
442 /* /\* tree->current->parent = newNode; *\/ */ | |
443 | |
444 /* /\* goto meta(context, FindMax1); *\/ */ | |
445 /* /\* } *\/ */ | |
446 | |
447 /* /\* goto meta(context, Delete2); *\/ */ | |
448 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
449 |
83 | 450 /* /\* __code replaceNodeForDelete2_stub(struct Context* context) { *\/ */ |
451 /* /\* goto replaceNodeForDelete2(context, &context->data[Tree]->tree, &context->data[context->dataNum]->node); *\/ */ | |
452 /* /\* } *\/ */ | |
453 | |
454 /* /\* __code findMax1(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ | |
455 /* /\* *newNode = *oldNode; *\/ */ | |
456 | |
457 /* /\* if (newNode->right) *\/ */ | |
458 /* /\* goto meta(context, FindMax2); *\/ */ | |
459 | |
460 /* /\* tree->deleted->key = newNode->key; *\/ */ | |
461 /* /\* tree->deleted->value = newNode->value; *\/ */ | |
462 | |
463 /* /\* tree->current = newNode; *\/ */ | |
464 | |
465 /* /\* goto meta(context, Delete2); *\/ */ | |
466 /* /\* } *\/ */ | |
467 | |
468 /* /\* __code findMax1_stub(struct Context* context) { *\/ */ | |
469 /* /\* goto findMax1(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ | |
470 /* /\* } *\/ */ | |
471 | |
472 | |
473 /* /\* __code findMax2(struct Context* context, struct Tree* tree, struct Node* oldNode, struct Node* newNode) { *\/ */ | |
474 /* /\* *newNode = *oldNode; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
475 |
83 | 476 /* /\* if (newNode->right->right) { *\/ */ |
477 /* /\* tree->current = newNode->right; *\/ */ | |
478 /* /\* newNode->right = context->heap; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
479 |
83 | 480 /* /\* allocator(context); *\/ */ |
481 /* /\* tree->current->parent = newNode; *\/ */ | |
482 | |
483 /* /\* goto meta(context, FindMax2); *\/ */ | |
484 /* /\* } *\/ */ | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
485 |
83 | 486 /* /\* tree->deleted->key = newNode->right->key; *\/ */ |
487 /* /\* tree->deleted->value = newNode->right->value; *\/ */ | |
488 | |
489 /* /\* tree->current = newNode; *\/ */ | |
490 | |
491 /* /\* goto meta(context, Delete2); *\/ */ | |
492 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
493 |
83 | 494 /* /\* __code findMax2_stub(struct Context* context) { *\/ */ |
495 /* /\* goto findMax2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current, &context->data[context->dataNum]->node); *\/ */ | |
496 /* /\* } *\/ */ | |
497 | |
498 /* /\* __code deleteCase1(struct Context* context, struct Node* current) { *\/ */ | |
499 /* /\* if (current->parent) *\/ */ | |
500 /* /\* goto meta(context, DeleteCase2); *\/ */ | |
501 | |
502 /* /\* goto meta(context, Delete3); *\/ */ | |
503 /* /\* } *\/ */ | |
504 | |
505 /* /\* __code deleteCase1_stub(struct Context* context) { *\/ */ | |
506 /* /\* goto deleteCase1(context, context->data[Tree]->tree.current); *\/ */ | |
507 /* /\* } *\/ */ | |
508 | |
509 /* /\* __code deleteCase2(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
510 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
511 | |
512 /* /\* if ((sibling == NULL ? Black : sibling->color) == Red) { *\/ */ | |
513 /* /\* current->parent->color = Red; *\/ */ | |
514 /* /\* sibling->color = Black; *\/ */ | |
515 | |
516 /* /\* current == current->parent->left ? (current->parent->left = context->heap) : (current->parent->right = context->heap); *\/ */ | |
517 /* /\* allocator(context); *\/ */ | |
518 /* /\* context->data[context->dataNum]->node = *sibling; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
519 |
83 | 520 /* /\* tree->current = current->parent; *\/ */ |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
521 |
83 | 522 /* /\* context->next = DeleteCase3; *\/ */ |
523 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
524 |
83 | 525 /* /\* if (current == current->parent->left) *\/ */ |
526 /* /\* goto meta(context, RotateL); *\/ */ | |
527 /* /\* else *\/ */ | |
528 /* /\* goto meta(context, RotateR); *\/ */ | |
529 /* /\* } *\/ */ | |
530 | |
531 /* /\* goto meta(context, DeleteCase3); *\/ */ | |
532 /* /\* } *\/ */ | |
533 | |
534 /* /\* __code deleteCase2_stub(struct Context* context) { *\/ */ | |
535 /* /\* goto deleteCase2(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
536 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
537 |
83 | 538 /* /\* __code deleteCase3(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ |
539 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
540 | |
541 /* /\* if (current->parent->color == Black && *\/ */ | |
542 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
543 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
544 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
545 /* /\* sibling->color = Red; *\/ */ | |
546 | |
547 /* /\* tree->current = current->parent; *\/ */ | |
548 /* /\* goto meta(context, DeleteCase1); *\/ */ | |
549 /* /\* } *\/ */ | |
550 | |
551 /* /\* goto meta(context, DeleteCase4); *\/ */ | |
552 /* /\* } *\/ */ | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
553 |
83 | 554 /* /\* __code deleteCase3_stub(struct Context* context) { *\/ */ |
555 /* /\* goto deleteCase3(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
556 /* /\* } *\/ */ | |
557 | |
558 /* /\* __code deleteCase4(struct Context* context, struct Node* current) { *\/ */ | |
559 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
560 | |
561 /* /\* if (current->parent->color == Red && *\/ */ | |
562 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
563 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
564 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
565 /* /\* sibling->color = Red; *\/ */ | |
566 /* /\* current->parent->color = Black; *\/ */ | |
567 | |
568 /* /\* goto meta(context, Delete3); *\/ */ | |
569 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
570 |
83 | 571 /* /\* goto meta(context, DeleteCase5); *\/ */ |
572 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
573 |
83 | 574 /* /\* __code deleteCase4_stub(struct Context* context) { *\/ */ |
575 /* /\* goto deleteCase4(context, context->data[Tree]->tree.current); *\/ */ | |
576 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
577 |
83 | 578 /* /\* __code deleteCase5(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ |
579 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
580 /* /\* sibling->parent = current->parent; *\/ */ | |
581 | |
582 /* /\* if (current == current->parent->left && *\/ */ | |
583 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
584 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Red && *\/ */ | |
585 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Black) { *\/ */ | |
586 /* /\* sibling->color = Red; *\/ */ | |
587 /* /\* sibling->left->color = Black; *\/ */ | |
588 | |
589 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ | |
590 /* /\* allocator(context); *\/ */ | |
591 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ | |
592 /* /\* *tmp = *sibling; *\/ */ | |
593 /* /\* tmp->parent = current; *\/ */ | |
594 | |
595 /* /\* tmp->left = context->heap; *\/ */ | |
596 /* /\* allocator(context); *\/ */ | |
597 /* /\* context->data[context->dataNum]->node = *sibling->left; *\/ */ | |
598 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ | |
69
368306e1bfed
llrb deletion(not work).
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
599 |
83 | 600 /* /\* tree->current = tmp; *\/ */ |
601 | |
602 /* /\* context->next = DeleteCase6; *\/ */ | |
603 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
604 |
83 | 605 /* /\* goto meta(context, RotateR); *\/ */ |
606 /* /\* } else if (current == current->parent->right && *\/ */ | |
607 /* /\* (sibling == NULL ? Black : sibling->color) == Black && *\/ */ | |
608 /* /\* (sibling->left == NULL ? Black : sibling->left->color) == Black && *\/ */ | |
609 /* /\* (sibling->right == NULL ? Black : sibling->right->color) == Red) { *\/ */ | |
610 /* /\* sibling->color = Red; *\/ */ | |
611 /* /\* sibling->right->color = Black; *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
612 |
83 | 613 /* /\* sibling == sibling->parent->left ? (sibling->parent->left = context->heap) : (sibling->parent->right = context->heap); *\/ */ |
614 /* /\* allocator(context); *\/ */ | |
615 /* /\* struct Node* tmp = &context->data[context->dataNum]->node; *\/ */ | |
616 /* /\* *tmp = *sibling; *\/ */ | |
617 /* /\* tmp->parent = current; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
618 |
83 | 619 /* /\* tmp->right = context->heap; *\/ */ |
620 /* /\* allocator(context); *\/ */ | |
621 /* /\* context->data[context->dataNum]->node = *sibling->right; *\/ */ | |
622 /* /\* context->data[context->dataNum]->node.parent = tmp; *\/ */ | |
623 | |
624 /* /\* tree->current = tmp; *\/ */ | |
625 | |
626 /* /\* context->next = DeleteCase6; *\/ */ | |
627 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
628 /* /\* goto meta(context, RotateL); *\/ */ | |
629 /* /\* } *\/ */ | |
630 | |
631 /* /\* goto meta(context, DeleteCase6); *\/ */ | |
632 /* /\* } *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
633 |
83 | 634 /* /\* __code deleteCase5_stub(struct Context* context) { *\/ */ |
635 /* /\* goto deleteCase5(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
636 /* /\* } *\/ */ | |
637 | |
638 /* /\* __code deleteCase6(struct Context* context, struct Tree* tree, struct Node* current) { *\/ */ | |
639 /* /\* struct Node* sibling = current == current->parent->left ? current->parent->right : current->parent->left; *\/ */ | |
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->color = current->parent->color; *\/ */ | |
648 /* /\* current->parent->color = Black; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
649 |
83 | 650 /* /\* context->next = Delete3; *\/ */ |
651 /* /\* stack_push(context->code_stack, &context->next); *\/ */ | |
652 | |
653 /* /\* if (current == current->parent->left) { *\/ */ | |
654 /* /\* tmp->right->color = Black; *\/ */ | |
655 /* /\* tree->current = current->parent; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
656 |
83 | 657 /* /\* goto meta(context, RotateL); *\/ */ |
658 /* /\* } else { *\/ */ | |
659 /* /\* tmp->left->color = Black; *\/ */ | |
660 /* /\* tree->current = current->parent; *\/ */ | |
81
dc6f665bb753
implement delete(tail call). do not work
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
661 |
83 | 662 /* /\* goto meta(context, RotateR); *\/ */ |
663 /* /\* } *\/ */ | |
664 /* /\* } *\/ */ | |
73
2667c3251a00
implement llrb deletion
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
72
diff
changeset
|
665 |
83 | 666 /* /\* __code deleteCase6_stub(struct Context* context) { *\/ */ |
667 /* /\* goto deleteCase6(context, &context->data[Tree]->tree, context->data[Tree]->tree.current); *\/ */ | |
668 /* /\* } *\/ */ |