RTEMS: cpukit/include/rtems/score/rbtree.h Source File

| RTEMS 5.4-rc2

Loading...
Searching...
No Matches
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
28extern "C" {
29#endif
30
47struct 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
88 RTEMS_INLINE_ROUTINE void _RBTree_Set_off_tree( RBTree_Node *the_node )
89{
90 RB_COLOR( the_node, Node ) = -1;
91}
92
103 RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_tree(
104 const RBTree_Node *the_node
105)
106{
107 return RB_COLOR( the_node, Node ) == -1;
108}
109
116void _RBTree_Insert_color(
117 RBTree_Control *the_rbtree,
118 RBTree_Node *the_node
119);
120
129 RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node( RBTree_Node *the_node )
130{
131#if defined(RTEMS_DEBUG)
132 _RBTree_Set_off_tree( the_node );
133#else
134 (void) the_node;
135#endif
136}
137
145 RTEMS_INLINE_ROUTINE void _RBTree_Add_child(
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
206 RTEMS_INLINE_ROUTINE void _RBTree_Insert_with_parent(
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
229void _RBTree_Extract(
230 RBTree_Control *the_rbtree,
231 RBTree_Node *the_node
232);
233
246 RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Root(
247 const RBTree_Control *the_rbtree
248)
249{
250 return RB_ROOT( the_rbtree );
251}
252
261 RTEMS_INLINE_ROUTINE RBTree_Node **_RBTree_Root_reference(
262 RBTree_Control *the_rbtree
263)
264{
265 return &RB_ROOT( the_rbtree );
266}
267
276 RTEMS_INLINE_ROUTINE RBTree_Node * const *_RBTree_Root_const_reference(
277 const RBTree_Control *the_rbtree
278)
279{
280 return &RB_ROOT( the_rbtree );
281}
282
295 RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Parent(
296 const RBTree_Node *the_node
297)
298{
299 return RB_PARENT( the_node, Node );
300}
301
311 RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Left(
312 const RBTree_Node *the_node
313)
314{
315 return RB_LEFT( the_node, Node );
316}
317
326 RTEMS_INLINE_ROUTINE RBTree_Node **_RBTree_Left_reference(
327 RBTree_Node *the_node
328)
329{
330 return &RB_LEFT( the_node, Node );
331}
332
342 RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Right(
343 const RBTree_Node *the_node
344)
345{
346 return RB_RIGHT( the_node, Node );
347}
348
357 RTEMS_INLINE_ROUTINE RBTree_Node **_RBTree_Right_reference(
358 RBTree_Node *the_node
359)
360{
361 return &RB_RIGHT( the_node, Node );
362}
363
375 RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty(
376 const RBTree_Control *the_rbtree
377)
378{
379 return RB_EMPTY( the_rbtree );
380}
381
396 RTEMS_INLINE_ROUTINE bool _RBTree_Is_root(
397 const RBTree_Node *the_node
398)
399{
400 return _RBTree_Parent( the_node ) == NULL;
401}
402
410 RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty(
411 RBTree_Control *the_rbtree
412)
413{
414 RB_INIT( the_rbtree );
415}
416
424 RTEMS_INLINE_ROUTINE void _RBTree_Initialize_one(
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
465 RBTree_Node *_RBTree_Predecessor( const RBTree_Node *node );
466
475 RBTree_Node *_RBTree_Successor( const RBTree_Node *node );
476
484void _RBTree_Replace_node(
485 RBTree_Control *the_rbtree,
486 RBTree_Node *victim,
487 RBTree_Node *replacement
488);
489
508 RTEMS_INLINE_ROUTINE bool _RBTree_Insert_inline(
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
557 RTEMS_INLINE_ROUTINE void *_RBTree_Find_inline(
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
631void *_RBTree_Postorder_first(
632 const RBTree_Control *the_rbtree,
633 size_t offset
634);
635
647void *_RBTree_Postorder_next(
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 */
assert.h
Information for the Assert Handler.
basedefs.h
Basic Definitions.
NULL
#define NULL
Requests a GPIO pin group configuration.
Definition: bestcomm_api.h:77
_Assert
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG.
Definition: assert.h:100
RTEMS_INLINE_ROUTINE
#define RTEMS_INLINE_ROUTINE
Definition: basedefs.h:66
_RBTree_Predecessor
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreenext.c:51
_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.
Definition: rbtree.h:145
_RBTree_Parent
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Parent(const RBTree_Node *the_node)
Returns a pointer to the parent of this node.
Definition: rbtree.h:295
_RBTree_Is_empty
RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty(const RBTree_Control *the_rbtree)
Checks if the RBTree is empty.
Definition: rbtree.h:375
_RBTree_Extract
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:35
_RBTree_Maximum
RBTree_Node * _RBTree_Maximum(const RBTree_Control *the_rbtree)
Returns the maximum node of the red-black tree.
Definition: rbtreenext.c:41
_RBTree_Initialize_empty
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty(RBTree_Control *the_rbtree)
Initializes this RBTree as empty.
Definition: rbtree.h:410
_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.
Definition: rbtree.h:103
_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_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.
Definition: rbtree.h:424
RB_HEAD
typedef RB_HEAD(RBTree_Control, RBTree_Node) RBTree_Control
Red-black tree control.
_RBTree_Find_inline
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.
Definition: rbtree.h:557
_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.
Definition: rbtree.h:88
_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.
Definition: rbtree.h:396
_RBTree_Right
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Right(const RBTree_Node *the_node)
Returns pointer to the right of this node.
Definition: rbtree.h:342
_RBTree_Successor
RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:46
_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_Insert_color
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
_RBTree_Left
RTEMS_INLINE_ROUTINE RBTree_Node * _RBTree_Left(const RBTree_Node *the_node)
Returns pointer to the left of this node.
Definition: rbtree.h:311
_RBTree_Insert_inline
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.
Definition: rbtree.h:508
_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_Initialize_node
RTEMS_INLINE_ROUTINE void _RBTree_Initialize_node(RBTree_Node *the_node)
Initializes a red-black tree node.
Definition: rbtree.h:129
_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_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.
Definition: rbtreereplace.c:29
_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
_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.
Definition: rbtree.h:276
_RBTree_Minimum
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreenext.c:36
_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.
Definition: rbtree.h:246
link
int link(const char *path1, const char *path2)
Definition: link.c:28
RBTree_Node
Red-black tree node.
Definition: rbtree.h:55
_map
Definition: mm.c:60