|
|
@ -27,104 +27,107 @@
|
|
|
|
static void tree_delete(usrtc_t *, usrtc_node_t *);
|
|
|
|
static void tree_delete(usrtc_t *, usrtc_node_t *);
|
|
|
|
|
|
|
|
|
|
|
|
usrtc_functions_t usrtc_tree_fu = {
|
|
|
|
usrtc_functions_t usrtc_tree_fu = {
|
|
|
|
usrtc_tree_init,
|
|
|
|
usrtc_tree_init,
|
|
|
|
usrtc_tree_insert,
|
|
|
|
usrtc_tree_insert,
|
|
|
|
tree_delete,
|
|
|
|
tree_delete,
|
|
|
|
usrtc_tree_lookup,
|
|
|
|
usrtc_tree_lookup,
|
|
|
|
usrtc_tree_lower_bound,
|
|
|
|
usrtc_tree_lower_bound,
|
|
|
|
usrtc_tree_upper_bound,
|
|
|
|
usrtc_tree_upper_bound,
|
|
|
|
usrtc_tree_first,
|
|
|
|
usrtc_tree_first,
|
|
|
|
usrtc_tree_last,
|
|
|
|
usrtc_tree_last,
|
|
|
|
usrtc_tree_next,
|
|
|
|
usrtc_tree_next,
|
|
|
|
usrtc_tree_prev,
|
|
|
|
usrtc_tree_prev,
|
|
|
|
usrtc_tree_convert_to_list,
|
|
|
|
usrtc_tree_convert_to_list,
|
|
|
|
usrtc_tree_convert_from_list,
|
|
|
|
usrtc_tree_convert_from_list,
|
|
|
|
usrtc_bst
|
|
|
|
usrtc_bst
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*implementation*/
|
|
|
|
/*implementation*/
|
|
|
|
|
|
|
|
|
|
|
|
void usrtc_tree_init(usrtc_t *us)
|
|
|
|
void usrtc_tree_init(usrtc_t *us)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
us->sentinel.left=&us->sentinel;
|
|
|
|
us->sentinel.left = &us->sentinel;
|
|
|
|
us->sentinel.right=&us->sentinel;
|
|
|
|
us->sentinel.right = &us->sentinel;
|
|
|
|
us->sentinel.parent=&us->sentinel;
|
|
|
|
us->sentinel.parent = &us->sentinel;
|
|
|
|
|
|
|
|
|
|
|
|
us->sentinel.impl_specific.usrtc_dummy=0;
|
|
|
|
us->sentinel.impl_specific.usrtc_dummy = 0;
|
|
|
|
us->sentinel.data=0;
|
|
|
|
us->sentinel.data = 0;
|
|
|
|
us->sentinel.key=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 *where = tree_root_priv(us);
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *parent=nil;
|
|
|
|
usrtc_node_t *parent = nil;
|
|
|
|
long res=-1;
|
|
|
|
long res = -1;
|
|
|
|
|
|
|
|
|
|
|
|
node->key=key;
|
|
|
|
node->key = key;
|
|
|
|
|
|
|
|
|
|
|
|
while(where!=nil) {
|
|
|
|
while(where != nil) {
|
|
|
|
parent=where;
|
|
|
|
parent = where;
|
|
|
|
res=us->compare(key,where->key);
|
|
|
|
res = us->compare(key, where->key);
|
|
|
|
|
|
|
|
|
|
|
|
if(us->dupes_allowed && !res) /*trying to put duplicate to the disabled dupe tree*/
|
|
|
|
if(us->dupes_allowed && !res) /*trying to put duplicate to the disabled dupe tree*/
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if(res<0)
|
|
|
|
if(res<0)
|
|
|
|
where=where->left;
|
|
|
|
where = where->left;
|
|
|
|
else
|
|
|
|
else
|
|
|
|
where=where->right;
|
|
|
|
where = where->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*assert(where==nil);*/
|
|
|
|
/*assert(where==nil);*/
|
|
|
|
|
|
|
|
|
|
|
|
if(res<0)
|
|
|
|
if(res < 0)
|
|
|
|
parent->left=node;
|
|
|
|
parent->left = node;
|
|
|
|
else
|
|
|
|
else
|
|
|
|
parent->right=node;
|
|
|
|
parent->right = node;
|
|
|
|
|
|
|
|
|
|
|
|
node->parent=parent;
|
|
|
|
node->parent = parent;
|
|
|
|
node->left=nil;
|
|
|
|
node->left = nil;
|
|
|
|
node->right=nil;
|
|
|
|
node->right = nil;
|
|
|
|
|
|
|
|
|
|
|
|
us->nodecount++;
|
|
|
|
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 *child;
|
|
|
|
usrtc_node_t *delparent=node->parent;
|
|
|
|
usrtc_node_t *delparent = node->parent;
|
|
|
|
usrtc_node_t *next=node;
|
|
|
|
usrtc_node_t *next = node;
|
|
|
|
usrtc_node_t *nextparent;
|
|
|
|
usrtc_node_t *nextparent;
|
|
|
|
|
|
|
|
|
|
|
|
if(node->left!=nil && node->right!=nil) {
|
|
|
|
if(node->left != nil && node->right != nil) {
|
|
|
|
next=usrtc_tree_next(us,node);
|
|
|
|
next = usrtc_tree_next(us, node);
|
|
|
|
nextparent=next->parent;
|
|
|
|
nextparent = next->parent;
|
|
|
|
|
|
|
|
|
|
|
|
/*if(next!=nil && next->parent!=nil && next->parent==nil)
|
|
|
|
/*if(next!=nil && next->parent!=nil && next->parent==nil)
|
|
|
|
return;*/
|
|
|
|
return;*/
|
|
|
|
|
|
|
|
|
|
|
|
child=next->right;
|
|
|
|
child = next->right;
|
|
|
|
child->parent=nextparent;
|
|
|
|
child->parent = nextparent;
|
|
|
|
|
|
|
|
|
|
|
|
if(nextparent->left==next)
|
|
|
|
if(nextparent->left == next)
|
|
|
|
nextparent->left=child;
|
|
|
|
nextparent->left = child;
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
//if(nextparent->right!=next)
|
|
|
|
//if(nextparent->right!=next)
|
|
|
|
//return;
|
|
|
|
//return;
|
|
|
|
nextparent->right=child;
|
|
|
|
nextparent->right = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
next->parent=delparent;
|
|
|
|
next->parent = delparent;
|
|
|
|
next->left=node->left;
|
|
|
|
next->left = node->left;
|
|
|
|
next->right=node->right;
|
|
|
|
next->right = node->right;
|
|
|
|
next->left->parent=next;
|
|
|
|
next->left->parent = next;
|
|
|
|
next->right->parent=next;
|
|
|
|
next->right->parent = next;
|
|
|
|
|
|
|
|
|
|
|
|
if(delparent->left==node) {
|
|
|
|
if(delparent->left == node) {
|
|
|
|
delparent->left=next;
|
|
|
|
delparent->left = next;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
//if(delparent->right!=node)
|
|
|
|
//if(delparent->right!=node)
|
|
|
|
// return;
|
|
|
|
// return;
|
|
|
@ -134,55 +137,57 @@ void usrtc_tree_delete(usrtc_t *us,usrtc_node_t *node,usrtc_node_t **pswap,usrtc
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
/*if(node==nil)
|
|
|
|
/*if(node==nil)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
if(node->left!=nil && node->right!=nil)
|
|
|
|
if(node->left!=nil && node->right!=nil)
|
|
|
|
return;*/
|
|
|
|
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) {
|
|
|
|
if(node == delparent->left) {
|
|
|
|
delparent->left=child;
|
|
|
|
delparent->left = child;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
/*if(node!=delparent->right)
|
|
|
|
/*if(node!=delparent->right)
|
|
|
|
return;*/
|
|
|
|
return;*/
|
|
|
|
delparent->right=child;
|
|
|
|
delparent->right = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
node->parent=0;
|
|
|
|
node->parent = NULL;
|
|
|
|
node->right=0;
|
|
|
|
node->right = NULL;
|
|
|
|
node->left=0;
|
|
|
|
node->left = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
us->nodecount--;
|
|
|
|
us->nodecount--;
|
|
|
|
|
|
|
|
|
|
|
|
*pswap = next;
|
|
|
|
*pswap = next;
|
|
|
|
*pchild = child;
|
|
|
|
*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 *root = tree_root_priv(us);
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *saved;
|
|
|
|
usrtc_node_t *saved;
|
|
|
|
long res;
|
|
|
|
long res;
|
|
|
|
|
|
|
|
|
|
|
|
while (root!=nil) {
|
|
|
|
while (root != nil) {
|
|
|
|
res=us->compare(key,root->key);
|
|
|
|
res = us->compare(key, root->key);
|
|
|
|
if(res<0)
|
|
|
|
if(res < 0)
|
|
|
|
root=root->left;
|
|
|
|
root = root->left;
|
|
|
|
else if(res>0)
|
|
|
|
else if(res > 0)
|
|
|
|
root=root->right;
|
|
|
|
root = root->right;
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
if(!us->dupes_allowed)
|
|
|
|
if(!us->dupes_allowed)
|
|
|
|
return root; /*no duplicates*/
|
|
|
|
return root; /*no duplicates*/
|
|
|
|
else { /*duplicate, then find left occurence*/
|
|
|
|
else { /*duplicate, then find left occurence*/
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
saved=root;
|
|
|
|
saved = root;
|
|
|
|
root=root->left;
|
|
|
|
root = root->left;
|
|
|
|
while(root!=nil && us->compare(key,root->key))
|
|
|
|
while(root!=nil && us->compare(key, root->key))
|
|
|
|
root=root->right;
|
|
|
|
root = root->right;
|
|
|
|
} while(root!=nil);
|
|
|
|
} while(root != nil);
|
|
|
|
return saved;
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -191,27 +196,27 @@ usrtc_node_t *usrtc_tree_lookup(usrtc_t *us,const void *key)
|
|
|
|
return NULL;
|
|
|
|
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 *root = tree_root_priv(us);
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *tentative=NULL;
|
|
|
|
usrtc_node_t *tentative = NULL;
|
|
|
|
long res;
|
|
|
|
long res;
|
|
|
|
|
|
|
|
|
|
|
|
while(root!=nil) {
|
|
|
|
while(root != nil) {
|
|
|
|
res=us->compare(key,root->key);
|
|
|
|
res = us->compare(key, root->key);
|
|
|
|
|
|
|
|
|
|
|
|
if(res>0) {
|
|
|
|
if(res > 0) {
|
|
|
|
root=root->right;
|
|
|
|
root = root->right;
|
|
|
|
} else if(res<0) {
|
|
|
|
} else if(res < 0) {
|
|
|
|
tentative=root;
|
|
|
|
tentative = root;
|
|
|
|
root=root->left;
|
|
|
|
root = root->left;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (!us->dupes_allowed)
|
|
|
|
if (!us->dupes_allowed)
|
|
|
|
return root;
|
|
|
|
return root;
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
tentative=root;
|
|
|
|
tentative = root;
|
|
|
|
root=root->left;
|
|
|
|
root = root->left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -219,27 +224,27 @@ usrtc_node_t *usrtc_tree_lower_bound(usrtc_t *us,const void *key)
|
|
|
|
return tentative;
|
|
|
|
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 *root = tree_root_priv(us);
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *tentative=NULL;
|
|
|
|
usrtc_node_t *tentative = NULL;
|
|
|
|
long res;
|
|
|
|
long res;
|
|
|
|
|
|
|
|
|
|
|
|
while(root!=nil) {
|
|
|
|
while(root != nil) {
|
|
|
|
res=us->compare(key,root->key);
|
|
|
|
res = us->compare(key, root->key);
|
|
|
|
|
|
|
|
|
|
|
|
if(res>0) {
|
|
|
|
if(res > 0) {
|
|
|
|
root=root->left;
|
|
|
|
root = root->left;
|
|
|
|
} else if(res<0) {
|
|
|
|
} else if(res < 0) {
|
|
|
|
tentative=root;
|
|
|
|
tentative = root;
|
|
|
|
root=root->right;
|
|
|
|
root = root->right;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (!us->dupes_allowed)
|
|
|
|
if (!us->dupes_allowed)
|
|
|
|
return root;
|
|
|
|
return root;
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
tentative=root;
|
|
|
|
tentative = root;
|
|
|
|
root=root->right;
|
|
|
|
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 *usrtc_tree_last(usrtc_t *us)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *root=tree_root_priv(us);
|
|
|
|
usrtc_node_t *root = tree_root_priv(us);
|
|
|
|
usrtc_node_t *right;
|
|
|
|
usrtc_node_t *right;
|
|
|
|
|
|
|
|
|
|
|
|
if(root!=nil)
|
|
|
|
if(root != nil)
|
|
|
|
while((right=root->right)!=nil)
|
|
|
|
while((right = root->right) != nil)
|
|
|
|
root=right;
|
|
|
|
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 *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 *parent;
|
|
|
|
usrtc_node_t *left;
|
|
|
|
usrtc_node_t *left;
|
|
|
|
|
|
|
|
|
|
|
|
if(curr->right!=nil) {
|
|
|
|
if(curr->right != nil) {
|
|
|
|
curr=curr->right;
|
|
|
|
curr = curr->right;
|
|
|
|
while((left=curr->left)!=nil)
|
|
|
|
while((left = curr->left) != nil)
|
|
|
|
curr=left;
|
|
|
|
curr = left;
|
|
|
|
return curr;
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parent=curr->parent;
|
|
|
|
parent = curr->parent;
|
|
|
|
|
|
|
|
|
|
|
|
while(parent!=nil && curr==parent->right) {
|
|
|
|
while((parent != nil) && (curr == parent->right)) {
|
|
|
|
curr=parent;
|
|
|
|
curr = parent;
|
|
|
|
parent=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 *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 *parent;
|
|
|
|
usrtc_node_t *right;
|
|
|
|
usrtc_node_t *right;
|
|
|
|
|
|
|
|
|
|
|
|
if(curr->left!=nil) {
|
|
|
|
if(curr->left != nil) {
|
|
|
|
curr=curr->left;
|
|
|
|
curr = curr->left;
|
|
|
|
while((right=curr->right)!=nil)
|
|
|
|
while((right = curr->right) != nil)
|
|
|
|
curr=right;
|
|
|
|
curr = right;
|
|
|
|
return curr;
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parent=curr->parent;
|
|
|
|
parent = curr->parent;
|
|
|
|
|
|
|
|
|
|
|
|
while(parent!=nil && curr==parent->left) {
|
|
|
|
while((parent != nil) && (curr == parent->left)) {
|
|
|
|
curr=parent;
|
|
|
|
curr = parent;
|
|
|
|
parent=curr->parent;
|
|
|
|
parent = curr->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (parent==nil) ? NULL : parent;
|
|
|
|
return (parent == nil) ? NULL : parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*uff, convertation between trees and lists*/
|
|
|
|
/*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 *node;
|
|
|
|
usrtc_node_t tempsentinel;
|
|
|
|
usrtc_node_t tempsentinel;
|
|
|
|
usrtc_node_t *nil=&tempsentinel;
|
|
|
|
usrtc_node_t *nil = &tempsentinel;
|
|
|
|
usrtc_node_t *tail,*next;
|
|
|
|
usrtc_node_t *tail, *next;
|
|
|
|
usrtc_node_t *treenil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *treenil = tree_null_priv(us);
|
|
|
|
|
|
|
|
|
|
|
|
if(us->nodecount==0) /* no nodes */
|
|
|
|
if(us->nodecount == 0) /* no nodes */
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
tempsentinel.next=nil;
|
|
|
|
tempsentinel.next = nil;
|
|
|
|
tempsentinel.prev=nil;
|
|
|
|
tempsentinel.prev = nil;
|
|
|
|
|
|
|
|
|
|
|
|
/* two passes */
|
|
|
|
/* two passes */
|
|
|
|
for(tail=nil,node=usrtc_tree_first(us);node!=0;tail=node,node=next) {
|
|
|
|
for(tail = nil, node = usrtc_tree_first(us); node != 0; tail = node,
|
|
|
|
next=usrtc_tree_next(us,node);
|
|
|
|
node = next) {
|
|
|
|
node->prev=tail;
|
|
|
|
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)
|
|
|
|
for(tail = nil, node = nil->prev; node != nil; tail = node,
|
|
|
|
node->next=tail;
|
|
|
|
node = node->prev)
|
|
|
|
|
|
|
|
node->next = tail;
|
|
|
|
|
|
|
|
|
|
|
|
nil->next=tail;
|
|
|
|
nil->next = tail;
|
|
|
|
|
|
|
|
|
|
|
|
us->sentinel.next=tempsentinel.next;
|
|
|
|
us->sentinel.next = tempsentinel.next;
|
|
|
|
us->sentinel.prev=tempsentinel.prev;
|
|
|
|
us->sentinel.prev = tempsentinel.prev;
|
|
|
|
us->sentinel.next->prev=treenil;
|
|
|
|
us->sentinel.next->prev = treenil;
|
|
|
|
us->sentinel.prev->next=treenil;
|
|
|
|
us->sentinel.prev->next = treenil;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void usrtc_tree_convert_from_list(usrtc_t *us)
|
|
|
|
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 *curr;
|
|
|
|
usrtc_node_t *nil=tree_null_priv(us);
|
|
|
|
usrtc_node_t *nil = tree_null_priv(us);
|
|
|
|
usrtc_node_t *next;
|
|
|
|
usrtc_node_t *next;
|
|
|
|
usrtc_node_t *complete=NULL;
|
|
|
|
usrtc_node_t *complete = NULL;
|
|
|
|
usrtc_count_t fullcount=(usrtc_count_t)USRTC_COUNT_T_MAX;
|
|
|
|
usrtc_count_t fullcount = (usrtc_count_t)USRTC_COUNT_T_MAX;
|
|
|
|
usrtc_count_t nodecount=us->nodecount;
|
|
|
|
usrtc_count_t nodecount = us->nodecount;
|
|
|
|
usrtc_count_t botrowcount;
|
|
|
|
usrtc_count_t botrowcount;
|
|
|
|
int baselevel=0;
|
|
|
|
int baselevel = 0;
|
|
|
|
int level=0;
|
|
|
|
int level = 0;
|
|
|
|
int i=0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (fullcount>=nodecount && fullcount) /* calc */
|
|
|
|
while ((fullcount >= nodecount) && fullcount) /* calc */
|
|
|
|
fullcount>>=1;
|
|
|
|
fullcount >>= 1;
|
|
|
|
|
|
|
|
|
|
|
|
botrowcount=nodecount-fullcount;
|
|
|
|
botrowcount = nodecount - fullcount;
|
|
|
|
|
|
|
|
|
|
|
|
for(curr=nil->next;curr!=nil;curr=next) {
|
|
|
|
for(curr = nil->next; curr != nil; curr = next) {
|
|
|
|
next=curr->next;
|
|
|
|
next = curr->next;
|
|
|
|
|
|
|
|
|
|
|
|
if(complete==NULL && botrowcount-- ==0) {
|
|
|
|
if((complete == NULL) && (botrowcount-- == 0)) {
|
|
|
|
baselevel=level=1;
|
|
|
|
baselevel = level = 1;
|
|
|
|
complete=tree[0];
|
|
|
|
complete = tree[0];
|
|
|
|
|
|
|
|
|
|
|
|
if(complete!=NULL) {
|
|
|
|
if(complete != NULL) {
|
|
|
|
tree[0]=0;
|
|
|
|
tree[0] = 0;
|
|
|
|
complete->right=nil;
|
|
|
|
complete->right = nil;
|
|
|
|
while(tree[level]!=NULL) {
|
|
|
|
while(tree[level] != NULL) {
|
|
|
|
tree[level]->right=complete;
|
|
|
|
tree[level]->right = complete;
|
|
|
|
complete->parent=tree[level];
|
|
|
|
complete->parent = tree[level];
|
|
|
|
complete=tree[level];
|
|
|
|
complete = tree[level];
|
|
|
|
tree[level++]=0;
|
|
|
|
tree[level++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(complete==NULL) {
|
|
|
|
if(complete == NULL) {
|
|
|
|
curr->left=nil;
|
|
|
|
curr->left = nil;
|
|
|
|
curr->right=nil;
|
|
|
|
curr->right = nil;
|
|
|
|
complete=curr;
|
|
|
|
complete = curr;
|
|
|
|
|
|
|
|
|
|
|
|
if(level!=baselevel)
|
|
|
|
if(level != baselevel)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
while(tree[level]!=NULL) {
|
|
|
|
while(tree[level] != NULL) {
|
|
|
|
tree[level]->right=complete;
|
|
|
|
tree[level]->right = complete;
|
|
|
|
complete->parent=tree[level];
|
|
|
|
complete->parent = tree[level];
|
|
|
|
complete=tree[level];
|
|
|
|
complete = tree[level];
|
|
|
|
tree[level++]=0;
|
|
|
|
tree[level++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
curr->left=complete;
|
|
|
|
curr->left = complete;
|
|
|
|
complete->parent=curr;
|
|
|
|
complete->parent = curr;
|
|
|
|
tree[level]=curr;
|
|
|
|
tree[level] = curr;
|
|
|
|
complete=NULL;
|
|
|
|
complete = NULL;
|
|
|
|
level=baselevel;
|
|
|
|
level = baselevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(complete==NULL)
|
|
|
|
if(complete == NULL)
|
|
|
|
complete=nil;
|
|
|
|
complete = nil;
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0;i<TREE_DEPTH_MAX;i++) {
|
|
|
|
for(i = 0; i < TREE_DEPTH_MAX; i++) {
|
|
|
|
if(tree[i]!=NULL) {
|
|
|
|
if(tree[i] != NULL) {
|
|
|
|
tree[i]->right=complete;
|
|
|
|
tree[i]->right = complete;
|
|
|
|
complete->parent=tree[i];
|
|
|
|
complete->parent = tree[i];
|
|
|
|
complete=tree[i];
|
|
|
|
complete = tree[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nil->right=nil;
|
|
|
|
nil->right = nil;
|
|
|
|
nil->left=complete;
|
|
|
|
nil->left = complete;
|
|
|
|
complete->parent=nil;
|
|
|
|
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 *leftgrandchild;
|
|
|
|
usrtc_node_t *grandpa;
|
|
|
|
usrtc_node_t *grandpa;
|
|
|
|
|
|
|
|
|
|
|
|
if(parent->right!=child)
|
|
|
|
if(parent->right != child)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
child=parent->right;
|
|
|
|
child = parent->right;
|
|
|
|
parent->right=leftgrandchild=child->left;
|
|
|
|
parent->right = leftgrandchild = child->left;
|
|
|
|
leftgrandchild->parent=parent;
|
|
|
|
leftgrandchild->parent = parent;
|
|
|
|
|
|
|
|
|
|
|
|
child->parent=grandpa=parent->parent;
|
|
|
|
child->parent = grandpa = parent->parent;
|
|
|
|
|
|
|
|
|
|
|
|
if(parent==grandpa->left) {
|
|
|
|
if(parent == grandpa->left) {
|
|
|
|
grandpa->left=child;
|
|
|
|
grandpa->left=child;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if(parent!=grandpa->right)
|
|
|
|
if(parent != grandpa->right)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
grandpa->right=child;
|
|
|
|
grandpa->right = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
child->left=parent;
|
|
|
|
child->left = parent;
|
|
|
|
parent->parent=child;
|
|
|
|
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 *rightgrandchild;
|
|
|
|
usrtc_node_t *grandpa;
|
|
|
|
usrtc_node_t *grandpa;
|
|
|
|
|
|
|
|
|
|
|
|
if(parent->left!=child)
|
|
|
|
if(parent->left != child)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
parent->left=rightgrandchild=child->right;
|
|
|
|
parent->left = rightgrandchild = child->right;
|
|
|
|
rightgrandchild->parent=parent;
|
|
|
|
rightgrandchild->parent = parent;
|
|
|
|
|
|
|
|
|
|
|
|
child->parent=grandpa=parent->parent;
|
|
|
|
child->parent = grandpa = parent->parent;
|
|
|
|
|
|
|
|
|
|
|
|
if(parent==grandpa->right) {
|
|
|
|
if(parent == grandpa->right) {
|
|
|
|
grandpa->right=child;
|
|
|
|
grandpa->right = child;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if(parent!=grandpa->left)
|
|
|
|
if(parent != grandpa->left)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
grandpa->left=child;
|
|
|
|
grandpa->left = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
child->right=parent;
|
|
|
|
child->right = parent;
|
|
|
|
parent->parent=child;
|
|
|
|
parent->parent = child;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* local functions */
|
|
|
|
/* local functions */
|
|
|
|