2 splay_tree.c -- splay tree and linked list convenience
3 Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
22 #include "splay_tree.h"
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;
39 while((c = tree->compare(data, root->data))) {
40 if(c < 0 && (child = root->left)) {
41 c = tree->compare(data, child->data);
43 if(c < 0 && (grandchild = child->left)) {
44 rightbottom->left = child;
45 child->parent = rightbottom;
48 if((root->left = child->right))
49 child->right->parent = root;
55 grandchild->parent = NULL;
58 } else if (c > 0 && (grandchild = child->right)) {
59 leftbottom->right = child;
60 child->parent = leftbottom;
64 grandchild->parent = NULL;
66 rightbottom->left = root;
67 root->parent = rightbottom;
74 rightbottom->left = root;
75 root->parent = rightbottom;
84 } else if(c > 0 && (child = root->right)) {
85 c = tree->compare(data, child->data);
87 if(c > 0 && (grandchild = child->right)) {
88 leftbottom->right = child;
89 child->parent = leftbottom;
92 if((root->right = child->left))
93 child->left->parent = root;
99 grandchild->parent = NULL;
102 } else if (c < 0 && (grandchild = child->left)) {
103 rightbottom->left = child;
104 child->parent = rightbottom;
108 grandchild->parent = NULL;
110 leftbottom->right = root;
111 root->parent = leftbottom;
118 leftbottom->right = root;
119 root->parent = leftbottom;
123 child->parent = NULL;
137 leftbottom->right = root->left;
138 root->left->parent = leftbottom;
140 root->left = left.right;
141 left.right->parent = root;
146 rightbottom->left = root->right;
147 root->right->parent = rightbottom;
149 root->right = right.left;
150 right.left->parent = root;
162 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
163 splay_node_t *parent, *grandparent, *greatgrandparent;
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;
172 if((parent->right = node->left))
173 parent->right->parent = parent;
177 parent->parent = node;
180 greatgrandparent = grandparent->parent;
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;
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;
198 if((parent->right = node->left))
199 parent->right->parent = 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;
206 parent->parent = node;
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;
218 if((grandparent->right = node->left))
219 grandparent->right->parent = grandparent;
220 node->left = grandparent;
221 grandparent->parent = node;
224 if((node->parent = greatgrandparent)) {
225 if(grandparent == greatgrandparent->left)
226 greatgrandparent->left = node;
228 greatgrandparent->right = node;
236 /* (De)constructors */
238 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
241 tree = xmalloc_and_zero(sizeof(splay_tree_t));
242 tree->compare = compare;
243 tree->delete = delete;
248 void splay_free_tree(splay_tree_t *tree) {
252 splay_node_t *splay_alloc_node(void) {
253 return xmalloc_and_zero(sizeof(splay_node_t));
256 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
257 if(node->data && tree->delete)
258 tree->delete(node->data);
265 void *splay_search(splay_tree_t *tree, const void *data) {
268 node = splay_search_node(tree, data);
270 return node ? node->data : NULL;
273 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
276 node = splay_search_closest_node(tree, data, result);
278 return node ? node->data : NULL;
281 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
284 node = splay_search_closest_smaller_node(tree, data);
286 return node ? node->data : NULL;
289 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
292 node = splay_search_closest_greater_node(tree, data);
294 return node ? node->data : NULL;
297 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
301 node = splay_search_closest_node(tree, data, &result);
303 return result ? NULL : node;
306 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
319 c = tree->compare(data, node->data);
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);
345 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
349 node = splay_search_closest_node(tree, data, &result);
357 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
361 node = splay_search_closest_node(tree, data, &result);
369 /* Insertion and deletion */
371 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
372 splay_node_t *closest, *new;
376 new = splay_alloc_node();
378 splay_insert_top(tree, new);
380 closest = splay_search_closest_node(tree, data, &result);
385 new = splay_alloc_node();
389 splay_insert_before(tree, closest, new);
391 splay_insert_after(tree, closest, new);
397 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
398 splay_node_t *closest;
402 splay_insert_top(tree, node);
404 closest = splay_search_closest_node(tree, node->data, &result);
410 splay_insert_before(tree, closest, node);
412 splay_insert_after(tree, closest, node);
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;
423 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
426 splay_insert_after(tree, tree->tail, node);
428 splay_insert_top(tree, node);
433 if((node->prev = before->prev))
434 before->prev->next = node;
439 splay_bottom_up(tree, before);
441 node->right = before;
442 before->parent = node;
443 if((node->left = before->left))
444 before->left->parent = node;
451 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
454 splay_insert_before(tree, tree->head, node);
456 splay_insert_top(tree, node);
461 if((node->next = after->next))
462 after->next->prev = node;
467 splay_bottom_up(tree, after);
470 after->parent = node;
471 if((node->right = after->right))
472 after->right->parent = node;
479 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
482 node = splay_search_node(tree, data);
485 splay_unlink_node(tree, node);
490 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
492 node->prev->next = node->next;
494 tree->head = node->next;
497 node->next->prev = node->prev;
499 tree->tail = node->prev;
501 splay_bottom_up(tree, node);
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;
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);
521 void splay_delete(splay_tree_t *tree, void *data) {
524 node = splay_search_node(tree, data);
527 splay_delete_node(tree, node);
530 /* Fast tree cleanup */
532 void splay_delete_tree(splay_tree_t *tree) {
533 splay_node_t *node, *next;
535 for(node = tree->head; node; node = next) {
537 splay_free_node(tree, node);
540 splay_free_tree(tree);
545 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
546 splay_node_t *node, *next;
548 for(node = tree->head; node; node = next) {
554 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
555 splay_node_t *node, *next;
557 for(node = tree->head; node; node = next) {