GNU libmicrohttpd  0.9.29
tsearch.c
Go to the documentation of this file.
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 
12 #include "tsearch.h"
13 #include <stdlib.h>
14 
15 typedef struct node {
16  const void *key;
17  struct node *llink, *rlink;
18 } node_t;
19 
20 /* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */
21 /* find or insert datum into search tree */
22 void *
23 tsearch(const void *vkey, /* key to be located */
24  void **vrootp, /* address of tree root */
25  int (*compar)(const void *, const void *))
26 {
27  node_t *q;
28  node_t **rootp = (node_t **)vrootp;
29 
30  if (rootp == NULL)
31  return NULL;
32 
33  while (*rootp != NULL) { /* Knuth's T1: */
34  int r;
35 
36  if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
37  return *rootp; /* we found it! */
38 
39  rootp = (r < 0) ?
40  &(*rootp)->llink : /* T3: follow left branch */
41  &(*rootp)->rlink; /* T4: follow right branch */
42  }
43 
44  q = malloc(sizeof(node_t)); /* T5: key not found */
45  if (q) { /* make new node */
46  *rootp = q; /* link new node to old */
47  q->key = vkey; /* initialize new node */
48  q->llink = q->rlink = NULL;
49  }
50  return q;
51 }
52 
53 /* $NetBSD: tfind.c,v 1.5 2005/03/23 08:16:53 kleink Exp $ */
54 /* find a node, or return NULL */
55 void *
56 tfind(const void *vkey, /* key to be found */
57  void * const *vrootp, /* address of the tree root */
58  int (*compar)(const void *, const void *))
59 {
60  node_t * const *rootp = (node_t * const*)vrootp;
61 
62  if (rootp == NULL)
63  return NULL;
64 
65  while (*rootp != NULL) { /* T1: */
66  int r;
67 
68  if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
69  return *rootp; /* key found */
70  rootp = (r < 0) ?
71  &(*rootp)->llink : /* T3: follow left branch */
72  &(*rootp)->rlink; /* T4: follow right branch */
73  }
74  return NULL;
75 }
76 
77 /* $NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $ */
78 /*
79  * delete node with given key
80  *
81  * vkey: key to be deleted
82  * vrootp: address of the root of the tree
83  * compar: function to carry out node comparisons
84  */
85 void *
86 tdelete(const void * __restrict vkey, void ** __restrict vrootp,
87  int (*compar)(const void *, const void *))
88 {
89  node_t **rootp = (node_t **)vrootp;
90  node_t *p, *q, *r;
91  int cmp;
92 
93  if (rootp == NULL || (p = *rootp) == NULL)
94  return NULL;
95 
96  while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
97  p = *rootp;
98  rootp = (cmp < 0) ?
99  &(*rootp)->llink : /* follow llink branch */
100  &(*rootp)->rlink; /* follow rlink branch */
101  if (*rootp == NULL)
102  return NULL; /* key not found */
103  }
104  r = (*rootp)->rlink; /* D1: */
105  if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
106  q = r;
107  else if (r != NULL) { /* Right link is NULL? */
108  if (r->llink == NULL) { /* D2: Find successor */
109  r->llink = q;
110  q = r;
111  } else { /* D3: Find NULL link */
112  for (q = r->llink; q->llink != NULL; q = r->llink)
113  r = q;
114  r->llink = q->rlink;
115  q->llink = (*rootp)->llink;
116  q->rlink = (*rootp)->rlink;
117  }
118  }
119  free(*rootp); /* D4: Free node */
120  *rootp = q; /* link parent to new node */
121  return p;
122 }
123 
124 /* end of tsearch.c */
#define NULL
Definition: reason_phrase.c:30
struct node node_t
void * tdelete(const void *__restrict vkey, void **__restrict vrootp, int(*compar)(const void *, const void *))
Definition: tsearch.c:86
void * tfind(const void *vkey, void *const *vrootp, int(*compar)(const void *, const void *))
Definition: tsearch.c:56
void * tsearch(const void *vkey, void **vrootp, int(*compar)(const void *, const void *))
Definition: tsearch.c:23