RTEMS
5.1
|
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... | |
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 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.
typedef bool(* RBTree_Visitor) (const RBTree_Node *node, void *visitor_arg) |
Red-black tree visitor.
[in] | node | The node. |
[in] | visitor_arg | The visitor argument. |
true | Stop the iteration. |
false | Continue the iteration. |
RTEMS_INLINE_ROUTINE void _RBTree_Add_child | ( | RBTree_Node * | child, |
RBTree_Node * | parent, | ||
RBTree_Node ** | link | ||
) |
Adds a child node to a parent node.
child | The child node. | |
[out] | parent | The parent node. |
[out] | link | The child node link of the parent node. |
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.
[in,out] | the_rbtree | The red-black tree control. |
[out] | the_node | The node to extract. |
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.
the_rbtree | The red-black tree control. |
key | The key to look after. |
equal | Must return true if the specified key equals the key of the node, otherwise false. |
less | Must return true if the specified key is less than the key of the node, otherwise false. |
map | In 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. |
object | An object with the specified key. |
NULL | No object with the specified key exists in the red-black tree. |
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.
[out] | the_rbtree | The rbtree to initialize. |
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.
[out] | the_node | The red-black tree node to initialize. |
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.
[out] | the_rbtree | The red-black tree control. |
[out] | the_node | The one and only node. |
void _RBTree_Insert_color | ( | RBTree_Control * | the_rbtree, |
RBTree_Node * | the_node | ||
) |
Rebalances the red-black tree after insertion of the node.
[in,out] | the_rbtree | The red-black tree control. |
[in,out] | the_node | The most recently inserted node. |
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.
[in,out] | the_rbtree | The red-black tree control. |
[out] | the_node | The node to insert. |
key | The 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. | |
less | Must return true if the specified key is less than the key of the node, otherwise false. |
true | The inserted node is the new minimum node according to the specified less order function. |
false | The inserted node is not the new minimum node according to the specified less order function. |
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.
[in,out] | the_rbtree | The red-black tree control. |
[in,out] | the_node | The node to insert. |
[out] | parent | The parent node. |
[out] | link | The child node link of the parent node. |
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.
the_rbtree | is the rbtree to be operated upon. |
true | There are no nodes on the_rbtree. |
false | There are nodes on the_rbtree. |
RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_tree | ( | const RBTree_Node * | the_node | ) |
Checks if this red-black tree node is off-tree.
the_node | The node to test. |
true | The node is not a part of a tree (off-tree). |
false | The node is part of a tree. |
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.
the_node | The node of interest. |
true | the_node is the root node. |
false | the_node is not the root node. |
void _RBTree_Iterate | ( | const RBTree_Control * | rbtree, |
RBTree_Visitor | visitor, | ||
void * | visitor_arg | ||
) |
Red-black tree iteration.
rbtree | The red-black tree. |
visitor | The visitor. |
visitor_arg | The visitor argument. |
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.
the_node | is the node to be operated upon. |
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.
the_node | is the node to be operated upon. |
RBTree_Node* _RBTree_Maximum | ( | const RBTree_Control * | the_rbtree | ) |
Returns the maximum node of the red-black tree.
the_rbtree | The red-black tree control. |
node | The maximum node. |
NULL | The red-black tree is empty. |
RBTree_Node* _RBTree_Minimum | ( | const RBTree_Control * | the_rbtree | ) |
Returns the minimum node of the red-black tree.
the_rbtree | The red-black tree control. |
node | The minimum node. |
NULL | The red-black tree is empty. |
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().
the_node | The node of interest. |
parent | The parent of this node. |
undefined | The node is the root node or not part of a tree. |
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.
the_rbtree | The red-black tree control. |
offset | The offset to the red-black tree node in the container structure. |
container | The container of the first node of the specified red-black tree in postorder. |
NULL | The red-black tree is empty. |
void* _RBTree_Postorder_next | ( | const RBTree_Node * | the_node, |
size_t | offset | ||
) |
Returns the container of the next node in postorder.
the_node | The red-black tree node. The node must not be NULL. |
offset | The offset to the red-black tree node in the container structure. |
container | The container of the next node in postorder. |
NULL | The node is NULL or there is no next node in postorder. |
RBTree_Node* _RBTree_Predecessor | ( | const RBTree_Node * | node | ) |
Returns the predecessor of a node.
node | is the node. |
node | The predecessor node. |
NULL | The predecessor does not exist. |
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.
[in,out] | the_rbtree | The red-black tree control. |
victim | The victim node. | |
[out] | replacement | The replacement node. |
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.
the_node | is the node to be operated upon. |
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.
the_node | is the node to be operated upon. |
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.
the_rbtree | The red-black tree control. |
root | The root node. |
NULL | The tree is empty. |
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.
the_rbtree | The red-black tree control. |
pointer | Pointer to the root node. |
NULL | The tree is empty. |
RTEMS_INLINE_ROUTINE RBTree_Node** _RBTree_Root_reference | ( | RBTree_Control * | the_rbtree | ) |
Returns a reference to the root pointer of the red-black tree.
the_rbtree | The red-black tree control. |
pointer | Pointer to the root node. |
NULL | The tree is empty. |
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.
[out] | the_node | The node to set off-tree. |
RBTree_Node* _RBTree_Successor | ( | const RBTree_Node * | node | ) |
Returns the successor of a node.
node | is the node. |
node | The successor node. |
NULL | The successor does not exist. |
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.