source: rtems/cpukit/score/include/rtems/score/rbtree.h @ 8abbbdde

4.115
Last change on this file since 8abbbdde was 8abbbdde, checked in by Sebastian Huber <sebastian.huber@…>, on 07/21/14 at 16:29:00

rbtree: Do not set node off-tree in extract

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/**
2 *  @file  rtems/score/rbtree.h
3 *
4 *  @brief Constants and Structures Associated with the Red-Black Tree Handler
5 *
6 *  This include file contains all the constants and structures associated
7 *  with the Red-Black Tree Handler.
8 */
9
10/*
11 *  Copyright (c) 2010 Gedare Bloom.
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.org/license/LICENSE.
16 */
17
18#ifndef _RTEMS_SCORE_RBTREE_H
19#define _RTEMS_SCORE_RBTREE_H
20
21#include <stddef.h>
22
23#include <rtems/score/address.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
30 *  @defgroup ScoreRBTree Red-Black Tree Handler
31 *
32 *  @ingroup Score
33 *
34 *  The Red-Black Tree Handler is used to manage sets of entities.  This handler
35 *  provides two data structures.  The rbtree Node data structure is included
36 *  as the first part of every data structure that will be placed on
37 *  a RBTree.  The second data structure is rbtree Control which is used
38 *  to manage a set of rbtree Nodes.
39 */
40/**@{*/
41
42/**
43 *  @typedef RBTree_Node
44 *
45 *  This type definition promotes the name for the RBTree Node used by
46 *  all RTEMS code.  It is a separate type definition because a forward
47 *  reference is required to define it.  See @ref RBTree_Node_struct for
48 *  detailed information.
49 */
50typedef struct RBTree_Node_struct RBTree_Node;
51
52/**
53 * This enum type defines the colors available for the RBTree Nodes
54 */
55typedef enum {
56  RBT_BLACK,
57  RBT_RED
58} RBTree_Color;
59
60/**
61 *  @struct RBTree_Node_struct
62 *
63 *  This is used to manage each element (node) which is placed
64 *  on a RBT.
65 *
66 *  @note Typically, a more complicated structure will use the
67 *        rbtree package.  The more complicated structure will
68 *        include a rbtree node as the first element in its
69 *        control structure.  It will then call the rbtree package
70 *        with a pointer to that node element.  The node pointer
71 *        and the higher level structure start at the same address
72 *        so the user can cast the pointers back and forth.
73 *
74 */
75struct RBTree_Node_struct {
76  /** This points to the node's parent */
77  RBTree_Node *parent;
78  /** child[0] points to the left child, child[1] points to the right child */
79  RBTree_Node *child[2];
80  /** The color of the node. Either red or black */
81  RBTree_Color color;
82};
83
84/**
85 * @brief Macro to return the structure containing the @a node.
86 *
87 * This macro returns a pointer of type @a container_type that points
88 * to the structure containing @a node, where @a node_field_name is the
89 * field name of the RBTree_Node structure in @a container_type.
90 *
91 */
92#define _RBTree_Container_of(node, container_type, node_field_name) \
93( \
94  (container_type*) \
95    ( (uintptr_t)(node) - offsetof(container_type, node_field_name) ) \
96)
97
98/**
99 *  This type indicates the direction.
100 */
101typedef enum {
102  RBT_LEFT=0,
103  RBT_RIGHT=1
104} RBTree_Direction;
105
106/**
107 * @brief Compares two red-black tree nodes.
108 *
109 * @param[in] first The first node.
110 * @param[in] second The second node.
111 *
112 * @retval positive The key value of the first node is greater than the one of
113 *   the second node.
114 * @retval 0 The key value of the first node is equal to the one of the second
115 *   node.
116 * @retval negative The key value of the first node is less than the one of the
117 *   second node.
118 */
119typedef int ( *RBTree_Compare )(
120  const RBTree_Node *first,
121  const RBTree_Node *second
122);
123
124/**
125 *  @struct RBTree_Control
126 *
127 * This is used to manage a RBT.  A rbtree consists of a tree of zero or more
128 * nodes.
129 *
130 * @note This implementation does not require special checks for
131 *   manipulating the root element of the RBT.
132 *   To accomplish this the @a RBTree_Control structure can be overlaid
133 *   with a @ref RBTree_Node structure to act as a "dummy root",
134 *   which has a NULL parent and its left child is the root.
135 */
136
137/* the RBTree_Control is actually part of the RBTree structure as an
138 * RBTree_Node. The mapping of fields from RBTree_Control to RBTree_Node are:
139 *   permanent_null == parent
140 *   root == left
141 *   first[0] == right
142 */
143typedef struct {
144  /** This points to a NULL. Useful for finding the root. */
145  RBTree_Node *permanent_null;
146  /** This points to the root node of the RBT. */
147  RBTree_Node *root;
148  /** This points to the min and max nodes of this RBT. */
149  RBTree_Node *first[2];
150} RBTree_Control;
151
152/**
153 *  @brief RBTree initializer for an empty rbtree with designator @a name.
154 */
155#define RBTREE_INITIALIZER_EMPTY( name ) \
156  { NULL, NULL, { NULL, NULL } }
157
158/**
159 *  @brief RBTree definition for an empty rbtree with designator @a name.
160 */
161#define RBTREE_DEFINE_EMPTY( name ) \
162  RBTree_Control name = RBTREE_INITIALIZER_EMPTY( name )
163
164/**
165 *  @brief RBTree_Node initializer for an empty node with designator @a name.
166 */
167#define RBTREE_NODE_INITIALIZER_EMPTY( name ) \
168  { NULL, { NULL, NULL }, RBT_RED }
169
170/**
171 *  @brief RBTree definition for an empty rbtree with designator @a name.
172 */
173#define RBTREE_NODE_DEFINE_EMPTY( name ) \
174  RBTree_Node name = RBTREE_NODE_INITIALIZER_EMPTY( name )
175
176/**
177 *  @brief Initialize a RBTree Header.
178 *
179 *  This routine initializes @a the_rbtree structure to manage the
180 *  contiguous array of @a number_nodes nodes which starts at
181 *  @a starting_address.  Each node is of @a node_size bytes.
182 *
183 *  @param[in] the_rbtree is the pointer to rbtree header
184 *  @param[in] compare The node compare function.
185 *  @param[in] starting_address is the starting address of first node
186 *  @param[in] number_nodes is the number of nodes in rbtree
187 *  @param[in] node_size is the size of node in bytes
188 *  @param[in] is_unique If true, then reject nodes with a duplicate key, else
189 *    otherwise.
190 */
191void _RBTree_Initialize(
192  RBTree_Control *the_rbtree,
193  RBTree_Compare  compare,
194  void           *starting_address,
195  size_t          number_nodes,
196  size_t          node_size,
197  bool            is_unique
198);
199
200/**
201 * @brief Tries to find a node for the specified key in the tree.
202 *
203 * @param[in] the_rbtree The red-black tree control.
204 * @param[in] the_node A node specifying the key.
205 * @param[in] compare The node compare function.
206 * @param[in] is_unique If true, then return the first node with a key equal to
207 *   the one of the node specified if it exits, else return the last node if it
208 *   exists.
209 *
210 * @retval node A node corresponding to the key.  If the tree is not unique
211 * and contains duplicate keys, the set of duplicate keys acts as FIFO.
212 * @retval NULL No node exists in the tree for the key.
213 */
214RBTree_Node *_RBTree_Find(
215  const RBTree_Control *the_rbtree,
216  const RBTree_Node    *the_node,
217  RBTree_Compare        compare,
218  bool                  is_unique
219);
220
221/**
222 *  @brief Insert @a the_node on the Red-Black Tree @a the_rbtree.
223 *
224 *  This routine inserts @a the_node on the Red-Black Tree @a the_rbtree.
225 *
226 * @param[in] the_rbtree The red-black tree control.
227 * @param[in] the_node The node to insert.
228 * @param[in] compare The node compare function.
229 * @param[in] is_unique If true, then reject nodes with a duplicate key, else
230 *   otherwise.
231 *
232 * @retval NULL Successfully inserted.
233 * @retval existing_node This is a unique insert and there exists a node with
234 *   an equal key in the tree already.
235 */
236RBTree_Node *_RBTree_Insert(
237  RBTree_Control *the_rbtree,
238  RBTree_Node    *the_node,
239  RBTree_Compare  compare,
240  bool            is_unique
241);
242
243/**
244 * @brief Extracts (removes) the node from the red-black tree.
245 *
246 * This function does not set the node off-tree.  In case this is desired, then
247 * call _RBTree_Set_off_rbtree() after the extraction.
248 *
249 * In case the node to extract is not a node of the tree, then this function
250 * yields unpredictable results.
251 *
252 * @param[in] the_rbtree The red-black tree control.
253 * @param[in] the_node The node to extract.
254 */
255void _RBTree_Extract(
256  RBTree_Control *the_rbtree,
257  RBTree_Node *the_node
258);
259
260/**
261 * @brief Returns the in-order next node of a node.
262 *
263 * @param[in] node The node.
264 * @param[in] dir The direction.
265 *
266 * @retval NULL The in-order next node does not exist.
267 * @retval otherwise The next node.
268 */
269RBTree_Node *_RBTree_Next(
270  const RBTree_Node *node,
271  RBTree_Direction dir
272);
273
274/**
275 * @brief Set off RBtree.
276 *
277 * This function sets the parent and child fields of the @a node to NULL
278 * indicating the @a node is not part of a rbtree.
279 *
280 */
281RTEMS_INLINE_ROUTINE void _RBTree_Set_off_rbtree(
282    RBTree_Node *node
283    )
284{
285  node->parent = node->child[RBT_LEFT] = node->child[RBT_RIGHT] = NULL;
286}
287
288/**
289 * @brief Is the node off RBTree.
290 *
291 * This function returns true if the @a node is not on a rbtree. A @a node is
292 * off rbtree if the parent and child fields are set to NULL.
293 */
294RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_rbtree(
295    const RBTree_Node *node
296    )
297{
298  return (node->parent == NULL) &&
299         (node->child[RBT_LEFT] == NULL) &&
300         (node->child[RBT_RIGHT] == NULL);
301}
302
303/**
304 * @brief Return pointer to RBTree's root node.
305 *
306 * This function returns a pointer to the root node of @a the_rbtree.
307 */
308RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Root(
309  const RBTree_Control *the_rbtree
310)
311{
312  return the_rbtree->root;
313}
314
315/**
316 * @brief Return pointer to RBTree's first node.
317 *
318 * This function returns a pointer to the first node on @a the_rbtree,
319 * where @a dir specifies whether to return the minimum (0) or maximum (1).
320 */
321RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_First(
322  const RBTree_Control *the_rbtree,
323  RBTree_Direction dir
324)
325{
326  return the_rbtree->first[dir];
327}
328
329/**
330 * @brief Return pointer to the parent of this node.
331 *
332 * This function returns a pointer to the parent node of @a the_node.
333 */
334RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Parent(
335  const RBTree_Node *the_node
336)
337{
338  if (!the_node->parent->parent) return NULL;
339  return the_node->parent;
340}
341
342/**
343 * @brief Return pointer to the left of this node.
344 *
345 * This function returns a pointer to the left node of this node.
346 *
347 * @param[in] the_node is the node to be operated upon.
348 *
349 * @return This method returns the left node on the rbtree.
350 */
351RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Left(
352  const RBTree_Node *the_node
353)
354{
355  return the_node->child[RBT_LEFT];
356}
357
358/**
359 * @brief Return pointer to the right of this node.
360 *
361 * This function returns a pointer to the right node of this node.
362 *
363 * @param[in] the_node is the node to be operated upon.
364 *
365 * @return This method returns the right node on the rbtree.
366 */
367RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Right(
368  const RBTree_Node *the_node
369)
370{
371  return the_node->child[RBT_RIGHT];
372}
373
374/**
375 * @brief Is the RBTree empty.
376 *
377 * This function returns true if there are no nodes on @a the_rbtree and
378 * false otherwise.
379 *
380 * @param[in] the_rbtree is the rbtree to be operated upon.
381 *
382 * @retval true There are no nodes on @a the_rbtree.
383 * @retval false There are nodes on @a the_rbtree.
384 */
385RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty(
386  const RBTree_Control *the_rbtree
387)
388{
389  return (the_rbtree->root == NULL);
390}
391
392/**
393 * @brief Is this the first node on the RBTree.
394 *
395 * This function returns true if @a the_node is the first node on
396 * @a the_rbtree and false otherwise. @a dir specifies whether first means
397 * minimum (0) or maximum (1).
398 *
399 * @retval true @a the_node is the first node on @a the_rbtree.
400 * @retval false @a the_node is not the first node on @a the_rbtree.
401 *
402 */
403RTEMS_INLINE_ROUTINE bool _RBTree_Is_first(
404  const RBTree_Control *the_rbtree,
405  const RBTree_Node *the_node,
406  RBTree_Direction dir
407)
408{
409  return (the_node == _RBTree_First(the_rbtree, dir));
410}
411
412/**
413 * @brief Is this node the RBTree root.
414 *
415 * This function returns true if @a the_node is the root of @a the_rbtree and
416 * false otherwise.
417 *
418 * @retval true @a the_node is the root of @a the_rbtree.
419 * @retval false @a the_node is not the root of @a the_rbtree.
420 */
421RTEMS_INLINE_ROUTINE bool _RBTree_Is_root(
422  const RBTree_Control *the_rbtree,
423  const RBTree_Node    *the_node
424)
425{
426  return (the_node == _RBTree_Root(the_rbtree));
427}
428
429/**
430 * @brief Find the RBTree_Control header given a node in the tree.
431 *
432 * This function returns a pointer to the header of the Red Black
433 * Tree containing @a the_node if it exists, and NULL if not.
434 */
435RTEMS_INLINE_ROUTINE RBTree_Control *_RBTree_Find_header(
436    RBTree_Node *the_node
437    )
438{
439  if(!the_node) return NULL;
440  if(!(the_node->parent)) return NULL;
441  while(the_node->parent) the_node = the_node->parent;
442  return (RBTree_Control*)the_node;
443}
444
445/**
446 * @brief Initialize this RBTree as empty.
447 *
448 * This routine initializes @a the_rbtree to contain zero nodes.
449 */
450RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty(
451  RBTree_Control *the_rbtree
452)
453{
454  the_rbtree->permanent_null   = NULL;
455  the_rbtree->root             = NULL;
456  the_rbtree->first[RBT_LEFT]  = NULL;
457  the_rbtree->first[RBT_RIGHT] = NULL;
458}
459
460/**
461 * @brief Returns the predecessor of a node.
462 *
463 * @param[in] node is the node.
464 *
465 * @retval NULL The predecessor does not exist.  Otherwise it returns
466 *         the predecessor node.
467 */
468RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Predecessor(
469  const RBTree_Node *node
470)
471{
472  return _RBTree_Next( node, RBT_LEFT );
473}
474
475/**
476 * @brief Returns the successor of a node.
477 *
478 * @param[in] node is the node.
479 *
480 * @retval NULL The successor does not exist.  Otherwise the successor node.
481 */
482RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Successor(
483  const RBTree_Node *node
484)
485{
486  return _RBTree_Next( node, RBT_RIGHT );
487}
488
489/**
490 * @brief Gets a node with an extremal key value.
491 *
492 * This function extracts a node with the minimum or maximum key value from
493 * tree and returns a pointer to that node if it exists.  In case multiple
494 * nodes with an extremal key value exist, then they are extracted in FIFO
495 * order.
496 *
497 * @param[in] the_rbtree The red-black tree control.
498 * @param[in] dir Specifies whether to get a node with the minimum (RBT_LEFT)
499 *   or maximum (RBT_RIGHT) key value.
500 *
501 * @retval NULL The tree is empty.
502 * @retval extremal_node A node with the minimal or maximal key value on the
503 *   tree.
504 */
505RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Get(
506  RBTree_Control *the_rbtree,
507  RBTree_Direction dir
508)
509{
510  RBTree_Node *the_node = the_rbtree->first[ dir ];
511
512  if ( the_node != NULL ) {
513    _RBTree_Extract( the_rbtree, the_node );
514  }
515
516  return the_node;
517}
518
519/**@}*/
520
521#ifdef __cplusplus
522}
523#endif
524
525#endif
526/* end of include file */
Note: See TracBrowser for help on using the repository browser.