separate make pair

This commit is contained in:
hugogogo
2022-07-01 11:56:23 +02:00
parent ae646ee58a
commit 6e230109db
10 changed files with 104 additions and 1119 deletions

View File

@@ -196,27 +196,6 @@ 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;
@@ -224,52 +203,13 @@ MP_TPL void MP::
{
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;
}
@@ -299,44 +239,6 @@ MP_TPL void MP::
_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;
if (!(n->left && n->right))
{
if (n->left)
n_del = _shift_nodes(n, n->left);
else if (n->right)
n_del = _shift_nodes(n, n->right);
else
n_del = _shift_nodes(n, NULL);
}
else
{
next = n->right->min();
if (next->up != n)
{
_shift_nodes(next, next->right);
next->right = n->right;
next->right->up = next;
}
n_del = _shift_nodes(n, next);
next->left = n->left;
next->left->up = next;
}
_allocator_node.destroy(n);
_allocator_node.deallocate(n, 1);
_size--;
_balance(n_del, ERASE);
*/
}
MP_TPL void MP::
erase(iterator first, iterator last) {
@@ -514,81 +416,6 @@ MP_TPL typename MP::allocator_type MP::
/*********************
* private functions :
*********************/
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)
{
_root = n_new;
_sentinel->child = _root;
}
else if (n_old == p->left)
p->left = n_new;
else
p->right = n_new;
if (n_new == NULL)
return (p);
n_new->up = p;
return (n_new);
}
MP_TPL void MP::
_balance(node<value_type>* n, bool action) {