Solaris 10 compatability, try two
[exim.git] / src / src / tree.c
CommitLineData
059ec3d9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
3386088d 5/* Copyright (c) University of Cambridge 1995 - 2015 */
059ec3d9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8/* Functions for maintaining binary balanced trees and some associated
9functions as well. */
10
11
12#include "exim.h"
13
14
165acdd1 15#ifndef MACRO_PREDEF
059ec3d9
PH
16
17
18/*************************************************
19* Add entry to non-recipients tree *
20*************************************************/
21
22/* Duplicates are just discarded.
23
24Arguments:
25 s string to add
26
27Returns: nothing
28*/
29
30void
31tree_add_nonrecipient(uschar *s)
32{
33tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s));
34Ustrcpy(node->name, s);
35node->data.ptr = NULL;
36if (!tree_insertnode(&tree_nonrecipients, node)) store_reset(node);
37}
38
39
40
41/*************************************************
42* Add entry to duplicates tree *
43*************************************************/
44
45/* Duplicates are just discarded.
46
47Argument:
48 s string to add
49 addr the address is is a duplicate of
50
51Returns: nothing
52*/
53
54void
55tree_add_duplicate(uschar *s, address_item *addr)
56{
57tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s));
58Ustrcpy(node->name, s);
59node->data.ptr = addr;
60if (!tree_insertnode(&tree_duplicates, node)) store_reset(node);
61}
62
63
64
65/*************************************************
66* Add entry to unusable addresses tree *
67*************************************************/
68
69/* Duplicates are simply discarded.
70
71Argument: the host item
72Returns: nothing
73*/
74
75void
76tree_add_unusable(host_item *h)
77{
78tree_node *node;
79uschar s[256];
80sprintf(CS s, "T:%.200s:%s", h->name, h->address);
81node = store_get(sizeof(tree_node) + Ustrlen(s));
82Ustrcpy(node->name, s);
83node->data.val = h->why;
84if (h->status == hstatus_unusable_expired) node->data.val += 256;
85if (!tree_insertnode(&tree_unusable, node)) store_reset(node);
86}
87
88
89
90/*************************************************
91* Write a tree in re-readable form *
92*************************************************/
93
94/* This function writes out a tree in a form in which it can
95easily be re-read. It is used for writing out the non-recipients
96tree onto the spool, for retrieval at the next retry time.
97
98The format is as follows:
99
100 . If the tree is empty, write one line containing XX.
101
102 . Otherwise, each node is written, preceded by two letters
103 (Y/N) indicating whether it has left or right children.
104
105 . The left subtree (if any) then follows, then the right subtree.
106
107First, there's an internal recursive subroutine.
108
109Arguments:
110 p current node
111 f FILE to write to
112
113Returns: nothing
114*/
115
116static void
117write_tree(tree_node *p, FILE *f)
118{
165acdd1
JH
119fprintf(f, "%c%c %s\n", p->left ? 'Y':'N', p->right ? 'Y':'N', p->name);
120if (p->left) write_tree(p->left, f);
121if (p->right) write_tree(p->right, f);
059ec3d9
PH
122}
123
124/* This is the top-level function, with the same arguments. */
125
126void
127tree_write(tree_node *p, FILE *f)
128{
165acdd1 129if (!p)
059ec3d9
PH
130 {
131 fprintf(f, "XX\n");
132 return;
133 }
134write_tree(p, f);
135}
136
137
165acdd1 138#endif
059ec3d9
PH
139
140
141
142/***********************************************************
143* Binary Balanced Tree Management Routines *
144***********************************************************/
145
146/* This set of routines maintains a balanced binary tree using
147the algorithm given in Knuth Vol 3 page 455.
148
149The routines make use of uschar * pointers as byte pointers,
150so as to be able to do arithmetic on them, since ANSI Standard
151C does not permit additions and subtractions on void pointers. */
152
153
154/*************************************************
155* Flags and Parameters *
156*************************************************/
157
158#define tree_lbal 1 /* left subtree is longer */
159#define tree_rbal 2 /* right subtree is longer */
160#define tree_bmask 3 /* mask for flipping bits */
161
162
163/*************************************************
164* Insert a new node into a tree *
165*************************************************/
166
167/* The node->name field must (obviously) be set, but the other
168fields need not be initialized.
169
170Arguments:
171 treebase pointer to the root of the tree
172 node the note to insert, with name field set
173
174Returns: TRUE if node inserted; FALSE if not (duplicate)
175*/
176
177int
178tree_insertnode(tree_node **treebase, tree_node *node)
179{
180tree_node *p = *treebase;
181tree_node **q, *r, *s, **t;
182int a;
183
184node->left = node->right = NULL;
185node->balance = 0;
186
187/* Deal with an empty tree */
188
165acdd1 189if (!p)
059ec3d9
PH
190 {
191 *treebase = node;
192 return TRUE;
193 }
194
195/* The tree is not empty. While finding the insertion point,
196q points to the pointer to p, and t points to the pointer to
197the potential re-balancing point. */
198
199q = treebase;
200t = q;
201
202/* Loop to search tree for place to insert new node */
203
204for (;;)
205 {
206 int c = Ustrcmp(node->name, p->name);
207 if (c == 0) return FALSE; /* Duplicate node encountered */
208
209 /* Deal with climbing down the tree, exiting from the loop
210 when we reach a leaf. */
211
165acdd1 212 q = c > 0 ? &p->right : &p->left;
059ec3d9 213 p = *q;
165acdd1 214 if (!p) break;
059ec3d9
PH
215
216 /* Save the address of the pointer to the last node en route
217 which has a non-zero balance factor. */
218
219 if (p->balance != 0) t = q;
220 }
221
222/* When the above loop completes, q points to the pointer to NULL;
223that is the place at which the new node must be inserted. */
224
225*q = node;
226
227/* Set up s as the potential re-balancing point, and r as the
228next node after it along the route. */
229
230s = *t;
165acdd1 231r = Ustrcmp(node->name, s->name) > 0 ? s->right : s->left;
059ec3d9
PH
232
233/* Adjust balance factors along the route from s to node. */
234
235p = r;
236
237while (p != node)
059ec3d9
PH
238 if (Ustrcmp(node->name, p->name) < 0)
239 {
240 p->balance = tree_lbal;
241 p = p->left;
242 }
243 else
244 {
245 p->balance = tree_rbal;
246 p = p->right;
247 }
059ec3d9
PH
248
249/* Now the World-Famous Balancing Act */
250
165acdd1 251a = Ustrcmp(node->name, s->name) < 0 ? tree_lbal : tree_rbal;
059ec3d9 252
165acdd1
JH
253if (s->balance == 0)
254 s->balance = (uschar)a; /* The tree has grown higher */
255else if (s->balance != (uschar)a)
256 s->balance = 0; /* It's become more balanced */
257else /* It's got out of balance */
059ec3d9
PH
258 {
259 /* Perform a single rotation */
260
261 if (r->balance == (uschar)a)
262 {
263 p = r;
264 if (a == tree_rbal)
265 {
266 s->right = r->left;
267 r->left = s;
268 }
269 else
270 {
271 s->left = r->right;
272 r->right = s;
273 }
274 s->balance = 0;
275 r->balance = 0;
276 }
277
278 /* Perform a double rotation There was an occasion when the balancing
279 factors were screwed up by a bug in the code that reads a tree from
280 the spool. In case this ever happens again, check for changing p to NULL
281 and don't do it. It is better to have an unbalanced tree than a crash. */
282
283 else
284 {
285 if (a == tree_rbal)
286 {
165acdd1 287 if (!r->left) return TRUE; /* Bail out if tree corrupt */
059ec3d9
PH
288 p = r->left;
289 r->left = p->right;
290 p->right = r;
291 s->right = p->left;
292 p->left = s;
293 }
294 else
295 {
165acdd1 296 if (!r->right) return TRUE; /* Bail out if tree corrupt */
059ec3d9
PH
297 p = r->right;
298 r->right = p->left;
299 p->left = r;
300 s->left = p->right;
301 p->right = s;
302 }
303
165acdd1
JH
304 s->balance = p->balance == (uschar)a ? (uschar)(a^tree_bmask) : 0;
305 r->balance = p->balance == (uschar)(a^tree_bmask) ? (uschar)a : 0;
059ec3d9
PH
306 p->balance = 0;
307 }
308
309 /* Finishing touch */
310
311 *t = p;
312 }
313
314return TRUE; /* Successful insertion */
315}
316
317
318
319/*************************************************
320* Search tree for node by name *
321*************************************************/
322
323/*
324Arguments:
325 p root of tree
326 name key to search for
327
328Returns: pointer to node, or NULL if not found
329*/
330
331tree_node *
55414b25 332tree_search(tree_node *p, const uschar *name)
059ec3d9 333{
165acdd1
JH
334int c;
335for ( ; p; p = c < 0 ? p->left : p->right)
336 if ((c = Ustrcmp(name, p->name)) == 0)
337 return p;
059ec3d9
PH
338return NULL;
339}
340
341
165acdd1 342#ifndef MACRO_PREDEF
38a0a95f
PH
343
344/*************************************************
345* Walk tree recursively and execute function *
346*************************************************/
347
348/*
349Arguments:
350 p root of the tree
351 f function to execute for each name-value-pair
352 ctx context data for f
353*/
354
355void
356tree_walk(tree_node *p, void (*f)(uschar*, uschar*, void*), void *ctx)
357{
2c0f3ea1 358if (!p) return;
38a0a95f 359f(p->name, p->data.ptr, ctx);
2c0f3ea1
JH
360tree_walk(p->left, f, ctx);
361tree_walk(p->right, f, ctx);
38a0a95f
PH
362}
363
165acdd1 364#endif
38a0a95f 365
059ec3d9 366/* End of tree.c */