|
|
@ -32,19 +32,19 @@ static void redblack_delete(usrtc_t *, usrtc_node_t *);
|
|
|
|
static void redblack_convert_from_list(usrtc_t *);
|
|
|
|
static void redblack_convert_from_list(usrtc_t *);
|
|
|
|
|
|
|
|
|
|
|
|
usrtc_functions_t usrtc_redblack_fu = {
|
|
|
|
usrtc_functions_t usrtc_redblack_fu = {
|
|
|
|
redblack_init,
|
|
|
|
redblack_init,
|
|
|
|
redblack_insert,
|
|
|
|
redblack_insert,
|
|
|
|
redblack_delete,
|
|
|
|
redblack_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,
|
|
|
|
redblack_convert_from_list,
|
|
|
|
redblack_convert_from_list,
|
|
|
|
usrtc_bst
|
|
|
|
usrtc_bst
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*implementation*/
|
|
|
|
/*implementation*/
|
|
|
@ -59,251 +59,254 @@ static void redblack_insert(usrtc_t *us, usrtc_node_t *node, const void *key)
|
|
|
|
usrtc_node_t *parent;
|
|
|
|
usrtc_node_t *parent;
|
|
|
|
|
|
|
|
|
|
|
|
/* simple bt insert */
|
|
|
|
/* simple bt insert */
|
|
|
|
usrtc_tree_insert(us,node,key);
|
|
|
|
usrtc_tree_insert(us, node, key);
|
|
|
|
|
|
|
|
|
|
|
|
/* implementation specific insert */
|
|
|
|
/* implementation specific insert */
|
|
|
|
node->color = usrtc_red;
|
|
|
|
node->color = usrtc_red;
|
|
|
|
parent=node->parent;
|
|
|
|
parent = node->parent;
|
|
|
|
|
|
|
|
|
|
|
|
while(parent->color==usrtc_red) {
|
|
|
|
while(parent->color == usrtc_red) {
|
|
|
|
usrtc_node_t *grandpa=parent->parent;
|
|
|
|
usrtc_node_t *grandpa = parent->parent;
|
|
|
|
|
|
|
|
|
|
|
|
if(parent==grandpa->left) {
|
|
|
|
if(parent == grandpa->left) {
|
|
|
|
usrtc_node_t *uncle=grandpa->right;
|
|
|
|
usrtc_node_t *uncle = grandpa->right;
|
|
|
|
if(uncle->color==usrtc_red) { /*red parent->red uncle*/
|
|
|
|
if(uncle->color == usrtc_red) { /*red parent->red uncle*/
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
uncle->color=usrtc_black;
|
|
|
|
uncle->color = usrtc_black;
|
|
|
|
grandpa->color=usrtc_red;
|
|
|
|
grandpa->color = usrtc_red;
|
|
|
|
node=grandpa;
|
|
|
|
node = grandpa;
|
|
|
|
parent=grandpa->parent;
|
|
|
|
parent = grandpa->parent;
|
|
|
|
} else { /*red parent->black uncle */
|
|
|
|
} else { /*red parent->black uncle */
|
|
|
|
if(node==parent->right) {
|
|
|
|
if(node == parent->right) {
|
|
|
|
usrtc_tree_rotate_left(node,parent);
|
|
|
|
usrtc_tree_rotate_left(node, parent);
|
|
|
|
parent=node;
|
|
|
|
parent = node;
|
|
|
|
if(grandpa!=parent->parent)
|
|
|
|
if(grandpa != parent->parent)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
grandpa->color=usrtc_red;
|
|
|
|
grandpa->color = usrtc_red;
|
|
|
|
usrtc_tree_rotate_right(parent,grandpa);
|
|
|
|
usrtc_tree_rotate_right(parent, grandpa);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /*ooh,parent == parent->parent->right*/
|
|
|
|
} else { /* parent == parent->parent->right */
|
|
|
|
usrtc_node_t *uncle=grandpa->left;
|
|
|
|
usrtc_node_t *uncle = grandpa->left;
|
|
|
|
if(uncle->color==usrtc_red) {
|
|
|
|
if(uncle->color == usrtc_red) {
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
uncle->color=usrtc_black;
|
|
|
|
uncle->color = usrtc_black;
|
|
|
|
grandpa->color=usrtc_red;
|
|
|
|
grandpa->color = usrtc_red;
|
|
|
|
node=grandpa;
|
|
|
|
node = grandpa;
|
|
|
|
parent=grandpa->parent;
|
|
|
|
parent = grandpa->parent;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if(node==parent->left) {
|
|
|
|
if(node == parent->left) {
|
|
|
|
usrtc_tree_rotate_right(node,parent);
|
|
|
|
usrtc_tree_rotate_right(node, parent);
|
|
|
|
parent=node;
|
|
|
|
parent = node;
|
|
|
|
|
|
|
|
|
|
|
|
if(grandpa!=parent->parent)
|
|
|
|
if(grandpa != parent->parent)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
grandpa->color=usrtc_red;
|
|
|
|
grandpa->color = usrtc_red;
|
|
|
|
usrtc_tree_rotate_left(parent,grandpa);
|
|
|
|
usrtc_tree_rotate_left(parent, grandpa);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tree_root_priv(us)->color=usrtc_black;
|
|
|
|
tree_root_priv(us)->color = usrtc_black;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void redblack_delete(usrtc_t *us,usrtc_node_t *node)
|
|
|
|
static void redblack_delete(usrtc_t *us, usrtc_node_t *node)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
usrtc_node_t *swap;
|
|
|
|
usrtc_node_t *swap;
|
|
|
|
usrtc_node_t *child;
|
|
|
|
usrtc_node_t *child;
|
|
|
|
usrtc_rb_color_t savecolor;
|
|
|
|
usrtc_rb_color_t savecolor;
|
|
|
|
|
|
|
|
|
|
|
|
/*basic bt delete*/
|
|
|
|
/*basic bt delete*/
|
|
|
|
usrtc_tree_delete(us,node,&swap,&child);
|
|
|
|
usrtc_tree_delete(us, node, &swap, &child);
|
|
|
|
|
|
|
|
|
|
|
|
/*implementation specific deletion*/
|
|
|
|
/*implementation specific deletion*/
|
|
|
|
savecolor=node->color;
|
|
|
|
savecolor = node->color;
|
|
|
|
node->color=swap->color;
|
|
|
|
node->color = swap->color;
|
|
|
|
swap->color=savecolor;
|
|
|
|
swap->color = savecolor;
|
|
|
|
|
|
|
|
|
|
|
|
if(node->color==usrtc_black) { /*black*/
|
|
|
|
if(node->color == usrtc_black) { /*black*/
|
|
|
|
usrtc_node_t *parent;
|
|
|
|
usrtc_node_t *parent;
|
|
|
|
usrtc_node_t *sister;
|
|
|
|
usrtc_node_t *sister;
|
|
|
|
|
|
|
|
|
|
|
|
tree_root_priv(us)->color=usrtc_red;
|
|
|
|
tree_root_priv(us)->color = usrtc_red;
|
|
|
|
|
|
|
|
|
|
|
|
while(child->color==usrtc_black) {
|
|
|
|
while(child->color == usrtc_black) {
|
|
|
|
parent=child->parent;
|
|
|
|
parent = child->parent;
|
|
|
|
if(child==parent->left) {
|
|
|
|
if(child == parent->left) {
|
|
|
|
sister=parent->right;
|
|
|
|
sister = parent->right;
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if(sister->color==usrtc_red) {
|
|
|
|
if(sister->color == usrtc_red) {
|
|
|
|
sister->color=usrtc_black;
|
|
|
|
sister->color = usrtc_black;
|
|
|
|
parent->color=usrtc_red;
|
|
|
|
parent->color = usrtc_red;
|
|
|
|
usrtc_tree_rotate_left(sister,parent);
|
|
|
|
usrtc_tree_rotate_left(sister, parent);
|
|
|
|
sister=parent->right;
|
|
|
|
sister = parent->right;
|
|
|
|
|
|
|
|
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(sister->left->color==usrtc_black && sister->right->color==usrtc_black) {
|
|
|
|
if((sister->left->color == usrtc_black) &&
|
|
|
|
sister->color=usrtc_red;
|
|
|
|
(sister->right->color == usrtc_black)) {
|
|
|
|
child=parent;
|
|
|
|
sister->color = usrtc_red;
|
|
|
|
} else {
|
|
|
|
child = parent;
|
|
|
|
if(sister->right->color==usrtc_black) {
|
|
|
|
} else {
|
|
|
|
if(sister->left->color!=usrtc_red)
|
|
|
|
if(sister->right->color == usrtc_black) {
|
|
|
|
return;
|
|
|
|
if(sister->left->color != usrtc_red)
|
|
|
|
|
|
|
|
return;
|
|
|
|
sister->left->color=usrtc_black;
|
|
|
|
|
|
|
|
sister->color=usrtc_red;
|
|
|
|
sister->left->color = usrtc_black;
|
|
|
|
usrtc_tree_rotate_right(sister->left,sister);
|
|
|
|
sister->color = usrtc_red;
|
|
|
|
sister=parent->right;
|
|
|
|
usrtc_tree_rotate_right(sister->left, sister);
|
|
|
|
|
|
|
|
sister = parent->right;
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
|
|
|
|
return;
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
sister->color=parent->color;
|
|
|
|
}
|
|
|
|
sister->right->color=usrtc_black;
|
|
|
|
sister->color = parent->color;
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
sister->right->color = usrtc_black;
|
|
|
|
usrtc_tree_rotate_left(sister,parent);
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
break;
|
|
|
|
usrtc_tree_rotate_left(sister, parent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
} else { /*!child == child->parent->right*/
|
|
|
|
} else { /*!child == child->parent->right*/
|
|
|
|
if(child!=parent->right)
|
|
|
|
if(child != parent->right)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
sister=parent->left;
|
|
|
|
sister = parent->left;
|
|
|
|
|
|
|
|
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if(sister->color==usrtc_red) {
|
|
|
|
if(sister->color == usrtc_red) {
|
|
|
|
sister->color=usrtc_black;
|
|
|
|
sister->color = usrtc_black;
|
|
|
|
parent->color=usrtc_red;
|
|
|
|
parent->color = usrtc_red;
|
|
|
|
usrtc_tree_rotate_right(sister,parent);
|
|
|
|
usrtc_tree_rotate_right(sister, parent);
|
|
|
|
sister = parent->left;
|
|
|
|
sister = parent->left;
|
|
|
|
|
|
|
|
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(sister->right->color==usrtc_black && sister->left->color==usrtc_black) {
|
|
|
|
if((sister->right->color == usrtc_black) &&
|
|
|
|
sister->color=usrtc_red;
|
|
|
|
(sister->left->color == usrtc_black)) {
|
|
|
|
child=parent;
|
|
|
|
sister->color = usrtc_red;
|
|
|
|
} else {
|
|
|
|
child = parent;
|
|
|
|
if(sister->left->color == usrtc_black) {
|
|
|
|
} else {
|
|
|
|
if(sister->right->color!=usrtc_red)
|
|
|
|
if(sister->left->color == usrtc_black) {
|
|
|
|
return;
|
|
|
|
if(sister->right->color != usrtc_red)
|
|
|
|
|
|
|
|
return;
|
|
|
|
sister->right->color=usrtc_black;
|
|
|
|
|
|
|
|
sister->color=usrtc_red;
|
|
|
|
sister->right->color = usrtc_black;
|
|
|
|
usrtc_tree_rotate_left(sister->right, sister);
|
|
|
|
sister->color = usrtc_red;
|
|
|
|
sister=parent->left;
|
|
|
|
usrtc_tree_rotate_left(sister->right, sister);
|
|
|
|
|
|
|
|
sister = parent->left;
|
|
|
|
if(sister==tree_null_priv(us))
|
|
|
|
|
|
|
|
return;
|
|
|
|
if(sister == tree_null_priv(us))
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
sister->color=parent->color;
|
|
|
|
}
|
|
|
|
sister->left->color=usrtc_black;
|
|
|
|
sister->color = parent->color;
|
|
|
|
parent->color=usrtc_black;
|
|
|
|
sister->left->color = usrtc_black;
|
|
|
|
usrtc_tree_rotate_right(sister,parent);
|
|
|
|
parent->color = usrtc_black;
|
|
|
|
break;
|
|
|
|
usrtc_tree_rotate_right(sister, parent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
child->color=usrtc_black;
|
|
|
|
child->color = usrtc_black;
|
|
|
|
tree_root_priv(us)->color=usrtc_black;
|
|
|
|
tree_root_priv(us)->color = usrtc_black;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void redblack_convert_from_list(usrtc_t *us)
|
|
|
|
static void redblack_convert_from_list(usrtc_t *us)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
usrtc_node_t *tree[TREE_DEPTH_MAX] = { NULL };
|
|
|
|
usrtc_node_t *tree[TREE_DEPTH_MAX] = { NULL };
|
|
|
|
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;
|
|
|
|
unsigned int baselevel=0;
|
|
|
|
unsigned int baselevel = 0;
|
|
|
|
unsigned int level=0;
|
|
|
|
unsigned int level = 0;
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
|
|
if(usrtc_red!=0 && usrtc_black!=1)
|
|
|
|
if((usrtc_red!=0) && (usrtc_black != 1))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
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]=NULL;
|
|
|
|
tree[0] = NULL;
|
|
|
|
complete->right=nil;
|
|
|
|
complete->right = nil;
|
|
|
|
while(tree[level]!=0) {
|
|
|
|
while(tree[level] != 0) {
|
|
|
|
tree[level]->right=complete;
|
|
|
|
tree[level]->right = complete;
|
|
|
|
complete->parent=tree[level];
|
|
|
|
complete->parent = tree[level];
|
|
|
|
complete=tree[level];
|
|
|
|
complete = tree[level];
|
|
|
|
tree[level++]=NULL;
|
|
|
|
tree[level++] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(complete==NULL) {
|
|
|
|
if(complete == NULL) {
|
|
|
|
curr->left=nil;
|
|
|
|
curr->left = nil;
|
|
|
|
curr->right=nil;
|
|
|
|
curr->right = nil;
|
|
|
|
curr->color=level%2;
|
|
|
|
curr->color = level%2;
|
|
|
|
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++]=NULL;
|
|
|
|
tree[level++] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
curr->left=complete;
|
|
|
|
curr->left = complete;
|
|
|
|
curr->color=(level+1)%2;
|
|
|
|
curr->color = (level + 1)%2;
|
|
|
|
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;
|
|
|
|
nil->color=usrtc_black;
|
|
|
|
nil->color = usrtc_black;
|
|
|
|
complete->parent=nil;
|
|
|
|
complete->parent = nil;
|
|
|
|
complete->color=usrtc_black;
|
|
|
|
complete->color = usrtc_black;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|