1 #include <rtems/score/rbtree.h>
2 #include <rtems/score/thread.h>
6 static void _Rotate_Left(
7 EDF_Chain_Control *chain,
11 EDF_Node *right = node->right;
13 if ((node->right = right->left))
14 right->left->parent = node;
16 if ((right->parent = node->parent))
18 if (node == node->parent->left)
19 node->parent->left = right;
21 node->parent->right = right;
28 static void _Rotate_Right(
29 EDF_Chain_Control *chain,
33 EDF_Node *left = node->left;
35 if ((node->left = left->right))
36 left->right->parent = node;
39 if ((left->parent = node->parent))
41 if (node == node->parent->right)
42 node->parent->right = left;
44 node->parent->left = left;
52 EDF_Chain_Control *chain,
56 EDF_Node *parent, *child, *uncle, *gparent, *tmp;
61 /* Insert new node into tree */
66 child = ( node->abs_deadline < child->abs_deadline ) \
67 ? child->left : child->right;
70 node->parent = parent;
77 if ( node->abs_deadline < parent->abs_deadline )
80 if ( parent == chain->first ) chain->first = node;
82 else parent->right = node;
90 /* check and fix red-back properties eventually */
91 while ((parent = node->parent) && parent->color == RED)
93 gparent = parent->parent;
95 if (parent == gparent->left)
97 uncle = gparent->right;
99 if ( uncle && uncle->color == RED)
101 parent->color = BLACK;
102 uncle->color = BLACK;
103 gparent->color = RED;
108 if (node == parent->right)
110 _Rotate_Left(chain,parent);
116 parent->color = BLACK;
117 gparent->color = RED;
118 _Rotate_Right(chain, gparent);
121 uncle = gparent->left;
123 if (uncle && uncle->color == RED )
125 parent->color = BLACK;
126 uncle->color = BLACK;
127 gparent->color = RED;
128 node = parent->parent;
132 if (node == parent->left)
134 _Rotate_Right(chain, parent);
141 parent->color = BLACK;
142 gparent->color = RED;
143 _Rotate_Left(chain, gparent);
147 chain->root->color = BLACK;
150 static void _RBT_Delete_Fixup(
151 EDF_Chain_Control *chain,
156 EDF_Node *other, *o_child;
158 while ((!node || node->color == BLACK) && node != chain->root)
160 if (parent->left == node)
162 other = parent->right;
163 if (other->color == RED)
165 other->color = BLACK;
167 _Rotate_Left(chain, parent);
168 other = parent->right;
171 other->left->color == BLACK)
173 other->right->color == BLACK))
177 parent = node->parent;
182 other->right->color == BLACK)
184 if ((o_child = other->left))
185 o_child->color = BLACK;
187 _Rotate_Right(chain,other);
188 other = parent->right;
190 other->color = parent->color;
191 parent->color = BLACK;
193 other->right->color = BLACK;
194 _Rotate_Left(chain,parent);
201 other = parent->left;
202 if (other->color == RED)
204 other->color = BLACK;
206 _Rotate_Right(chain,parent);
207 other = parent->left;
210 other->left->color == BLACK)
212 other->right->color == BLACK))
216 parent = node->parent;
221 other->left->color == BLACK)
223 register struct rb_node *o_right;
224 if ((o_child = other->right))
225 o_child->color = BLACK;
227 _Rotate_Left(chain,other);
228 other = parent->left;
230 other->color = parent->color;
231 parent->color = BLACK;
233 other->left->color = BLACK;
234 _Rotate_Right(chain,parent);
246 EDF_Chain_Control *chain,
250 EDF_Node *child, *parent;
253 if ( chain->first == node )
255 if ( chain->first->right) chain->first = chain->first->right;
256 else chain->first = chain->first->parent;
261 else if (!node->right)
265 /* this will never happen since only the first node
266 * is being removed every time
269 EDF_Node *old = node, *left;
272 while ((left = node->left) != NULL)
275 parent = node->parent;
279 child->parent = parent;
282 if (parent->left == node)
283 parent->left = child;
285 parent->right = child;
290 if (node->parent == old)
292 node->parent = old->parent;
293 node->color = old->color;
294 node->right = old->right;
295 node->left = old->left;
299 if (old->parent->left == old)
300 old->parent->left = node;
302 old->parent->right = node;
306 old->left->parent = node;
308 old->right->parent = node;
312 parent = node->parent;
316 child->parent = parent;
319 if (parent->left == node)
320 parent->left = child;
322 parent->right = child;
329 _RBT_Delete_Fixup(chain,child,parent);
333 * TRUE if there is only one node in tree
336 boolean _RBT_Has_only_one_node(
337 EDF_Chain_Control *chain
340 if ( !chain->root ) return 0;
341 return ( chain->root->left == chain->root->right == NULL ) ;
345 * Returns node with specific absolute deadline
348 EDF_Node* _RBT_Search(
349 EDF_Chain_Control *chain,
350 Deadline_Control absdeadline
355 current = chain->root;
356 while (current != NULL) {
357 if ( absdeadline == current->abs_deadline) return current;
359 current = (absdeadline < current->abs_deadline) ?
360 current->left : current->right;