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