Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F151332146
D11053.id29255.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D11053.id29255.diff
View Options
Index: head/lib/libc/stdlib/tsearch.3
===================================================================
--- head/lib/libc/stdlib/tsearch.3
+++ head/lib/libc/stdlib/tsearch.3
@@ -27,7 +27,7 @@
.\" OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp
.\" $FreeBSD$
.\"
-.Dd October 9, 2016
+.Dd June 4, 2017
.Dt TSEARCH 3
.Os
.Sh NAME
@@ -130,6 +130,60 @@
The
.Fn twalk
function returns no value.
+.Sh EXAMPLES
+The following example takes in four strings and compares each one of them
+against each other and then adding them to the tree and attempting to add the
+fourth one a second time, using
+.Fn tfind
+to verify that a duplicate is not added and uses
+.Fn tdelete
+to remove it.
+Finally,
+.Fn twalk
+is used to walk the whole tree printing each node.
+.Bd -literal
+#include <stdio.h>
+#include <search.h>
+#include <string.h>
+
+int
+comp(const void *a, const void *b)
+{
+
+ return strcmp(a, b);
+}
+
+void
+printwalk(const posix_tnode * node, VISIT v, int __unused0)
+{
+
+ if (v == postorder || v == leaf) {
+ printf("node: %s\n", *(char **)node);
+ }
+}
+
+int
+main(void)
+{
+ posix_tnode *root = NULL;
+
+ char one[] = "blah1";
+ char two[] = "blah-2";
+ char three[] = "blah-3";
+ char four[] = "blah-4";
+
+ tsearch(one, &root, comp);
+ tsearch(two, &root, comp);
+ tsearch(three, &root, comp);
+ tsearch(four, &root, comp);
+ tsearch(four, &root, comp);
+ printf("four: %s\n", *(char **)tfind(four, &root, comp));
+ tdelete(four, &root, comp);
+
+ twalk(root, printwalk);
+ return 0;
+}
+.Ed
.Sh SEE ALSO
.Xr bsearch 3 ,
.Xr hsearch 3 ,
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Apr 8, 4:09 PM (22 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
31043582
Default Alt Text
D11053.id29255.diff (1 KB)
Attached To
Mode
D11053: Add a complete example to tsearch(3)
Attached
Detach File
Event Timeline
Log In to Comment