new version of map erase
This commit is contained in:
@@ -175,6 +175,7 @@ private:
|
|||||||
// BBST
|
// BBST
|
||||||
enum {INSERT, ERASE};
|
enum {INSERT, ERASE};
|
||||||
node<value_type>* _swap_nodes(node<value_type>* st_old, node<value_type>* st_new);
|
node<value_type>* _swap_nodes(node<value_type>* st_old, node<value_type>* st_new);
|
||||||
|
node<value_type>* _shift_nodes(node<value_type>* st_old, node<value_type>* st_new);
|
||||||
void _balance(node<value_type>* n, bool action);
|
void _balance(node<value_type>* n, bool action);
|
||||||
short _compute_height(node<value_type>* n);
|
short _compute_height(node<value_type>* n);
|
||||||
short _balance_factor(node<value_type>* n);
|
short _balance_factor(node<value_type>* n);
|
||||||
|
|||||||
@@ -196,6 +196,113 @@ MP_TPL template < typename InputIt > void MP::
|
|||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
erase(iterator pos) {
|
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 = pos.get_node();
|
||||||
node<value_type>* n_del = NULL;
|
node<value_type>* n_del = NULL;
|
||||||
node<value_type>* next;
|
node<value_type>* next;
|
||||||
@@ -203,11 +310,11 @@ MP_TPL void MP::
|
|||||||
if (!(n->left && n->right))
|
if (!(n->left && n->right))
|
||||||
{
|
{
|
||||||
if (n->left)
|
if (n->left)
|
||||||
n_del = _swap_nodes(n, n->left);
|
n_del = _shift_nodes(n, n->left);
|
||||||
else if (n->right)
|
else if (n->right)
|
||||||
n_del = _swap_nodes(n, n->right);
|
n_del = _shift_nodes(n, n->right);
|
||||||
else
|
else
|
||||||
n_del = _swap_nodes(n, NULL);
|
n_del = _shift_nodes(n, NULL);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -215,11 +322,11 @@ MP_TPL void MP::
|
|||||||
|
|
||||||
if (next->up != n)
|
if (next->up != n)
|
||||||
{
|
{
|
||||||
_swap_nodes(next, next->right);
|
_shift_nodes(next, next->right);
|
||||||
next->right = n->right;
|
next->right = n->right;
|
||||||
next->right->up = next;
|
next->right->up = next;
|
||||||
}
|
}
|
||||||
n_del = _swap_nodes(n, next);
|
n_del = _shift_nodes(n, next);
|
||||||
next->left = n->left;
|
next->left = n->left;
|
||||||
next->left->up = next;
|
next->left->up = next;
|
||||||
}
|
}
|
||||||
@@ -229,6 +336,7 @@ MP_TPL void MP::
|
|||||||
_size--;
|
_size--;
|
||||||
|
|
||||||
_balance(n_del, ERASE);
|
_balance(n_del, ERASE);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
erase(iterator first, iterator last) {
|
erase(iterator first, iterator last) {
|
||||||
@@ -409,6 +517,61 @@ MP_TPL typename MP::allocator_type MP::
|
|||||||
MP_TPL node<typename MP::value_type>* MP::
|
MP_TPL node<typename MP::value_type>* MP::
|
||||||
_swap_nodes(node<value_type>* n_old, node<value_type>* n_new) {
|
_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;
|
node<value_type>* p = n_old->up;
|
||||||
|
|
||||||
if (n_old == _root)
|
if (n_old == _root)
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ TEST_M(tests_map_operator_assignation)
|
|||||||
PRINT(first)
|
PRINT(first)
|
||||||
PRINT(second)
|
PRINT(second)
|
||||||
|
|
||||||
second=first; // second now contains 3 ints
|
second=first; // second now contains 3 ints
|
||||||
first=ft::map<T,U>(); // and first is now empty
|
first=ft::map<T,U>(); // and first is now empty
|
||||||
|
|
||||||
std::cout << "Size of first: " << first.size() << '\n';
|
std::cout << "Size of first: " << first.size() << '\n';
|
||||||
std::cout << "Size of second: " << second.size() << '\n';
|
std::cout << "Size of second: " << second.size() << '\n';
|
||||||
@@ -287,6 +287,122 @@ TEST_M(tests_map_erase)
|
|||||||
|
|
||||||
PRINT(mymap)
|
PRINT(mymap)
|
||||||
|
|
||||||
|
|
||||||
|
// title
|
||||||
|
TITLE(more complexe tree)
|
||||||
|
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
mymap[VALT('e')]=VALU(50);
|
||||||
|
mymap[VALT('g')]=VALU(24);
|
||||||
|
mymap[VALT('h')]=VALU(64);
|
||||||
|
mymap[VALT('i')]=VALU(52);
|
||||||
|
mymap[VALT('j')]=VALU(14);
|
||||||
|
mymap[VALT('k')]=VALU(12);
|
||||||
|
mymap[VALT('l')]=VALU(46);
|
||||||
|
mymap[VALT('m')]=VALU(37);
|
||||||
|
mymap[VALT('n')]=VALU(31);
|
||||||
|
mymap[VALT('o')]=VALU(58);
|
||||||
|
mymap[VALT('p')]=VALU(18);
|
||||||
|
mymap[VALT('q')]=VALU(25);
|
||||||
|
mymap[VALT('r')]=VALU(36);
|
||||||
|
mymap[VALT('s')]=VALU(43);
|
||||||
|
mymap[VALT('t')]=VALU(49);
|
||||||
|
mymap[VALT('u')]=VALU(21);
|
||||||
|
mymap[VALT('v')]=VALU(55);
|
||||||
|
mymap[VALT('w')]=VALU(33);
|
||||||
|
mymap[VALT('x')]=VALU(44);
|
||||||
|
mymap[VALT('y')]=VALU(11);
|
||||||
|
mymap[VALT('z')]=VALU(22);
|
||||||
|
|
||||||
|
mymap.erase (VALT('a'));
|
||||||
|
mymap[VALT('a')]=VALU(10);
|
||||||
|
mymap.erase (VALT('b'));
|
||||||
|
mymap[VALT('b')]=VALU(20);
|
||||||
|
mymap.erase (VALT('c'));
|
||||||
|
mymap[VALT('c')]=VALU(30);
|
||||||
|
mymap.erase (VALT('d'));
|
||||||
|
mymap[VALT('d')]=VALU(40);
|
||||||
|
mymap.erase (VALT('e'));
|
||||||
|
mymap[VALT('e')]=VALU(50);
|
||||||
|
mymap.erase (VALT('f'));
|
||||||
|
mymap[VALT('f')]=VALU(60);
|
||||||
|
mymap.erase (VALT('g'));
|
||||||
|
mymap[VALT('g')]=VALU(24);
|
||||||
|
mymap.erase (VALT('h'));
|
||||||
|
mymap[VALT('h')]=VALU(64);
|
||||||
|
mymap.erase (VALT('i'));
|
||||||
|
mymap[VALT('i')]=VALU(52);
|
||||||
|
mymap.erase (VALT('j'));
|
||||||
|
mymap[VALT('j')]=VALU(14);
|
||||||
|
mymap.erase (VALT('k'));
|
||||||
|
mymap[VALT('k')]=VALU(12);
|
||||||
|
mymap.erase (VALT('l'));
|
||||||
|
mymap[VALT('l')]=VALU(46);
|
||||||
|
mymap.erase (VALT('m'));
|
||||||
|
mymap[VALT('m')]=VALU(37);
|
||||||
|
mymap.erase (VALT('n'));
|
||||||
|
mymap[VALT('n')]=VALU(31);
|
||||||
|
mymap.erase (VALT('o'));
|
||||||
|
mymap[VALT('o')]=VALU(58);
|
||||||
|
mymap.erase (VALT('p'));
|
||||||
|
mymap[VALT('p')]=VALU(18);
|
||||||
|
mymap.erase (VALT('q'));
|
||||||
|
mymap[VALT('q')]=VALU(25);
|
||||||
|
mymap.erase (VALT('r'));
|
||||||
|
mymap[VALT('r')]=VALU(36);
|
||||||
|
mymap.erase (VALT('s'));
|
||||||
|
mymap[VALT('s')]=VALU(43);
|
||||||
|
mymap.erase (VALT('t'));
|
||||||
|
mymap[VALT('t')]=VALU(49);
|
||||||
|
mymap.erase (VALT('u'));
|
||||||
|
mymap[VALT('u')]=VALU(21);
|
||||||
|
mymap.erase (VALT('v'));
|
||||||
|
mymap[VALT('v')]=VALU(55);
|
||||||
|
mymap.erase (VALT('w'));
|
||||||
|
mymap[VALT('w')]=VALU(33);
|
||||||
|
mymap.erase (VALT('x'));
|
||||||
|
mymap[VALT('x')]=VALU(44);
|
||||||
|
mymap.erase (VALT('y'));
|
||||||
|
mymap[VALT('y')]=VALU(11);
|
||||||
|
mymap.erase (VALT('z'));
|
||||||
|
mymap[VALT('z')]=VALU(22);
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
|
|
||||||
|
// title
|
||||||
|
TITLE(erasing entire tree)
|
||||||
|
|
||||||
|
mymap.erase (VALT('a'));
|
||||||
|
mymap.erase (VALT('b'));
|
||||||
|
mymap.erase (VALT('c'));
|
||||||
|
mymap.erase (VALT('d'));
|
||||||
|
mymap.erase (VALT('e'));
|
||||||
|
mymap.erase (VALT('f'));
|
||||||
|
mymap.erase (VALT('g'));
|
||||||
|
mymap.erase (VALT('h'));
|
||||||
|
mymap.erase (VALT('i'));
|
||||||
|
mymap.erase (VALT('j'));
|
||||||
|
mymap.erase (VALT('k'));
|
||||||
|
mymap.erase (VALT('l'));
|
||||||
|
mymap.erase (VALT('m'));
|
||||||
|
mymap.erase (VALT('n'));
|
||||||
|
mymap.erase (VALT('o'));
|
||||||
|
mymap.erase (VALT('p'));
|
||||||
|
mymap.erase (VALT('q'));
|
||||||
|
mymap.erase (VALT('r'));
|
||||||
|
mymap.erase (VALT('s'));
|
||||||
|
mymap.erase (VALT('t'));
|
||||||
|
mymap.erase (VALT('u'));
|
||||||
|
mymap.erase (VALT('v'));
|
||||||
|
mymap.erase (VALT('w'));
|
||||||
|
mymap.erase (VALT('x'));
|
||||||
|
mymap.erase (VALT('y'));
|
||||||
|
mymap.erase (VALT('z'));
|
||||||
|
|
||||||
|
PRINT(mymap)
|
||||||
|
|
||||||
DELETE
|
DELETE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user