1 .\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $
3 .\" Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by Niels Provos.
17 .\" 4. The name of the author may not be used to endorse or promote products
18 .\" derived from this software without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 .Nm ARB_PROTOTYPE_STATIC ,
39 .Nm ARB_PROTOTYPE_INSERT ,
40 .Nm ARB_PROTOTYPE_INSERT_COLOR ,
41 .Nm ARB_PROTOTYPE_REMOVE ,
42 .Nm ARB_PROTOTYPE_REMOVE_COLOR ,
43 .Nm ARB_PROTOTYPE_FIND ,
44 .Nm ARB_PROTOTYPE_NFIND ,
45 .Nm ARB_PROTOTYPE_NEXT ,
46 .Nm ARB_PROTOTYPE_PREV ,
47 .Nm ARB_PROTOTYPE_MINMAX ,
49 .Nm ARB_GENERATE_STATIC ,
50 .Nm ARB_GENERATE_INSERT ,
51 .Nm ARB_GENERATE_INSERT_COLOR ,
52 .Nm ARB_GENERATE_REMOVE ,
53 .Nm ARB_GENERATE_REMOVE_COLOR ,
54 .Nm ARB_GENERATE_FIND ,
55 .Nm ARB_GENERATE_NFIND ,
56 .Nm ARB_GENERATE_NEXT ,
57 .Nm ARB_GENERATE_PREV ,
58 .Nm ARB_GENERATE_MINMAX ,
87 .Nm ARB_FOREACH_FROM ,
88 .Nm ARB_FOREACH_SAFE ,
89 .Nm ARB_FOREACH_REVERSE ,
90 .Nm ARB_FOREACH_REVERSE_FROM ,
91 .Nm ARB_FOREACH_REVERSE_SAFE ,
95 .Nd "array-based red-black trees"
98 .Fn ARB_PROTOTYPE NAME TYPE FIELD CMP
99 .Fn ARB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
100 .Fn ARB_PROTOTYPE_INSERT NAME TYPE ATTR
101 .Fn ARB_PROTOTYPE_INSERT_COLOR NAME TYPE ATTR
102 .Fn ARB_PROTOTYPE_REMOVE NAME TYPE ATTR
103 .Fn ARB_PROTOTYPE_REMOVE_COLOR NAME TYPE ATTR
104 .Fn ARB_PROTOTYPE_FIND NAME TYPE ATTR
105 .Fn ARB_PROTOTYPE_NFIND NAME TYPE ATTR
106 .Fn ARB_PROTOTYPE_NEXT NAME TYPE ATTR
107 .Fn ARB_PROTOTYPE_PREV NAME TYPE ATTR
108 .Fn ARB_PROTOTYPE_MINMAX NAME TYPE ATTR
109 .Fn ARB_GENERATE NAME TYPE FIELD CMP
110 .Fn ARB_GENERATE_STATIC NAME TYPE FIELD CMP
111 .Fn ARB_GENERATE_INSERT NAME TYPE FIELD CMP ATTR
112 .Fn ARB_GENERATE_INSERT_COLOR NAME TYPE FIELD ATTR
113 .Fn ARB_GENERATE_REMOVE NAME TYPE FIELD ATTR
114 .Fn ARB_GENERATE_REMOVE_COLOR NAME TYPE FIELD ATTR
115 .Fn ARB_GENERATE_FIND NAME TYPE FIELD CMP ATTR
116 .Fn ARB_GENERATE_NFIND NAME TYPE FIELD CMP ATTR
117 .Fn ARB_GENERATE_NEXT NAME TYPE FIELD ATTR
118 .Fn ARB_GENERATE_PREV NAME TYPE FIELD ATTR
119 .Fn ARB_GENERATE_MINMAX NAME TYPE FIELD ATTR
120 .Fn ARB<8|16|32>_ENTRY
121 .Fn ARB<8|16|32>_HEAD HEADNAME TYPE
123 .Fn ARB_ALLOCSIZE "ARB_HEAD *head" "int<8|16|32>_t maxnodes" "struct TYPE *elm"
124 .Fn ARB_INITIALIZER "ARB_HEAD *head" "int<8|16|32>_t maxnodes"
126 .Fn ARB_ROOT "ARB_HEAD *head"
128 .Fn ARB_EMPTY "ARB_HEAD *head"
130 .Fn ARB_FULL "ARB_HEAD *head"
132 .Fn ARB_CURNODES "ARB_HEAD *head"
134 .Fn ARB_MAXNODES "ARB_HEAD *head"
136 .Fn ARB_NEXT NAME "ARB_HEAD *head" "struct TYPE *elm"
138 .Fn ARB_PREV NAME "ARB_HEAD *head" "struct TYPE *elm"
140 .Fn ARB_MIN NAME "ARB_HEAD *head"
142 .Fn ARB_MAX NAME "ARB_HEAD *head"
144 .Fn ARB_FIND NAME "ARB_HEAD *head" "struct TYPE *elm"
146 .Fn ARB_NFIND NAME "ARB_HEAD *head" "struct TYPE *elm"
148 .Fn ARB_LEFT "struct TYPE *elm" "ARB_ENTRY NAME"
150 .Fn ARB_LEFTIDX "struct TYPE *elm" "ARB_ENTRY NAME"
152 .Fn ARB_RIGHT "struct TYPE *elm" "ARB_ENTRY NAME"
154 .Fn ARB_RIGHTIDX "struct TYPE *elm" "ARB_ENTRY NAME"
156 .Fn ARB_PARENT "struct TYPE *elm" "ARB_ENTRY NAME"
158 .Fn ARB_PARENTIDX "struct TYPE *elm" "ARB_ENTRY NAME"
160 .Fn ARB_GETFREE "ARB_HEAD *head" "FIELD"
162 .Fn ARB_FREEIDX "ARB_HEAD *head"
163 .Fn ARB_FOREACH VARNAME NAME "ARB_HEAD *head"
164 .Fn ARB_FOREACH_FROM "VARNAME" "NAME" "POS_VARNAME"
165 .Fn ARB_FOREACH_SAFE "VARNAME" "NAME" "ARB_HEAD *head" "TEMP_VARNAME"
166 .Fn ARB_FOREACH_REVERSE VARNAME NAME "ARB_HEAD *head"
167 .Fn ARB_FOREACH_REVERSE_FROM "VARNAME" "NAME" "POS_VARNAME"
168 .Fn ARB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "ARB_HEAD *head" "TEMP_VARNAME"
170 .Fn ARB_INIT "struct TYPE *elm" "FIELD" "ARB_HEAD *head" "int<8|16|32>_t maxnodes"
172 .Fn ARB_INSERT NAME "ARB_HEAD *head" "struct TYPE *elm"
174 .Fn ARB_REMOVE NAME "ARB_HEAD *head" "struct TYPE *elm"
176 These macros define data structures for and array-based red-black trees.
177 They use a single, continuous chunk of memory, and are useful
178 e.g., when the tree needs to be transferred between userspace and kernel.
180 In the macro definitions,
182 is the name tag of a user defined structure that must contain a field of type
188 is the name tag of a user defined structure that must be declared
194 has to be a unique name prefix for every tree that is defined.
196 The function prototypes are declared with
199 .Fn ARB_PROTOTYPE_STATIC .
200 The function bodies are generated with
203 .Fn ARB_GENERATE_STATIC .
204 See the examples below for further explanation of how these macros are used.
206 A red-black tree is a binary search tree with the node color as an
208 It fulfills a set of conditions:
209 .Bl -enum -offset indent
211 Every search path from the root to a leaf consists of the same number of
214 Each red node (except for the root) has a black parent.
216 Each leaf node is black.
219 Every operation on a red-black tree is bounded as
221 The maximum height of a red-black tree is
225 trees require entries to be allocated as an array, and uses array
226 indices to link entries together.
227 The maximum number of
229 tree entries is therefore constrained by the minimum of array size and choice of
230 signed integer data type used to store array indices.
233 to compute the size of memory chunk to allocate.
235 A red-black tree is headed by a structure defined by the
239 structure is declared with either of the following:
240 .Bd -ragged -offset indent
241 .Fn ARB<8|16|32>_HEAD HEADNAME TYPE
247 is the name of the structure to be defined, and struct
249 is the type of the elements to be inserted into the tree.
253 variant includes a suffix denoting the signed integer data type size
255 used to store array indices.
258 creates a red-black tree head strucutre with 8-bit signed array indices capable
259 of indexing up to 128 entries.
263 macro declares a structure that allows elements to be connected in the tree.
265 .Fn ARB<8|16|32>_HEAD
268 variant includes a suffix denoting the signed integer data type size
270 used to store array indices.
271 Entries should use the same number of bits as the tree head structure they will
274 In order to use the functions that manipulate the tree structure,
275 their prototypes need to be declared with the
278 .Fn ARB_PROTOTYPE_STATIC
282 is a unique identifier for this particular tree.
285 argument is the type of the structure that is being managed
289 argument is the name of the element defined by
291 Individual prototypes can be declared with
292 .Fn ARB_PROTOTYPE_INSERT ,
293 .Fn ARB_PROTOTYPE_INSERT_COLOR ,
294 .Fn ARB_PROTOTYPE_REMOVE ,
295 .Fn ARB_PROTOTYPE_REMOVE_COLOR ,
296 .Fn ARB_PROTOTYPE_FIND ,
297 .Fn ARB_PROTOTYPE_NFIND ,
298 .Fn ARB_PROTOTYPE_NEXT ,
299 .Fn ARB_PROTOTYPE_PREV ,
301 .Fn ARB_PROTOTYPE_MINMAX
302 in case not all functions are required.
303 The individual prototype macros expect
311 argument must be empty for global functions or
313 for static functions.
315 The function bodies are generated with the
318 .Fn ARB_GENERATE_STATIC
320 These macros take the same arguments as the
323 .Fn ARB_PROTOTYPE_STATIC
324 macros, but should be used only once.
325 As an alternative individual function bodies are generated with the
326 .Fn ARB_GENERATE_INSERT ,
327 .Fn ARB_GENERATE_INSERT_COLOR ,
328 .Fn ARB_GENERATE_REMOVE ,
329 .Fn ARB_GENERATE_REMOVE_COLOR ,
330 .Fn ARB_GENERATE_FIND ,
331 .Fn ARB_GENERATE_NFIND ,
332 .Fn ARB_GENERATE_NEXT ,
333 .Fn ARB_GENERATE_PREV ,
335 .Fn ARB_GENERATE_MINMAX
341 argument is the name of a function used to compare tree nodes
343 The function takes two arguments of type
344 .Vt "struct TYPE *" .
345 If the first argument is smaller than the second, the function returns a
346 value smaller than zero.
347 If they are equal, the function returns zero.
348 Otherwise, it should return a value greater than zero.
350 function defines the order of the tree elements.
354 macro initializes the tree referenced by
356 with the array length of
359 The red-black tree can also be initialized statically by using the
362 .Bd -ragged -offset indent
363 .Fn ARB<8|16|32>_HEAD HEADNAME TYPE
366 .Fn ARB_INITIALIZER &head maxnodes ;
371 macro inserts the new element
377 macro removes the element
379 from the tree pointed by
386 macros can be used to find a particular element in the tree.
387 .Bd -literal -offset indent
388 struct TYPE find, *res;
390 res = RB_FIND(NAME, head, &find);
400 macros can be used to traverse the tree:
402 .Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
404 Or, for simplicity, one can use the
407 .Fn ARB_FOREACH_REVERSE
409 .Bd -ragged -offset indent
410 .Fn RB_FOREACH np NAME head
416 .Fn ARB_FOREACH_REVERSE_SAFE
417 traverse the tree referenced by head
418 in a forward or reverse direction respectively,
419 assigning each element in turn to np.
420 However, unlike their unsafe counterparts,
421 they permit both the removal of np
422 as well as freeing it from within the loop safely
423 without interfering with the traversal.
428 .Fn ARB_FOREACH_REVERSE_FROM
429 may be used to continue an interrupted traversal
430 in a forward or reverse direction respectively.
431 The head pointer is not required.
432 The pointer to the node from where to resume the traversal
433 should be passed as their last argument,
434 and will be overwritten to provide safe traversal.
438 macro should be used to check whether a red-black tree is empty.
440 Given that ARB trees have an intrinsic upper bound on the number of entries,
441 some ARB-specific additional macros are defined.
444 macro returns a boolean indicating whether the current number of tree entries
445 equals the tree's maximum.
450 macros return the current and maximum number of entries respectively.
453 macro returns a pointer to the next free entry in the array of entries, ready to
454 be linked into the tree.
459 if the element was inserted in the tree successfully, otherwise they
460 return a pointer to the element with the colliding key.
464 returns the pointer to the removed element otherwise they return
466 to indicate an error.
473 macros first appeared in
478 macros were implemented by
479 .An Lawrence Stewart Aq Mt lstewart@FreeBSD.org ,