RTEMS  5.1
Files | Data Structures | 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  rbtreeiterate.c
 _RBTree_Iterate() implementation.
 
file  rbtreenext.c
 _RBTree_Next() and _RBTree_Next() implementation.
 
file  rbtreepostorder.c
 _RBTree_Postorder_first() and _RBTree_Postorder_next() implementation.
 
file  rbtreereplace.c
 _RBTree_Replace_node() implementation.
 

Data Structures

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...
 
RTEMS_INLINE_ROUTINE void _RBTree_Set_off_tree (RBTree_Node *the_node)
 Sets a red-black tree node as off-tree. More...
 
RTEMS_INLINE_ROUTINE 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...
 
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node (RBTree_Node *the_node)
 Initializes a red-black tree node. More...
 
RTEMS_INLINE_ROUTINE void _RBTree_Add_child (RBTree_Node *child, RBTree_Node *parent, RBTree_Node **link)
 Adds a child node to a parent node. More...
 
RTEMS_INLINE_ROUTINE 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...
 
RTEMS_INLINE_ROUTINE RBTree_Node_RBTree_Root (const RBTree_Control *the_rbtree)
 Returns a pointer to root node of the red-black tree. More...
 
RTEMS_INLINE_ROUTINE RBTree_Node ** _RBTree_Root_reference (RBTree_Control *the_rbtree)
 Returns a reference to the root pointer of the red-black tree. More...
 
RTEMS_INLINE_ROUTINE 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...
 
RTEMS_INLINE_ROUTINE RBTree_Node_RBTree_Parent (const RBTree_Node *the_node)
 Returns a pointer to the parent of this node. More...
 
RTEMS_INLINE_ROUTINE RBTree_Node_RBTree_Left (const RBTree_Node *the_node)
 Returns pointer to the left of this node. More...
 
RTEMS_INLINE_ROUTINE RBTree_Node ** _RBTree_Left_reference (RBTree_Node *the_node)
 Returns a reference to the left child pointer of the red-black tree node. More...
 
RTEMS_INLINE_ROUTINE RBTree_Node_RBTree_Right (const RBTree_Node *the_node)
 Returns pointer to the right of this node. More...
 
RTEMS_INLINE_ROUTINE RBTree_Node ** _RBTree_Right_reference (RBTree_Node *the_node)
 Returns a reference to the right child pointer of the red-black tree node. More...
 
RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty (const RBTree_Control *the_rbtree)
 Checks if the RBTree is empty. More...
 
RTEMS_INLINE_ROUTINE bool _RBTree_Is_root (const RBTree_Node *the_node)
 Checks if this node is the root node of a red-black tree. More...
 
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty (RBTree_Control *the_rbtree)
 Initializes this RBTree as empty. More...
 
RTEMS_INLINE_ROUTINE 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...
 
RTEMS_INLINE_ROUTINE 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...
 
RTEMS_INLINE_ROUTINE 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().

Function Documentation

◆ _RBTree_Add_child()

RTEMS_INLINE_ROUTINE void _RBTree_Add_child ( RBTree_Node child,
RBTree_Node parent,
RBTree_Node **  link 
)

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.

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

◆ _RBTree_Find_inline()

RTEMS_INLINE_ROUTINE 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 
)

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.

◆ _RBTree_Initialize_empty()

RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty ( RBTree_Control *  the_rbtree)

Initializes this RBTree as empty.

This routine initializes the_rbtree to contain zero nodes.

Parameters
[out]the_rbtreeThe rbtree to initialize.

◆ _RBTree_Initialize_node()

RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node ( RBTree_Node the_node)

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.

◆ _RBTree_Initialize_one()

RTEMS_INLINE_ROUTINE void _RBTree_Initialize_one ( RBTree_Control *  the_rbtree,
RBTree_Node the_node 
)

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.

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

◆ _RBTree_Insert_inline()

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

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.

◆ _RBTree_Insert_with_parent()

RTEMS_INLINE_ROUTINE 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.

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 *parent = NULL;
while ( *link != NULL ) {
parent = *link;
if ( _Some_Less( &the_node->Node, parent ) ) {
} else {
}
}
_RBTree_Insert_with_parent( the_rbtree, &the_node->Node, parent, link );
}

◆ _RBTree_Is_empty()

RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty ( const RBTree_Control *  the_rbtree)

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.

◆ _RBTree_Is_node_off_tree()

RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_tree ( const RBTree_Node the_node)

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().

◆ _RBTree_Is_root()

RTEMS_INLINE_ROUTINE bool _RBTree_Is_root ( const RBTree_Node the_node)

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().

◆ _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()

RTEMS_INLINE_ROUTINE RBTree_Node* _RBTree_Left ( const RBTree_Node the_node)

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.

◆ _RBTree_Left_reference()

RTEMS_INLINE_ROUTINE RBTree_Node** _RBTree_Left_reference ( RBTree_Node the_node)

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.

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

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

◆ _RBTree_Parent()

RTEMS_INLINE_ROUTINE RBTree_Node* _RBTree_Parent ( const RBTree_Node the_node)

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.

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

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

◆ _RBTree_Right()

RTEMS_INLINE_ROUTINE RBTree_Node* _RBTree_Right ( const RBTree_Node the_node)

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.

◆ _RBTree_Right_reference()

RTEMS_INLINE_ROUTINE RBTree_Node** _RBTree_Right_reference ( RBTree_Node the_node)

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.

◆ _RBTree_Root()

RTEMS_INLINE_ROUTINE RBTree_Node* _RBTree_Root ( const RBTree_Control *  the_rbtree)

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().

◆ _RBTree_Root_const_reference()

RTEMS_INLINE_ROUTINE 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.

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

◆ _RBTree_Root_reference()

RTEMS_INLINE_ROUTINE RBTree_Node** _RBTree_Root_reference ( RBTree_Control *  the_rbtree)

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.

◆ _RBTree_Set_off_tree()

RTEMS_INLINE_ROUTINE void _RBTree_Set_off_tree ( RBTree_Node the_node)

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().

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

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