RTEMS: Red-Black Tree Handler

| RTEMS 5.4-rc2

Loading...
Searching...
No Matches
Files | Data Structures | Macros | Typedefs | Functions
Red-Black Tree Handler
Internal » SuperCore

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.
typedef bool(* | RBTree_Visitor) (const RBTree_Node *node, void *visitor_arg)
| Red-black tree visitor.

## Functions
typedef | RB_HEAD (RBTree_Control, RBTree_Node) RBTree_Control
| Red-black tree control.
RTEMS_INLINE_ROUTINE void | _RBTree_Set_off_tree (RBTree_Node *the_node)
| Sets a red-black tree node as 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.
void | _RBTree_Insert_color (RBTree_Control *the_rbtree, RBTree_Node *the_node)
| Rebalances the red-black tree after insertion of the node.
RTEMS_INLINE_ROUTINE void | _RBTree_Initialize_node (RBTree_Node *the_node)
| Initializes a red-black tree node.
RTEMS_INLINE_ROUTINE void | _RBTree_Add_child (RBTree_Node *child, RBTree_Node *parent, RBTree_Node **link)
| Adds a child node to a parent node.
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.
void | _RBTree_Extract (RBTree_Control *the_rbtree, RBTree_Node *the_node)
| Extracts (removes) the node from the red-black tree.
RTEMS_INLINE_ROUTINE RBTree_Node * | _RBTree_Root (const RBTree_Control *the_rbtree)
| Returns a pointer to root node of the red-black tree.
RTEMS_INLINE_ROUTINE RBTree_Node ** | _RBTree_Root_reference (RBTree_Control *the_rbtree)
| Returns a reference to the root pointer of the red-black tree.
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.
RTEMS_INLINE_ROUTINE RBTree_Node * | _RBTree_Parent (const RBTree_Node *the_node)
| Returns a pointer to the parent of this node.
RTEMS_INLINE_ROUTINE RBTree_Node * | _RBTree_Left (const RBTree_Node *the_node)
| Returns pointer to the left of this node.
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.
RTEMS_INLINE_ROUTINE RBTree_Node * | _RBTree_Right (const RBTree_Node *the_node)
| Returns pointer to the right of this node.
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.
RTEMS_INLINE_ROUTINE bool | _RBTree_Is_empty (const RBTree_Control *the_rbtree)
| Checks if the RBTree is empty.
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.
RTEMS_INLINE_ROUTINE void | _RBTree_Initialize_empty (RBTree_Control *the_rbtree)
| Initializes this RBTree as empty.
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.
RBTree_Node * | _RBTree_Minimum (const RBTree_Control *the_rbtree)
| Returns the minimum node of the red-black tree.
RBTree_Node * | _RBTree_Maximum (const RBTree_Control *the_rbtree)
| Returns the maximum node of the red-black tree.
RBTree_Node * | _RBTree_Predecessor (const RBTree_Node *node)
| Returns the predecessor of a node.
RBTree_Node * | _RBTree_Successor (const RBTree_Node *node)
| Returns the successor of a 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.
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.
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.
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.
void * | _RBTree_Postorder_next (const RBTree_Node *the_node, size_t offset)
| Returns the container of the next node in postorder.
void | _RBTree_Iterate (const RBTree_Control *rbtree, RBTree_Visitor visitor, void *visitor_arg)
| Red-black tree iteration.

## 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] | node | The node.
  [in] | visitor_arg | The visitor argument.

Return values

  true | Stop the iteration.
  false | Continue 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

  | child | The child node.
  [out] | parent | The parent node.
  [out] | link | The 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_rbtree | The red-black tree control.
  [out] | the_node | The 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_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.

Return values

  object | An object with the specified key.
  NULL | No 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_rbtree | The 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_node | The 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_rbtree | The red-black tree control.
  [out] | the_node | The 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_rbtree | The red-black tree control.
  [in,out] | the_node | The 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_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.

Return values

  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.

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

#include <rtems/score/rbtree.h>
typedef struct {
int value;
RBTree_Node Node;
} 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 );
}
NULL
#define NULL
Requests a GPIO pin group configuration.
Definition: bestcomm_api.h:77
RTEMS_CONTAINER_OF
#define RTEMS_CONTAINER_OF(_m, _type, _member_name)
Returns a pointer to the container of a specified member pointer.
Definition: basedefs.h:430
_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.
Definition: rbtree.h:206
_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.
Definition: rbtree.h:261
_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.
Definition: rbtree.h:326
_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.
Definition: rbtree.h:357
link
int link(const char *path1, const char *path2)
Definition: link.c:28
rbtree.h
Constants and Structures Associated with the Red-Black Tree Handler.
RBTree_Node
Red-black tree node.
Definition: rbtree.h:55

## ◆ _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_rbtree | is the rbtree to be operated upon.

Return values

  true | There are no nodes on the_rbtree.
  false | There 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_node | The node to test.

Return values

  true | The node is not a part of a tree (off-tree).
  false | The 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_node | The node of interest.

Return values

  true | the_node is the root node.
  false | the_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

  rbtree | The red-black tree.
  visitor | The visitor.
  visitor_arg | The 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_node | is 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_node | is 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_rbtree | The red-black tree control.

Return values

  node | The maximum node.
  NULL | The 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_rbtree | The red-black tree control.

Return values

  node | The minimum node.
  NULL | The 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_node | The node of interest.

Return values

  parent | The parent of this node.
  undefined | The 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_rbtree | The red-black tree control.
  offset | The offset to the red-black tree node in the container structure.

Return values

  container | The container of the first node of the specified red-black tree in postorder.
  NULL | The red-black tree is empty.

See also
  _RBTree_Postorder_next().

#include <rtems/score/rbtree.h>
typedef struct {
int data;
RBTree_Node Node;
} 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_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.
Definition: rbtreepostorder.c:68
_RBTree_Postorder_next
void * _RBTree_Postorder_next(const RBTree_Node *the_node, size_t offset)
Returns the container of the next node in postorder.
Definition: rbtreepostorder.c:46

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

Return values

  container | The container of the next node in postorder.
  NULL | The 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

  node | is the node.

Return values

  node | The predecessor node.
  NULL | The 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_rbtree | The red-black tree control.
  | victim | The victim node.
  [out] | replacement | The 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_node | is 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_node | is 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_rbtree | The red-black tree control.

Return values

  root | The root node.
  NULL | The 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_rbtree | The red-black tree control.

Return values

  pointer | Pointer to the root node.
  NULL | The 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_rbtree | The red-black tree control.

Return values

  pointer | Pointer to the root node.
  NULL | The 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_node | The 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

  node | is the node.

Return values

  node | The successor node.
  NULL | The 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.