RTEMS
Files | Classes | Macros | Typedefs | Functions
Red-Black Tree Handler

Red-Black Tree Handler. More...

Files

file  rbtree.h
 Constants and Structures Associated with the Red-Black Tree Handler.
 
file  rbtreeimpl.h
 Inlined Routines Associated with Red-Black Trees.
 
file  rbtreenext.c
 _RBTree_Next() and _RBTree_Next() implementation.
 
file  rbtreereplace.c
 _RBTree_Replace_node() implementation.
 

Classes

struct  RBTree_Node
 Red-black tree node. More...
 

Macros

#define RBTREE_INITIALIZER_EMPTY(name)   RB_INITIALIZER( name )
 Initializer for an empty red-black tree with designator name.
 
#define RBTREE_DEFINE_EMPTY(name)   RBTree_Control name = RBTREE_INITIALIZER_EMPTY( name )
 Definition for an empty red-black tree with designator name.
 

Typedefs

typedef struct RBTree_Node RBTree_Node
 Red-black tree node. More...
 
typedef bool(* RBTree_Visitor) (const RBTree_Node *node, void *visitor_arg)
 Red-black tree visitor. More...
 

Functions

typedef RB_HEAD (RBTree_Control, RBTree_Node) RBTree_Control
 Red-black tree control. More...
 
static __inline__ void _RBTree_Set_off_tree (RBTree_Node *the_node)
 Sets a red-black tree node as off-tree. More...
 
static __inline__ bool _RBTree_Is_node_off_tree (const RBTree_Node *the_node)
 Checks if this red-black tree node is off-tree. More...
 
void _RBTree_Insert_color (RBTree_Control *the_rbtree, RBTree_Node *the_node)
 Rebalances the red-black tree after insertion of the node. More...
 
static __inline__ void _RBTree_Initialize_node (RBTree_Node *the_node)
 Initializes a red-black tree node. More...
 
static __inline__ void _RBTree_Add_child (RBTree_Node *child, RBTree_Node *parent, RBTree_Node **link)
 Adds a child node to a parent node. More...
 
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. More...
 
void _RBTree_Extract (RBTree_Control *the_rbtree, RBTree_Node *the_node)
 Extracts (removes) the node from the red-black tree. More...
 
static __inline__ RBTree_Node_RBTree_Root (const RBTree_Control *the_rbtree)
 Returns a pointer to root node of the red-black tree. More...
 
static __inline__ RBTree_Node ** _RBTree_Root_reference (RBTree_Control *the_rbtree)
 Returns a reference to the root pointer of the red-black tree. More...
 
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. More...
 
static __inline__ RBTree_Node_RBTree_Parent (const RBTree_Node *the_node)
 Returns a pointer to the parent of this node. More...
 
static __inline__ RBTree_Node_RBTree_Left (const RBTree_Node *the_node)
 Returns pointer to the left of this node. More...
 
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. More...
 
static __inline__ RBTree_Node_RBTree_Right (const RBTree_Node *the_node)
 Returns pointer to the right of this node. More...
 
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. More...
 
static __inline__ bool _RBTree_Is_empty (const RBTree_Control *the_rbtree)
 Checks if the RBTree is empty. More...
 
static __inline__ bool _RBTree_Is_root (const RBTree_Node *the_node)
 Checks if this node is the root node of a red-black tree. More...
 
static __inline__ void _RBTree_Initialize_empty (RBTree_Control *the_rbtree)
 Initializes this RBTree as empty. More...
 
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. More...
 
RBTree_Node_RBTree_Minimum (const RBTree_Control *the_rbtree)
 Returns the minimum node of the red-black tree. More...
 
RBTree_Node_RBTree_Maximum (const RBTree_Control *the_rbtree)
 Returns the maximum node of the red-black tree. More...
 
RBTree_Node_RBTree_Predecessor (const RBTree_Node *node)
 Returns the predecessor of a node. More...
 
RBTree_Node_RBTree_Successor (const RBTree_Node *node)
 Returns the successor of a node. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void * _RBTree_Postorder_next (const RBTree_Node *the_node, size_t offset)
 Returns the container of the next node in postorder. More...
 
void _RBTree_Iterate (const RBTree_Control *rbtree, RBTree_Visitor visitor, void *visitor_arg)
 Red-black tree iteration. More...
 

Detailed Description

Red-Black Tree Handler.

The Red-Black Tree Handler is used to manage sets of entities. This handler provides two data structures. The rbtree Node data structure is included as the first part of every data structure that will be placed on a RBTree. The second data structure is rbtree Control which is used to manage a set of rbtree Nodes.

Typedef Documentation

◆ RBTree_Node

typedef struct RBTree_Node RBTree_Node

Red-black tree node.

This is used to manage each node (element) which is placed on a red-black tree.

◆ RBTree_Visitor

typedef bool(* RBTree_Visitor) (const RBTree_Node *node, void *visitor_arg)

Red-black tree visitor.

Parameters
[in]nodeThe node.
[in]visitor_argThe visitor argument.
Return values
trueStop the iteration.
falseContinue the iteration.
See also
_RBTree_Iterate().

Definition at line 50 of file rbtreeimpl.h.

Function Documentation

◆ _RBTree_Add_child()

static __inline__ void _RBTree_Add_child ( RBTree_Node child,
RBTree_Node parent,
RBTree_Node **  link 
)
static

Adds a child node to a parent node.

Parameters
childThe child node.
[out]parentThe parent node.
[out]linkThe child node link of the parent node.

Definition at line 145 of file rbtree.h.

◆ _RBTree_Extract()

void _RBTree_Extract ( RBTree_Control *  the_rbtree,
RBTree_Node the_node 
)

Extracts (removes) the node from the red-black tree.

This function does not set the node off-tree. In the case this is desired, then call _RBTree_Set_off_tree() after the extraction.

In the case the node to extract is not a node of the tree, then this function yields unpredictable results.

Parameters
[in,out]the_rbtreeThe red-black tree control.
[out]the_nodeThe node to extract.

Definition at line 35 of file rbtreeextract.c.

◆ _RBTree_Find_inline()

static __inline__ void* _RBTree_Find_inline ( const RBTree_Control *  the_rbtree,
const void *  key,
bool(*)(const void *, const RBTree_Node *)  equal,
bool(*)(const void *, const RBTree_Node *)  less,
void *(*)(RBTree_Node *)  map 
)
static

Finds an object in the red-black tree with the specified key.

Parameters
the_rbtreeThe red-black tree control.
keyThe key to look after.
equalMust return true if the specified key equals the key of the node, otherwise false.
lessMust return true if the specified key is less than the key of the node, otherwise false.
mapIn case a node with the specified key is found, then this function is called to map the node to the object returned. Usually it performs some offset operation via RTEMS_CONTAINER_OF() to map the node to its containing object. Thus, the return type is a void pointer and not a red-black tree node.
Return values
objectAn object with the specified key.
NULLNo object with the specified key exists in the red-black tree.

Definition at line 557 of file rbtree.h.

◆ _RBTree_Initialize_empty()

static __inline__ void _RBTree_Initialize_empty ( RBTree_Control *  the_rbtree)
static

Initializes this RBTree as empty.

This routine initializes the_rbtree to contain zero nodes.

Parameters
[out]the_rbtreeThe rbtree to initialize.

Definition at line 410 of file rbtree.h.

◆ _RBTree_Initialize_node()

static __inline__ void _RBTree_Initialize_node ( RBTree_Node the_node)
static

Initializes a red-black tree node.

In debug configurations, the node is set off tree. In all other configurations, this function does nothing.

Parameters
[out]the_nodeThe red-black tree node to initialize.

Definition at line 129 of file rbtree.h.

◆ _RBTree_Initialize_one()

static __inline__ void _RBTree_Initialize_one ( RBTree_Control *  the_rbtree,
RBTree_Node the_node 
)
static

Initializes this red-black tree to contain exactly the specified node.

Parameters
[out]the_rbtreeThe red-black tree control.
[out]the_nodeThe one and only node.

Definition at line 424 of file rbtree.h.

◆ _RBTree_Insert_color()

void _RBTree_Insert_color ( RBTree_Control *  the_rbtree,
RBTree_Node the_node 
)

Rebalances the red-black tree after insertion of the node.

Parameters
[in,out]the_rbtreeThe red-black tree control.
[in,out]the_nodeThe most recently inserted node.

Definition at line 17 of file rbtreeinsert.c.

◆ _RBTree_Insert_inline()

static __inline__ bool _RBTree_Insert_inline ( RBTree_Control *  the_rbtree,
RBTree_Node the_node,
const void *  key,
bool(*)(const void *, const RBTree_Node *)  less 
)
static

Inserts the node into the red-black tree.

Parameters
[in,out]the_rbtreeThe red-black tree control.
[out]the_nodeThe node to insert.
keyThe key of the node to insert. This key must be equal to the key stored in the node to insert. The separate key parameter is provided for two reasons. Firstly, it allows to share the less operator with _RBTree_Find_inline(). Secondly, the compiler may generate better code if the key is stored in a local variable.
lessMust return true if the specified key is less than the key of the node, otherwise false.
Return values
trueThe inserted node is the new minimum node according to the specified less order function.
falseThe inserted node is not the new minimum node according to the specified less order function.

Definition at line 508 of file rbtree.h.

◆ _RBTree_Insert_with_parent()

static __inline__ void _RBTree_Insert_with_parent ( RBTree_Control *  the_rbtree,
RBTree_Node the_node,
RBTree_Node parent,
RBTree_Node **  link 
)
static

Inserts the node into the red-black tree using the specified parent node and link.

Parameters
[in,out]the_rbtreeThe red-black tree control.
[in,out]the_nodeThe node to insert.
[out]parentThe parent node.
[out]linkThe child node link of the parent node.
typedef struct {
int value;
} Some_Node;
bool _Some_Less(
const RBTree_Node *a,
const RBTree_Node *b
)
{
const Some_Node *aa = RTEMS_CONTAINER_OF( a, Some_Node, Node );
const Some_Node *bb = RTEMS_CONTAINER_OF( b, Some_Node, Node );
return aa->value < bb->value;
}
void _Some_Insert(
RBTree_Control *the_rbtree,
Some_Node *the_node
)
{
RBTree_Node **link = _RBTree_Root_reference( the_rbtree );
RBTree_Node *parent = NULL;
while ( *link != NULL ) {
parent = *link;
if ( _Some_Less( &the_node->Node, parent ) ) {
link = _RBTree_Left_reference( parent );
} else {
link = _RBTree_Right_reference( parent );
}
}
_RBTree_Insert_with_parent( the_rbtree, &the_node->Node, parent, link );
}

Definition at line 206 of file rbtree.h.

◆ _RBTree_Is_empty()

static __inline__ bool _RBTree_Is_empty ( const RBTree_Control *  the_rbtree)
static

Checks if the RBTree is empty.

This function returns true if there are no nodes on the_rbtree and false otherwise.

Parameters
the_rbtreeis the rbtree to be operated upon.
Return values
trueThere are no nodes on the_rbtree.
falseThere are nodes on the_rbtree.

Definition at line 375 of file rbtree.h.

◆ _RBTree_Is_node_off_tree()

static __inline__ bool _RBTree_Is_node_off_tree ( const RBTree_Node the_node)
static

Checks if this red-black tree node is off-tree.

Parameters
the_nodeThe node to test.
Return values
trueThe node is not a part of a tree (off-tree).
falseThe node is part of a tree.
See also
_RBTree_Set_off_tree().

Definition at line 103 of file rbtree.h.

◆ _RBTree_Is_root()

static __inline__ bool _RBTree_Is_root ( const RBTree_Node the_node)
static

Checks if this node is the root node of a red-black tree.

The root node may change after insert or extract operations. In case the node is not a node of a tree, then this function yields unpredictable results.

Parameters
the_nodeThe node of interest.
Return values
truethe_node is the root node.
falsethe_node is not the root node.
See also
_RBTree_Root().

Definition at line 396 of file rbtree.h.

◆ _RBTree_Iterate()

void _RBTree_Iterate ( const RBTree_Control *  rbtree,
RBTree_Visitor  visitor,
void *  visitor_arg 
)

Red-black tree iteration.

Parameters
rbtreeThe red-black tree.
visitorThe visitor.
visitor_argThe visitor argument.

◆ _RBTree_Left()

static __inline__ RBTree_Node* _RBTree_Left ( const RBTree_Node the_node)
static

Returns pointer to the left of this node.

This function returns a pointer to the left node of this node.

Parameters
the_nodeis the node to be operated upon.
Returns
This method returns the left node on the rbtree.

Definition at line 311 of file rbtree.h.

◆ _RBTree_Left_reference()

static __inline__ RBTree_Node** _RBTree_Left_reference ( RBTree_Node the_node)
static

Returns a reference to the left child pointer of the red-black tree node.

Parameters
the_nodeis the node to be operated upon.
Returns
This method returns a reference to the left child pointer on the rbtree.

Definition at line 326 of file rbtree.h.

◆ _RBTree_Maximum()

RBTree_Node* _RBTree_Maximum ( const RBTree_Control *  the_rbtree)

Returns the maximum node of the red-black tree.

Parameters
the_rbtreeThe red-black tree control.
Return values
nodeThe maximum node.
NULLThe red-black tree is empty.

Definition at line 41 of file rbtreenext.c.

◆ _RBTree_Minimum()

RBTree_Node* _RBTree_Minimum ( const RBTree_Control *  the_rbtree)

Returns the minimum node of the red-black tree.

Parameters
the_rbtreeThe red-black tree control.
Return values
nodeThe minimum node.
NULLThe red-black tree is empty.

Definition at line 36 of file rbtreenext.c.

◆ _RBTree_Parent()

static __inline__ RBTree_Node* _RBTree_Parent ( const RBTree_Node the_node)
static

Returns a pointer to the parent of this node.

The node must have a parent, thus it is invalid to use this function for the root node or a node that is not part of a tree. To test for the root node compare with _RBTree_Root() or use _RBTree_Is_root().

Parameters
the_nodeThe node of interest.
Return values
parentThe parent of this node.
undefinedThe node is the root node or not part of a tree.

Definition at line 295 of file rbtree.h.

◆ _RBTree_Postorder_first()

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.

Postorder traversal may be used to delete all nodes of a red-black tree.

Parameters
the_rbtreeThe red-black tree control.
offsetThe offset to the red-black tree node in the container structure.
Return values
containerThe container of the first node of the specified red-black tree in postorder.
NULLThe red-black tree is empty.
See also
_RBTree_Postorder_next().
typedef struct {
int data;
} Container_Control;
void visit( Container_Control *the_container );
void postorder_traversal( RBTree_Control *the_rbtree )
{
Container_Control *the_container;
the_container = _RBTree_Postorder_first(
the_rbtree,
offsetof( Container_Control, Node )
);
while ( the_container != NULL ) {
visit( the_container );
the_container = _RBTree_Postorder_next(
&the_container->Node,
offsetof( Container_Control, Node )
);
}
}

◆ _RBTree_Postorder_next()

void* _RBTree_Postorder_next ( const RBTree_Node the_node,
size_t  offset 
)

Returns the container of the next node in postorder.

Parameters
the_nodeThe red-black tree node. The node must not be NULL.
offsetThe offset to the red-black tree node in the container structure.
Return values
containerThe container of the next node in postorder.
NULLThe node is NULL or there is no next node in postorder.
See also
_RBTree_Postorder_first().

◆ _RBTree_Predecessor()

RBTree_Node* _RBTree_Predecessor ( const RBTree_Node node)

Returns the predecessor of a node.

Parameters
nodeis the node.
Return values
nodeThe predecessor node.
NULLThe predecessor does not exist.

Definition at line 51 of file rbtreenext.c.

◆ _RBTree_Replace_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.

Parameters
[in,out]the_rbtreeThe red-black tree control.
victimThe victim node.
[out]replacementThe replacement node.

Definition at line 29 of file rbtreereplace.c.

◆ _RBTree_Right()

static __inline__ RBTree_Node* _RBTree_Right ( const RBTree_Node the_node)
static

Returns pointer to the right of this node.

This function returns a pointer to the right node of this node.

Parameters
the_nodeis the node to be operated upon.
Returns
This method returns the right node on the rbtree.

Definition at line 342 of file rbtree.h.

◆ _RBTree_Right_reference()

static __inline__ RBTree_Node** _RBTree_Right_reference ( RBTree_Node the_node)
static

Returns a reference to the right child pointer of the red-black tree node.

Parameters
the_nodeis the node to be operated upon.
Returns
This method returns a reference to the right child pointer on the rbtree.

Definition at line 357 of file rbtree.h.

◆ _RBTree_Root()

static __inline__ RBTree_Node* _RBTree_Root ( const RBTree_Control *  the_rbtree)
static

Returns a pointer to root node of the red-black tree.

The root node may change after insert or extract operations.

Parameters
the_rbtreeThe red-black tree control.
Return values
rootThe root node.
NULLThe tree is empty.
See also
_RBTree_Is_root().

Definition at line 246 of file rbtree.h.

◆ _RBTree_Root_const_reference()

static __inline__ RBTree_Node* const* _RBTree_Root_const_reference ( const RBTree_Control *  the_rbtree)
static

Returns a constant reference to the root pointer of the red-black tree.

Parameters
the_rbtreeThe red-black tree control.
Return values
pointerPointer to the root node.
NULLThe tree is empty.

Definition at line 276 of file rbtree.h.

◆ _RBTree_Root_reference()

static __inline__ RBTree_Node** _RBTree_Root_reference ( RBTree_Control *  the_rbtree)
static

Returns a reference to the root pointer of the red-black tree.

Parameters
the_rbtreeThe red-black tree control.
Return values
pointerPointer to the root node.
NULLThe tree is empty.

Definition at line 261 of file rbtree.h.

◆ _RBTree_Set_off_tree()

static __inline__ void _RBTree_Set_off_tree ( RBTree_Node the_node)
static

Sets a red-black tree node as off-tree.

Do not use this function on nodes which are a part of a tree.

Parameters
[out]the_nodeThe node to set off-tree.
See also
_RBTree_Is_node_off_tree().

Definition at line 88 of file rbtree.h.

◆ _RBTree_Successor()

RBTree_Node* _RBTree_Successor ( const RBTree_Node node)

Returns the successor of a node.

Parameters
nodeis the node.
Return values
nodeThe successor node.
NULLThe successor does not exist.

Definition at line 46 of file rbtreenext.c.

◆ RB_HEAD()

typedef RB_HEAD ( RBTree_Control  ,
RBTree_Node   
)

Red-black tree control.

This is used to manage a red-black tree. A red-black tree consists of a tree of zero or more nodes.