1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Constants and Structures Associated with the RBTree API in RTEMS |
---|
5 | * |
---|
6 | * This include file contains all the constants and structures associated |
---|
7 | * with the RBTree API in RTEMS. The rbtree is a Red Black Tree that |
---|
8 | * is part of the Super Core. This is the published interface to that |
---|
9 | * code. |
---|
10 | */ |
---|
11 | |
---|
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_RBTREE_H |
---|
21 | #define _RTEMS_RBTREE_H |
---|
22 | |
---|
23 | #include <rtems/score/rbtree.h> |
---|
24 | |
---|
25 | #ifdef __cplusplus |
---|
26 | extern "C" { |
---|
27 | #endif |
---|
28 | |
---|
29 | /** |
---|
30 | * @defgroup ClassicRBTrees Red-Black Trees |
---|
31 | * |
---|
32 | * @ingroup ClassicRTEMS |
---|
33 | * |
---|
34 | * @brief A Red-Black Tree container. |
---|
35 | * |
---|
36 | * The red-black tree container offers no internal protection against |
---|
37 | * concurrent access. The user must ensure that at most one thread at once can |
---|
38 | * access a red-black tree instance. |
---|
39 | * |
---|
40 | * @{ |
---|
41 | */ |
---|
42 | |
---|
43 | /** |
---|
44 | * @typedef rtems_rbtree_node |
---|
45 | * |
---|
46 | * A node that can be manipulated in the rbtree. |
---|
47 | */ |
---|
48 | typedef RBTree_Node rtems_rbtree_node; |
---|
49 | |
---|
50 | /** |
---|
51 | * @typedef rtems_rbtree_control |
---|
52 | * |
---|
53 | * The rbtree's control anchors the rbtree. |
---|
54 | */ |
---|
55 | typedef RBTree_Control rtems_rbtree_control; |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief Integer type for compare results. |
---|
59 | * |
---|
60 | * The type is large enough to represent pointers and 32-bit signed integers. |
---|
61 | * |
---|
62 | * @see rtems_rbtree_compare. |
---|
63 | */ |
---|
64 | typedef long rtems_rbtree_compare_result; |
---|
65 | |
---|
66 | /** |
---|
67 | * @brief Compares two red-black tree nodes. |
---|
68 | * |
---|
69 | * @param[in] first The first node. |
---|
70 | * @param[in] second The second node. |
---|
71 | * |
---|
72 | * @retval positive The key value of the first node is greater than the one of |
---|
73 | * the second node. |
---|
74 | * @retval 0 The key value of the first node is equal to the one of the second |
---|
75 | * node. |
---|
76 | * @retval negative The key value of the first node is less than the one of the |
---|
77 | * second node. |
---|
78 | */ |
---|
79 | typedef rtems_rbtree_compare_result ( *rtems_rbtree_compare )( |
---|
80 | const RBTree_Node *first, |
---|
81 | const RBTree_Node *second |
---|
82 | ); |
---|
83 | |
---|
84 | /** |
---|
85 | * @brief RBTree initializer for an empty rbtree with designator @a name. |
---|
86 | */ |
---|
87 | #define RTEMS_RBTREE_INITIALIZER_EMPTY(name) \ |
---|
88 | RBTREE_INITIALIZER_EMPTY(name) |
---|
89 | |
---|
90 | /** |
---|
91 | * @brief RBTree definition for an empty rbtree with designator @a name. |
---|
92 | */ |
---|
93 | #define RTEMS_RBTREE_DEFINE_EMPTY(name) \ |
---|
94 | RBTREE_DEFINE_EMPTY(name) |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief Initialize a RBTree header. |
---|
98 | * |
---|
99 | * This routine initializes @a the_rbtree structure to manage the |
---|
100 | * contiguous array of @a number_nodes nodes which starts at |
---|
101 | * @a starting_address. Each node is of @a node_size bytes. |
---|
102 | * |
---|
103 | * @param[in] the_rbtree is the pointer to rbtree header |
---|
104 | * @param[in] compare The node compare function. |
---|
105 | * @param[in] starting_address is the starting address of first node |
---|
106 | * @param[in] number_nodes is the number of nodes in rbtree |
---|
107 | * @param[in] node_size is the size of node in bytes |
---|
108 | * @param[in] is_unique If true, then reject nodes with a duplicate key, else |
---|
109 | * otherwise. |
---|
110 | */ |
---|
111 | void rtems_rbtree_initialize( |
---|
112 | rtems_rbtree_control *the_rbtree, |
---|
113 | rtems_rbtree_compare compare, |
---|
114 | void *starting_address, |
---|
115 | size_t number_nodes, |
---|
116 | size_t node_size, |
---|
117 | bool is_unique |
---|
118 | ); |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief Initialize this RBTree as Empty |
---|
122 | * |
---|
123 | * This routine initializes @a the_rbtree to contain zero nodes. |
---|
124 | */ |
---|
125 | RTEMS_INLINE_ROUTINE void rtems_rbtree_initialize_empty( |
---|
126 | rtems_rbtree_control *the_rbtree |
---|
127 | ) |
---|
128 | { |
---|
129 | _RBTree_Initialize_empty( the_rbtree ); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * @brief Set off RBtree. |
---|
134 | * |
---|
135 | * This function sets the next and previous fields of the @a node to NULL |
---|
136 | * indicating the @a node is not part of any rbtree. |
---|
137 | */ |
---|
138 | RTEMS_INLINE_ROUTINE void rtems_rbtree_set_off_tree( |
---|
139 | rtems_rbtree_node *node |
---|
140 | ) |
---|
141 | { |
---|
142 | _RBTree_Set_off_tree( node ); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * @brief Is the Node off RBTree. |
---|
147 | * |
---|
148 | * This function returns true if the @a node is not on a rbtree. A @a node is |
---|
149 | * off rbtree if the next and previous fields are set to NULL. |
---|
150 | */ |
---|
151 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_node_off_tree( |
---|
152 | const rtems_rbtree_node *node |
---|
153 | ) |
---|
154 | { |
---|
155 | return _RBTree_Is_node_off_tree( node ); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief Return pointer to RBTree root. |
---|
160 | * |
---|
161 | * This function returns a pointer to the root node of @a the_rbtree. |
---|
162 | */ |
---|
163 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_root( |
---|
164 | const rtems_rbtree_control *the_rbtree |
---|
165 | ) |
---|
166 | { |
---|
167 | return _RBTree_Root( the_rbtree ); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * @copydoc _RBTree_Minimum() |
---|
172 | */ |
---|
173 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_min( |
---|
174 | const rtems_rbtree_control *the_rbtree |
---|
175 | ) |
---|
176 | { |
---|
177 | return _RBTree_Minimum( the_rbtree ); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * @copydoc _RBTree_Maximum() |
---|
182 | */ |
---|
183 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_max( |
---|
184 | const rtems_rbtree_control *the_rbtree |
---|
185 | ) |
---|
186 | { |
---|
187 | return _RBTree_Maximum( the_rbtree ); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * @brief Return pointer to the left child node from this node. |
---|
192 | * |
---|
193 | * This function returns a pointer to the left child node of @a the_node. |
---|
194 | */ |
---|
195 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_left( |
---|
196 | const rtems_rbtree_node *the_node |
---|
197 | ) |
---|
198 | { |
---|
199 | return _RBTree_Left( the_node ); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Return pointer to the right child node from this node. |
---|
204 | * |
---|
205 | * This function returns a pointer to the right child node of @a the_node. |
---|
206 | */ |
---|
207 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_right( |
---|
208 | const rtems_rbtree_node *the_node |
---|
209 | ) |
---|
210 | { |
---|
211 | return _RBTree_Right( the_node ); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * @copydoc _RBTree_Parent() |
---|
216 | */ |
---|
217 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_parent( |
---|
218 | const rtems_rbtree_node *the_node |
---|
219 | ) |
---|
220 | { |
---|
221 | return _RBTree_Parent( the_node ); |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * @brief Is the RBTree empty. |
---|
226 | * |
---|
227 | * This function returns true if there a no nodes on @a the_rbtree and |
---|
228 | * false otherwise. |
---|
229 | */ |
---|
230 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_empty( |
---|
231 | const rtems_rbtree_control *the_rbtree |
---|
232 | ) |
---|
233 | { |
---|
234 | return _RBTree_Is_empty( the_rbtree ); |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * @brief Is this the minimum node on the RBTree. |
---|
239 | * |
---|
240 | * This function returns true if @a the_node is the min node on @a the_rbtree |
---|
241 | * and false otherwise. |
---|
242 | */ |
---|
243 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_min( |
---|
244 | const rtems_rbtree_control *the_rbtree, |
---|
245 | const rtems_rbtree_node *the_node |
---|
246 | ) |
---|
247 | { |
---|
248 | return rtems_rbtree_min( the_rbtree ) == the_node; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * @brief Is this the maximum node on the RBTree. |
---|
253 | * |
---|
254 | * This function returns true if @a the_node is the max node on @a the_rbtree |
---|
255 | * and false otherwise. |
---|
256 | */ |
---|
257 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_max( |
---|
258 | const rtems_rbtree_control *the_rbtree, |
---|
259 | const rtems_rbtree_node *the_node |
---|
260 | ) |
---|
261 | { |
---|
262 | return rtems_rbtree_max( the_rbtree ) == the_node; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * @copydoc _RBTree_Is_root() |
---|
267 | */ |
---|
268 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_root( |
---|
269 | const rtems_rbtree_node *the_node |
---|
270 | ) |
---|
271 | { |
---|
272 | return _RBTree_Is_root( the_node ); |
---|
273 | } |
---|
274 | |
---|
275 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_equal( |
---|
276 | rtems_rbtree_compare_result compare_result |
---|
277 | ) |
---|
278 | { |
---|
279 | return compare_result == 0; |
---|
280 | } |
---|
281 | |
---|
282 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_greater( |
---|
283 | rtems_rbtree_compare_result compare_result |
---|
284 | ) |
---|
285 | { |
---|
286 | return compare_result > 0; |
---|
287 | } |
---|
288 | |
---|
289 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_lesser( |
---|
290 | rtems_rbtree_compare_result compare_result |
---|
291 | ) |
---|
292 | { |
---|
293 | return compare_result < 0; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief Tries to find a node for the specified key in the tree. |
---|
298 | * |
---|
299 | * @param[in] the_rbtree The red-black tree control. |
---|
300 | * @param[in] the_node A node specifying the key. |
---|
301 | * @param[in] compare The node compare function. |
---|
302 | * @param[in] is_unique If true, then return the first node with a key equal to |
---|
303 | * the one of the node specified if it exits, else return the last node if it |
---|
304 | * exists. |
---|
305 | * |
---|
306 | * @retval node A node corresponding to the key. If the tree is not unique |
---|
307 | * and contains duplicate keys, the set of duplicate keys acts as FIFO. |
---|
308 | * @retval NULL No node exists in the tree for the key. |
---|
309 | */ |
---|
310 | rtems_rbtree_node* rtems_rbtree_find( |
---|
311 | const rtems_rbtree_control *the_rbtree, |
---|
312 | const rtems_rbtree_node *the_node, |
---|
313 | rtems_rbtree_compare compare, |
---|
314 | bool is_unique |
---|
315 | ); |
---|
316 | |
---|
317 | /** |
---|
318 | * @copydoc _RBTree_Predecessor() |
---|
319 | */ |
---|
320 | RTEMS_INLINE_ROUTINE rtems_rbtree_node* rtems_rbtree_predecessor( |
---|
321 | const rtems_rbtree_node *node |
---|
322 | ) |
---|
323 | { |
---|
324 | return _RBTree_Predecessor( node ); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * @copydoc _RBTree_Successor() |
---|
329 | */ |
---|
330 | RTEMS_INLINE_ROUTINE rtems_rbtree_node* rtems_rbtree_successor( |
---|
331 | const rtems_rbtree_node *node |
---|
332 | ) |
---|
333 | { |
---|
334 | return _RBTree_Successor( node ); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * @copydoc _RBTree_Extract() |
---|
339 | */ |
---|
340 | RTEMS_INLINE_ROUTINE void rtems_rbtree_extract( |
---|
341 | rtems_rbtree_control *the_rbtree, |
---|
342 | rtems_rbtree_node *the_node |
---|
343 | ) |
---|
344 | { |
---|
345 | _RBTree_Extract( the_rbtree, the_node ); |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * @brief Gets a node with the minimum key value from the red-black tree. |
---|
350 | * |
---|
351 | * This function extracts a node with the minimum key value from |
---|
352 | * tree and returns a pointer to that node if it exists. In case multiple |
---|
353 | * nodes with a minimum key value exist, then they are extracted in FIFO order. |
---|
354 | * |
---|
355 | * @param[in] the_rbtree The red-black tree control. |
---|
356 | * |
---|
357 | * @retval NULL The tree is empty. |
---|
358 | * @retval node A node with the minimal key value on the tree. |
---|
359 | */ |
---|
360 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_get_min( |
---|
361 | rtems_rbtree_control *the_rbtree |
---|
362 | ) |
---|
363 | { |
---|
364 | rtems_rbtree_node *the_node = rtems_rbtree_min( the_rbtree ); |
---|
365 | |
---|
366 | if ( the_node != NULL ) { |
---|
367 | rtems_rbtree_extract( the_rbtree, the_node ); |
---|
368 | } |
---|
369 | |
---|
370 | return the_node; |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * @brief Gets a node with the maximal key value from the red-black tree. |
---|
375 | * |
---|
376 | * This function extracts a node with the maximum key value from tree and |
---|
377 | * returns a pointer to that node if it exists. In case multiple nodes with a |
---|
378 | * maximum key value exist, then they are extracted in LIFO order. |
---|
379 | * |
---|
380 | * @param[in] the_rbtree The red-black tree control. |
---|
381 | * |
---|
382 | * @retval NULL The tree is empty. |
---|
383 | * @retval node A node with the maximal key value on the tree. |
---|
384 | */ |
---|
385 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_get_max( |
---|
386 | rtems_rbtree_control *the_rbtree |
---|
387 | ) |
---|
388 | { |
---|
389 | rtems_rbtree_node *the_node = rtems_rbtree_max( the_rbtree ); |
---|
390 | |
---|
391 | if ( the_node != NULL ) { |
---|
392 | rtems_rbtree_extract( the_rbtree, the_node ); |
---|
393 | } |
---|
394 | |
---|
395 | return the_node; |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | * @brief Peek at the min node on a rbtree. |
---|
400 | * |
---|
401 | * This function returns a pointer to the min node from @a the_rbtree |
---|
402 | * without changing the tree. If @a the_rbtree is empty, |
---|
403 | * then NULL is returned. |
---|
404 | */ |
---|
405 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_peek_min( |
---|
406 | const rtems_rbtree_control *the_rbtree |
---|
407 | ) |
---|
408 | { |
---|
409 | return rtems_rbtree_min( the_rbtree ); |
---|
410 | } |
---|
411 | |
---|
412 | /** |
---|
413 | * @brief Peek at the max node on a rbtree. |
---|
414 | * |
---|
415 | * This function returns a pointer to the max node from @a the_rbtree |
---|
416 | * without changing the tree. If @a the_rbtree is empty, |
---|
417 | * then NULL is returned. |
---|
418 | */ |
---|
419 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_peek_max( |
---|
420 | const rtems_rbtree_control *the_rbtree |
---|
421 | ) |
---|
422 | { |
---|
423 | return rtems_rbtree_max( the_rbtree ); |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * @brief Inserts the node into the red-black tree. |
---|
428 | * |
---|
429 | * In case the node is already a node of a tree, then this function yields |
---|
430 | * unpredictable results. |
---|
431 | * |
---|
432 | * @param[in] the_rbtree The red-black tree control. |
---|
433 | * @param[in] the_node The node to insert. |
---|
434 | * @param[in] compare The node compare function. |
---|
435 | * @param[in] is_unique If true, then reject nodes with a duplicate key, else |
---|
436 | * insert nodes in FIFO order in case the key value is equal to existing nodes. |
---|
437 | * |
---|
438 | * @retval NULL Successfully inserted. |
---|
439 | * @retval existing_node This is a unique insert and there exists a node with |
---|
440 | * an equal key in the tree already. |
---|
441 | */ |
---|
442 | rtems_rbtree_node *rtems_rbtree_insert( |
---|
443 | RBTree_Control *the_rbtree, |
---|
444 | RBTree_Node *the_node, |
---|
445 | rtems_rbtree_compare compare, |
---|
446 | bool is_unique |
---|
447 | ); |
---|
448 | |
---|
449 | /** @} */ |
---|
450 | |
---|
451 | #ifdef __cplusplus |
---|
452 | } |
---|
453 | #endif |
---|
454 | |
---|
455 | #endif |
---|
456 | /* end of include file */ |
---|