macOS vout: fix forgotten ';'
[vlc.git] / compat / tfind.c
blob804ecb8e43579f28064fc255f9b4f9e59df4d0a3
1 /*****************************************************************************
2 * tfind.c : implement every t* fuctions
3 *****************************************************************************/
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
9 /** search.h is not present so every t* functions has to be implemented */
10 #ifndef HAVE_SEARCH_H
12 #include <assert.h>
13 #include <stdlib.h>
15 typedef struct node {
16 char *key;
17 struct node *llink, *rlink;
18 } node_t;
20 /* $NetBSD: tdelete.c,v 1.4 2006/03/19 01:12:08 christos Exp $ */
23 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
24 * the AT&T man page says.
26 * The node_t structure is for internal use only, lint doesn't grok it.
28 * Written by reading the System V Interface Definition, not the code.
30 * Totally public domain.
33 /* delete node with given key */
34 void *
35 tdelete(vkey, vrootp, compar)
36 const void *vkey; /* key to be deleted */
37 void **vrootp; /* address of the root of tree */
38 int (*compar) (const void *, const void *);
40 node_t **rootp = (node_t **)vrootp;
41 node_t *p, *q, *r;
42 int cmp;
44 assert(vkey != NULL);
45 assert(compar != NULL);
47 if (rootp == NULL || (p = *rootp) == NULL)
48 return NULL;
50 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
51 p = *rootp;
52 rootp = (cmp < 0) ?
53 &(*rootp)->llink : /* follow llink branch */
54 &(*rootp)->rlink; /* follow rlink branch */
55 if (*rootp == NULL)
56 return NULL; /* key not found */
58 r = (*rootp)->rlink; /* D1: */
59 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
60 q = r;
61 else if (r != NULL) { /* Right link is NULL? */
62 if (r->llink == NULL) { /* D2: Find successor */
63 r->llink = q;
64 q = r;
65 } else { /* D3: Find NULL link */
66 for (q = r->llink; q->llink != NULL; q = r->llink)
67 r = q;
68 r->llink = q->rlink;
69 q->llink = (*rootp)->llink;
70 q->rlink = (*rootp)->rlink;
73 if (p != *rootp)
74 free(*rootp); /* D4: Free node */
75 *rootp = q; /* link parent to new node */
76 return p;
80 /* $NetBSD: tdestroy.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
83 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
84 * the AT&T man page says.
86 * The node_t structure is for internal use only, lint doesn't grok it.
88 * Written by reading the System V Interface Definition, not the code.
90 * Totally public domain.
93 /* Walk the nodes of a tree */
94 static void
95 tdestroy_recurse(node_t* root, void (*free_action)(void *))
97 if (root->llink != NULL)
98 tdestroy_recurse(root->llink, free_action);
99 if (root->rlink != NULL)
100 tdestroy_recurse(root->rlink, free_action);
102 (*free_action) ((void *) root->key);
103 free(root);
106 void
107 tdestroy(vrootp, freefct)
108 void *vrootp;
109 void (*freefct)(void *);
111 node_t *root = (node_t *) vrootp;
113 if (root != NULL)
114 tdestroy_recurse(root, freefct);
118 /* $NetBSD: tfind.c,v 1.5 2005/03/23 08:16:53 kleink Exp $ */
121 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
122 * the AT&T man page says.
124 * The node_t structure is for internal use only, lint doesn't grok it.
126 * Written by reading the System V Interface Definition, not the code.
128 * Totally public domain.
131 /* find a node, or return 0 */
132 void *
133 tfind(vkey, vrootp, compar)
134 const void *vkey; /* key to be found */
135 const void **vrootp; /* address of the tree root */
136 int (*compar) (const void *, const void *);
138 node_t * const *rootp = (node_t * const*)vrootp;
140 assert(vkey != NULL);
141 assert(compar != NULL);
143 if (rootp == NULL)
144 return NULL;
146 while (*rootp != NULL) { /* T1: */
147 int r;
149 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
150 return *rootp; /* key found */
151 rootp = (r < 0) ?
152 &(*rootp)->llink : /* T3: follow left branch */
153 &(*rootp)->rlink; /* T4: follow right branch */
155 return NULL;
159 /* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */
162 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
163 * the AT&T man page says.
165 * The node_t structure is for internal use only, lint doesn't grok it.
167 * Written by reading the System V Interface Definition, not the code.
169 * Totally public domain.
172 /* find or insert datum into search tree */
173 void *
174 tsearch(vkey, vrootp, compar)
175 const void *vkey; /* key to be located */
176 void **vrootp; /* address of tree root */
177 int (*compar) (const void *, const void *);
179 node_t *q;
180 node_t **rootp = (node_t **)vrootp;
182 assert(vkey != NULL);
183 assert(compar != NULL);
185 if (rootp == NULL)
186 return NULL;
188 while (*rootp != NULL) { /* Knuth's T1: */
189 int r;
191 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
192 return *rootp; /* we found it! */
194 rootp = (r < 0) ?
195 &(*rootp)->llink : /* T3: follow left branch */
196 &(*rootp)->rlink; /* T4: follow right branch */
199 q = malloc(sizeof(node_t)); /* T5: key not found */
200 if (q != 0) { /* make new node */
201 *rootp = q; /* link new node to old */
202 q->key = (void*)vkey; /* initialize new node */
203 q->llink = q->rlink = NULL;
205 return q;
209 /* $NetBSD: twalk.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
212 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
213 * the AT&T man page says.
215 * The node_t structure is for internal use only, lint doesn't grok it.
217 * Written by reading the System V Interface Definition, not the code.
219 * Totally public domain.
222 /* Walk the nodes of a tree */
223 static void
224 twalk_recurse(root, action, level)
225 const node_t *root; /* Root of the tree to be walked */
226 void (*action) (const void *, VISIT, int);
227 int level;
229 assert(root != NULL);
230 assert(action != NULL);
232 if (root->llink == NULL && root->rlink == NULL)
233 (*action)(root, leaf, level);
234 else {
235 (*action)(root, preorder, level);
236 if (root->llink != NULL)
237 twalk_recurse(root->llink, action, level + 1);
238 (*action)(root, postorder, level);
239 if (root->rlink != NULL)
240 twalk_recurse(root->rlink, action, level + 1);
241 (*action)(root, endorder, level);
245 /* Walk the nodes of a tree */
246 void
247 twalk(vroot, action)
248 const void *vroot; /* Root of the tree to be walked */
249 void (*action) (const void *, VISIT, int);
251 if (vroot != NULL && action != NULL)
252 twalk_recurse(vroot, action, 0);
255 #endif // HAVE_SEARCH_H