Sync libc/stdlib with FreeBSD (ignoring jemalloc, pts, and gdtoa):
[dragonfly.git] / lib / libc / stdlib / twalk.c
1 /*
2  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3  * the AT&T man page says.
4  *
5  * The node_t structure is for internal use only, lint doesn't grok it.
6  *
7  * Written by reading the System V Interface Definition, not the code.
8  *
9  * Totally public domain.
10  *
11  * $NetBSD: twalk.c,v 1.1 1999/02/22 10:33:16 christos Exp $
12  * $FreeBSD: src/lib/libc/stdlib/twalk.c,v 1.5 2003/01/05 02:43:18 tjr Exp $
13  * $DragonFly: src/lib/libc/stdlib/twalk.c,v 1.5 2005/11/24 17:18:30 swildner Exp $
14  */
15
16 #define _SEARCH_PRIVATE
17 #include <search.h>
18 #include <stdlib.h>
19
20 static void trecurse(const node_t *,
21                      void (*action)(const void *, VISIT, int), int level);
22
23 /*
24  * Walk the nodes of a tree
25  *
26  * Parameters:
27  *      root:   Root of the tree to be walked
28  */
29 static void
30 trecurse(const node_t *root, void (*action)(const void *, VISIT, int),
31          int level)
32 {
33
34         if (root->llink == NULL && root->rlink == NULL)
35                 (*action)(root, leaf, level);
36         else {
37                 (*action)(root, preorder, level);
38                 if (root->llink != NULL)
39                         trecurse(root->llink, action, level + 1);
40                 (*action)(root, postorder, level);
41                 if (root->rlink != NULL)
42                         trecurse(root->rlink, action, level + 1);
43                 (*action)(root, endorder, level);
44         }
45 }
46
47 /*
48  * Walk the nodes of a tree
49  *
50  * Parameters:
51  *      vroot:  Root of the tree to be walked
52  */
53 void
54 twalk(const void *vroot,
55       void (*action)(const void *, VISIT, int))
56 {
57         if (vroot != NULL && action != NULL)
58                 trecurse(vroot, action, 0);
59 }