]> git.saurik.com Git - apple/libc.git/blame - gen/NetBSD/rbtree.3
Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / NetBSD / rbtree.3
CommitLineData
b061a43b 1.\" $NetBSD: rbtree.3,v 1.12 2016/08/30 05:12:00 dholland Exp $
6465356a
A
2.\"
3.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" Portions Copyright (c) 2012 Apple Inc. All rights reserved.
7.\"
8.\" This code is derived from software contributed to The NetBSD Foundation
9.\" by Matt Thomas, Niels Provos, and David Young.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\" notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\" notice, this list of conditions and the following disclaimer in the
18.\" documentation and/or other materials provided with the distribution.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30.\" POSSIBILITY OF SUCH DAMAGE.
31.\"
b061a43b 32.Dd August 29, 2016
6465356a
A
33.Dt RBTREE 3
34.Os
35.Sh NAME
b061a43b
A
36.Nm rbtree ,
37.Nm rb_tree_init ,
38.Nm rb_tree_insert_node ,
39.Nm rb_tree_remove_node ,
40.Nm rb_tree_find_node ,
41.Nm rb_tree_find_node_geq ,
42.Nm rb_tree_find_node_leq ,
43.Nm rb_tree_iterate ,
44.Nm rb_tree_count ,
45.Nm RB_TREE_MIN ,
46.Nm RB_TREE_MAX ,
47.Nm RB_TREE_FOREACH ,
507116e3
A
48.Nm RB_TREE_FOREACH_SAFE ,
49.Nm RB_TREE_FOREACH_REVERSE ,
50.Nm RB_TREE_FOREACH_REVERSE_SAFE
6465356a
A
51.Nd red-black tree
52.Sh LIBRARY
53.Lb libc
54.Sh SYNOPSIS
55.In sys/rbtree.h
56.Ft void
57.Fn rb_tree_init "rb_tree_t *rbt" "const rb_tree_ops_t *ops"
58.Ft void *
59.Fn rb_tree_insert_node "rb_tree_t *rbt" "void *rb"
60.Ft void
61.Fn rb_tree_remove_node "rb_tree_t *rbt" "void *rb"
62.Ft void *
63.Fn rb_tree_find_node "rb_tree_t *rbt" "const void *key"
64.Ft void *
65.Fn rb_tree_find_node_geq "rb_tree_t *rbt" "const void *key"
66.Ft void *
67.Fn rb_tree_find_node_leq "rb_tree_t *rbt" "const void *key"
68.Ft void *
69.Fn rb_tree_iterate "rb_tree_t *rbt" "void *rb" "const unsigned int direction"
70.Ft size_t
71.Fn rb_tree_count "rb_tree_t *rbt"
b061a43b
A
72.Ft void *
73.Fn RB_TREE_MIN "rb_tree_t *rbt"
74.Ft void *
75.Fn RB_TREE_MAX "rb_tree_t *rbt"
76.Fn RB_TREE_FOREACH "void *rb" "rb_tree_t *rbt"
507116e3 77.Fn RB_TREE_FOREACH_SAFE "void *rb" "rb_tree_t *rbt" "void *tmp"
b061a43b 78.Fn RB_TREE_FOREACH_REVERSE "void *rb" "rb_tree_t *rbt"
507116e3 79.Fn RB_TREE_FOREACH_REVERSE_SAFE "void *rb" "rb_tree_t *rbt" "void *tmp"
6465356a
A
80.Sh DESCRIPTION
81.Nm
82provides red-black trees.
83A red-black tree is a binary search tree with the node color as an
84extra attribute.
85It fulfills a set of conditions:
86.Bl -enum -offset indent
87.It
88Every search path from the root to a leaf consists of the same number of
89black nodes.
90.It
91Each red node (except for the root) has a black parent.
92.It
93Each leaf node is black.
94.El
95.Pp
96Every operation on a red-black tree is bounded as O(lg n).
97The maximum height of a red-black tree is 2lg (n+1).
98.Sh TYPES
99.Bl -tag -width compact
100.It Vt rb_tree_t
101A red-black tree.
102.It Vt typedef signed int \
103(* rbto_compare_nodes_fn)(void *context, const void *node1, const void *node2);
b061a43b 104The node-comparison function.
6465356a
A
105Defines an ordering on nodes.
106Returns a negative value if the first node
107.Ar node1
108precedes the second node
109.Ar node2 .
110Returns a positive value if the first node
111.Ar node1
112follows the second node
113.Ar node2 .
114Returns 0 if the first node
115.Ar node1
116and the second node
117.Ar node2
118are identical according to the ordering.
119.It Vt typedef signed int \
120(* rbto_compare_key_fn)(void *context, const void *node, const void *key);
b061a43b 121The node-key comparison function.
6465356a
A
122Defines the order of nodes and keys.
123Returns a negative value if the node
124.Ar node
125precedes the key
126.Ar key .
127Returns a positive value if the node
128.Ar node
129follows the key
130.Ar key .
131Returns 0 if the node
132.Ar node
133is identical to the key
134.Ar key
135according to the ordering.
136.It Vt rb_tree_ops_t
b061a43b
A
137Defines the function for comparing two nodes in the same tree,
138the function for comparing a node in the tree with a key,
6465356a
A
139the offset of member
140.Vt rb_node_t
b061a43b
A
141within the node type,
142and the opaque context pointer passed to the comparison functions.
6465356a
A
143Members of
144.Vt rb_tree_ops_t
145are
146.Bd -literal
147 rbto_compare_nodes_fn rbto_compare_nodes;
148 rbto_compare_key_fn rbto_compare_key;
149 size_t rbto_node_offset;
150 void *rbto_context;
151.Ed
152.It Vt rb_node_t
153A node in a red-black tree has this structure as a member.
b061a43b
A
154The offset of the
155.Vt rb_node_t
156member in the caller's node structure should be provided as
157.Va rbto_node_offset .
158(None of the functions in the
159.Nm
160interface are meant to take pointers directly to the
161.Vt rb_node_t
162member.)
6465356a
A
163.El
164.Sh FUNCTIONS
165.Bl -tag -width compact
166.It Fn rb_tree_init "rbt" "ops"
167Initialize the red-black tree
168.Fa rbt .
b061a43b 169Let the comparison functions given by
6465356a
A
170.Fa ops
171define the order of nodes in the tree for
172the purposes of insertion, search, and iteration.
173.Fn rb_tree_init
174always succeeds.
175.It Fn rb_tree_insert_node "rbt" "rb"
176Insert the node
177.Fa rb
178into the tree
179.Fa rbt .
180Return inserted node on success,
181already existing node on failure.
182.It Fn rb_tree_remove_node "rbt" "rb"
183Remove the node
184.Fa rb
185from the tree
186.Fa rbt .
187.It Fn rb_tree_find_node "rbt" "key"
188Search the tree
189.Fa rbt
190for a node exactly matching
191.Fa key .
192If no such node is in the tree, return
193.Dv NULL .
194Otherwise, return the matching node.
195.It Fn rb_tree_find_node_geq "rbt" "key"
196Search the tree
197.Fa rbt
198for a node that exactly matches
199.Fa key
200and return it.
201If no such node is present, return the first node following
202.Fa key
203or, if no such node is in the tree, return
204.Dv NULL .
205.It Fn rb_tree_find_node_leq "rbt" "key"
206Search the tree
207.Fa rbt
208for a node that exactly matches
209.Fa key
210and return it.
211If no such node is present, return the first node preceding
212.Fa key
213or, if no such node is in the tree, return
214.Dv NULL .
215.It Fn rb_tree_iterate "rbt" "rb" "direction"
216If
217.Fa direction
218is
219.Dv RB_DIR_LEFT ,
220return the node in the tree
221.Fa rbt
222immediately preceding the node
223.Fa rb
224or, if
225.Fa rb
226is
227.Dv NULL ,
b061a43b 228return the first node in
6465356a
A
229.Fa rbt
230or, if the tree is empty, return
231.Dv NULL .
232.Pp
233If
234.Fa direction
235is
236.Dv RB_DIR_RIGHT ,
237return the node in the tree
238.Fa rbt
239immediately following the node
240.Fa rb
241or, if
242.Fa rb
243is
244.Dv NULL ,
b061a43b 245return the last node in
6465356a
A
246.Fa rbt
247or, if the tree is empty, return
248.Dv NULL .
249.It Fn rb_tree_count "rbt"
250Return the number of nodes in the tree
251.Fa rbt .
b061a43b 252If
6465356a
A
253.Fa rbt
254is
255.Dv NULL ,
2560 is returned.
b061a43b
A
257.It Fn RB_TREE_MIN "rbt"
258Return the first node in
259.Fa rbt ,
260i.e. the node with the least key, or
261.Dv NULL
262if
263.Fa rbt
264is empty.
265.It Fn RB_TREE_MAX "rbt"
266Return the last node in
267.Fa rbt ,
268i.e. the node with the greatest key, or
269.Dv NULL
270if
271.Fa rbt
272is empty.
273.It Fn RB_TREE_FOREACH "rb" "rbt"
274.Nm RB_TREE_FOREACH
275is a macro to be used in the place of a
276.Dv for
277header preceding a statement to traverse the nodes in
278.Fa rbt
279from least to greatest, assigning
280.Fa rb
281to each node in turn and executing the statement.
507116e3
A
282.It Fn RB_TREE_FOREACH_SAFE "rb" "rbt" "tmp"
283.Nm RB_TREE_FOREACH_SAFE
284is a macro to be used like
285.Nm RB_TREE_FOREACH
286but which uses a temporary variable to permit safe modification or deletion of
287.Fa rb
288in the body of the loop.
b061a43b
A
289.It Fn RB_TREE_FOREACH_REVERSE "rb" "rbt"
290.Nm RB_TREE_FOREACH_REVERSE
291is a macro to be used in the place of a
292.Dv for
293header preceding a statement to traverse the nodes in
294.Fa rbt
295from greatest to least, assigning
296.Fa rb
297to each node in turn and executing the statement.
507116e3
A
298.It Fn RB_TREE_FOREACH_REVERSE_SAFE "rb" "rbt" "tmp"
299.Nm RB_TREE_FOREACH_REVERSE_SAFE
300is a macro to be used like
301.Nm RB_TREE_FOREACH_REVERSE
302but which uses a temporary variable to permit safe modification or deletion of
303.Fa rb
304in the body of the loop.
6465356a 305.El
6465356a 306.Sh SEE ALSO
b061a43b
A
307.Xr queue 3 ,
308.Xr tree 3
6465356a
A
309.Sh HISTORY
310The
311.Nm
312interface first appeared in
313.Nx 6.0 .
314.Sh AUTHORS
b061a43b 315.An Matt Thomas Aq Mt matt@NetBSD.org
6465356a
A
316wrote
317.Nm .
318.Pp
b061a43b 319.An Niels Provos Aq Mt provos@citi.umich.edu
6465356a
A
320wrote the
321.Xr tree 3
322manual page.
323Portions of this page derive from that page.
324.\" .Sh CAVEATS
325.\" .Sh BUGS
326.\" .Sh SECURITY CONSIDERATIONS