You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.9 KiB
C

10 years ago
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/*
* splay.c
* Copyright (C) 2006, 2013 Alexander Vdolainen <avdolainen@zoho.com>
10 years ago
*
* libtdata is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
10 years ago
* (at your option) any later version.
*
* libtdata 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*/
#include <tdata/usrtc.h>
#include <tdata/macro.h>
#include <tdata/tree.h>
/*prototypes*/
static void splay_insert(usrtc_t *, usrtc_node_t *, const void *);
static void splay_delete(usrtc_t *, usrtc_node_t *);
static usrtc_node_t *splay_lookup(usrtc_t *, const void *);
10 years ago
usrtc_functions_t usrtc_splay_fu = {
usrtc_tree_init,
splay_insert,
splay_delete,
splay_lookup,
usrtc_tree_lower_bound,
usrtc_tree_upper_bound,
usrtc_tree_first,
usrtc_tree_last,
usrtc_tree_next,
usrtc_tree_prev,
usrtc_tree_convert_to_list,
usrtc_tree_convert_from_list,
usrtc_bst
10 years ago
};
static void right_zig_zig(usrtc_node_t *, usrtc_node_t *, usrtc_node_t *);
static void left_zig_zig(usrtc_node_t *, usrtc_node_t *, usrtc_node_t *);
static void right_zig_zag(usrtc_node_t *, usrtc_node_t *, usrtc_node_t *);
static void left_zig_zag(usrtc_node_t *, usrtc_node_t *, usrtc_node_t *);
static void splay_node(usrtc_t *, usrtc_node_t *);
10 years ago
/*implementation*/
static void splay_insert(usrtc_t *us, usrtc_node_t *node, const void *key)
10 years ago
{
usrtc_tree_insert(us, node, key);
while(node != tree_root_priv(us))
splay_node(us, node);
return;
10 years ago
}
static void splay_delete(usrtc_t *us, usrtc_node_t *node)
10 years ago
{
usrtc_node_t *dummy;
usrtc_tree_delete(us, node, &dummy, &dummy);
return;
10 years ago
}
static usrtc_node_t *splay_lookup(usrtc_t *us, const void *key)
10 years ago
{
usrtc_node_t *node=usrtc_tree_lookup(us, key);
10 years ago
if(node)
while(node != tree_root_priv(us))
splay_node(us, node);
10 years ago
return node;
}
static void right_zig_zig(usrtc_node_t *child, usrtc_node_t *parent,
usrtc_node_t *grandpa)
10 years ago
{
usrtc_tree_rotate_right(parent, grandpa);
usrtc_tree_rotate_right(child, parent);
return;
10 years ago
}
static void left_zig_zig(usrtc_node_t *child, usrtc_node_t *parent,
usrtc_node_t *grandpa)
10 years ago
{
usrtc_tree_rotate_left(parent, grandpa);
usrtc_tree_rotate_left(child, parent);
return;
10 years ago
}
static void right_zig_zag(usrtc_node_t *child, usrtc_node_t *parent,
usrtc_node_t *grandpa)
10 years ago
{
usrtc_tree_rotate_right(child, parent);
usrtc_tree_rotate_left(child, grandpa);
return;
10 years ago
}
static void left_zig_zag(usrtc_node_t *child, usrtc_node_t *parent,
usrtc_node_t *grandpa)
10 years ago
{
usrtc_tree_rotate_left(child, parent);
usrtc_tree_rotate_right(child, grandpa);
return;
10 years ago
}
static void splay_node(usrtc_t *us, usrtc_node_t *node)
10 years ago
{
usrtc_node_t *root = tree_root_priv(us);
usrtc_node_t *parent = node->parent;
if(parent->left == node) {
if(parent == root) {
usrtc_tree_rotate_right(node, parent);
10 years ago
} else {
usrtc_node_t *grandpa = parent->parent;
if(grandpa->left == parent)
right_zig_zig(node, parent, grandpa);
10 years ago
else {
if(grandpa->right != parent)
return;
10 years ago
right_zig_zag(node, parent, grandpa);
10 years ago
}
}
} else {
if(parent->right != node)
10 years ago
return;
if(parent == root) {
usrtc_tree_rotate_left(node, parent);
10 years ago
} else {
usrtc_node_t *grandpa = parent->parent;
if(grandpa->right == parent) {
left_zig_zig(node, parent, grandpa);
10 years ago
} else {
if(grandpa->left != parent)
return;
10 years ago
left_zig_zag(node, parent, grandpa);
10 years ago
}
}
10 years ago
}
return;
10 years ago
}