diff options
Diffstat (limited to 'src/lib/libc/stdlib/tsearch.3')
-rw-r--r-- | src/lib/libc/stdlib/tsearch.3 | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3 new file mode 100644 index 0000000000..dbdae7943c --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.3 | |||
@@ -0,0 +1,116 @@ | |||
1 | .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. The name of the author may not be used to endorse or promote products | ||
13 | .\" derived from this software without specific prior written permission. | ||
14 | .\" | ||
15 | .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
16 | .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
17 | .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
18 | .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
20 | .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
21 | .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
22 | .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
23 | .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
24 | .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | .\" | ||
26 | .\" $OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp $ | ||
27 | .\" | ||
28 | .Dd June 15, 1997 | ||
29 | .Dt TSEARCH 3 | ||
30 | .Os | ||
31 | .Sh NAME | ||
32 | .Nm tsearch, tfind, tdelete, twalk | ||
33 | .Nd manipulate binary search trees | ||
34 | .Sh SYNOPSIS | ||
35 | .Fd #include <search.h> | ||
36 | .Ft void * | ||
37 | .Fn tdelete "const void *key" "void **rootp", "int (*compar) (const void *, const void *)" | ||
38 | .Ft void * | ||
39 | .Fn tfind "const void *key" "void * const *rootp", "int (*compar) (const void *, const void *)" | ||
40 | .Ft void * | ||
41 | .Fn tsearch "const void *key" "void **rootp", "int (*compar) (const void *, const void *)" | ||
42 | .Ft void | ||
43 | .Fn twalk "const void *root" "void (*compar) (const void *, VISIT, int)" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn tdelete , | ||
47 | .Fn tfind , | ||
48 | .Fn tsearch , | ||
49 | and | ||
50 | .Fn twalk | ||
51 | functions manage binary search trees based on algorithms T and D | ||
52 | from Knuth (6.2.2). The comparison function passed in by | ||
53 | the user has the same style of return values as | ||
54 | .Xr strcmp 3 . | ||
55 | .Pp | ||
56 | .Fn Tfind | ||
57 | searches for the datum matched by the argument | ||
58 | .Fa key | ||
59 | in the binary tree rooted at | ||
60 | .Fa rootp , | ||
61 | returning a pointer to the datum if it is found and NULL | ||
62 | if it is not. | ||
63 | .Pp | ||
64 | .Fn Tsearch | ||
65 | is identical to | ||
66 | .Fn tfind | ||
67 | except that if no match is found, | ||
68 | .Fa key | ||
69 | is inserted into the tree and a pointer to it is returned. If | ||
70 | .Fa rootp | ||
71 | points to a NULL value a new binary search tree is created. | ||
72 | .Pp | ||
73 | .Fn Tdelete | ||
74 | deletes a node from the specified binary search tree and returns | ||
75 | a pointer to the parent of the node to be deleted. | ||
76 | It takes the same arguments as | ||
77 | .Fn tfind | ||
78 | and | ||
79 | .Fn tsearch . | ||
80 | If the node to be deleted is the root of the binary search tree, | ||
81 | .Fa rootp | ||
82 | will be adjusted. | ||
83 | .Pp | ||
84 | .Fn Twalk | ||
85 | walks the binary search tree rooted in | ||
86 | .fa root | ||
87 | and calls the function | ||
88 | .Fa action | ||
89 | on each node. | ||
90 | .Fa Action | ||
91 | is called with three arguments: a pointer to the current node, | ||
92 | a value from the enum | ||
93 | .Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;" | ||
94 | specifying the traversal type, and a node level (where level | ||
95 | zero is the root of the tree). | ||
96 | .Sh SEE ALSO | ||
97 | .Xr bsearch 3 , | ||
98 | .Xr hsearch 3 , | ||
99 | .Xr lsearch 3 | ||
100 | .Sh RETURN VALUES | ||
101 | The | ||
102 | .Fn tsearch | ||
103 | function returns NULL if allocation of a new node fails (usually | ||
104 | due to a lack of free memory). | ||
105 | .Pp | ||
106 | .Fn Tfind , | ||
107 | .Fn tsearch , | ||
108 | and | ||
109 | .Fn tdelete | ||
110 | return NULL if | ||
111 | .Fa rootp | ||
112 | is NULL or the datum cannot be found. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fn twalk | ||
116 | function returns no value. | ||