aboutsummaryrefslogtreecommitdiff
path: root/src/lib/tsearch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/tsearch.c')
-rw-r--r--src/lib/tsearch.c122
1 files changed, 61 insertions, 61 deletions
diff --git a/src/lib/tsearch.c b/src/lib/tsearch.c
index fe5fcd5b..e43d758f 100644
--- a/src/lib/tsearch.c
+++ b/src/lib/tsearch.c
@@ -13,7 +13,7 @@
13#include <stdlib.h> 13#include <stdlib.h>
14 14
15 15
16typedef struct node 16typedef struct node
17{ 17{
18 const void *key; 18 const void *key;
19 struct node *llink; 19 struct node *llink;
@@ -24,35 +24,35 @@ typedef struct node
24/* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */ 24/* $NetBSD: tsearch.c,v 1.5 2005/11/29 03:12:00 christos Exp $ */
25/* find or insert datum into search tree */ 25/* find or insert datum into search tree */
26void * 26void *
27tsearch (const void *vkey, /* key to be located */ 27tsearch (const void *vkey, /* key to be located */
28 void **vrootp, /* address of tree root */ 28 void **vrootp, /* address of tree root */
29 int (*compar)(const void *, const void *)) 29 int (*compar)(const void *, const void *))
30{ 30{
31 node_t *q; 31 node_t *q;
32 node_t **rootp = (node_t **)vrootp; 32 node_t **rootp = (node_t **) vrootp;
33 33
34 if (NULL == rootp) 34 if (NULL == rootp)
35 return NULL; 35 return NULL;
36 36
37 while (*rootp != NULL) 37 while (*rootp != NULL)
38 { /* Knuth's T1: */ 38 { /* Knuth's T1: */
39 int r; 39 int r;
40 40
41 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */ 41 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
42 return *rootp; /* we found it! */ 42 return *rootp; /* we found it! */
43 43
44 rootp = (r < 0) ? 44 rootp = (r < 0) ?
45 &(*rootp)->llink : /* T3: follow left branch */ 45 &(*rootp)->llink : /* T3: follow left branch */
46 &(*rootp)->rlink; /* T4: follow right branch */ 46 &(*rootp)->rlink; /* T4: follow right branch */
47 } 47 }
48 48
49 q = malloc (sizeof(node_t)); /* T5: key not found */ 49 q = malloc (sizeof(node_t)); /* T5: key not found */
50 if (q) 50 if (q)
51 { /* make new node */ 51 { /* make new node */
52 *rootp = q; /* link new node to old */ 52 *rootp = q; /* link new node to old */
53 q->key = vkey; /* initialize new node */ 53 q->key = vkey; /* initialize new node */
54 q->llink = q->rlink = NULL; 54 q->llink = q->rlink = NULL;
55 } 55 }
56 return q; 56 return q;
57} 57}
58 58
@@ -61,24 +61,24 @@ tsearch (const void *vkey, /* key to be located */
61/* find a node, or return NULL */ 61/* find a node, or return NULL */
62void * 62void *
63tfind (const void *vkey, /* key to be found */ 63tfind (const void *vkey, /* key to be found */
64 void * const *vrootp, /* address of the tree root */ 64 void *const *vrootp, /* address of the tree root */
65 int (*compar)(const void *, const void *)) 65 int (*compar)(const void *, const void *))
66{ 66{
67 node_t * const *rootp = (node_t * const*)vrootp; 67 node_t *const *rootp = (node_t *const*) vrootp;
68 68
69 if (NULL == rootp) 69 if (NULL == rootp)
70 return NULL; 70 return NULL;
71 71
72 while (*rootp != NULL) 72 while (*rootp != NULL)
73 { /* T1: */ 73 { /* T1: */
74 int r; 74 int r;
75 75
76 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */ 76 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
77 return *rootp; /* key found */ 77 return *rootp; /* key found */
78 rootp = (r < 0) ? 78 rootp = (r < 0) ?
79 &(*rootp)->llink : /* T3: follow left branch */ 79 &(*rootp)->llink : /* T3: follow left branch */
80 &(*rootp)->rlink; /* T4: follow right branch */ 80 &(*rootp)->rlink; /* T4: follow right branch */
81 } 81 }
82 return NULL; 82 return NULL;
83} 83}
84 84
@@ -92,51 +92,51 @@ tfind (const void *vkey, /* key to be found */
92 * compar: function to carry out node comparisons 92 * compar: function to carry out node comparisons
93 */ 93 */
94void * 94void *
95tdelete (const void * __restrict vkey, 95tdelete (const void *__restrict vkey,
96 void ** __restrict vrootp, 96 void **__restrict vrootp,
97 int (*compar)(const void *, const void *)) 97 int (*compar)(const void *, const void *))
98{ 98{
99 node_t **rootp = (node_t **)vrootp; 99 node_t **rootp = (node_t **) vrootp;
100 node_t *p; 100 node_t *p;
101 node_t *q; 101 node_t *q;
102 node_t *r; 102 node_t *r;
103 int cmp; 103 int cmp;
104 104
105 if (rootp == NULL || (p = *rootp) == NULL) 105 if ((rootp == NULL)||((p = *rootp) == NULL))
106 return NULL; 106 return NULL;
107 107
108 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) 108 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0)
109 { 109 {
110 p = *rootp; 110 p = *rootp;
111 rootp = (cmp < 0) ? 111 rootp = (cmp < 0) ?
112 &(*rootp)->llink : /* follow llink branch */ 112 &(*rootp)->llink : /* follow llink branch */
113 &(*rootp)->rlink; /* follow rlink branch */ 113 &(*rootp)->rlink; /* follow rlink branch */
114 if (*rootp == NULL) 114 if (*rootp == NULL)
115 return NULL; /* key not found */ 115 return NULL; /* key not found */
116 } 116 }
117 r = (*rootp)->rlink; /* D1: */ 117 r = (*rootp)->rlink; /* D1: */
118 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */ 118 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
119 { 119 {
120 q = r;
121 }
122 else if (r != NULL)
123 { /* Right link is NULL? */
124 if (r->llink == NULL)
125 { /* D2: Find successor */
126 r->llink = q;
120 q = r; 127 q = r;
121 } 128 }
122 else if (r != NULL) 129 else
123 { /* Right link is NULL? */ 130 { /* D3: Find NULL link */
124 if (r->llink == NULL) 131 for (q = r->llink; q->llink != NULL; q = r->llink)
125 { /* D2: Find successor */ 132 r = q;
126 r->llink = q; 133 r->llink = q->rlink;
127 q = r; 134 q->llink = (*rootp)->llink;
128 } 135 q->rlink = (*rootp)->rlink;
129 else
130 { /* D3: Find NULL link */
131 for (q = r->llink; q->llink != NULL; q = r->llink)
132 r = q;
133 r->llink = q->rlink;
134 q->llink = (*rootp)->llink;
135 q->rlink = (*rootp)->rlink;
136 }
137 } 136 }
138 free(*rootp); /* D4: Free node */ 137 }
139 *rootp = q; /* link parent to new node */ 138 free (*rootp); /* D4: Free node */
139 *rootp = q; /* link parent to new node */
140 return p; 140 return p;
141} 141}
142 142