source: rtems/cpukit/score/include/rtems/score/rbtree.h @ d7a94693

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

rbtree: Remove superfluous NULL pointer checks

  • Property mode set to 100644
File size: 13.7 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) @a the_node from @a the_rbtree.
245 *
246 *  This routine extracts (removes) @a the_node from @a the_rbtree.
247 */
248void _RBTree_Extract(
249  RBTree_Control *the_rbtree,
250  RBTree_Node *the_node
251);
252
253/**
254 * @brief Returns the in-order next node of a node.
255 *
256 * @param[in] node The node.
257 * @param[in] dir The direction.
258 *
259 * @retval NULL The in-order next node does not exist.
260 * @retval otherwise The next node.
261 */
262RBTree_Node *_RBTree_Next(
263  const RBTree_Node *node,
264  RBTree_Direction dir
265);
266
267/**
268 * @brief Set off RBtree.
269 *
270 * This function sets the parent and child fields of the @a node to NULL
271 * indicating the @a node is not part of a rbtree.
272 *
273 */
274RTEMS_INLINE_ROUTINE void _RBTree_Set_off_rbtree(
275    RBTree_Node *node
276    )
277{
278  node->parent = node->child[RBT_LEFT] = node->child[RBT_RIGHT] = NULL;
279}
280
281/**
282 * @brief Is the node off RBTree.
283 *
284 * This function returns true if the @a node is not on a rbtree. A @a node is
285 * off rbtree if the parent and child fields are set to NULL.
286 */
287RTEMS_INLINE_ROUTINE bool _RBTree_Is_node_off_rbtree(
288    const RBTree_Node *node
289    )
290{
291  return (node->parent == NULL) &&
292         (node->child[RBT_LEFT] == NULL) &&
293         (node->child[RBT_RIGHT] == NULL);
294}
295
296/**
297 * @brief Return pointer to RBTree's root node.
298 *
299 * This function returns a pointer to the root node of @a the_rbtree.
300 */
301RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Root(
302  const RBTree_Control *the_rbtree
303)
304{
305  return the_rbtree->root;
306}
307
308/**
309 * @brief Return pointer to RBTree's first node.
310 *
311 * This function returns a pointer to the first node on @a the_rbtree,
312 * where @a dir specifies whether to return the minimum (0) or maximum (1).
313 */
314RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_First(
315  const RBTree_Control *the_rbtree,
316  RBTree_Direction dir
317)
318{
319  return the_rbtree->first[dir];
320}
321
322/**
323 * @brief Return pointer to the parent of this node.
324 *
325 * This function returns a pointer to the parent node of @a the_node.
326 */
327RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Parent(
328  const RBTree_Node *the_node
329)
330{
331  if (!the_node->parent->parent) return NULL;
332  return the_node->parent;
333}
334
335/**
336 * @brief Return pointer to the left of this node.
337 *
338 * This function returns a pointer to the left node of this node.
339 *
340 * @param[in] the_node is the node to be operated upon.
341 *
342 * @return This method returns the left node on the rbtree.
343 */
344RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Left(
345  const RBTree_Node *the_node
346)
347{
348  return the_node->child[RBT_LEFT];
349}
350
351/**
352 * @brief Return pointer to the right of this node.
353 *
354 * This function returns a pointer to the right node of this node.
355 *
356 * @param[in] the_node is the node to be operated upon.
357 *
358 * @return This method returns the right node on the rbtree.
359 */
360RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Right(
361  const RBTree_Node *the_node
362)
363{
364  return the_node->child[RBT_RIGHT];
365}
366
367/**
368 * @brief Is the RBTree empty.
369 *
370 * This function returns true if there are no nodes on @a the_rbtree and
371 * false otherwise.
372 *
373 * @param[in] the_rbtree is the rbtree to be operated upon.
374 *
375 * @retval true There are no nodes on @a the_rbtree.
376 * @retval false There are nodes on @a the_rbtree.
377 */
378RTEMS_INLINE_ROUTINE bool _RBTree_Is_empty(
379  const RBTree_Control *the_rbtree
380)
381{
382  return (the_rbtree->root == NULL);
383}
384
385/**
386 * @brief Is this the first node on the RBTree.
387 *
388 * This function returns true if @a the_node is the first node on
389 * @a the_rbtree and false otherwise. @a dir specifies whether first means
390 * minimum (0) or maximum (1).
391 *
392 * @retval true @a the_node is the first node on @a the_rbtree.
393 * @retval false @a the_node is not the first node on @a the_rbtree.
394 *
395 */
396RTEMS_INLINE_ROUTINE bool _RBTree_Is_first(
397  const RBTree_Control *the_rbtree,
398  const RBTree_Node *the_node,
399  RBTree_Direction dir
400)
401{
402  return (the_node == _RBTree_First(the_rbtree, dir));
403}
404
405/**
406 * @brief Is this node the RBTree root.
407 *
408 * This function returns true if @a the_node is the root of @a the_rbtree and
409 * false otherwise.
410 *
411 * @retval true @a the_node is the root of @a the_rbtree.
412 * @retval false @a the_node is not the root of @a the_rbtree.
413 */
414RTEMS_INLINE_ROUTINE bool _RBTree_Is_root(
415  const RBTree_Control *the_rbtree,
416  const RBTree_Node    *the_node
417)
418{
419  return (the_node == _RBTree_Root(the_rbtree));
420}
421
422/**
423 * @brief Find the RBTree_Control header given a node in the tree.
424 *
425 * This function returns a pointer to the header of the Red Black
426 * Tree containing @a the_node if it exists, and NULL if not.
427 */
428RTEMS_INLINE_ROUTINE RBTree_Control *_RBTree_Find_header(
429    RBTree_Node *the_node
430    )
431{
432  if(!the_node) return NULL;
433  if(!(the_node->parent)) return NULL;
434  while(the_node->parent) the_node = the_node->parent;
435  return (RBTree_Control*)the_node;
436}
437
438/**
439 * @brief Initialize this RBTree as empty.
440 *
441 * This routine initializes @a the_rbtree to contain zero nodes.
442 */
443RTEMS_INLINE_ROUTINE void _RBTree_Initialize_empty(
444  RBTree_Control *the_rbtree
445)
446{
447  the_rbtree->permanent_null   = NULL;
448  the_rbtree->root             = NULL;
449  the_rbtree->first[RBT_LEFT]  = NULL;
450  the_rbtree->first[RBT_RIGHT] = NULL;
451}
452
453/**
454 * @brief Returns the predecessor of a node.
455 *
456 * @param[in] node is the node.
457 *
458 * @retval NULL The predecessor does not exist.  Otherwise it returns
459 *         the predecessor node.
460 */
461RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Predecessor(
462  const RBTree_Node *node
463)
464{
465  return _RBTree_Next( node, RBT_LEFT );
466}
467
468/**
469 * @brief Returns the successor of a node.
470 *
471 * @param[in] node is the node.
472 *
473 * @retval NULL The successor does not exist.  Otherwise the successor node.
474 */
475RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Successor(
476  const RBTree_Node *node
477)
478{
479  return _RBTree_Next( node, RBT_RIGHT );
480}
481
482/**
483 * @brief Gets a node with an extremal key value.
484 *
485 * This function extracts a node with the minimum or maximum key value from
486 * tree and returns a pointer to that node if it exists.  In case multiple
487 * nodes with an extremal key value exist, then they are extracted in FIFO
488 * order.
489 *
490 * @param[in] the_rbtree The red-black tree control.
491 * @param[in] dir Specifies whether to get a node with the minimum (RBT_LEFT)
492 *   or maximum (RBT_RIGHT) key value.
493 *
494 * @retval NULL The tree is empty.
495 * @retval extremal_node A node with the minimal or maximal key value on the
496 *   tree.
497 */
498RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Get(
499  RBTree_Control *the_rbtree,
500  RBTree_Direction dir
501)
502{
503  RBTree_Node *the_node = the_rbtree->first[ dir ];
504
505  if ( the_node != NULL ) {
506    _RBTree_Extract( the_rbtree, the_node );
507  }
508
509  return the_node;
510}
511
512/**@}*/
513
514#ifdef __cplusplus
515}
516#endif
517
518#endif
519/* end of include file */
Note: See TracBrowser for help on using the repository browser.