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 | * @copydoc RBTree_Compare_result |
---|
59 | */ |
---|
60 | typedef RBTree_Compare_result rtems_rbtree_compare_result; |
---|
61 | |
---|
62 | /** |
---|
63 | * @copydoc RBTree_Compare |
---|
64 | */ |
---|
65 | typedef RBTree_Compare rtems_rbtree_compare; |
---|
66 | |
---|
67 | /** |
---|
68 | * @brief RBTree initializer for an empty rbtree with designator @a name. |
---|
69 | */ |
---|
70 | #define RTEMS_RBTREE_INITIALIZER_EMPTY(name) \ |
---|
71 | RBTREE_INITIALIZER_EMPTY(name) |
---|
72 | |
---|
73 | /** |
---|
74 | * @brief RBTree definition for an empty rbtree with designator @a name. |
---|
75 | */ |
---|
76 | #define RTEMS_RBTREE_DEFINE_EMPTY(name) \ |
---|
77 | RBTREE_DEFINE_EMPTY(name) |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief Initialize a RBTree header. |
---|
81 | * |
---|
82 | * This routine initializes @a the_rbtree structure to manage the |
---|
83 | * contiguous array of @a number_nodes nodes which starts at |
---|
84 | * @a starting_address. Each node is of @a node_size bytes. |
---|
85 | */ |
---|
86 | RTEMS_INLINE_ROUTINE void rtems_rbtree_initialize( |
---|
87 | rtems_rbtree_control *the_rbtree, |
---|
88 | rtems_rbtree_compare compare, |
---|
89 | void *starting_address, |
---|
90 | size_t number_nodes, |
---|
91 | size_t node_size, |
---|
92 | bool is_unique |
---|
93 | ) |
---|
94 | { |
---|
95 | _RBTree_Initialize( the_rbtree, compare, starting_address, |
---|
96 | number_nodes, node_size, is_unique); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @brief Initialize this RBTree as Empty |
---|
101 | * |
---|
102 | * This routine initializes @a the_rbtree to contain zero nodes. |
---|
103 | */ |
---|
104 | RTEMS_INLINE_ROUTINE void rtems_rbtree_initialize_empty( |
---|
105 | rtems_rbtree_control *the_rbtree |
---|
106 | ) |
---|
107 | { |
---|
108 | _RBTree_Initialize_empty( the_rbtree ); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * @brief Set off RBtree. |
---|
113 | * |
---|
114 | * This function sets the next and previous fields of the @a node to NULL |
---|
115 | * indicating the @a node is not part of any rbtree. |
---|
116 | */ |
---|
117 | RTEMS_INLINE_ROUTINE void rtems_rbtree_set_off_tree( |
---|
118 | rtems_rbtree_node *node |
---|
119 | ) |
---|
120 | { |
---|
121 | _RBTree_Set_off_tree( node ); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief Is the Node off RBTree. |
---|
126 | * |
---|
127 | * This function returns true if the @a node is not on a rbtree. A @a node is |
---|
128 | * off rbtree if the next and previous fields are set to NULL. |
---|
129 | */ |
---|
130 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_node_off_tree( |
---|
131 | const rtems_rbtree_node *node |
---|
132 | ) |
---|
133 | { |
---|
134 | return _RBTree_Is_node_off_tree( node ); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @brief Return pointer to RBTree root. |
---|
139 | * |
---|
140 | * This function returns a pointer to the root node of @a the_rbtree. |
---|
141 | */ |
---|
142 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_root( |
---|
143 | const rtems_rbtree_control *the_rbtree |
---|
144 | ) |
---|
145 | { |
---|
146 | return _RBTree_Root( the_rbtree ); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * @copydoc _RBTree_Minimum() |
---|
151 | */ |
---|
152 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_min( |
---|
153 | const rtems_rbtree_control *the_rbtree |
---|
154 | ) |
---|
155 | { |
---|
156 | return _RBTree_Minimum( the_rbtree ); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @copydoc _RBTree_Maximum() |
---|
161 | */ |
---|
162 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_max( |
---|
163 | const rtems_rbtree_control *the_rbtree |
---|
164 | ) |
---|
165 | { |
---|
166 | return _RBTree_Maximum( the_rbtree ); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * @brief Return pointer to the left child node from this node. |
---|
171 | * |
---|
172 | * This function returns a pointer to the left child node of @a the_node. |
---|
173 | */ |
---|
174 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_left( |
---|
175 | const rtems_rbtree_node *the_node |
---|
176 | ) |
---|
177 | { |
---|
178 | return _RBTree_Left( the_node ); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Return pointer to the right child node from this node. |
---|
183 | * |
---|
184 | * This function returns a pointer to the right child node of @a the_node. |
---|
185 | */ |
---|
186 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_right( |
---|
187 | const rtems_rbtree_node *the_node |
---|
188 | ) |
---|
189 | { |
---|
190 | return _RBTree_Right( the_node ); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * @copydoc _RBTree_Parent() |
---|
195 | */ |
---|
196 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_parent( |
---|
197 | const rtems_rbtree_node *the_node |
---|
198 | ) |
---|
199 | { |
---|
200 | return _RBTree_Parent( the_node ); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * @brief Is the RBTree empty. |
---|
205 | * |
---|
206 | * This function returns true if there a no nodes on @a the_rbtree and |
---|
207 | * false otherwise. |
---|
208 | */ |
---|
209 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_empty( |
---|
210 | const rtems_rbtree_control *the_rbtree |
---|
211 | ) |
---|
212 | { |
---|
213 | return _RBTree_Is_empty( the_rbtree ); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * @brief Is this the minimum node on the RBTree. |
---|
218 | * |
---|
219 | * This function returns true if @a the_node is the min node on @a the_rbtree |
---|
220 | * and false otherwise. |
---|
221 | */ |
---|
222 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_min( |
---|
223 | const rtems_rbtree_control *the_rbtree, |
---|
224 | const rtems_rbtree_node *the_node |
---|
225 | ) |
---|
226 | { |
---|
227 | return rtems_rbtree_min( the_rbtree ) == the_node; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * @brief Is this the maximum node on the RBTree. |
---|
232 | * |
---|
233 | * This function returns true if @a the_node is the max node on @a the_rbtree |
---|
234 | * and false otherwise. |
---|
235 | */ |
---|
236 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_max( |
---|
237 | const rtems_rbtree_control *the_rbtree, |
---|
238 | const rtems_rbtree_node *the_node |
---|
239 | ) |
---|
240 | { |
---|
241 | return rtems_rbtree_max( the_rbtree ) == the_node; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * @copydoc _RBTree_Is_root() |
---|
246 | */ |
---|
247 | RTEMS_INLINE_ROUTINE bool rtems_rbtree_is_root( |
---|
248 | const rtems_rbtree_node *the_node |
---|
249 | ) |
---|
250 | { |
---|
251 | return _RBTree_Is_root( the_node ); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @copydoc _RBTree_Find() |
---|
256 | */ |
---|
257 | RTEMS_INLINE_ROUTINE rtems_rbtree_node* rtems_rbtree_find( |
---|
258 | const rtems_rbtree_control *the_rbtree, |
---|
259 | const rtems_rbtree_node *the_node, |
---|
260 | rtems_rbtree_compare compare, |
---|
261 | bool is_unique |
---|
262 | ) |
---|
263 | { |
---|
264 | return _RBTree_Find( the_rbtree, the_node, compare, is_unique ); |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * @copydoc _RBTree_Predecessor() |
---|
269 | */ |
---|
270 | RTEMS_INLINE_ROUTINE rtems_rbtree_node* rtems_rbtree_predecessor( |
---|
271 | const rtems_rbtree_node *node |
---|
272 | ) |
---|
273 | { |
---|
274 | return _RBTree_Predecessor( node ); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * @copydoc _RBTree_Successor() |
---|
279 | */ |
---|
280 | RTEMS_INLINE_ROUTINE rtems_rbtree_node* rtems_rbtree_successor( |
---|
281 | const rtems_rbtree_node *node |
---|
282 | ) |
---|
283 | { |
---|
284 | return _RBTree_Successor( node ); |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * @copydoc _RBTree_Extract() |
---|
289 | */ |
---|
290 | RTEMS_INLINE_ROUTINE void rtems_rbtree_extract( |
---|
291 | rtems_rbtree_control *the_rbtree, |
---|
292 | rtems_rbtree_node *the_node |
---|
293 | ) |
---|
294 | { |
---|
295 | _RBTree_Extract( the_rbtree, the_node ); |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * @brief Gets a node with the minimum key value from the red-black tree. |
---|
300 | * |
---|
301 | * This function extracts a node with the minimum key value from |
---|
302 | * tree and returns a pointer to that node if it exists. In case multiple |
---|
303 | * nodes with a minimum key value exist, then they are extracted in FIFO order. |
---|
304 | * |
---|
305 | * @param[in] the_rbtree The red-black tree control. |
---|
306 | * |
---|
307 | * @retval NULL The tree is empty. |
---|
308 | * @retval node A node with the minimal key value on the tree. |
---|
309 | */ |
---|
310 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_get_min( |
---|
311 | rtems_rbtree_control *the_rbtree |
---|
312 | ) |
---|
313 | { |
---|
314 | rtems_rbtree_node *the_node = rtems_rbtree_min( the_rbtree ); |
---|
315 | |
---|
316 | if ( the_node != NULL ) { |
---|
317 | rtems_rbtree_extract( the_rbtree, the_node ); |
---|
318 | } |
---|
319 | |
---|
320 | return the_node; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * @brief Gets a node with the maximal key value from the red-black tree. |
---|
325 | * |
---|
326 | * This function extracts a node with the maximum key value from tree and |
---|
327 | * returns a pointer to that node if it exists. In case multiple nodes with a |
---|
328 | * maximum key value exist, then they are extracted in LIFO order. |
---|
329 | * |
---|
330 | * @param[in] the_rbtree The red-black tree control. |
---|
331 | * |
---|
332 | * @retval NULL The tree is empty. |
---|
333 | * @retval node A node with the maximal key value on the tree. |
---|
334 | */ |
---|
335 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_get_max( |
---|
336 | rtems_rbtree_control *the_rbtree |
---|
337 | ) |
---|
338 | { |
---|
339 | rtems_rbtree_node *the_node = rtems_rbtree_max( the_rbtree ); |
---|
340 | |
---|
341 | if ( the_node != NULL ) { |
---|
342 | rtems_rbtree_extract( the_rbtree, the_node ); |
---|
343 | } |
---|
344 | |
---|
345 | return the_node; |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * @brief Peek at the min node on a rbtree. |
---|
350 | * |
---|
351 | * This function returns a pointer to the min node from @a the_rbtree |
---|
352 | * without changing the tree. If @a the_rbtree is empty, |
---|
353 | * then NULL is returned. |
---|
354 | */ |
---|
355 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_peek_min( |
---|
356 | const rtems_rbtree_control *the_rbtree |
---|
357 | ) |
---|
358 | { |
---|
359 | return rtems_rbtree_min( the_rbtree ); |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | * @brief Peek at the max node on a rbtree. |
---|
364 | * |
---|
365 | * This function returns a pointer to the max node from @a the_rbtree |
---|
366 | * without changing the tree. If @a the_rbtree is empty, |
---|
367 | * then NULL is returned. |
---|
368 | */ |
---|
369 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_peek_max( |
---|
370 | const rtems_rbtree_control *the_rbtree |
---|
371 | ) |
---|
372 | { |
---|
373 | return rtems_rbtree_max( the_rbtree ); |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * @copydoc _RBTree_Find_control() |
---|
378 | */ |
---|
379 | RTEMS_INLINE_ROUTINE rtems_rbtree_control *rtems_rbtree_find_control( |
---|
380 | const rtems_rbtree_node *the_node |
---|
381 | ) |
---|
382 | { |
---|
383 | return _RBTree_Find_control( the_node ); |
---|
384 | } |
---|
385 | |
---|
386 | /** |
---|
387 | * @copydoc _RBTree_Insert() |
---|
388 | */ |
---|
389 | RTEMS_INLINE_ROUTINE rtems_rbtree_node *rtems_rbtree_insert( |
---|
390 | rtems_rbtree_control *the_rbtree, |
---|
391 | rtems_rbtree_node *the_node, |
---|
392 | rtems_rbtree_compare compare, |
---|
393 | bool is_unique |
---|
394 | ) |
---|
395 | { |
---|
396 | return _RBTree_Insert( the_rbtree, the_node, compare, is_unique ); |
---|
397 | } |
---|
398 | |
---|
399 | /** @} */ |
---|
400 | |
---|
401 | #ifdef __cplusplus |
---|
402 | } |
---|
403 | #endif |
---|
404 | |
---|
405 | #endif |
---|
406 | /* end of include file */ |
---|