tinc/lib/splay_tree.c

564 lines
12 KiB
C
Raw Normal View History

2004-11-02 20:50:53 +00:00
/*
splay_tree.c -- splay tree and linked list convenience
Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
2004-11-02 20:50:53 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: splay_tree.c 1374 2004-03-21 14:21:22Z guus $
*/
#include "system.h"
#include "splay_tree.h"
#include "xalloc.h"
/* Splay operation */
static splay_node_t *splay_top_down(splay_tree_t *tree, const void *data, int *result) {
splay_node_t left = {0}, right = {0};
2004-11-18 20:34:48 +00:00
splay_node_t *leftbottom = &left, *rightbottom = &right, *child, *grandchild;
splay_node_t *root = tree->root;
2004-11-02 20:50:53 +00:00
int c;
2004-11-18 20:34:48 +00:00
if(!root) {
if(result)
*result = 0;
return NULL;
}
while((c = tree->compare(data, root->data))) {
if(c < 0 && (child = root->left)) {
c = tree->compare(data, child->data);
2004-11-02 20:50:53 +00:00
2004-11-18 20:34:48 +00:00
if(c < 0 && (grandchild = child->left)) {
rightbottom->left = child;
child->parent = rightbottom;
rightbottom = child;
2004-11-02 20:50:53 +00:00
2004-11-18 20:34:48 +00:00
if((root->left = child->right))
child->right->parent = root;
child->right = root;
root->parent = child;
child->left = NULL;
grandchild->parent = NULL;
root = grandchild;
} else if (c > 0 && (grandchild = child->right)) {
leftbottom->right = child;
child->parent = leftbottom;
leftbottom = child;
child->right = NULL;
grandchild->parent = NULL;
rightbottom->left = root;
root->parent = rightbottom;
rightbottom = root;
root->left = NULL;
root = grandchild;
2004-11-02 20:50:53 +00:00
} else {
2004-11-18 20:34:48 +00:00
rightbottom->left = root;
root->parent = rightbottom;
rightbottom = root;
root->left = NULL;
child->parent = NULL;
root = child;
2004-11-02 20:50:53 +00:00
break;
}
2004-11-18 20:34:48 +00:00
} else if(c > 0 && (child = root->right)) {
c = tree->compare(data, child->data);
2004-11-02 20:50:53 +00:00
2004-11-18 20:34:48 +00:00
if(c > 0 && (grandchild = child->right)) {
leftbottom->right = child;
child->parent = leftbottom;
leftbottom = child;
if((root->right = child->left))
child->left->parent = root;
2004-11-02 20:50:53 +00:00
2004-11-18 20:34:48 +00:00
child->left = root;
root->parent = child;
child->right = NULL;
grandchild->parent = NULL;
root = grandchild;
} else if (c < 0 && (grandchild = child->left)) {
rightbottom->left = child;
child->parent = rightbottom;
rightbottom = child;
child->left = NULL;
grandchild->parent = NULL;
leftbottom->right = root;
root->parent = leftbottom;
leftbottom = root;
root->right = NULL;
root = grandchild;
2004-11-02 20:50:53 +00:00
} else {
2004-11-18 20:34:48 +00:00
leftbottom->right = root;
root->parent = leftbottom;
leftbottom = root;
root->right = NULL;
child->parent = NULL;
root = child;
2004-11-02 20:50:53 +00:00
break;
}
2004-11-18 20:34:48 +00:00
} else {
break;
2004-11-02 20:50:53 +00:00
}
}
/* Merge trees */
if(left.right) {
2004-11-18 20:34:48 +00:00
if(root->left) {
leftbottom->right = root->left;
root->left->parent = leftbottom;
2004-11-02 20:50:53 +00:00
}
2004-11-18 20:34:48 +00:00
root->left = left.right;
left.right->parent = root;
2004-11-02 20:50:53 +00:00
}
if(right.left) {
2004-11-18 20:34:48 +00:00
if(root->right) {
rightbottom->left = root->right;
root->right->parent = rightbottom;
2004-11-02 20:50:53 +00:00
}
2004-11-18 20:34:48 +00:00
root->right = right.left;
right.left->parent = root;
2004-11-02 20:50:53 +00:00
}
2004-11-18 20:34:48 +00:00
/* Return result */
tree->root = root;
2004-11-02 20:50:53 +00:00
if(result)
*result = c;
2004-11-18 20:34:48 +00:00
return tree->root;
2004-11-02 20:50:53 +00:00
}
2004-11-18 20:34:48 +00:00
2004-11-02 20:50:53 +00:00
static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
2004-11-18 20:34:48 +00:00
splay_node_t *parent, *grandparent, *greatgrandparent;
2004-11-02 20:50:53 +00:00
2004-11-18 20:34:48 +00:00
while((parent = node->parent)) {
if(!(grandparent = parent->parent)) { /* zig */
2004-11-02 20:50:53 +00:00
if(node == parent->left) {
2004-11-18 20:34:48 +00:00
if((parent->left = node->right))
parent->left->parent = parent;
2004-11-02 20:50:53 +00:00
node->right = parent;
} else {
2004-11-18 20:34:48 +00:00
if((parent->right = node->left))
parent->right->parent = parent;
2004-11-02 20:50:53 +00:00
node->left = parent;
}
parent->parent = node;
node->parent = NULL;
} else {
2004-11-18 20:34:48 +00:00
greatgrandparent = grandparent->parent;
if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
if((grandparent->left = parent->right))
grandparent->left->parent = grandparent;
2004-11-02 20:50:53 +00:00
parent->right = grandparent;
grandparent->parent = parent;
2004-11-18 20:34:48 +00:00
if((parent->left = node->right))
parent->left->parent = parent;
2004-11-02 20:50:53 +00:00
node->right = parent;
parent->parent = node;
2004-11-18 20:34:48 +00:00
} else if(node == parent->right && parent == grandparent->right) { /* right zig-zig */
if((grandparent->right = parent->left))
grandparent->right->parent = grandparent;
2004-11-02 20:50:53 +00:00
parent->left = grandparent;
grandparent->parent = parent;
2004-11-18 20:34:48 +00:00
if((parent->right = node->left))
parent->right->parent = parent;
2004-11-02 20:50:53 +00:00
node->left = parent;
parent->parent = node;
2004-11-18 20:34:48 +00:00
} else if(node == parent->right && parent == grandparent->left) { /* left-right zig-zag */
if((parent->right = node->left))
parent->right->parent = parent;
2004-11-02 20:50:53 +00:00
node->left = parent;
parent->parent = node;
2004-11-18 20:34:48 +00:00
if((grandparent->left = node->right))
grandparent->left->parent = grandparent;
2004-11-02 20:50:53 +00:00
node->right = grandparent;
grandparent->parent = node;
} else { /* right-left zig-zag */
2004-11-18 20:34:48 +00:00
if((parent->left = node->right))
parent->left->parent = parent;
2004-11-02 20:50:53 +00:00
node->right = parent;
parent->parent = node;
2004-11-18 20:34:48 +00:00
if((grandparent->right = node->left))
grandparent->right->parent = grandparent;
2004-11-02 20:50:53 +00:00
node->left = grandparent;
grandparent->parent = node;
}
2004-11-18 20:34:48 +00:00
if((node->parent = greatgrandparent)) {
if(grandparent == greatgrandparent->left)
greatgrandparent->left = node;
2004-11-02 20:50:53 +00:00
else
2004-11-18 20:34:48 +00:00
greatgrandparent->right = node;
2004-11-02 20:50:53 +00:00
}
}
}
tree->root = node;
}
/* (De)constructors */
splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
splay_tree_t *tree;
tree = xmalloc_and_zero(sizeof(splay_tree_t));
tree->compare = compare;
tree->delete = delete;
return tree;
}
void splay_free_tree(splay_tree_t *tree) {
free(tree);
}
splay_node_t *splay_alloc_node(void) {
return xmalloc_and_zero(sizeof(splay_node_t));
}
void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
if(node->data && tree->delete)
tree->delete(node->data);
free(node);
}
/* Searching */
void *splay_search(splay_tree_t *tree, const void *data) {
splay_node_t *node;
node = splay_search_node(tree, data);
return node ? node->data : NULL;
}
void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
splay_node_t *node;
node = splay_search_closest_node(tree, data, result);
return node ? node->data : NULL;
}
void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
splay_node_t *node;
node = splay_search_closest_smaller_node(tree, data);
return node ? node->data : NULL;
}
void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
splay_node_t *node;
node = splay_search_closest_greater_node(tree, data);
return node ? node->data : NULL;
}
splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
splay_node_t *node;
int result;
node = splay_search_closest_node(tree, data, &result);
return result ? NULL : node;
}
splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
splay_node_t *node;
int c;
node = tree->root;
if(!node) {
if(result)
*result = 0;
return NULL;
}
for(;;) {
c = tree->compare(data, node->data);
if(c < 0) {
if(node->left)
node = node->left;
2004-11-18 20:34:48 +00:00
else
2004-11-02 20:50:53 +00:00
break;
} else if(c > 0) {
if(node->right)
node = node->right;
2004-11-18 20:34:48 +00:00
else
2004-11-02 20:50:53 +00:00
break;
} else {
break;
}
}
2004-11-18 20:34:48 +00:00
if(result)
*result = c;
2004-11-02 20:50:53 +00:00
return node;
}
splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
return splay_top_down(tree, data, result);
}
splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
splay_node_t *node;
int result;
node = splay_search_closest_node(tree, data, &result);
if(result < 0)
node = node->prev;
return node;
}
splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
splay_node_t *node;
int result;
node = splay_search_closest_node(tree, data, &result);
if(result > 0)
node = node->next;
return node;
}
/* Insertion and deletion */
splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
splay_node_t *closest, *new;
int result;
if(!tree->root) {
new = splay_alloc_node();
new->data = data;
splay_insert_top(tree, new);
} else {
2005-05-04 15:56:25 +00:00
closest = splay_search_closest_node(tree, data, &result);
2004-11-02 20:50:53 +00:00
if(!result)
return NULL;
new = splay_alloc_node();
new->data = data;
if(result < 0)
splay_insert_before(tree, closest, new);
else
splay_insert_after(tree, closest, new);
}
return new;
}
splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
splay_node_t *closest;
int result;
if(!tree->root)
splay_insert_top(tree, node);
else {
2005-05-04 15:56:25 +00:00
closest = splay_search_closest_node(tree, node->data, &result);
2004-11-02 20:50:53 +00:00
if(!result)
return NULL;
if(result < 0)
splay_insert_before(tree, closest, node);
else
splay_insert_after(tree, closest, node);
}
return node;
}
void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
2005-05-04 15:56:25 +00:00
node->prev = node->next = node->left = node->right = node->parent = NULL;
2004-11-02 20:50:53 +00:00
tree->head = tree->tail = tree->root = node;
}
void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
if(!before) {
if(tree->tail)
splay_insert_after(tree, tree->tail, node);
else
splay_insert_top(tree, node);
return;
}
node->next = before;
2005-05-04 15:56:25 +00:00
if((node->prev = before->prev))
2004-11-02 20:50:53 +00:00
before->prev->next = node;
else
tree->head = node;
before->prev = node;
2005-05-04 15:56:25 +00:00
splay_bottom_up(tree, before);
node->right = before;
before->parent = node;
if((node->left = before->left))
before->left->parent = node;
before->left = NULL;
node->parent = NULL;
tree->root = node;
2004-11-02 20:50:53 +00:00
}
void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
if(!after) {
if(tree->head)
splay_insert_before(tree, tree->head, node);
else
splay_insert_top(tree, node);
return;
}
node->prev = after;
2005-05-04 15:56:25 +00:00
if((node->next = after->next))
2004-11-02 20:50:53 +00:00
after->next->prev = node;
else
tree->tail = node;
after->next = node;
2005-05-04 15:56:25 +00:00
splay_bottom_up(tree, after);
node->left = after;
after->parent = node;
if((node->right = after->right))
after->right->parent = node;
after->right = NULL;
node->parent = NULL;
tree->root = node;
2004-11-02 20:50:53 +00:00
}
splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
splay_node_t *node;
2005-05-04 15:56:25 +00:00
node = splay_search_node(tree, data);
2004-11-02 20:50:53 +00:00
2005-05-04 15:56:25 +00:00
if(node)
2004-11-02 20:50:53 +00:00
splay_unlink_node(tree, node);
return node;
}
void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
if(node->prev)
node->prev->next = node->next;
else
tree->head = node->next;
if(node->next)
node->next->prev = node->prev;
else
tree->tail = node->prev;
2005-05-04 15:56:25 +00:00
splay_bottom_up(tree, node);
if(node->prev) {
2004-11-02 20:50:53 +00:00
node->left->parent = NULL;
tree->root = node->left;
2005-05-04 15:56:25 +00:00
if((node->prev->right = node->right))
2004-11-02 20:50:53 +00:00
node->right->parent = node->prev;
2005-05-04 15:56:25 +00:00
} else if(node->next) {
2004-11-02 20:50:53 +00:00
tree->root = node->right;
2005-05-04 15:56:25 +00:00
node->right->parent = NULL;
2004-11-18 20:34:48 +00:00
} else {
tree->root = NULL;
2004-11-02 20:50:53 +00:00
}
}
void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
splay_unlink_node(tree, node);
splay_free_node(tree, node);
}
void splay_delete(splay_tree_t *tree, void *data) {
splay_node_t *node;
node = splay_search_node(tree, data);
if(node)
splay_delete_node(tree, node);
}
/* Fast tree cleanup */
void splay_delete_tree(splay_tree_t *tree) {
splay_node_t *node, *next;
for(node = tree->root; node; node = next) {
next = node->next;
splay_free_node(tree, node);
}
splay_free_tree(tree);
}
/* Tree walking */
void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
splay_node_t *node, *next;
for(node = tree->head; node; node = next) {
next = node->next;
action(node->data);
}
}
void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
splay_node_t *node, *next;
for(node = tree->head; node; node = next) {
next = node->next;
action(node);
}
}