Fix reading configuration files that do not end with a newline. Again.
[tinc] / lib / splay_tree.c
1 /*
2     splay_tree.c -- splay tree and linked list convenience
3     Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "splay_tree.h"
23 #include "xalloc.h"
24
25 /* Splay operation */
26
27 static splay_node_t *splay_top_down(splay_tree_t *tree, const void *data, int *result) {
28         splay_node_t left = {0}, right = {0};
29         splay_node_t *leftbottom = &left, *rightbottom = &right, *child, *grandchild;
30         splay_node_t *root = tree->root;
31         int c;
32
33         if(!root) {
34                 if(result)
35                         *result = 0;
36                 return NULL;
37         }
38
39         while((c = tree->compare(data, root->data))) {
40                 if(c < 0 && (child = root->left)) {
41                         c = tree->compare(data, child->data);
42
43                         if(c < 0 && (grandchild = child->left)) {
44                                 rightbottom->left = child;
45                                 child->parent = rightbottom;
46                                 rightbottom = child;
47                                 
48                                 if((root->left = child->right))
49                                         child->right->parent = root;
50                                 
51                                 child->right = root;
52                                 root->parent = child;
53
54                                 child->left = NULL;
55                                 grandchild->parent = NULL;
56
57                                 root = grandchild;
58                         } else if (c > 0 && (grandchild = child->right)) {
59                                 leftbottom->right = child;
60                                 child->parent = leftbottom;
61                                 leftbottom = child;
62
63                                 child->right = NULL;
64                                 grandchild->parent = NULL;
65
66                                 rightbottom->left = root;
67                                 root->parent = rightbottom;
68                                 rightbottom = root;
69
70                                 root->left = NULL;
71
72                                 root = grandchild;
73                         } else {
74                                 rightbottom->left = root;
75                                 root->parent = rightbottom;
76                                 rightbottom = root;
77                                 
78                                 root->left = NULL;
79                                 child->parent = NULL;
80
81                                 root = child;
82                                 break;
83                         }
84                 } else if(c > 0 && (child = root->right)) {
85                         c = tree->compare(data, child->data);
86
87                         if(c > 0 && (grandchild = child->right)) {
88                                 leftbottom->right = child;
89                                 child->parent = leftbottom;
90                                 leftbottom = child;
91                                 
92                                 if((root->right = child->left))
93                                         child->left->parent = root;
94                                 
95                                 child->left = root;
96                                 root->parent = child;
97
98                                 child->right = NULL;
99                                 grandchild->parent = NULL;
100
101                                 root = grandchild;
102                         } else if (c < 0 && (grandchild = child->left)) {
103                                 rightbottom->left = child;
104                                 child->parent = rightbottom;
105                                 rightbottom = child;
106
107                                 child->left = NULL;
108                                 grandchild->parent = NULL;
109
110                                 leftbottom->right = root;
111                                 root->parent = leftbottom;
112                                 leftbottom = root;
113
114                                 root->right = NULL;
115
116                                 root = grandchild;
117                         } else {
118                                 leftbottom->right = root;
119                                 root->parent = leftbottom;
120                                 leftbottom = root;
121
122                                 root->right = NULL;
123                                 child->parent = NULL;
124
125                                 root = child;
126                                 break;
127                         }
128                 } else {
129                         break;
130                 }
131         }
132
133         /* Merge trees */
134
135         if(left.right) {
136                 if(root->left) {
137                         leftbottom->right = root->left;
138                         root->left->parent = leftbottom;
139                 }
140                 root->left = left.right;
141                 left.right->parent = root;
142         }
143
144         if(right.left) {
145                 if(root->right) {
146                         rightbottom->left = root->right;
147                         root->right->parent = rightbottom;
148                 }
149                 root->right = right.left;
150                 right.left->parent = root;
151         }
152
153         /* Return result */
154
155         tree->root = root;
156         if(result)
157                 *result = c;
158
159         return tree->root;
160 }
161                         
162 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
163         splay_node_t *parent, *grandparent, *greatgrandparent;
164
165         while((parent = node->parent)) {
166                 if(!(grandparent = parent->parent)) { /* zig */
167                         if(node == parent->left) {
168                                 if((parent->left = node->right))                                
169                                         parent->left->parent = parent;
170                                 node->right = parent;
171                         } else {
172                                 if((parent->right = node->left))
173                                         parent->right->parent = parent;
174                                 node->left = parent;
175                         }
176
177                         parent->parent = node;
178                         node->parent = NULL;
179                 } else {
180                         greatgrandparent = grandparent->parent;
181
182                         if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
183                                 if((grandparent->left = parent->right))
184                                         grandparent->left->parent = grandparent;
185                                 parent->right = grandparent;
186                                 grandparent->parent = parent;
187
188                                 if((parent->left = node->right))
189                                         parent->left->parent = parent;
190                                 node->right = parent;
191                                 parent->parent = node;
192                         } else if(node == parent->right && parent == grandparent->right) { /* right zig-zig */
193                                 if((grandparent->right = parent->left))
194                                         grandparent->right->parent = grandparent;
195                                 parent->left = grandparent;
196                                 grandparent->parent = parent;
197
198                                 if((parent->right = node->left))
199                                         parent->right->parent = parent;
200                                 node->left = parent;
201                                 parent->parent = node;
202                         } else if(node == parent->right && parent == grandparent->left) { /* left-right zig-zag */
203                                 if((parent->right = node->left))
204                                         parent->right->parent = parent;
205                                 node->left = parent;
206                                 parent->parent = node;
207
208                                 if((grandparent->left = node->right))
209                                         grandparent->left->parent = grandparent;
210                                 node->right = grandparent;
211                                 grandparent->parent = node;
212                         } else { /* right-left zig-zag */
213                                 if((parent->left = node->right))
214                                         parent->left->parent = parent;
215                                 node->right = parent;
216                                 parent->parent = node;
217
218                                 if((grandparent->right = node->left))
219                                         grandparent->right->parent = grandparent;
220                                 node->left = grandparent;
221                                 grandparent->parent = node;
222                         }
223
224                         if((node->parent = greatgrandparent)) {
225                                 if(grandparent == greatgrandparent->left)
226                                         greatgrandparent->left = node;
227                                 else
228                                         greatgrandparent->right = node;
229                         }
230                 }
231         }
232
233         tree->root = node;
234 }
235
236 /* (De)constructors */
237
238 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
239         splay_tree_t *tree;
240
241         tree = xmalloc_and_zero(sizeof(splay_tree_t));
242         tree->compare = compare;
243         tree->delete = delete;
244
245         return tree;
246 }
247
248 void splay_free_tree(splay_tree_t *tree) {
249         free(tree);
250 }
251
252 splay_node_t *splay_alloc_node(void) {
253         return xmalloc_and_zero(sizeof(splay_node_t));
254 }
255
256 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
257         if(node->data && tree->delete)
258                 tree->delete(node->data);
259
260         free(node);
261 }
262
263 /* Searching */
264
265 void *splay_search(splay_tree_t *tree, const void *data) {
266         splay_node_t *node;
267
268         node = splay_search_node(tree, data);
269
270         return node ? node->data : NULL;
271 }
272
273 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
274         splay_node_t *node;
275
276         node = splay_search_closest_node(tree, data, result);
277
278         return node ? node->data : NULL;
279 }
280
281 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
282         splay_node_t *node;
283
284         node = splay_search_closest_smaller_node(tree, data);
285
286         return node ? node->data : NULL;
287 }
288
289 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
290         splay_node_t *node;
291
292         node = splay_search_closest_greater_node(tree, data);
293
294         return node ? node->data : NULL;
295 }
296
297 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
298         splay_node_t *node;
299         int result;
300
301         node = splay_search_closest_node(tree, data, &result);
302
303         return result ? NULL : node;
304 }
305
306 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
307         splay_node_t *node;
308         int c;
309
310         node = tree->root;
311
312         if(!node) {
313                 if(result)
314                         *result = 0;
315                 return NULL;
316         }
317
318         for(;;) {
319                 c = tree->compare(data, node->data);
320
321                 if(c < 0) {
322                         if(node->left)
323                                 node = node->left;
324                         else
325                                 break;
326                 } else if(c > 0) {
327                         if(node->right)
328                                 node = node->right;
329                         else 
330                                 break;
331                 } else {
332                         break;
333                 }
334         }
335
336         if(result)
337                 *result = c;
338         return node;
339 }
340
341 splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
342         return splay_top_down(tree, data, result);
343 }
344
345 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
346         splay_node_t *node;
347         int result;
348
349         node = splay_search_closest_node(tree, data, &result);
350
351         if(result < 0)
352                 node = node->prev;
353
354         return node;
355 }
356
357 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
358         splay_node_t *node;
359         int result;
360
361         node = splay_search_closest_node(tree, data, &result);
362
363         if(result > 0)
364                 node = node->next;
365
366         return node;
367 }
368
369 /* Insertion and deletion */
370
371 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
372         splay_node_t *closest, *new;
373         int result;
374
375         if(!tree->root) {
376                 new = splay_alloc_node();
377                 new->data = data;
378                 splay_insert_top(tree, new);
379         } else {
380                 closest = splay_search_closest_node(tree, data, &result);
381
382                 if(!result)
383                         return NULL;
384
385                 new = splay_alloc_node();
386                 new->data = data;
387                 
388                 if(result < 0)
389                         splay_insert_before(tree, closest, new);
390                 else
391                         splay_insert_after(tree, closest, new);
392         }                       
393
394         return new;
395 }
396
397 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
398         splay_node_t *closest;
399         int result;
400
401         if(!tree->root)
402                 splay_insert_top(tree, node);
403         else {
404                 closest = splay_search_closest_node(tree, node->data, &result);
405                 
406                 if(!result)
407                         return NULL;
408
409                 if(result < 0)
410                         splay_insert_before(tree, closest, node);
411                 else
412                         splay_insert_after(tree, closest, node);
413         }
414
415         return node;
416 }
417
418 void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
419         node->prev = node->next = node->left = node->right = node->parent = NULL;
420         tree->head = tree->tail = tree->root = node;
421 }
422
423 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
424         if(!before) {
425                 if(tree->tail)
426                         splay_insert_after(tree, tree->tail, node);
427                 else
428                         splay_insert_top(tree, node);
429                 return;
430         }
431
432         node->next = before;
433         if((node->prev = before->prev))
434                 before->prev->next = node;
435         else
436                 tree->head = node;
437         before->prev = node;
438
439         splay_bottom_up(tree, before);
440
441         node->right = before;
442         before->parent = node;
443         if((node->left = before->left))
444                 before->left->parent = node;
445         before->left = NULL;
446
447         node->parent = NULL;
448         tree->root = node;
449 }
450
451 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
452         if(!after) {
453                 if(tree->head)
454                         splay_insert_before(tree, tree->head, node);
455                 else
456                         splay_insert_top(tree, node);
457                 return;
458         }
459
460         node->prev = after;
461         if((node->next = after->next))
462                 after->next->prev = node;
463         else
464                 tree->tail = node;
465         after->next = node;
466
467         splay_bottom_up(tree, after);
468
469         node->left = after;
470         after->parent = node;
471         if((node->right = after->right))
472                 after->right->parent = node;
473         after->right = NULL;
474
475         node->parent = NULL;
476         tree->root = node;
477 }
478
479 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
480         splay_node_t *node;
481
482         node = splay_search_node(tree, data);
483
484         if(node)
485                 splay_unlink_node(tree, node);
486
487         return node;
488 }
489
490 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
491         if(node->prev)
492                 node->prev->next = node->next;
493         else
494                 tree->head = node->next;
495
496         if(node->next)
497                 node->next->prev = node->prev;
498         else
499                 tree->tail = node->prev;
500
501         splay_bottom_up(tree, node);
502
503         if(node->prev) {
504                 node->left->parent = NULL;
505                 tree->root = node->left;
506                 if((node->prev->right = node->right))
507                         node->right->parent = node->prev;
508         } else if(node->next) {
509                 tree->root = node->right;
510                 node->right->parent = NULL;
511         } else {
512                 tree->root = NULL;
513         }
514 }
515
516 void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
517         splay_unlink_node(tree, node);
518         splay_free_node(tree, node);
519 }
520
521 void splay_delete(splay_tree_t *tree, void *data) {
522         splay_node_t *node;
523
524         node = splay_search_node(tree, data);
525
526         if(node)
527                 splay_delete_node(tree, node);
528 }
529
530 /* Fast tree cleanup */
531
532 void splay_delete_tree(splay_tree_t *tree) {
533         splay_node_t *node, *next;
534
535         for(node = tree->head; node; node = next) {
536                 next = node->next;
537                 splay_free_node(tree, node);
538         }
539
540         splay_free_tree(tree);
541 }
542
543 /* Tree walking */
544
545 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
546         splay_node_t *node, *next;
547
548         for(node = tree->head; node; node = next) {
549                 next = node->next;
550                 action(node->data);
551         }
552 }
553
554 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
555         splay_node_t *node, *next;
556
557         for(node = tree->head; node; node = next) {
558                 next = node->next;
559                 action(node);
560         }
561 }