new version of map erase
This commit is contained in:
@@ -196,6 +196,113 @@ MP_TPL template < typename InputIt > void MP::
|
||||
MP_TPL void MP::
|
||||
erase(iterator pos) {
|
||||
|
||||
/* version 1 -- NOT WORKING
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* n_del = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
_swap_nodes(n, n->right->min());
|
||||
if (n->left)
|
||||
n_del = _swap_nodes(n, n->left);
|
||||
else if (n->right)
|
||||
n_del = _swap_nodes(n, n->right);
|
||||
else
|
||||
n_del = _swap_nodes(n, NULL);
|
||||
|
||||
_allocator_node.destroy(n_del);
|
||||
_allocator_node.deallocate(n_del, 1);
|
||||
_size--;
|
||||
|
||||
_balance(n_del, ERASE);
|
||||
*/
|
||||
|
||||
/* version 2 -- WORKING
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* replace = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
{
|
||||
replace = n->right->min();
|
||||
|
||||
if (replace->up != n)
|
||||
{
|
||||
_shift_nodes(replace, replace->right);
|
||||
replace->right = n->right;
|
||||
replace->right->up = replace;
|
||||
}
|
||||
_shift_nodes(n, replace);
|
||||
replace->left = n->left;
|
||||
replace->left->up = replace;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->left)
|
||||
replace = n->left;
|
||||
else if (n->right)
|
||||
replace = n->right;
|
||||
else
|
||||
replace = NULL;
|
||||
_shift_nodes(n, replace);
|
||||
}
|
||||
|
||||
_allocator_node.destroy(n);
|
||||
_allocator_node.deallocate(n, 1);
|
||||
_size--;
|
||||
|
||||
_balance(replace, ERASE);
|
||||
*/
|
||||
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* replace = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
{
|
||||
replace = n->right->min();
|
||||
|
||||
// change connection replace->left with n->left
|
||||
replace->left = n->left;
|
||||
replace->left->up = replace;
|
||||
// if replace and n are not connexes
|
||||
if (replace->up != n)
|
||||
{
|
||||
// connecte replace->right with replace->up
|
||||
replace->up->left = replace->right;
|
||||
if (replace->right != NULL)
|
||||
replace->right->up = replace->up;
|
||||
// change connection replace->right with n->right
|
||||
replace->right = n->right;
|
||||
replace->right->up = replace;
|
||||
}
|
||||
}
|
||||
else if (n->left)
|
||||
replace = n->left;
|
||||
else if (n->right)
|
||||
replace = n->right;
|
||||
else
|
||||
replace = NULL;
|
||||
|
||||
// share parents from n to replace
|
||||
if (n == _root)
|
||||
{
|
||||
_root = replace;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
else if (n == n->up->left)
|
||||
n->up->left = replace;
|
||||
else
|
||||
n->up->right = replace;
|
||||
if (replace != NULL)
|
||||
replace->up = n->up;
|
||||
|
||||
_allocator_node.destroy(n);
|
||||
_allocator_node.deallocate(n, 1);
|
||||
_size--;
|
||||
|
||||
_balance(replace, ERASE);
|
||||
/* version 3 -- WIP
|
||||
*/
|
||||
|
||||
/* version 0 -- WORKING
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* n_del = NULL;
|
||||
node<value_type>* next;
|
||||
@@ -203,11 +310,11 @@ MP_TPL void MP::
|
||||
if (!(n->left && n->right))
|
||||
{
|
||||
if (n->left)
|
||||
n_del = _swap_nodes(n, n->left);
|
||||
n_del = _shift_nodes(n, n->left);
|
||||
else if (n->right)
|
||||
n_del = _swap_nodes(n, n->right);
|
||||
n_del = _shift_nodes(n, n->right);
|
||||
else
|
||||
n_del = _swap_nodes(n, NULL);
|
||||
n_del = _shift_nodes(n, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -215,11 +322,11 @@ MP_TPL void MP::
|
||||
|
||||
if (next->up != n)
|
||||
{
|
||||
_swap_nodes(next, next->right);
|
||||
_shift_nodes(next, next->right);
|
||||
next->right = n->right;
|
||||
next->right->up = next;
|
||||
}
|
||||
n_del = _swap_nodes(n, next);
|
||||
n_del = _shift_nodes(n, next);
|
||||
next->left = n->left;
|
||||
next->left->up = next;
|
||||
}
|
||||
@@ -229,6 +336,7 @@ MP_TPL void MP::
|
||||
_size--;
|
||||
|
||||
_balance(n_del, ERASE);
|
||||
*/
|
||||
}
|
||||
MP_TPL void MP::
|
||||
erase(iterator first, iterator last) {
|
||||
@@ -409,6 +517,61 @@ MP_TPL typename MP::allocator_type MP::
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_swap_nodes(node<value_type>* n_old, node<value_type>* n_new) {
|
||||
|
||||
node<value_type>* tmp;
|
||||
// tmp = _allocator_node.allocate(1);
|
||||
// _allocator_node.construct(tmp, node<value_type>(n_old->value));
|
||||
|
||||
tmp = n_old->up;
|
||||
n_old->up = n_new->up;
|
||||
n_new->up = tmp;
|
||||
|
||||
tmp = n_old->right;
|
||||
n_old->right = n_new->right;
|
||||
n_new->right = tmp;
|
||||
|
||||
tmp = n_old->left;
|
||||
n_old->left = n_new->left;
|
||||
n_new->left = tmp;
|
||||
|
||||
// n_old
|
||||
tmp = n_old->up;
|
||||
if (tmp)
|
||||
{
|
||||
if (tmp->left == n_new)
|
||||
tmp->left = n_old;
|
||||
else
|
||||
tmp->right = n_old;
|
||||
}
|
||||
tmp = n_old->left;
|
||||
if (tmp)
|
||||
tmp->up = n_old;
|
||||
tmp = n_old->right;
|
||||
if (tmp)
|
||||
tmp->up = n_old;
|
||||
|
||||
// n_new
|
||||
tmp = n_new->up;
|
||||
if (tmp)
|
||||
{
|
||||
if (tmp->left == n_old)
|
||||
tmp->left = n_new;
|
||||
else
|
||||
tmp->right = n_new;
|
||||
}
|
||||
tmp = n_new->left;
|
||||
if (tmp)
|
||||
tmp->up = n_new;
|
||||
tmp = n_new->right;
|
||||
if (tmp)
|
||||
tmp->up = n_new;
|
||||
|
||||
// _allocator_node.destroy(tmp);
|
||||
// _allocator_node.deallocate(tmp, 1);
|
||||
return (n_old);
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_shift_nodes(node<value_type>* n_old, node<value_type>* n_new) {
|
||||
|
||||
node<value_type>* p = n_old->up;
|
||||
|
||||
if (n_old == _root)
|
||||
|
||||
Reference in New Issue
Block a user