annotate src/parallel_execution/RedBlackTree.agda @ 427:07ccd411ad70

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