1 | /** |
---|
2 | * @file rtems/score/rbtree.h |
---|
3 | * |
---|
4 | * This include file contains all the constants and structures associated |
---|
5 | * with the Red-Black Tree Handler. |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2010 Gedare Bloom. |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_RBTREE_H |
---|
19 | #define _RTEMS_SCORE_RBTREE_H |
---|
20 | |
---|
21 | /** |
---|
22 | * @defgroup ScoreRBTree Red-Black Tree Handler |
---|
23 | * |
---|
24 | * The Red-Black Tree Handler is used to manage sets of entities. This handler |
---|
25 | * provides two data structures. The rbtree Node data structure is included |
---|
26 | * as the first part of every data structure that will be placed on |
---|
27 | * a RBTree. The second data structure is rbtree Control which is used |
---|
28 | * to manage a set of rbtree Nodes. |
---|
29 | */ |
---|
30 | /**@{*/ |
---|
31 | |
---|
32 | #ifdef __cplusplus |
---|
33 | extern "C" { |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <rtems/score/address.h> |
---|
37 | |
---|
38 | /** |
---|
39 | * @typedef RBTree_Node |
---|
40 | * |
---|
41 | * This type definition promotes the name for the RBTree Node used by |
---|
42 | * all RTEMS code. It is a separate type definition because a forward |
---|
43 | * reference is required to define it. See @ref RBTree_Node_struct for |
---|
44 | * detailed information. |
---|
45 | */ |
---|
46 | typedef struct RBTree_Node_struct RBTree_Node; |
---|
47 | |
---|
48 | /** |
---|
49 | * This enum type defines the colors available for the RBTree Nodes |
---|
50 | */ |
---|
51 | typedef enum { |
---|
52 | RBT_BLACK, |
---|
53 | RBT_RED |
---|
54 | } RBTree_Color; |
---|
55 | |
---|
56 | /** |
---|
57 | * @struct RBTree_Node_struct |
---|
58 | * |
---|
59 | * This is used to manage each element (node) which is placed |
---|
60 | * on a RBT. |
---|
61 | * |
---|
62 | * @note Typically, a more complicated structure will use the |
---|
63 | * rbtree package. The more complicated structure will |
---|
64 | * include a rbtree node as the first element in its |
---|
65 | * control structure. It will then call the rbtree package |
---|
66 | * with a pointer to that node element. The node pointer |
---|
67 | * and the higher level structure start at the same address |
---|
68 | * so the user can cast the pointers back and forth. |
---|
69 | * |
---|
70 | */ |
---|
71 | struct RBTree_Node_struct { |
---|
72 | /** This points to the node's parent */ |
---|
73 | RBTree_Node *parent; |
---|
74 | /** child[0] points to the left child, child[1] points to the right child */ |
---|
75 | RBTree_Node *child[2]; |
---|
76 | /** This is the integer value stored by this node, used for sorting */ |
---|
77 | unsigned int value; |
---|
78 | /** The color of the node. Either red or black */ |
---|
79 | RBTree_Color color; |
---|
80 | }; |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief macro to return the structure containing the @a node. |
---|
84 | * |
---|
85 | * This macro returns a pointer of type @a container_type that points |
---|
86 | * to the structure containing @a node, where @a node_field_name is the |
---|
87 | * field name of the RBTree_Node structure in @a container_type. |
---|
88 | * |
---|
89 | */ |
---|
90 | #define _RBTree_Container_of(node,container_type, node_field_name) \ |
---|
91 | ((container_type*) \ |
---|
92 | ((size_t)node - ((size_t)(&((container_type *)0)->node_field_name)))) |
---|
93 | |
---|
94 | /** |
---|
95 | * This type indicates the direction. |
---|
96 | */ |
---|
97 | typedef enum { |
---|
98 | RBT_LEFT=0, |
---|
99 | RBT_RIGHT=1 |
---|
100 | } RBTree_Direction; |
---|
101 | |
---|
102 | /** |
---|
103 | * @struct RBTree_Control |
---|
104 | * |
---|
105 | * This is used to manage a RBT. A rbtree consists of a tree of zero or more |
---|
106 | * nodes. |
---|
107 | * |
---|
108 | * @note This implementation does not require special checks for |
---|
109 | * manipulating the root element of the RBT. |
---|
110 | * To accomplish this the @a RBTree_Control structure can be overlaid |
---|
111 | * with a @ref RBTree_Node structure to act as a "dummy root", |
---|
112 | * which has a NULL parent and its left child is the root. |
---|
113 | */ |
---|
114 | |
---|
115 | /* the RBTree_Control is actually part of the RBTree structure as an |
---|
116 | * RBTree_Node. The mapping of fields from RBTree_Control to RBTree_Node are: |
---|
117 | * permanent_null == parent |
---|
118 | * root == left |
---|
119 | * first[0] == right |
---|
120 | */ |
---|
121 | typedef struct { |
---|
122 | /** This points to a NULL. Useful for finding the root. */ |
---|
123 | RBTree_Node *permanent_null; |
---|
124 | /** This points to the root node of the RBT. */ |
---|
125 | RBTree_Node *root; |
---|
126 | /** This points to the min and max nodes of this RBT. */ |
---|
127 | RBTree_Node *first[2]; |
---|
128 | } RBTree_Control; |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief RBTree initializer for an empty rbtree with designator @a name. |
---|
132 | */ |
---|
133 | #define RBTREE_INITIALIZER_EMPTY(name) \ |
---|
134 | { \ |
---|
135 | .permanent_null = NULL, \ |
---|
136 | .root = NULL, \ |
---|
137 | .first[0] = NULL, \ |
---|
138 | .first[1] = NULL, \ |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * @brief RBTree definition for an empty rbtree with designator @a name. |
---|
143 | */ |
---|
144 | #define RBTREE_DEFINE_EMPTY(name) \ |
---|
145 | RBTree_Control name = RBTREE_INITIALIZER_EMPTY(name) |
---|
146 | |
---|
147 | /** |
---|
148 | * @brief RBTree_Node initializer for an empty node with designator @a name. |
---|
149 | */ |
---|
150 | #define RBTREE_NODE_INITIALIZER_EMPTY(name) \ |
---|
151 | { \ |
---|
152 | .parent = NULL, \ |
---|
153 | .child[0] = NULL, \ |
---|
154 | .child[1] = NULL, \ |
---|
155 | .value = -1, \ |
---|
156 | RBT_RED \ |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief RBTree definition for an empty rbtree with designator @a name. |
---|
161 | */ |
---|
162 | #define RBTREE_NODE_DEFINE_EMPTY(name) \ |
---|
163 | RBTree_Node name = RBTREE_NODE_INITIALIZER_EMPTY(name) |
---|
164 | |
---|
165 | /** |
---|
166 | * @brief Initialize a RBTree Header |
---|
167 | * |
---|
168 | * This routine initializes @a the_rbtree structure to manage the |
---|
169 | * contiguous array of @a number_nodes nodes which starts at |
---|
170 | * @a starting_address. Each node is of @a node_size bytes. |
---|
171 | */ |
---|
172 | void _RBTree_Initialize( |
---|
173 | RBTree_Control *the_rbtree, |
---|
174 | void *starting_address, |
---|
175 | size_t number_nodes, |
---|
176 | size_t node_size |
---|
177 | ); |
---|
178 | |
---|
179 | /** |
---|
180 | * @brief Obtain the min or max node of a rbtree |
---|
181 | * |
---|
182 | * This function removes the min or max node from @a the_rbtree and returns |
---|
183 | * a pointer to that node. If @a the_rbtree is empty, then NULL is returned. |
---|
184 | * @a dir specifies whether to return the min (0) or max (1). |
---|
185 | * |
---|
186 | * @return This method returns a pointer to a node. If a node was removed, |
---|
187 | * then a pointer to that node is returned. If @a the_rbtree was |
---|
188 | * empty, then NULL is returned. |
---|
189 | * |
---|
190 | * @note It disables interrupts to ensure the atomicity of the get operation. |
---|
191 | */ |
---|
192 | RBTree_Node *_RBTree_Get( |
---|
193 | RBTree_Control *the_rbtree, |
---|
194 | RBTree_Direction dir |
---|
195 | ); |
---|
196 | |
---|
197 | /** |
---|
198 | * @brief Check the min or max node on a rbtree |
---|
199 | * |
---|
200 | * This function returns a pointer to the min or max node of @a the_rbtree. |
---|
201 | * If @a the_rbtree is empty, then NULL is returned. @a dir specifies |
---|
202 | * whether to return the min (0) or max (1). |
---|
203 | * |
---|
204 | * @return This method returns a pointer to a node. |
---|
205 | * If @a the_rbtree was empty, then NULL is returned. |
---|
206 | * |
---|
207 | * @note It disables interrupts to ensure the atomicity of the get operation. |
---|
208 | */ |
---|
209 | RBTree_Node *_RBTree_Peek( |
---|
210 | RBTree_Control *the_rbtree, |
---|
211 | RBTree_Direction dir |
---|
212 | ); |
---|
213 | |
---|
214 | /** |
---|
215 | * @brief Find the node with given value in the tree |
---|
216 | * |
---|
217 | * This function returns a pointer to the node with value equal to @a value |
---|
218 | * if it exists in the Red-Black Tree @a the_rbtree, and NULL if not. |
---|
219 | */ |
---|
220 | RBTree_Node *_RBTree_Find( |
---|
221 | RBTree_Control *the_rbtree, |
---|
222 | unsigned int value |
---|
223 | ); |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief Find the control structure of the tree containing the given node |
---|
227 | * |
---|
228 | * This function returns a pointer to the control structure of the tree |
---|
229 | * containing @a the_node, if it exists, and NULL if not. |
---|
230 | */ |
---|
231 | RBTree_Control *_RBTree_Find_header( |
---|
232 | RBTree_Node *the_node |
---|
233 | ); |
---|
234 | |
---|
235 | /** |
---|
236 | * @brief Insert a Node (unprotected) |
---|
237 | * |
---|
238 | * This routine inserts @a the_node on the Red-Black Tree @a the_rbtree. |
---|
239 | * |
---|
240 | * @retval 0 Successfully inserted. |
---|
241 | * @retval -1 NULL @a the_node. |
---|
242 | * @retval RBTree_Node* if one with equal value to @a the_node->value exists |
---|
243 | * in @a the_rbtree. |
---|
244 | * |
---|
245 | * @note It does NOT disable interrupts to ensure the atomicity |
---|
246 | * of the extract operation. |
---|
247 | */ |
---|
248 | RBTree_Node *_RBTree_Insert_unprotected( |
---|
249 | RBTree_Control *the_rbtree, |
---|
250 | RBTree_Node *the_node |
---|
251 | ); |
---|
252 | |
---|
253 | /** |
---|
254 | * @brief Insert a node on a rbtree |
---|
255 | * |
---|
256 | * This routine inserts @a the_node on the tree @a the_rbtree. |
---|
257 | * |
---|
258 | * @note It disables interrupts to ensure the atomicity |
---|
259 | * of the extract operation. |
---|
260 | */ |
---|
261 | void _RBTree_Insert( |
---|
262 | RBTree_Control *the_rbtree, |
---|
263 | RBTree_Node *the_node |
---|
264 | ); |
---|
265 | |
---|
266 | |
---|
267 | /** |
---|
268 | * @brief Extract a Node (unprotected) |
---|
269 | * |
---|
270 | * This routine extracts (removes) @a the_node from @a the_rbtree. |
---|
271 | * |
---|
272 | * @note It does NOT disable interrupts to ensure the atomicity |
---|
273 | * of the extract operation. |
---|
274 | */ |
---|
275 | void _RBTree_Extract_unprotected( |
---|
276 | RBTree_Control *the_rbtree, |
---|
277 | RBTree_Node *the_node |
---|
278 | ); |
---|
279 | |
---|
280 | /** |
---|
281 | * @brief Delete a node from the rbtree |
---|
282 | * |
---|
283 | * This routine deletes @a the_node from @a the_rbtree. |
---|
284 | * |
---|
285 | * @note It disables interrupts to ensure the atomicity of the |
---|
286 | * append operation. |
---|
287 | */ |
---|
288 | void _RBTree_Extract( |
---|
289 | RBTree_Control *the_rbtree, |
---|
290 | RBTree_Node *the_node |
---|
291 | ); |
---|
292 | |
---|
293 | #ifndef __RTEMS_APPLICATION__ |
---|
294 | #include <rtems/score/rbtree.inl> |
---|
295 | #endif |
---|
296 | |
---|
297 | #ifdef __cplusplus |
---|
298 | } |
---|
299 | #endif |
---|
300 | |
---|
301 | /**@}*/ |
---|
302 | |
---|
303 | #endif |
---|
304 | /* end of include file */ |
---|