RTEMS 6.1-rc7
Loading...
Searching...
No Matches
rbtree.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
13/*
14 * Copyright (c) 2010 Gedare Bloom.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef _RTEMS_SCORE_RBTREE_H
39#define _RTEMS_SCORE_RBTREE_H
40
43#include <rtems/score/assert.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
65struct RBTree_Control;
66
73typedef struct RBTree_Node {
74 RTEMS_RB_ENTRY(RBTree_Node) Node;
76
83typedef RTEMS_RB_HEAD(RBTree_Control, RBTree_Node) RBTree_Control;
84
88#define RBTREE_INITIALIZER_EMPTY( name ) \
89 RTEMS_RB_INITIALIZER( name )
90
94#define RBTREE_DEFINE_EMPTY( name ) \
95 RBTree_Control name = RBTREE_INITIALIZER_EMPTY( name )
96
106static inline void _RBTree_Set_off_tree( RBTree_Node *the_node )
107{
108 RTEMS_RB_COLOR( the_node, Node ) = -1;
109}
110
121static inline bool _RBTree_Is_node_off_tree(
122 const RBTree_Node *the_node
123)
124{
125 return RTEMS_RB_COLOR( the_node, Node ) == -1;
126}
127
135 RBTree_Control *the_rbtree,
136 RBTree_Node *the_node
137);
138
147static inline void _RBTree_Initialize_node( RBTree_Node *the_node )
148{
149#if defined(RTEMS_DEBUG)
150 _RBTree_Set_off_tree( the_node );
151#else
152 (void) the_node;
153#endif
154}
155
163static inline void _RBTree_Add_child(
164 RBTree_Node *child,
165 RBTree_Node *parent,
167)
168{
169 _Assert( _RBTree_Is_node_off_tree( child ) );
170 RTEMS_RB_SET( child, parent, Node );
171 *link = child;
172}
173
224static inline void _RBTree_Insert_with_parent(
225 RBTree_Control *the_rbtree,
226 RBTree_Node *the_node,
227 RBTree_Node *parent,
229)
230{
231 _RBTree_Add_child( the_node, parent, link );
232 _RBTree_Insert_color( the_rbtree, the_node );
233}
234
247void _RBTree_Extract(
248 RBTree_Control *the_rbtree,
249 RBTree_Node *the_node
250);
251
264static inline RBTree_Node *_RBTree_Root(
265 const RBTree_Control *the_rbtree
266)
267{
268 return RTEMS_RB_ROOT( the_rbtree );
269}
270
279static inline RBTree_Node **_RBTree_Root_reference(
280 RBTree_Control *the_rbtree
281)
282{
283 return &RTEMS_RB_ROOT( the_rbtree );
284}
285
294static inline RBTree_Node * const *_RBTree_Root_const_reference(
295 const RBTree_Control *the_rbtree
296)
297{
298 return &RTEMS_RB_ROOT( the_rbtree );
299}
300
313static inline RBTree_Node *_RBTree_Parent(
314 const RBTree_Node *the_node
315)
316{
317 return RTEMS_RB_PARENT( the_node, Node );
318}
319
329static inline RBTree_Node *_RBTree_Left(
330 const RBTree_Node *the_node
331)
332{
333 return RTEMS_RB_LEFT( the_node, Node );
334}
335
344static inline RBTree_Node **_RBTree_Left_reference(
345 RBTree_Node *the_node
346)
347{
348 return &RTEMS_RB_LEFT( the_node, Node );
349}
350
360static inline RBTree_Node *_RBTree_Right(
361 const RBTree_Node *the_node
362)
363{
364 return RTEMS_RB_RIGHT( the_node, Node );
365}
366
375static inline RBTree_Node **_RBTree_Right_reference(
376 RBTree_Node *the_node
377)
378{
379 return &RTEMS_RB_RIGHT( the_node, Node );
380}
381
393static inline bool _RBTree_Is_empty(
394 const RBTree_Control *the_rbtree
395)
396{
397 return RTEMS_RB_EMPTY( the_rbtree );
398}
399
414static inline bool _RBTree_Is_root(
415 const RBTree_Node *the_node
416)
417{
418 return _RBTree_Parent( the_node ) == NULL;
419}
420
428static inline void _RBTree_Initialize_empty(
429 RBTree_Control *the_rbtree
430)
431{
432 RTEMS_RB_INIT( the_rbtree );
433}
434
442static inline void _RBTree_Initialize_one(
443 RBTree_Control *the_rbtree,
444 RBTree_Node *the_node
445)
446{
447 _Assert( _RBTree_Is_node_off_tree( the_node ) );
448 RTEMS_RB_ROOT( the_rbtree ) = the_node;
449 RTEMS_RB_PARENT( the_node, Node ) = NULL;
450 RTEMS_RB_LEFT( the_node, Node ) = NULL;
451 RTEMS_RB_RIGHT( the_node, Node ) = NULL;
452 RTEMS_RB_COLOR( the_node, Node ) = RTEMS_RB_BLACK;
453}
454
463RBTree_Node *_RBTree_Minimum( const RBTree_Control *the_rbtree );
464
473RBTree_Node *_RBTree_Maximum( const RBTree_Control *the_rbtree );
474
484
494
503 RBTree_Control *the_rbtree,
504 RBTree_Node *victim,
505 RBTree_Node *replacement
506);
507
526static inline bool _RBTree_Insert_inline(
527 RBTree_Control *the_rbtree,
528 RBTree_Node *the_node,
529 const void *key,
530 bool ( *less )( const void *, const RBTree_Node * )
531)
532{
534 RBTree_Node *parent;
535 bool is_new_minimum;
536
537 link = _RBTree_Root_reference( the_rbtree );
538 parent = NULL;
539 is_new_minimum = true;
540
541 while ( *link != NULL ) {
542 parent = *link;
543
544 if ( ( *less )( key, parent ) ) {
545 link = _RBTree_Left_reference( parent );
546 } else {
547 link = _RBTree_Right_reference( parent );
548 is_new_minimum = false;
549 }
550 }
551
552 _RBTree_Add_child( the_node, parent, link );
553 _RBTree_Insert_color( the_rbtree, the_node );
554 return is_new_minimum;
555}
556
575static inline void *_RBTree_Find_inline(
576 const RBTree_Control *the_rbtree,
577 const void *key,
578 bool ( *equal )( const void *, const RBTree_Node * ),
579 bool ( *less )( const void *, const RBTree_Node * ),
580 void *( *map )( RBTree_Node * )
581)
582{
583 RBTree_Node * const *link;
584 RBTree_Node *parent;
585
586 link = _RBTree_Root_const_reference( the_rbtree );
587 parent = NULL;
588
589 while ( *link != NULL ) {
590 parent = *link;
591
592 if ( ( *equal )( key, parent ) ) {
593 return ( *map )( parent );
594 } else if ( ( *less )( key, parent ) ) {
595 link = _RBTree_Left_reference( parent );
596 } else {
597 link = _RBTree_Right_reference( parent );
598 }
599 }
600
601 return NULL;
602}
603
650 const RBTree_Control *the_rbtree,
651 size_t offset
652);
653
666 const RBTree_Node *the_node,
667 size_t offset
668);
669
672#ifdef __cplusplus
673}
674#endif
675
676#endif
677/* end of include file */
This header file provides the interfaces of the Assert Handler.
This header file provides basic definitions used by the API and the implementation.
This header file provides a red-black tree implementation.
#define _Assert(_e)
Assertion similar to assert() controlled via RTEMS_DEBUG instead of NDEBUG and static analysis runs.
Definition: assert.h:96
RBTree_Node * _RBTree_Predecessor(const RBTree_Node *node)
Returns the predecessor of a node.
Definition: rbtreeprev.c:46
void _RBTree_Extract(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Extracts (removes) the node from the red-black tree.
Definition: rbtreeextract.c:63
RBTree_Node * _RBTree_Maximum(const RBTree_Control *the_rbtree)
Returns the maximum node of the red-black tree.
Definition: rbtreemax.c:43
RBTree_Node * _RBTree_Successor(const RBTree_Node *node)
Returns the successor of a node.
Definition: rbtreenext.c:47
void _RBTree_Insert_color(RBTree_Control *the_rbtree, RBTree_Node *the_node)
Rebalances the red-black tree after insertion of the node.
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:81
void * _RBTree_Postorder_next(const RBTree_Node *the_node, size_t offset)
Returns the container of the next node in postorder.
Definition: rbtreepostorder.c:59
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:43
RBTree_Node * _RBTree_Minimum(const RBTree_Control *the_rbtree)
Returns the minimum node of the red-black tree.
Definition: rbtreemin.c:43
Red-black tree node.
Definition: rbtree.h:73
Definition: mm.c:60