RTEMS
rbtree.h
Go to the documentation of this file.
1 
12 /*
13  * Copyright (c) 2010 Gedare Bloom.
14  *
15  * The license and distribution terms for this file may be
16  * found in the file LICENSE in this distribution or at
17  * http://www.rtems.org/license/LICENSE.
18  */
19 
20 #ifndef _RTEMS_SCORE_RBTREE_H
21 #define _RTEMS_SCORE_RBTREE_H
22 
23 #include <sys/tree.h>
24 #include <rtems/score/basedefs.h>
25 #include <rtems/score/assert.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
47 struct RBTree_Control;
48 
55 typedef struct RBTree_Node {
56  RB_ENTRY(RBTree_Node) Node;
57 } RBTree_Node;
58 
65 typedef RB_HEAD(RBTree_Control, RBTree_Node) RBTree_Control;
66 
70 #define RBTREE_INITIALIZER_EMPTY( name ) \
71  RB_INITIALIZER( name )
72 
76 #define RBTREE_DEFINE_EMPTY( name ) \
77  RBTree_Control name = RBTREE_INITIALIZER_EMPTY( name )
78 
89 {
90  RB_COLOR( the_node, Node ) = -1;
91 }
92 
104  const RBTree_Node *the_node
105 )
106 {
107  return RB_COLOR( the_node, Node ) == -1;
108 }
109 
117  RBTree_Control *the_rbtree,
118  RBTree_Node *the_node
119 );
120 
130 {
131 #if defined(RTEMS_DEBUG)
132  _RBTree_Set_off_tree( the_node );
133 #else
134  (void) the_node;
135 #endif
136 }
137 
146  RBTree_Node *child,
147  RBTree_Node *parent,
148  RBTree_Node **link
149 )
150 {
151  _Assert( _RBTree_Is_node_off_tree( child ) );
152  RB_SET( child, parent, Node );
153  *link = child;
154 }
155 
207  RBTree_Control *the_rbtree,
208  RBTree_Node *the_node,
209  RBTree_Node *parent,
210  RBTree_Node **link
211 )
212 {
213  _RBTree_Add_child( the_node, parent, link );
214  _RBTree_Insert_color( the_rbtree, the_node );
215 }
216 
229 void _RBTree_Extract(
230  RBTree_Control *the_rbtree,
231  RBTree_Node *the_node
232 );
233 
247  const RBTree_Control *the_rbtree
248 )
249 {
250  return RB_ROOT( the_rbtree );
251 }
252 
262  RBTree_Control *the_rbtree
263 )
264 {
265  return &RB_ROOT( the_rbtree );
266 }
267 
277  const RBTree_Control *the_rbtree
278 )
279 {
280  return &RB_ROOT( the_rbtree );
281 }
282 
296  const RBTree_Node *the_node
297 )
298 {
299  return RB_PARENT( the_node, Node );
300 }
301 
312  const RBTree_Node *the_node
313 )
314 {
315  return RB_LEFT( the_node, Node );
316 }
317 
327  RBTree_Node *the_node
328 )
329 {
330  return &RB_LEFT( the_node, Node );
331 }
332 
343  const RBTree_Node *the_node
344 )
345 {
346  return RB_RIGHT( the_node, Node );
347 }
348 
358  RBTree_Node *the_node
359 )
360 {
361  return &RB_RIGHT( the_node, Node );
362 }
363 
376  const RBTree_Control *the_rbtree
377 )
378 {
379  return RB_EMPTY( the_rbtree );
380 }
381 
397  const RBTree_Node *the_node
398 )
399 {
400  return _RBTree_Parent( the_node ) == NULL;
401 }
402 
411  RBTree_Control *the_rbtree
412 )
413 {
414  RB_INIT( the_rbtree );
415 }
416 
425  RBTree_Control *the_rbtree,
426  RBTree_Node *the_node
427 )
428 {
429  _Assert( _RBTree_Is_node_off_tree( the_node ) );
430  RB_ROOT( the_rbtree ) = the_node;
431  RB_PARENT( the_node, Node ) = NULL;
432  RB_LEFT( the_node, Node ) = NULL;
433  RB_RIGHT( the_node, Node ) = NULL;
434  RB_COLOR( the_node, Node ) = RB_BLACK;
435 }
436 
445 RBTree_Node *_RBTree_Minimum( const RBTree_Control *the_rbtree );
446 
455 RBTree_Node *_RBTree_Maximum( const RBTree_Control *the_rbtree );
456 
466 
476 
485  RBTree_Control *the_rbtree,
486  RBTree_Node *victim,
487  RBTree_Node *replacement
488 );
489 
509  RBTree_Control *the_rbtree,
510  RBTree_Node *the_node,
511  const void *key,
512  bool ( *less )( const void *, const RBTree_Node * )
513 )
514 {
515  RBTree_Node **link;
516  RBTree_Node *parent;
517  bool is_new_minimum;
518 
519  link = _RBTree_Root_reference( the_rbtree );
520  parent = NULL;
521  is_new_minimum = true;
522 
523  while ( *link != NULL ) {
524  parent = *link;
525 
526  if ( ( *less )( key, parent ) ) {
527  link = _RBTree_Left_reference( parent );
528  } else {
529  link = _RBTree_Right_reference( parent );
530  is_new_minimum = false;
531  }
532  }
533 
534  _RBTree_Add_child( the_node, parent, link );
535  _RBTree_Insert_color( the_rbtree, the_node );
536  return is_new_minimum;
537 }
538 
558  const RBTree_Control *the_rbtree,
559  const void *key,
560  bool ( *equal )( const void *, const RBTree_Node * ),
561  bool ( *less )( const void *, const RBTree_Node * ),
562  void *( *map )( RBTree_Node * )
563 )
564 {
565  RBTree_Node * const *link;
566  RBTree_Node *parent;
567 
568  link = _RBTree_Root_const_reference( the_rbtree );
569  parent = NULL;
570 
571  while ( *link != NULL ) {
572  parent = *link;
573 
574  if ( ( *equal )( key, parent ) ) {
575  return ( *map )( parent );
576  } else if ( ( *less )( key, parent ) ) {
577  link = _RBTree_Left_reference( parent );
578  } else {
579  link = _RBTree_Right_reference( parent );
580  }
581  }
582 
583  return NULL;
584 }
585 
632  const RBTree_Control *the_rbtree,
633  size_t offset
634 );
635 
648  const RBTree_Node *the_node,
649  size_t offset
650 );
651 
654 #ifdef __cplusplus
655 }
656 #endif
657 
658 #endif
659 /* end of include file */
static __inline__ void * _RBTree_Find_inline(const RBTree_Control *the_rbtree, const void *key, bool(*equal)(const void *, const RBTree_Node *), bool(*less)(const void *, const RBTree_Node *), void *(*map)(RBTree_Node *))
Finds an object in the red-black tree with the specified key.
Definition: rbtree.h:557
static __inline__ void _RBTree_Set_off_tree(RBTree_Node *the_node)
Sets a red-black tree node as off-tree.
Definition: rbtree.h:88
static __inline__ bool _RBTree_Insert_inline(RBTree_Control *the_rbtree, RBTree_Node *the_node, const void *key, bool(*less)(const void *, const RBTree_Node *))
Inserts the node into the red-black tree.
Definition: rbtree.h:508
static __inline__ RBTree_Node * _RBTree_Right(const RBTree_Node *the_node)
Returns pointer to the right of this node.
Definition: rbtree.h:342
static __inline__ RBTree_Node ** _RBTree_Root_reference(RBTree_Control *the_rbtree)
Returns a reference to the root pointer of the red-black tree.
Definition: rbtree.h:261
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreenext.c:51
static __inline__ void _RBTree_Initialize_empty(RBTree_Control *the_rbtree)
Initializes this RBTree as empty.
Definition: rbtree.h:410
Red-black tree node.
Definition: rbtree.h:55
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreenext.c:36
static __inline__ RBTree_Node *const * _RBTree_Root_const_reference(const RBTree_Control *the_rbtree)
Returns a constant reference to the root pointer of the red-black tree.
Definition: rbtree.h:276
static __inline__ void _RBTree_Insert_with_parent(RBTree_Control *the_rbtree, RBTree_Node *the_node, RBTree_Node *parent, RBTree_Node **link)
Inserts the node into the red-black tree using the specified parent node and link.
Definition: rbtree.h:206
Information for the Assert Handler.
static __inline__ bool _RBTree_Is_root(const RBTree_Node *the_node)
Checks if this node is the root node of a red-black tree.
Definition: rbtree.h:396
static __inline__ void _RBTree_Add_child(RBTree_Node *child, RBTree_Node *parent, RBTree_Node **link)
Adds a child node to a parent node.
Definition: rbtree.h:145
void * _RBTree_Postorder_first(const RBTree_Control *the_rbtree, size_t offset)
Returns the container of the first node of the specified red-black tree in postorder.
static __inline__ bool _RBTree_Is_empty(const RBTree_Control *the_rbtree)
Checks if the RBTree is empty.
Definition: rbtree.h:375
void _RBTree_Insert_color(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Rebalances the red-black tree after insertion of the node.
Definition: rbtreeinsert.c:17
static __inline__ RBTree_Node * _RBTree_Root(const RBTree_Control *the_rbtree)
Returns a pointer to root node of the red-black tree.
Definition: rbtree.h:246
struct RBTree_Node RBTree_Node
Red-black tree node.
void _RBTree_Replace_node(RBTree_Control *the_rbtree, RBTree_Node *victim, RBTree_Node *replacement)
Replaces a node in the red-black tree without a rebalance.
Definition: rbtreereplace.c:29
RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:46
static __inline__ bool _RBTree_Is_node_off_tree(const RBTree_Node *the_node)
Checks if this red-black tree node is off-tree.
Definition: rbtree.h:103
static __inline__ RBTree_Node * _RBTree_Parent(const RBTree_Node *the_node)
Returns a pointer to the parent of this node.
Definition: rbtree.h:295
typedef RB_HEAD(RBTree_Control, RBTree_Node) RBTree_Control
Red-black tree control.
static __inline__ void _RBTree_Initialize_one(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Initializes this red-black tree to contain exactly the specified node.
Definition: rbtree.h:424
static __inline__ RBTree_Node * _RBTree_Left(const RBTree_Node *the_node)
Returns pointer to the left of this node.
Definition: rbtree.h:311
static __inline__ RBTree_Node ** _RBTree_Left_reference(RBTree_Node *the_node)
Returns a reference to the left child pointer of the red-black tree node.
Definition: rbtree.h:326
static __inline__ void _RBTree_Initialize_node(RBTree_Node *the_node)
Initializes a red-black tree node.
Definition: rbtree.h:129
This header file provides basic definitions used by the API and the implementation.
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:35
#define RTEMS_INLINE_ROUTINE
Gives a hint to the compiler in a function declaration to inline this function.
Definition: basedefs.h:683
static __inline__ RBTree_Node ** _RBTree_Right_reference(RBTree_Node *the_node)
Returns a reference to the right child pointer of the red-black tree node.
Definition: rbtree.h:357
RBTree_Node * _RBTree_Maximum(const RBTree_Control *the_rbtree)
Returns the maximum node of the red-black tree.
Definition: rbtreenext.c:41
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100
void * _RBTree_Postorder_next(const RBTree_Node *the_node, size_t offset)
Returns the container of the next node in postorder.