1.\" $OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $ 2.\" 3.\" Copyright 2002 Niels Provos <provos@citi.umich.edu> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 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. 19.\" 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. 30.\" 31.\" $FreeBSD$ 32.\" 33.Dd October 2, 2019 34.Dt ARB 3 35.Os 36.Sh NAME 37.Nm ARB_PROTOTYPE , 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 , 48.Nm ARB_PROTOTYPE_REINSERT , 49.Nm ARB_GENERATE , 50.Nm ARB_GENERATE_STATIC , 51.Nm ARB_GENERATE_INSERT , 52.Nm ARB_GENERATE_INSERT_COLOR , 53.Nm ARB_GENERATE_REMOVE , 54.Nm ARB_GENERATE_REMOVE_COLOR , 55.Nm ARB_GENERATE_FIND , 56.Nm ARB_GENERATE_NFIND , 57.Nm ARB_GENERATE_NEXT , 58.Nm ARB_GENERATE_PREV , 59.Nm ARB_GENERATE_MINMAX , 60.Nm ARB_GENERATE_REINSERT , 61.Nm ARB8_ENTRY , 62.Nm ARB16_ENTRY , 63.Nm ARB32_ENTRY , 64.Nm ARB8_HEAD , 65.Nm ARB16_HEAD , 66.Nm ARB32_HEAD , 67.Nm ARB_ALLOCSIZE , 68.Nm ARB_INITIALIZER , 69.Nm ARB_ROOT , 70.Nm ARB_EMPTY , 71.Nm ARB_FULL , 72.Nm ARB_CURNODES , 73.Nm ARB_MAXNODES , 74.Nm ARB_NEXT , 75.Nm ARB_PREV , 76.Nm ARB_MIN , 77.Nm ARB_MAX , 78.Nm ARB_FIND , 79.Nm ARB_NFIND , 80.Nm ARB_LEFT , 81.Nm ARB_LEFTIDX , 82.Nm ARB_RIGHT , 83.Nm ARB_RIGHTIDX , 84.Nm ARB_PARENT , 85.Nm ARB_PARENTIDX , 86.Nm ARB_GETFREE , 87.Nm ARB_FREEIDX , 88.Nm ARB_FOREACH , 89.Nm ARB_FOREACH_FROM , 90.Nm ARB_FOREACH_SAFE , 91.Nm ARB_FOREACH_REVERSE , 92.Nm ARB_FOREACH_REVERSE_FROM , 93.Nm ARB_FOREACH_REVERSE_SAFE , 94.Nm ARB_INIT , 95.Nm ARB_INSERT , 96.Nm ARB_REMOVE , 97.Nm ARB_REINSERT , 98.Nm ARB_RESET_TREE 99.Nd "array-based red-black trees" 100.Sh SYNOPSIS 101.In sys/arb.h 102.Fn ARB_PROTOTYPE NAME TYPE FIELD CMP 103.Fn ARB_PROTOTYPE_STATIC NAME TYPE FIELD CMP 104.Fn ARB_PROTOTYPE_INSERT NAME TYPE ATTR 105.Fn ARB_PROTOTYPE_INSERT_COLOR NAME TYPE ATTR 106.Fn ARB_PROTOTYPE_REMOVE NAME TYPE ATTR 107.Fn ARB_PROTOTYPE_REMOVE_COLOR NAME TYPE ATTR 108.Fn ARB_PROTOTYPE_FIND NAME TYPE ATTR 109.Fn ARB_PROTOTYPE_NFIND NAME TYPE ATTR 110.Fn ARB_PROTOTYPE_NEXT NAME TYPE ATTR 111.Fn ARB_PROTOTYPE_PREV NAME TYPE ATTR 112.Fn ARB_PROTOTYPE_MINMAX NAME TYPE ATTR 113.Fn ARB_PROTOTYPE_REINSERT NAME TYPE ATTR 114.Fn ARB_GENERATE NAME TYPE FIELD CMP 115.Fn ARB_GENERATE_STATIC NAME TYPE FIELD CMP 116.Fn ARB_GENERATE_INSERT NAME TYPE FIELD CMP ATTR 117.Fn ARB_GENERATE_INSERT_COLOR NAME TYPE FIELD ATTR 118.Fn ARB_GENERATE_REMOVE NAME TYPE FIELD ATTR 119.Fn ARB_GENERATE_REMOVE_COLOR NAME TYPE FIELD ATTR 120.Fn ARB_GENERATE_FIND NAME TYPE FIELD CMP ATTR 121.Fn ARB_GENERATE_NFIND NAME TYPE FIELD CMP ATTR 122.Fn ARB_GENERATE_NEXT NAME TYPE FIELD ATTR 123.Fn ARB_GENERATE_PREV NAME TYPE FIELD ATTR 124.Fn ARB_GENERATE_MINMAX NAME TYPE FIELD ATTR 125.Fn ARB_GENERATE_REINSERT NAME TYPE FIELD CMP ATTR 126.Fn ARB<8|16|32>_ENTRY 127.Fn ARB<8|16|32>_HEAD HEADNAME TYPE 128.Ft "size_t" 129.Fn ARB_ALLOCSIZE "ARB_HEAD *head" "int<8|16|32>_t maxnodes" "struct TYPE *elm" 130.Fn ARB_INITIALIZER "ARB_HEAD *head" "int<8|16|32>_t maxnodes" 131.Ft "struct TYPE *" 132.Fn ARB_ROOT "ARB_HEAD *head" 133.Ft "bool" 134.Fn ARB_EMPTY "ARB_HEAD *head" 135.Ft "bool" 136.Fn ARB_FULL "ARB_HEAD *head" 137.Ft "int<8|16|32>_t" 138.Fn ARB_CURNODES "ARB_HEAD *head" 139.Ft "int<8|16|32>_t" 140.Fn ARB_MAXNODES "ARB_HEAD *head" 141.Ft "struct TYPE *" 142.Fn ARB_NEXT NAME "ARB_HEAD *head" "struct TYPE *elm" 143.Ft "struct TYPE *" 144.Fn ARB_PREV NAME "ARB_HEAD *head" "struct TYPE *elm" 145.Ft "struct TYPE *" 146.Fn ARB_MIN NAME "ARB_HEAD *head" 147.Ft "struct TYPE *" 148.Fn ARB_MAX NAME "ARB_HEAD *head" 149.Ft "struct TYPE *" 150.Fn ARB_FIND NAME "ARB_HEAD *head" "struct TYPE *elm" 151.Ft "struct TYPE *" 152.Fn ARB_NFIND NAME "ARB_HEAD *head" "struct TYPE *elm" 153.Ft "struct TYPE *" 154.Fn ARB_LEFT "struct TYPE *elm" "ARB_ENTRY NAME" 155.Ft "int<8|16|32>_t" 156.Fn ARB_LEFTIDX "struct TYPE *elm" "ARB_ENTRY NAME" 157.Ft "struct TYPE *" 158.Fn ARB_RIGHT "struct TYPE *elm" "ARB_ENTRY NAME" 159.Ft "int<8|16|32>_t" 160.Fn ARB_RIGHTIDX "struct TYPE *elm" "ARB_ENTRY NAME" 161.Ft "struct TYPE *" 162.Fn ARB_PARENT "struct TYPE *elm" "ARB_ENTRY NAME" 163.Ft "int<8|16|32>_t" 164.Fn ARB_PARENTIDX "struct TYPE *elm" "ARB_ENTRY NAME" 165.Ft "struct TYPE *" 166.Fn ARB_GETFREE "ARB_HEAD *head" "FIELD" 167.Ft "int<8|16|32>_t" 168.Fn ARB_FREEIDX "ARB_HEAD *head" 169.Fn ARB_FOREACH VARNAME NAME "ARB_HEAD *head" 170.Fn ARB_FOREACH_FROM "VARNAME" "NAME" "POS_VARNAME" 171.Fn ARB_FOREACH_SAFE "VARNAME" "NAME" "ARB_HEAD *head" "TEMP_VARNAME" 172.Fn ARB_FOREACH_REVERSE VARNAME NAME "ARB_HEAD *head" 173.Fn ARB_FOREACH_REVERSE_FROM "VARNAME" "NAME" "POS_VARNAME" 174.Fn ARB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "ARB_HEAD *head" "TEMP_VARNAME" 175.Ft void 176.Fn ARB_INIT "struct TYPE *elm" "FIELD" "ARB_HEAD *head" "int<8|16|32>_t maxnodes" 177.Ft "struct TYPE *" 178.Fn ARB_INSERT NAME "ARB_HEAD *head" "struct TYPE *elm" 179.Ft "struct TYPE *" 180.Fn ARB_REMOVE NAME "ARB_HEAD *head" "struct TYPE *elm" 181.Ft "struct TYPE *" 182.Fn ARB_REINSERT NAME "ARB_HEAD *head" "struct TYPE *elm" 183.Ft void 184.Fn ARB_RESET_TREE "ARB_HEAD *head" NAME "int<8|16|32>_t maxnodes" 185.Sh DESCRIPTION 186These macros define data structures for and array-based red-black trees. 187They use a single, continuous chunk of memory, and are useful 188e.g., when the tree needs to be transferred between userspace and kernel. 189.Pp 190In the macro definitions, 191.Fa TYPE 192is the name tag of a user defined structure that must contain a field of type 193.Vt ARB_ENTRY , 194named 195.Fa ENTRYNAME . 196The argument 197.Fa HEADNAME 198is the name tag of a user defined structure that must be declared 199using the 200.Fn ARB_HEAD 201macro. 202The argument 203.Fa NAME 204has to be a unique name prefix for every tree that is defined. 205.Pp 206The function prototypes are declared with 207.Fn ARB_PROTOTYPE , 208or 209.Fn ARB_PROTOTYPE_STATIC . 210The function bodies are generated with 211.Fn ARB_GENERATE , 212or 213.Fn ARB_GENERATE_STATIC . 214See the examples below for further explanation of how these macros are used. 215.Pp 216A red-black tree is a binary search tree with the node color as an 217extra attribute. 218It fulfills a set of conditions: 219.Bl -enum -offset indent 220.It 221Every search path from the root to a leaf consists of the same number of 222black nodes. 223.It 224Each red node (except for the root) has a black parent. 225.It 226Each leaf node is black. 227.El 228.Pp 229Every operation on a red-black tree is bounded as 230.Fn O "lg n" . 231The maximum height of a red-black tree is 232.Fn 2lg "n + 1" . 233.Pp 234.Fn ARB_* 235trees require entries to be allocated as an array, and uses array 236indices to link entries together. 237The maximum number of 238.Fn ARB_* 239tree entries is therefore constrained by the minimum of array size and choice of 240signed integer data type used to store array indices. 241Use 242.Fn ARB_ALLOCSIZE 243to compute the size of memory chunk to allocate. 244.Pp 245A red-black tree is headed by a structure defined by the 246.Fn ARB_HEAD 247macro. 248A 249structure is declared with either of the following: 250.Bd -ragged -offset indent 251.Fn ARB<8|16|32>_HEAD HEADNAME TYPE 252.Va head ; 253.Ed 254.Pp 255where 256.Fa HEADNAME 257is the name of the structure to be defined, and struct 258.Fa TYPE 259is the type of the elements to be inserted into the tree. 260.Pp 261The 262.Fn ARB_HEAD 263variant includes a suffix denoting the signed integer data type size 264.Pq in bits 265used to store array indices. 266For example, 267.Fn ARB_HEAD8 268creates a red-black tree head strucutre with 8-bit signed array indices capable 269of indexing up to 128 entries. 270.Pp 271The 272.Fn ARB_ENTRY 273macro declares a structure that allows elements to be connected in the tree. 274Similarly to the 275.Fn ARB<8|16|32>_HEAD 276macro, the 277.Fn ARB_ENTRY 278variant includes a suffix denoting the signed integer data type size 279.Pq in bits 280used to store array indices. 281Entries should use the same number of bits as the tree head structure they will 282be linked into. 283.Pp 284In order to use the functions that manipulate the tree structure, 285their prototypes need to be declared with the 286.Fn ARB_PROTOTYPE 287or 288.Fn ARB_PROTOTYPE_STATIC 289macro, 290where 291.Fa NAME 292is a unique identifier for this particular tree. 293The 294.Fa TYPE 295argument is the type of the structure that is being managed 296by the tree. 297The 298.Fa FIELD 299argument is the name of the element defined by 300.Fn ARB_ENTRY . 301Individual prototypes can be declared with 302.Fn ARB_PROTOTYPE_INSERT , 303.Fn ARB_PROTOTYPE_INSERT_COLOR , 304.Fn ARB_PROTOTYPE_REMOVE , 305.Fn ARB_PROTOTYPE_REMOVE_COLOR , 306.Fn ARB_PROTOTYPE_FIND , 307.Fn ARB_PROTOTYPE_NFIND , 308.Fn ARB_PROTOTYPE_NEXT , 309.Fn ARB_PROTOTYPE_PREV , 310.Fn ARB_PROTOTYPE_MINMAX , 311and 312.Fn ARB_PROTOTYPE_REINSERT 313in case not all functions are required. 314The individual prototype macros expect 315.Fa NAME , 316.Fa TYPE , 317and 318.Fa ATTR 319arguments. 320The 321.Fa ATTR 322argument must be empty for global functions or 323.Fa static 324for static functions. 325.Pp 326The function bodies are generated with the 327.Fn ARB_GENERATE 328or 329.Fn ARB_GENERATE_STATIC 330macro. 331These macros take the same arguments as the 332.Fn ARB_PROTOTYPE 333and 334.Fn ARB_PROTOTYPE_STATIC 335macros, but should be used only once. 336As an alternative individual function bodies are generated with the 337.Fn ARB_GENERATE_INSERT , 338.Fn ARB_GENERATE_INSERT_COLOR , 339.Fn ARB_GENERATE_REMOVE , 340.Fn ARB_GENERATE_REMOVE_COLOR , 341.Fn ARB_GENERATE_FIND , 342.Fn ARB_GENERATE_NFIND , 343.Fn ARB_GENERATE_NEXT , 344.Fn ARB_GENERATE_PREV , 345.Fn ARB_GENERATE_MINMAX , 346and 347.Fn ARB_GENERATE_REINSERT 348macros. 349.Pp 350Finally, 351the 352.Fa CMP 353argument is the name of a function used to compare tree nodes 354with each other. 355The function takes two arguments of type 356.Vt "struct TYPE *" . 357If the first argument is smaller than the second, the function returns a 358value smaller than zero. 359If they are equal, the function returns zero. 360Otherwise, it should return a value greater than zero. 361The compare 362function defines the order of the tree elements. 363.Pp 364The 365.Fn ARB_INIT 366macro initializes the tree referenced by 367.Fa head , 368with the array length of 369.Fa maxnodes . 370.Pp 371The red-black tree can also be initialized statically by using the 372.Fn ARB_INITIALIZER 373macro: 374.Bd -ragged -offset indent 375.Fn ARB<8|16|32>_HEAD HEADNAME TYPE 376.Va head 377= 378.Fn ARB_INITIALIZER &head maxnodes ; 379.Ed 380.Pp 381The 382.Fn ARB_INSERT 383macro inserts the new element 384.Fa elm 385into the tree. 386.Pp 387The 388.Fn ARB_REMOVE 389macro removes the element 390.Fa elm 391from the tree pointed by 392.Fa head . 393.Pp 394The 395.Fn ARB_FIND 396and 397.Fn ARB_NFIND 398macros can be used to find a particular element in the tree. 399.Bd -literal -offset indent 400struct TYPE find, *res; 401find.key = 30; 402res = RB_FIND(NAME, head, &find); 403.Ed 404.Pp 405The 406.Fn ARB_ROOT , 407.Fn ARB_MIN , 408.Fn ARB_MAX , 409.Fn ARB_NEXT , 410and 411.Fn ARB_PREV 412macros can be used to traverse the tree: 413.Pp 414.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))" 415.Pp 416Or, for simplicity, one can use the 417.Fn ARB_FOREACH 418or 419.Fn ARB_FOREACH_REVERSE 420macro: 421.Bd -ragged -offset indent 422.Fn RB_FOREACH np NAME head 423.Ed 424.Pp 425The macros 426.Fn ARB_FOREACH_SAFE 427and 428.Fn ARB_FOREACH_REVERSE_SAFE 429traverse the tree referenced by head 430in a forward or reverse direction respectively, 431assigning each element in turn to np. 432However, unlike their unsafe counterparts, 433they permit both the removal of np 434as well as freeing it from within the loop safely 435without interfering with the traversal. 436.Pp 437Both 438.Fn ARB_FOREACH_FROM 439and 440.Fn ARB_FOREACH_REVERSE_FROM 441may be used to continue an interrupted traversal 442in a forward or reverse direction respectively. 443The head pointer is not required. 444The pointer to the node from where to resume the traversal 445should be passed as their last argument, 446and will be overwritten to provide safe traversal. 447.Pp 448The 449.Fn ARB_EMPTY 450macro should be used to check whether a red-black tree is empty. 451.Pp 452Given that ARB trees have an intrinsic upper bound on the number of entries, 453some ARB-specific additional macros are defined. 454The 455.Fn ARB_FULL 456macro returns a boolean indicating whether the current number of tree entries 457equals the tree's maximum. 458The 459.Fn ARB_CURNODES 460and 461.Fn ARB_MAXNODES 462macros return the current and maximum number of entries respectively. 463The 464.Fn ARB_GETFREE 465macro returns a pointer to the next free entry in the array of entries, ready to 466be linked into the tree. 467The 468.Fn ARB_INSERT 469returns 470.Dv NULL 471if the element was inserted in the tree successfully, otherwise they 472return a pointer to the element with the colliding key. 473.Pp 474Accordingly, 475.Fn ARB_REMOVE 476returns the pointer to the removed element otherwise they return 477.Dv NULL 478to indicate an error. 479.Pp 480The 481.Fn ARB_REINSERT 482macro updates the position of the element 483.Fa elm 484in the tree. 485This must be called if a member of a 486.Nm tree 487is modified in a way that affects comparison, such as by modifying 488a node's key. 489This is a lower overhead alternative to removing the element 490and reinserting it again. 491.Pp 492The 493.Fn ARB_RESET_TREE 494macro discards the tree topology. 495It does not modify embedded object values or the free list. 496.Sh SEE ALSO 497.Xr queue 3 , 498.Xr tree 3 499.Sh HISTORY 500The 501.Nm ARB 502macros first appeared in 503.Fx 13.0 . 504.Sh AUTHORS 505The 506.Nm ARB 507macros were implemented by 508.An Lawrence Stewart Aq Mt lstewart@FreeBSD.org , 509based on 510.Xr tree 3 511macros written by 512.An Niels Provos . 513