small changes in map insert and erase

This commit is contained in:
hugogogo
2022-06-29 14:39:36 +02:00
parent b0b63d6238
commit eeb3c7dfc0
4 changed files with 26 additions and 32 deletions

Binary file not shown.

View File

@@ -172,11 +172,9 @@ private:
typename Alloc::template rebind< node<value_type> >::other _allocator_node;
typename Alloc::template rebind< sentinel<value_type> >::other _allocator_sentinel;
void _init_sentinel();
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
// BBST
enum {INSERT, ERASE};
node<value_type>* _swap_nodes(node<value_type>* st_old, node<value_type>* st_new);
void _balance(node<value_type>* n, bool action);
short _compute_height(node<value_type>* n);
short _balance_factor(node<value_type>* n);

View File

@@ -83,8 +83,8 @@ public:
return old;
}
node<value_type>* getNode() { return _node; }
const node<value_type>* getNode() const { return _node; }
node<value_type>* get_node() { return _node; }
const node<value_type>* get_node() const { return _node; }
const sentinel<value_type>* getSentinel() const { return _sentinel; }
friend bool operator==(const self &lhs, const self &rhs) {
@@ -120,7 +120,7 @@ public:
const sentinel<value_type>* sentinel)
: _node(node), _sentinel(sentinel) {}
map_const_iterator (const map_iterator< Key, T, Compare, Allocator >& src)
: _node(src.getNode()), _sentinel(src.getSentinel()) {}
: _node(src.get_node()), _sentinel(src.getSentinel()) {}
reference operator*() const {
return _node->value; }
@@ -175,8 +175,7 @@ public:
return old;
}
node<value_type>* getNode() const {
return _node; }
node<value_type>* get_node() const { return _node; }
friend bool operator==(const self &lhs, const self &rhs) {
return lhs._node == rhs._node; }

View File

@@ -16,7 +16,8 @@ MP_TPL MP::
, _comp(comp)
, _allocator(alloc) {
_init_sentinel();
_sentinel = _allocator_sentinel.allocate(1);
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
}
MP_TPL template < typename InputIt > MP::
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
@@ -25,7 +26,8 @@ MP_TPL template < typename InputIt > MP::
, _comp(comp)
, _allocator(alloc) {
_init_sentinel();
_sentinel = _allocator_sentinel.allocate(1);
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
insert(first, last);
}
MP_TPL MP::
@@ -35,7 +37,8 @@ MP_TPL MP::
, _comp(src._comp)
, _allocator(src._allocator) {
_init_sentinel();
_sentinel = _allocator_sentinel.allocate(1);
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
*this = src;
}
// destructor --------------------------------
@@ -128,7 +131,7 @@ MP_TPL typename MP::mapped_type& MP::
return (n->value.second);
}
n = insert( ft::make_pair(key, mapped_type()) ).first.getNode();
n = insert( ft::make_pair(key, mapped_type()) ).first.get_node();
return (n->value.second);
}
@@ -193,33 +196,33 @@ MP_TPL template < typename InputIt > void MP::
MP_TPL void MP::
erase(iterator pos) {
node<value_type>* n = pos.getNode();
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->right))
{
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);
}
else
{
next = n->right->min();
if (next->up != n)
{
_subtree_shift(next, next->right);
_swap_nodes(next, next->right);
next->right = n->right;
next->right->up = next;
}
n_del = _subtree_shift(n, next);
n_del = _swap_nodes(n, next);
next->left = n->left;
next->left->up = next;
}
else
{
if (n->left)
n_del = _subtree_shift(n, n->left);
else if (n->right)
n_del = _subtree_shift(n, n->right);
else
n_del = _subtree_shift(n, NULL);
}
_allocator_node.destroy(n);
_allocator_node.deallocate(n, 1);
@@ -403,14 +406,8 @@ MP_TPL typename MP::allocator_type MP::
/*********************
* private functions :
*********************/
MP_TPL void MP::
_init_sentinel() {
_sentinel = _allocator_sentinel.allocate(1);
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
}
MP_TPL node<typename MP::value_type>* MP::
_subtree_shift(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>* p = n_old->up;