RTEMS CPU Kit with SuperCore  4.11.2
rbtree.h
Go to the documentation of this file.
1 
10 /*
11  * Copyright (c) 2010 Gedare Bloom.
12  *
13  * The license and distribution terms for this file may be
14  * found in the file LICENSE in this distribution or at
15  * http://www.rtems.org/license/LICENSE.
16  */
17 
18 #ifndef _RTEMS_SCORE_RBTREE_H
19 #define _RTEMS_SCORE_RBTREE_H
20 
21 #include <stddef.h>
22 
23 #include <rtems/score/address.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
51 
55 typedef enum {
56  RBT_BLACK,
57  RBT_RED
58 } RBTree_Color;
59 
81  RBTree_Color color;
82 };
83 
87 typedef enum {
88  RBT_LEFT=0,
89  RBT_RIGHT=1
91 
99 typedef long RBTree_Compare_result;
100 
115  const RBTree_Node *first,
116  const RBTree_Node *second
117 );
118 
132 /* the RBTree_Control is actually part of the RBTree structure as an
133  * RBTree_Node. The mapping of fields from RBTree_Control to RBTree_Node are:
134  * permanent_null == parent
135  * root == left
136  * first[0] == right
137  */
138 typedef struct {
144  RBTree_Node *first[2];
146 
150 #define RBTREE_INITIALIZER_EMPTY( name ) \
151  { NULL, NULL, { NULL, NULL } }
152 
156 #define RBTREE_DEFINE_EMPTY( name ) \
157  RBTree_Control name = RBTREE_INITIALIZER_EMPTY( name )
158 
162 #define RBTREE_NODE_INITIALIZER_EMPTY( name ) \
163  { NULL, { NULL, NULL }, RBT_RED }
164 
168 #define RBTREE_NODE_DEFINE_EMPTY( name ) \
169  RBTree_Node name = RBTREE_NODE_INITIALIZER_EMPTY( name )
170 
186 void _RBTree_Initialize(
187  RBTree_Control *the_rbtree,
188  RBTree_Compare compare,
189  void *starting_address,
190  size_t number_nodes,
191  size_t node_size,
192  bool is_unique
193 );
194 
210  const RBTree_Control *the_rbtree,
211  const RBTree_Node *the_node,
212  RBTree_Compare compare,
213  bool is_unique
214 );
215 
233  RBTree_Control *the_rbtree,
234  RBTree_Node *the_node,
235  RBTree_Compare compare,
236  bool is_unique
237 );
238 
251 void _RBTree_Extract(
252  RBTree_Control *the_rbtree,
253  RBTree_Node *the_node
254 );
255 
266  const RBTree_Node *node,
267  RBTree_Direction dir
268 );
269 
280 {
281  the_node->parent = NULL;
282 }
283 
296  const RBTree_Node *the_node
297 )
298 {
299  return the_node->parent == NULL;
300 }
301 
315  const RBTree_Control *the_rbtree
316 )
317 {
318  return the_rbtree->root;
319 }
320 
328  const RBTree_Control *the_rbtree,
329  RBTree_Direction dir
330 )
331 {
332  return the_rbtree->first[dir];
333 }
334 
348  const RBTree_Node *the_node
349 )
350 {
351  return the_node->parent;
352 }
353 
364  const RBTree_Node *the_node
365 )
366 {
367  return the_node->child[RBT_LEFT];
368 }
369 
380  const RBTree_Node *the_node
381 )
382 {
383  return the_node->child[RBT_RIGHT];
384 }
385 
398  const RBTree_Control *the_rbtree
399 )
400 {
401  return (the_rbtree->root == NULL);
402 }
403 
416  const RBTree_Control *the_rbtree,
417  const RBTree_Node *the_node,
418  RBTree_Direction dir
419 )
420 {
421  return (the_node == _RBTree_First(the_rbtree, dir));
422 }
423 
440  const RBTree_Node *the_node
441 )
442 {
443  return _RBTree_Parent( _RBTree_Parent( the_node ) ) == NULL;
444 }
445 
452  RBTree_Control *the_rbtree
453 )
454 {
455  the_rbtree->permanent_null = NULL;
456  the_rbtree->root = NULL;
457  the_rbtree->first[RBT_LEFT] = NULL;
458  the_rbtree->first[RBT_RIGHT] = NULL;
459 }
460 
470  const RBTree_Node *node
471 )
472 {
473  return _RBTree_Next( node, RBT_LEFT );
474 }
475 
484  const RBTree_Node *node
485 )
486 {
487  return _RBTree_Next( node, RBT_RIGHT );
488 }
489 
508  RBTree_Control *the_rbtree,
509  RBTree_Direction dir
510 )
511 {
512  RBTree_Node *the_node = the_rbtree->first[ dir ];
513 
514  if ( the_node != NULL ) {
515  _RBTree_Extract( the_rbtree, the_node );
516  }
517 
518  return the_node;
519 }
520 
523 #ifdef __cplusplus
524 }
525 #endif
526 
527 #endif
528 /* end of include file */
RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty(const RBTree_Control *the_rbtree)
Is the RBTree empty.
Definition: rbtree.h:397
RBTree_Color
This enum type defines the colors available for the RBTree Nodes.
Definition: rbtree.h:55
RBTree_Node * _RBTree_Next(const RBTree_Node *node, RBTree_Direction dir)
Returns the in-order next node of a node.
Definition: rbtreenext.c:30
RBTree_Node * parent
This points to the node&#39;s parent.
Definition: rbtree.h:77
#define RTEMS_INLINE_ROUTINE
The following (in conjunction with compiler arguments) are used to choose between the use of static i...
Definition: basedefs.h:135
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Root(const RBTree_Control *the_rbtree)
Returns a pointer to root node of the red-black tree.
Definition: rbtree.h:314
RBTree_Node * root
This points to the root node of the RBT.
Definition: rbtree.h:142
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_First(const RBTree_Control *the_rbtree, RBTree_Direction dir)
Return pointer to RBTree&#39;s first node.
Definition: rbtree.h:327
RBTree_Compare_result(* RBTree_Compare)(const RBTree_Node *first, const RBTree_Node *second)
Compares two red-black tree nodes.
Definition: rbtree.h:114
long RBTree_Compare_result
Integer type for compare results.
Definition: rbtree.h:99
RBTree_Color color
The color of the node.
Definition: rbtree.h:81
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Get(RBTree_Control *the_rbtree, RBTree_Direction dir)
Gets a node with an extremal key value from the red-black tree.
Definition: rbtree.h:507
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtree.h:469
RBTree_Node * _RBTree_Insert(RBTree_Control *the_rbtree, RBTree_Node *the_node, RBTree_Compare compare, bool is_unique)
Inserts the node into the red-black tree.
Definition: rbtreeinsert.c:88
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Right(const RBTree_Node *the_node)
Return pointer to the right of this node.
Definition: rbtree.h:379
Information Required to Manipulate Physical Addresses.
RBTree_Node * first[2]
This points to the min and max nodes of this RBT.
Definition: rbtree.h:144
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Parent(const RBTree_Node *the_node)
Returns a pointer to the parent of this node.
Definition: rbtree.h:347
RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_tree(const RBTree_Node *the_node)
Returns true, if this red-black tree node is off-tree, and false otherwise.
Definition: rbtree.h:295
RTEMS_INLINE_ROUTINE void _RBTree_Set_off_tree(RBTree_Node *the_node)
Sets a red-black tree node as off-tree.
Definition: rbtree.h:279
RBTree_Direction
This type indicates the direction.
Definition: rbtree.h:87
void _RBTree_Initialize(RBTree_Control *the_rbtree, RBTree_Compare compare, void *starting_address, size_t number_nodes, size_t node_size, bool is_unique)
Initialize a RBTree Header.
Definition: rbtree.c:25
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtree.h:483
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Left(const RBTree_Node *the_node)
Return pointer to the left of this node.
Definition: rbtree.h:363
This is used to manage a RBT.
Definition: rbtree.h:138
RBTree_Node * child[2]
child[0] points to the left child, child[1] points to the right child
Definition: rbtree.h:79
RBTree_Node * permanent_null
This points to a NULL.
Definition: rbtree.h:140
RBTree_Node * _RBTree_Find(const RBTree_Control *the_rbtree, const RBTree_Node *the_node, RBTree_Compare compare, bool is_unique)
Tries to find a node for the specified key in the tree.
Definition: rbtreefind.c:22
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:95
RTEMS_INLINE_ROUTINE bool _RBTree_Is_root(const RBTree_Node *the_node)
Returns true if this node is the root node of a red-black tree, and false otherwise.
Definition: rbtree.h:439
This is used to manage each element (node) which is placed on a RBT.
Definition: rbtree.h:75
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty(RBTree_Control *the_rbtree)
Initialize this RBTree as empty.
Definition: rbtree.h:451
RTEMS_INLINE_ROUTINE bool _RBTree_Is_first(const RBTree_Control *the_rbtree, const RBTree_Node *the_node, RBTree_Direction dir)
Is this the first node on the RBTree.
Definition: rbtree.h:415