Code style compliance;
This commit is contained in:
parent
bb6a20313b
commit
4ab3fc2b3d
161
lib/splay.c
161
lib/splay.c
@ -22,121 +22,132 @@
|
||||
#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 *);
|
||||
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 *);
|
||||
|
||||
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
|
||||
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
|
||||
};
|
||||
|
||||
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 *);
|
||||
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 *);
|
||||
|
||||
/*implementation*/
|
||||
|
||||
static void splay_insert(usrtc_t *us,usrtc_node_t *node,const void *key)
|
||||
static void splay_insert(usrtc_t *us, usrtc_node_t *node, const void *key)
|
||||
{
|
||||
usrtc_tree_insert(us,node,key);
|
||||
|
||||
while(node!=tree_root_priv(us))
|
||||
splay_node(us,node);
|
||||
usrtc_tree_insert(us, node, key);
|
||||
|
||||
while(node != tree_root_priv(us))
|
||||
splay_node(us, node);
|
||||
return;
|
||||
}
|
||||
|
||||
static void splay_delete(usrtc_t *us,usrtc_node_t *node)
|
||||
static void splay_delete(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
usrtc_node_t *dummy;
|
||||
usrtc_tree_delete(us,node,&dummy,&dummy);
|
||||
usrtc_tree_delete(us, node, &dummy, &dummy);
|
||||
return;
|
||||
}
|
||||
|
||||
static usrtc_node_t *splay_lookup(usrtc_t *us,const void *key)
|
||||
static usrtc_node_t *splay_lookup(usrtc_t *us, const void *key)
|
||||
{
|
||||
usrtc_node_t *node=usrtc_tree_lookup(us,key);
|
||||
|
||||
usrtc_node_t *node=usrtc_tree_lookup(us, key);
|
||||
|
||||
if(node)
|
||||
while(node!=tree_root_priv(us))
|
||||
splay_node(us,node);
|
||||
|
||||
while(node != tree_root_priv(us))
|
||||
splay_node(us, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
static void right_zig_zig(usrtc_node_t *child,usrtc_node_t *parent,usrtc_node_t *grandpa)
|
||||
static void right_zig_zig(usrtc_node_t *child, usrtc_node_t *parent,
|
||||
usrtc_node_t *grandpa)
|
||||
{
|
||||
usrtc_tree_rotate_right(parent,grandpa);
|
||||
usrtc_tree_rotate_right(child,parent);
|
||||
usrtc_tree_rotate_right(parent, grandpa);
|
||||
usrtc_tree_rotate_right(child, parent);
|
||||
return;
|
||||
}
|
||||
|
||||
static void left_zig_zig(usrtc_node_t *child,usrtc_node_t *parent,usrtc_node_t *grandpa)
|
||||
static void left_zig_zig(usrtc_node_t *child, usrtc_node_t *parent,
|
||||
usrtc_node_t *grandpa)
|
||||
{
|
||||
usrtc_tree_rotate_left(parent,grandpa);
|
||||
usrtc_tree_rotate_left(child,parent);
|
||||
usrtc_tree_rotate_left(parent, grandpa);
|
||||
usrtc_tree_rotate_left(child, parent);
|
||||
return;
|
||||
}
|
||||
|
||||
static void right_zig_zag(usrtc_node_t *child,usrtc_node_t *parent,usrtc_node_t *grandpa)
|
||||
static void right_zig_zag(usrtc_node_t *child, usrtc_node_t *parent,
|
||||
usrtc_node_t *grandpa)
|
||||
{
|
||||
usrtc_tree_rotate_right(child,parent);
|
||||
usrtc_tree_rotate_left(child,grandpa);
|
||||
usrtc_tree_rotate_right(child, parent);
|
||||
usrtc_tree_rotate_left(child, grandpa);
|
||||
return;
|
||||
}
|
||||
|
||||
static void left_zig_zag(usrtc_node_t *child,usrtc_node_t *parent,usrtc_node_t *grandpa)
|
||||
static void left_zig_zag(usrtc_node_t *child, usrtc_node_t *parent,
|
||||
usrtc_node_t *grandpa)
|
||||
{
|
||||
usrtc_tree_rotate_left(child,parent);
|
||||
usrtc_tree_rotate_right(child,grandpa);
|
||||
usrtc_tree_rotate_left(child, parent);
|
||||
usrtc_tree_rotate_right(child, grandpa);
|
||||
return;
|
||||
}
|
||||
|
||||
static void splay_node(usrtc_t *us,usrtc_node_t *node)
|
||||
static void splay_node(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
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);
|
||||
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);
|
||||
} else {
|
||||
usrtc_node_t *grandpa=parent->parent;
|
||||
|
||||
if(grandpa->left==parent)
|
||||
right_zig_zig(node,parent,grandpa);
|
||||
else {
|
||||
if(grandpa->right!=parent)
|
||||
return;
|
||||
usrtc_node_t *grandpa = parent->parent;
|
||||
|
||||
right_zig_zag(node,parent,grandpa);
|
||||
if(grandpa->left == parent)
|
||||
right_zig_zig(node, parent, grandpa);
|
||||
else {
|
||||
if(grandpa->right != parent)
|
||||
return;
|
||||
|
||||
right_zig_zag(node, parent, grandpa);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(parent->right!=node)
|
||||
if(parent->right != node)
|
||||
return;
|
||||
|
||||
if(parent==root) {
|
||||
usrtc_tree_rotate_left(node,parent);
|
||||
} else {
|
||||
usrtc_node_t *grandpa=parent->parent;
|
||||
|
||||
if(grandpa->right==parent) {
|
||||
left_zig_zig(node,parent,grandpa);
|
||||
} else {
|
||||
if(grandpa->left!=parent)
|
||||
return;
|
||||
|
||||
left_zig_zag(node,parent,grandpa);
|
||||
if(parent == root) {
|
||||
usrtc_tree_rotate_left(node, parent);
|
||||
} else {
|
||||
usrtc_node_t *grandpa = parent->parent;
|
||||
|
||||
if(grandpa->right == parent) {
|
||||
left_zig_zig(node, parent, grandpa);
|
||||
} else {
|
||||
if(grandpa->left != parent)
|
||||
return;
|
||||
|
||||
left_zig_zag(node, parent, grandpa);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
451
lib/tree.c
451
lib/tree.c
@ -27,104 +27,107 @@
|
||||
static void tree_delete(usrtc_t *, usrtc_node_t *);
|
||||
|
||||
usrtc_functions_t usrtc_tree_fu = {
|
||||
usrtc_tree_init,
|
||||
usrtc_tree_insert,
|
||||
tree_delete,
|
||||
usrtc_tree_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
|
||||
usrtc_tree_init,
|
||||
usrtc_tree_insert,
|
||||
tree_delete,
|
||||
usrtc_tree_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
|
||||
};
|
||||
|
||||
/*implementation*/
|
||||
|
||||
void usrtc_tree_init(usrtc_t *us)
|
||||
{
|
||||
us->sentinel.left=&us->sentinel;
|
||||
us->sentinel.right=&us->sentinel;
|
||||
us->sentinel.parent=&us->sentinel;
|
||||
us->sentinel.left = &us->sentinel;
|
||||
us->sentinel.right = &us->sentinel;
|
||||
us->sentinel.parent = &us->sentinel;
|
||||
|
||||
us->sentinel.impl_specific.usrtc_dummy=0;
|
||||
us->sentinel.data=0;
|
||||
us->sentinel.key=0;
|
||||
us->sentinel.impl_specific.usrtc_dummy = 0;
|
||||
us->sentinel.data = 0;
|
||||
us->sentinel.key = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_tree_insert(usrtc_t *us,usrtc_node_t *node,const void *key)
|
||||
void usrtc_tree_insert(usrtc_t *us, usrtc_node_t *node, const void *key)
|
||||
{
|
||||
usrtc_node_t *where=tree_root_priv(us);
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *parent=nil;
|
||||
long res=-1;
|
||||
usrtc_node_t *where = tree_root_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *parent = nil;
|
||||
long res = -1;
|
||||
|
||||
node->key=key;
|
||||
node->key = key;
|
||||
|
||||
while(where!=nil) {
|
||||
parent=where;
|
||||
res=us->compare(key,where->key);
|
||||
while(where != nil) {
|
||||
parent = where;
|
||||
res = us->compare(key, where->key);
|
||||
|
||||
if(us->dupes_allowed && !res) /*trying to put duplicate to the disabled dupe tree*/
|
||||
return;
|
||||
|
||||
if(res<0)
|
||||
where=where->left;
|
||||
where = where->left;
|
||||
else
|
||||
where=where->right;
|
||||
where = where->right;
|
||||
}
|
||||
|
||||
/*assert(where==nil);*/
|
||||
|
||||
if(res<0)
|
||||
parent->left=node;
|
||||
if(res < 0)
|
||||
parent->left = node;
|
||||
else
|
||||
parent->right=node;
|
||||
parent->right = node;
|
||||
|
||||
node->parent=parent;
|
||||
node->left=nil;
|
||||
node->right=nil;
|
||||
node->parent = parent;
|
||||
node->left = nil;
|
||||
node->right = nil;
|
||||
|
||||
us->nodecount++;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_tree_delete(usrtc_t *us,usrtc_node_t *node,usrtc_node_t **pswap,usrtc_node_t **pchild)
|
||||
void usrtc_tree_delete(usrtc_t *us, usrtc_node_t *node, usrtc_node_t **pswap,
|
||||
usrtc_node_t **pchild)
|
||||
{
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *child;
|
||||
usrtc_node_t *delparent=node->parent;
|
||||
usrtc_node_t *next=node;
|
||||
usrtc_node_t *delparent = node->parent;
|
||||
usrtc_node_t *next = node;
|
||||
usrtc_node_t *nextparent;
|
||||
|
||||
if(node->left!=nil && node->right!=nil) {
|
||||
next=usrtc_tree_next(us,node);
|
||||
nextparent=next->parent;
|
||||
if(node->left != nil && node->right != nil) {
|
||||
next = usrtc_tree_next(us, node);
|
||||
nextparent = next->parent;
|
||||
|
||||
/*if(next!=nil && next->parent!=nil && next->parent==nil)
|
||||
return;*/
|
||||
|
||||
child=next->right;
|
||||
child->parent=nextparent;
|
||||
child = next->right;
|
||||
child->parent = nextparent;
|
||||
|
||||
if(nextparent->left==next)
|
||||
nextparent->left=child;
|
||||
if(nextparent->left == next)
|
||||
nextparent->left = child;
|
||||
else {
|
||||
//if(nextparent->right!=next)
|
||||
//return;
|
||||
nextparent->right=child;
|
||||
nextparent->right = child;
|
||||
}
|
||||
|
||||
next->parent=delparent;
|
||||
next->left=node->left;
|
||||
next->right=node->right;
|
||||
next->left->parent=next;
|
||||
next->right->parent=next;
|
||||
next->parent = delparent;
|
||||
next->left = node->left;
|
||||
next->right = node->right;
|
||||
next->left->parent = next;
|
||||
next->right->parent = next;
|
||||
|
||||
if(delparent->left==node) {
|
||||
delparent->left=next;
|
||||
if(delparent->left == node) {
|
||||
delparent->left = next;
|
||||
} else {
|
||||
//if(delparent->right!=node)
|
||||
// return;
|
||||
@ -134,55 +137,57 @@ void usrtc_tree_delete(usrtc_t *us,usrtc_node_t *node,usrtc_node_t **pswap,usrtc
|
||||
} else {
|
||||
/*if(node==nil)
|
||||
return;
|
||||
if(node->left!=nil && node->right!=nil)
|
||||
return;*/
|
||||
if(node->left!=nil && node->right!=nil)
|
||||
return;*/
|
||||
|
||||
child=(node->left!=nil) ? node->left : node->right;
|
||||
child = (node->left != nil) ? node->left : node->right;
|
||||
|
||||
child->parent=delparent=node->parent;
|
||||
child->parent = delparent = node->parent;
|
||||
|
||||
if(node==delparent->left) {
|
||||
delparent->left=child;
|
||||
if(node == delparent->left) {
|
||||
delparent->left = child;
|
||||
} else {
|
||||
/*if(node!=delparent->right)
|
||||
return;*/
|
||||
delparent->right=child;
|
||||
return;*/
|
||||
delparent->right = child;
|
||||
}
|
||||
}
|
||||
|
||||
node->parent=0;
|
||||
node->right=0;
|
||||
node->left=0;
|
||||
node->parent = NULL;
|
||||
node->right = NULL;
|
||||
node->left = NULL;
|
||||
|
||||
us->nodecount--;
|
||||
|
||||
*pswap = next;
|
||||
*pchild = child;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_tree_lookup(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_tree_lookup(usrtc_t *us, const void *key)
|
||||
{
|
||||
usrtc_node_t *root=tree_root_priv(us);
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *root = tree_root_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *saved;
|
||||
long res;
|
||||
|
||||
while (root!=nil) {
|
||||
res=us->compare(key,root->key);
|
||||
if(res<0)
|
||||
root=root->left;
|
||||
else if(res>0)
|
||||
root=root->right;
|
||||
while (root != nil) {
|
||||
res = us->compare(key, root->key);
|
||||
if(res < 0)
|
||||
root = root->left;
|
||||
else if(res > 0)
|
||||
root = root->right;
|
||||
else {
|
||||
if(!us->dupes_allowed)
|
||||
return root; /*no duplicates*/
|
||||
else { /*duplicate, then find left occurence*/
|
||||
do {
|
||||
saved=root;
|
||||
root=root->left;
|
||||
while(root!=nil && us->compare(key,root->key))
|
||||
root=root->right;
|
||||
} while(root!=nil);
|
||||
saved = root;
|
||||
root = root->left;
|
||||
while(root!=nil && us->compare(key, root->key))
|
||||
root = root->right;
|
||||
} while(root != nil);
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
@ -191,27 +196,27 @@ usrtc_node_t *usrtc_tree_lookup(usrtc_t *us,const void *key)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_tree_lower_bound(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_tree_lower_bound(usrtc_t *us, const void *key)
|
||||
{
|
||||
usrtc_node_t *root=tree_root_priv(us);
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *tentative=NULL;
|
||||
usrtc_node_t *root = tree_root_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *tentative = NULL;
|
||||
long res;
|
||||
|
||||
while(root!=nil) {
|
||||
res=us->compare(key,root->key);
|
||||
while(root != nil) {
|
||||
res = us->compare(key, root->key);
|
||||
|
||||
if(res>0) {
|
||||
root=root->right;
|
||||
} else if(res<0) {
|
||||
tentative=root;
|
||||
root=root->left;
|
||||
if(res > 0) {
|
||||
root = root->right;
|
||||
} else if(res < 0) {
|
||||
tentative = root;
|
||||
root = root->left;
|
||||
} else {
|
||||
if (!us->dupes_allowed)
|
||||
return root;
|
||||
else {
|
||||
tentative=root;
|
||||
root=root->left;
|
||||
tentative = root;
|
||||
root = root->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -219,27 +224,27 @@ usrtc_node_t *usrtc_tree_lower_bound(usrtc_t *us,const void *key)
|
||||
return tentative;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_tree_upper_bound(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_tree_upper_bound(usrtc_t *us, const void *key)
|
||||
{
|
||||
usrtc_node_t *root=tree_root_priv(us);
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *tentative=NULL;
|
||||
usrtc_node_t *root = tree_root_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *tentative = NULL;
|
||||
long res;
|
||||
|
||||
while(root!=nil) {
|
||||
res=us->compare(key,root->key);
|
||||
while(root != nil) {
|
||||
res = us->compare(key, root->key);
|
||||
|
||||
if(res>0) {
|
||||
root=root->left;
|
||||
} else if(res<0) {
|
||||
tentative=root;
|
||||
root=root->right;
|
||||
if(res > 0) {
|
||||
root = root->left;
|
||||
} else if(res < 0) {
|
||||
tentative = root;
|
||||
root = root->right;
|
||||
} else {
|
||||
if (!us->dupes_allowed)
|
||||
return root;
|
||||
else {
|
||||
tentative=root;
|
||||
root=root->right;
|
||||
tentative = root;
|
||||
root = root->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -262,61 +267,61 @@ usrtc_node_t *usrtc_tree_first(usrtc_t *us)
|
||||
|
||||
usrtc_node_t *usrtc_tree_last(usrtc_t *us)
|
||||
{
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *root=tree_root_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *root = tree_root_priv(us);
|
||||
usrtc_node_t *right;
|
||||
|
||||
if(root!=nil)
|
||||
while((right=root->right)!=nil)
|
||||
root=right;
|
||||
if(root != nil)
|
||||
while((right = root->right) != nil)
|
||||
root = right;
|
||||
|
||||
return (root==nil) ? NULL : root;
|
||||
return (root == nil) ? NULL : root;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_tree_next(usrtc_t *us, usrtc_node_t *curr)
|
||||
{
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *parent;
|
||||
usrtc_node_t *left;
|
||||
|
||||
if(curr->right!=nil) {
|
||||
curr=curr->right;
|
||||
while((left=curr->left)!=nil)
|
||||
curr=left;
|
||||
if(curr->right != nil) {
|
||||
curr = curr->right;
|
||||
while((left = curr->left) != nil)
|
||||
curr = left;
|
||||
return curr;
|
||||
}
|
||||
|
||||
parent=curr->parent;
|
||||
parent = curr->parent;
|
||||
|
||||
while(parent!=nil && curr==parent->right) {
|
||||
curr=parent;
|
||||
parent=curr->parent;
|
||||
while((parent != nil) && (curr == parent->right)) {
|
||||
curr = parent;
|
||||
parent = curr->parent;
|
||||
}
|
||||
|
||||
return (parent==nil) ? NULL : parent;
|
||||
return (parent == nil) ? NULL : parent;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_tree_prev(usrtc_t *us, usrtc_node_t *curr)
|
||||
{
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *parent;
|
||||
usrtc_node_t *right;
|
||||
|
||||
if(curr->left!=nil) {
|
||||
curr=curr->left;
|
||||
while((right=curr->right)!=nil)
|
||||
curr=right;
|
||||
if(curr->left != nil) {
|
||||
curr = curr->left;
|
||||
while((right = curr->right) != nil)
|
||||
curr = right;
|
||||
return curr;
|
||||
}
|
||||
|
||||
parent=curr->parent;
|
||||
parent = curr->parent;
|
||||
|
||||
while(parent!=nil && curr==parent->left) {
|
||||
curr=parent;
|
||||
parent=curr->parent;
|
||||
while((parent != nil) && (curr == parent->left)) {
|
||||
curr = parent;
|
||||
parent = curr->parent;
|
||||
}
|
||||
|
||||
return (parent==nil) ? NULL : parent;
|
||||
return (parent == nil) ? NULL : parent;
|
||||
}
|
||||
|
||||
/*uff, convertation between trees and lists*/
|
||||
@ -324,161 +329,167 @@ void usrtc_tree_convert_to_list(usrtc_t *us)
|
||||
{
|
||||
usrtc_node_t *node;
|
||||
usrtc_node_t tempsentinel;
|
||||
usrtc_node_t *nil=&tempsentinel;
|
||||
usrtc_node_t *tail,*next;
|
||||
usrtc_node_t *treenil=tree_null_priv(us);
|
||||
usrtc_node_t *nil = &tempsentinel;
|
||||
usrtc_node_t *tail, *next;
|
||||
usrtc_node_t *treenil = tree_null_priv(us);
|
||||
|
||||
if(us->nodecount==0) /* no nodes */
|
||||
if(us->nodecount == 0) /* no nodes */
|
||||
return;
|
||||
|
||||
tempsentinel.next=nil;
|
||||
tempsentinel.prev=nil;
|
||||
tempsentinel.next = nil;
|
||||
tempsentinel.prev = nil;
|
||||
|
||||
/* two passes */
|
||||
for(tail=nil,node=usrtc_tree_first(us);node!=0;tail=node,node=next) {
|
||||
next=usrtc_tree_next(us,node);
|
||||
node->prev=tail;
|
||||
for(tail = nil, node = usrtc_tree_first(us); node != 0; tail = node,
|
||||
node = next) {
|
||||
next = usrtc_tree_next(us, node);
|
||||
node->prev = tail;
|
||||
}
|
||||
|
||||
nil->prev=tail;
|
||||
nil->prev = tail;
|
||||
|
||||
for(tail=nil,node=nil->prev;node!=nil;tail=node,node=node->prev)
|
||||
node->next=tail;
|
||||
for(tail = nil, node = nil->prev; node != nil; tail = node,
|
||||
node = node->prev)
|
||||
node->next = tail;
|
||||
|
||||
nil->next=tail;
|
||||
nil->next = tail;
|
||||
|
||||
us->sentinel.next=tempsentinel.next;
|
||||
us->sentinel.prev=tempsentinel.prev;
|
||||
us->sentinel.next->prev=treenil;
|
||||
us->sentinel.prev->next=treenil;
|
||||
us->sentinel.next = tempsentinel.next;
|
||||
us->sentinel.prev = tempsentinel.prev;
|
||||
us->sentinel.next->prev = treenil;
|
||||
us->sentinel.prev->next = treenil;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_tree_convert_from_list(usrtc_t *us)
|
||||
{
|
||||
usrtc_node_t *tree[TREE_DEPTH_MAX]={ 0 };
|
||||
usrtc_node_t *tree[TREE_DEPTH_MAX] = { 0 };
|
||||
usrtc_node_t *curr;
|
||||
usrtc_node_t *nil=tree_null_priv(us);
|
||||
usrtc_node_t *nil = tree_null_priv(us);
|
||||
usrtc_node_t *next;
|
||||
usrtc_node_t *complete=NULL;
|
||||
usrtc_count_t fullcount=(usrtc_count_t)USRTC_COUNT_T_MAX;
|
||||
usrtc_count_t nodecount=us->nodecount;
|
||||
usrtc_node_t *complete = NULL;
|
||||
usrtc_count_t fullcount = (usrtc_count_t)USRTC_COUNT_T_MAX;
|
||||
usrtc_count_t nodecount = us->nodecount;
|
||||
usrtc_count_t botrowcount;
|
||||
int baselevel=0;
|
||||
int level=0;
|
||||
int i=0;
|
||||
int baselevel = 0;
|
||||
int level = 0;
|
||||
int i = 0;
|
||||
|
||||
while (fullcount>=nodecount && fullcount) /* calc */
|
||||
fullcount>>=1;
|
||||
while ((fullcount >= nodecount) && fullcount) /* calc */
|
||||
fullcount >>= 1;
|
||||
|
||||
botrowcount=nodecount-fullcount;
|
||||
botrowcount = nodecount - fullcount;
|
||||
|
||||
for(curr=nil->next;curr!=nil;curr=next) {
|
||||
next=curr->next;
|
||||
for(curr = nil->next; curr != nil; curr = next) {
|
||||
next = curr->next;
|
||||
|
||||
if(complete==NULL && botrowcount-- ==0) {
|
||||
baselevel=level=1;
|
||||
complete=tree[0];
|
||||
if((complete == NULL) && (botrowcount-- == 0)) {
|
||||
baselevel = level = 1;
|
||||
complete = tree[0];
|
||||
|
||||
if(complete!=NULL) {
|
||||
tree[0]=0;
|
||||
complete->right=nil;
|
||||
while(tree[level]!=NULL) {
|
||||
tree[level]->right=complete;
|
||||
complete->parent=tree[level];
|
||||
complete=tree[level];
|
||||
tree[level++]=0;
|
||||
if(complete != NULL) {
|
||||
tree[0] = 0;
|
||||
complete->right = nil;
|
||||
while(tree[level] != NULL) {
|
||||
tree[level]->right = complete;
|
||||
complete->parent = tree[level];
|
||||
complete = tree[level];
|
||||
tree[level++] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(complete==NULL) {
|
||||
curr->left=nil;
|
||||
curr->right=nil;
|
||||
complete=curr;
|
||||
if(complete == NULL) {
|
||||
curr->left = nil;
|
||||
curr->right = nil;
|
||||
complete = curr;
|
||||
|
||||
if(level!=baselevel)
|
||||
if(level != baselevel)
|
||||
return;
|
||||
while(tree[level]!=NULL) {
|
||||
tree[level]->right=complete;
|
||||
complete->parent=tree[level];
|
||||
complete=tree[level];
|
||||
tree[level++]=0;
|
||||
while(tree[level] != NULL) {
|
||||
tree[level]->right = complete;
|
||||
complete->parent = tree[level];
|
||||
complete = tree[level];
|
||||
tree[level++] = 0;
|
||||
}
|
||||
} else {
|
||||
curr->left=complete;
|
||||
complete->parent=curr;
|
||||
tree[level]=curr;
|
||||
complete=NULL;
|
||||
level=baselevel;
|
||||
curr->left = complete;
|
||||
complete->parent = curr;
|
||||
tree[level] = curr;
|
||||
complete = NULL;
|
||||
level = baselevel;
|
||||
}
|
||||
}
|
||||
|
||||
if(complete==NULL)
|
||||
complete=nil;
|
||||
if(complete == NULL)
|
||||
complete = nil;
|
||||
|
||||
for(i=0;i<TREE_DEPTH_MAX;i++) {
|
||||
if(tree[i]!=NULL) {
|
||||
tree[i]->right=complete;
|
||||
complete->parent=tree[i];
|
||||
complete=tree[i];
|
||||
for(i = 0; i < TREE_DEPTH_MAX; i++) {
|
||||
if(tree[i] != NULL) {
|
||||
tree[i]->right = complete;
|
||||
complete->parent = tree[i];
|
||||
complete = tree[i];
|
||||
}
|
||||
}
|
||||
|
||||
nil->right=nil;
|
||||
nil->left=complete;
|
||||
complete->parent=nil;
|
||||
nil->right = nil;
|
||||
nil->left = complete;
|
||||
complete->parent = nil;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_tree_rotate_left(usrtc_node_t *child,usrtc_node_t *parent)
|
||||
void usrtc_tree_rotate_left(usrtc_node_t *child, usrtc_node_t *parent)
|
||||
{
|
||||
usrtc_node_t *leftgrandchild;
|
||||
usrtc_node_t *grandpa;
|
||||
|
||||
if(parent->right!=child)
|
||||
if(parent->right != child)
|
||||
return;
|
||||
|
||||
child=parent->right;
|
||||
parent->right=leftgrandchild=child->left;
|
||||
leftgrandchild->parent=parent;
|
||||
child = parent->right;
|
||||
parent->right = leftgrandchild = child->left;
|
||||
leftgrandchild->parent = parent;
|
||||
|
||||
child->parent=grandpa=parent->parent;
|
||||
child->parent = grandpa = parent->parent;
|
||||
|
||||
if(parent==grandpa->left) {
|
||||
if(parent == grandpa->left) {
|
||||
grandpa->left=child;
|
||||
} else {
|
||||
if(parent!=grandpa->right)
|
||||
if(parent != grandpa->right)
|
||||
return;
|
||||
grandpa->right=child;
|
||||
grandpa->right = child;
|
||||
}
|
||||
|
||||
child->left=parent;
|
||||
parent->parent=child;
|
||||
child->left = parent;
|
||||
parent->parent = child;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_tree_rotate_right(usrtc_node_t *child,usrtc_node_t *parent)
|
||||
void usrtc_tree_rotate_right(usrtc_node_t *child, usrtc_node_t *parent)
|
||||
{
|
||||
usrtc_node_t *rightgrandchild;
|
||||
usrtc_node_t *grandpa;
|
||||
|
||||
if(parent->left!=child)
|
||||
if(parent->left != child)
|
||||
return;
|
||||
|
||||
parent->left=rightgrandchild=child->right;
|
||||
rightgrandchild->parent=parent;
|
||||
parent->left = rightgrandchild = child->right;
|
||||
rightgrandchild->parent = parent;
|
||||
|
||||
child->parent=grandpa=parent->parent;
|
||||
child->parent = grandpa = parent->parent;
|
||||
|
||||
if(parent==grandpa->right) {
|
||||
grandpa->right=child;
|
||||
if(parent == grandpa->right) {
|
||||
grandpa->right = child;
|
||||
} else {
|
||||
if(parent!=grandpa->left)
|
||||
if(parent != grandpa->left)
|
||||
return;
|
||||
grandpa->left=child;
|
||||
grandpa->left = child;
|
||||
}
|
||||
|
||||
child->right=parent;
|
||||
parent->parent=child;
|
||||
child->right = parent;
|
||||
parent->parent = child;
|
||||
return;
|
||||
}
|
||||
|
||||
/* local functions */
|
||||
|
142
lib/usrtc.c
142
lib/usrtc.c
@ -46,36 +46,40 @@ static usrtc_node_t *default_node_alloc(void *context)
|
||||
return malloc(sizeof *default_node_alloc(context));
|
||||
}
|
||||
|
||||
static void default_node_free(void *context,usrtc_node_t *node)
|
||||
static void default_node_free(void *context, usrtc_node_t *node)
|
||||
{
|
||||
free(node);
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_init(usrtc_t *us,int impl,usrtc_count_t maxcount,usrtc_compare_t compare)
|
||||
void usrtc_init(usrtc_t *us, int impl, usrtc_count_t maxcount,
|
||||
usrtc_compare_t compare)
|
||||
{
|
||||
if(!us)
|
||||
return;
|
||||
if(impl>MAX_IMPL)
|
||||
return;
|
||||
|
||||
us->nodecount=0;
|
||||
us->maxcount=maxcount;
|
||||
us->dupes_allowed=0;
|
||||
us->compare=compare;
|
||||
us->node_alloc=default_node_alloc;
|
||||
us->node_free=default_node_free;
|
||||
us->context=0;
|
||||
us->futable=impl_table[impl];
|
||||
us->nodecount = 0;
|
||||
us->maxcount = maxcount;
|
||||
us->dupes_allowed = 0;
|
||||
us->compare = compare;
|
||||
us->node_alloc = default_node_alloc;
|
||||
us->node_free = default_node_free;
|
||||
us->context = 0;
|
||||
us->futable = impl_table[impl];
|
||||
us->futable->usrtc_init(us);
|
||||
return;
|
||||
}
|
||||
|
||||
usrtc_t *usrtc_create(int impl,usrtc_count_t maxcount,usrtc_compare_t compare)
|
||||
usrtc_t *usrtc_create(int impl, usrtc_count_t maxcount,
|
||||
usrtc_compare_t compare)
|
||||
{
|
||||
usrtc_t *newrtc=(usrtc_t*)malloc(sizeof *newrtc);
|
||||
|
||||
usrtc_t *newrtc = (usrtc_t *) malloc(sizeof *newrtc);
|
||||
|
||||
if(newrtc)
|
||||
usrtc_init(newrtc,impl,maxcount,compare);
|
||||
|
||||
usrtc_init(newrtc, impl, maxcount, compare);
|
||||
|
||||
return newrtc;
|
||||
}
|
||||
|
||||
@ -86,25 +90,28 @@ void usrtc_destroy(usrtc_t *us)
|
||||
return;
|
||||
|
||||
free(us);
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_convert_to(usrtc_t *us,int impl)
|
||||
{
|
||||
if(impl_table[impl]==us->futable)
|
||||
if(impl_table[impl] == us->futable)
|
||||
return;
|
||||
|
||||
if(us->futable->usrtc_type==usrtc_bst && (impl==USRTC_BST || impl==USRTC_SPLAY)) {
|
||||
us->futable=impl_table[impl];
|
||||
if((us->futable->usrtc_type == usrtc_bst) &&
|
||||
(impl == USRTC_BST || impl == USRTC_SPLAY)) {
|
||||
us->futable = impl_table[impl];
|
||||
return;
|
||||
}
|
||||
|
||||
us->futable->usrtc_convert_to_list(us);
|
||||
us->futable=impl_table[impl];
|
||||
us->futable = impl_table[impl];
|
||||
|
||||
if(impl_table[impl]->usrtc_type==usrtc_lst)
|
||||
if(impl_table[impl]->usrtc_type == usrtc_lst)
|
||||
return;
|
||||
|
||||
us->futable->usrtc_convert_from_list(us);
|
||||
return;
|
||||
}
|
||||
|
||||
usrtc_count_t usrtc_count(usrtc_t *us)
|
||||
@ -122,29 +129,32 @@ int usrtc_isfull(usrtc_t *us)
|
||||
return us->nodecount == us->maxcount;
|
||||
}
|
||||
|
||||
int usrtc_alloc_insert(usrtc_t *us,const void *key,void *data)
|
||||
int usrtc_alloc_insert(usrtc_t *us, const void *key, void *data)
|
||||
{
|
||||
usrtc_node_t *newnode=us->node_alloc(us->context);
|
||||
|
||||
if(newnode!=NULL) {
|
||||
newnode->data=data;
|
||||
us->futable->usrtc_insert(us,newnode,key);
|
||||
usrtc_node_t *newnode = us->node_alloc(us->context);
|
||||
|
||||
if(newnode != NULL) {
|
||||
newnode->data = data;
|
||||
us->futable->usrtc_insert(us, newnode, key);
|
||||
}
|
||||
|
||||
return newnode!=NULL;
|
||||
|
||||
return newnode != NULL;
|
||||
}
|
||||
|
||||
void usrtc_delete_free(usrtc_t *us,usrtc_node_t *node)
|
||||
void usrtc_delete_free(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
us->futable->usrtc_delete(us,node);
|
||||
us->node_free(us->context,node);
|
||||
us->futable->usrtc_delete(us, node);
|
||||
us->node_free(us->context, node);
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_set_allocator(usrtc_t *us,usrtc_node_alloc_t alloc,usrtc_node_free_t n_free,void *context)
|
||||
void usrtc_set_allocator(usrtc_t *us, usrtc_node_alloc_t alloc,
|
||||
usrtc_node_free_t n_free, void *context)
|
||||
{
|
||||
us->node_alloc=alloc;
|
||||
us->node_free=n_free;
|
||||
us->context=context;
|
||||
us->node_alloc = alloc;
|
||||
us->node_free = n_free;
|
||||
us->context = context;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_allow_dupes(usrtc_t *us)
|
||||
@ -153,31 +163,33 @@ void usrtc_allow_dupes(usrtc_t *us)
|
||||
if(!us->nodecount)
|
||||
return;
|
||||
|
||||
us->dupes_allowed=1;
|
||||
us->dupes_allowed = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_node_init(usrtc_node_t *node,void *data)
|
||||
void usrtc_node_init(usrtc_node_t *node, void *data)
|
||||
{
|
||||
node->data=data;
|
||||
node->impl_specific.usrtc_dummy=0;
|
||||
node->left=NULL;
|
||||
node->right=NULL;
|
||||
node->parent=NULL;
|
||||
node->data = data;
|
||||
node->impl_specific.usrtc_dummy = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->parent = NULL;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_node_create(void *data)
|
||||
{
|
||||
usrtc_node_t *newnode=(usrtc_node_t*)malloc(sizeof *newnode);
|
||||
|
||||
if(newnode!=NULL)
|
||||
newnode->data=data;
|
||||
|
||||
usrtc_node_t *newnode = (usrtc_node_t *) malloc(sizeof *newnode);
|
||||
|
||||
if(newnode != NULL)
|
||||
newnode->data = data;
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
void usrtc_node_destroy(usrtc_node_t *node)
|
||||
{
|
||||
free(node);
|
||||
return;
|
||||
}
|
||||
|
||||
void *usrtc_node_getdata(usrtc_node_t *node)
|
||||
@ -185,9 +197,10 @@ void *usrtc_node_getdata(usrtc_node_t *node)
|
||||
return node->data;
|
||||
}
|
||||
|
||||
void usrtc_node_setdata(usrtc_node_t *node,void *data)
|
||||
void usrtc_node_setdata(usrtc_node_t *node, void *data)
|
||||
{
|
||||
node->data=data;
|
||||
node->data = data;
|
||||
return;
|
||||
}
|
||||
|
||||
const void *usrtc_node_getkey(usrtc_node_t *node)
|
||||
@ -195,29 +208,31 @@ const void *usrtc_node_getkey(usrtc_node_t *node)
|
||||
return node->key;
|
||||
}
|
||||
|
||||
void usrtc_insert(usrtc_t *us,usrtc_node_t *node,const void *key)
|
||||
void usrtc_insert(usrtc_t *us, usrtc_node_t *node, const void *key)
|
||||
{
|
||||
us->futable->usrtc_insert(us,node,key);
|
||||
us->futable->usrtc_insert(us, node, key);
|
||||
return;
|
||||
}
|
||||
|
||||
void usrtc_delete(usrtc_t *us,usrtc_node_t *node)
|
||||
void usrtc_delete(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
us->futable->usrtc_delete(us,node);
|
||||
us->futable->usrtc_delete(us, node);
|
||||
return;
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_lookup(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_lookup(usrtc_t *us, const void *key)
|
||||
{
|
||||
return us->futable->usrtc_lookup(us,key);
|
||||
return us->futable->usrtc_lookup(us, key);
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_lower_bound(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_lower_bound(usrtc_t *us, const void *key)
|
||||
{
|
||||
return us->futable->usrtc_lower_bound(us,key);
|
||||
return us->futable->usrtc_lower_bound(us, key);
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_upper_bound(usrtc_t *us,const void *key)
|
||||
usrtc_node_t *usrtc_upper_bound(usrtc_t *us, const void *key)
|
||||
{
|
||||
return us->futable->usrtc_upper_bound(us,key);
|
||||
return us->futable->usrtc_upper_bound(us, key);
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_first(usrtc_t *us)
|
||||
@ -230,14 +245,13 @@ usrtc_node_t *usrtc_last(usrtc_t *us)
|
||||
return us->futable->usrtc_last(us);
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_next(usrtc_t *us,usrtc_node_t *node)
|
||||
usrtc_node_t *usrtc_next(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
return us->futable->usrtc_next(us,node);
|
||||
return us->futable->usrtc_next(us, node);
|
||||
}
|
||||
|
||||
usrtc_node_t *usrtc_prev(usrtc_t *us,usrtc_node_t *node)
|
||||
usrtc_node_t *usrtc_prev(usrtc_t *us, usrtc_node_t *node)
|
||||
{
|
||||
return us->futable->usrtc_prev(us,node);
|
||||
return us->futable->usrtc_prev(us, node);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user