small changes in map insert and erase
This commit is contained in:
BIN
containers_ft
BIN
containers_ft
Binary file not shown.
@@ -172,11 +172,9 @@ private:
|
|||||||
typename Alloc::template rebind< node<value_type> >::other _allocator_node;
|
typename Alloc::template rebind< node<value_type> >::other _allocator_node;
|
||||||
typename Alloc::template rebind< sentinel<value_type> >::other _allocator_sentinel;
|
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
|
// BBST
|
||||||
enum {INSERT, ERASE};
|
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);
|
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);
|
||||||
|
|||||||
@@ -83,8 +83,8 @@ public:
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
node<value_type>* getNode() { return _node; }
|
node<value_type>* get_node() { return _node; }
|
||||||
const node<value_type>* getNode() const { return _node; }
|
const node<value_type>* get_node() const { return _node; }
|
||||||
const sentinel<value_type>* getSentinel() const { return _sentinel; }
|
const sentinel<value_type>* getSentinel() const { return _sentinel; }
|
||||||
|
|
||||||
friend bool operator==(const self &lhs, const self &rhs) {
|
friend bool operator==(const self &lhs, const self &rhs) {
|
||||||
@@ -120,7 +120,7 @@ public:
|
|||||||
const sentinel<value_type>* sentinel)
|
const sentinel<value_type>* sentinel)
|
||||||
: _node(node), _sentinel(sentinel) {}
|
: _node(node), _sentinel(sentinel) {}
|
||||||
map_const_iterator (const map_iterator< Key, T, Compare, Allocator >& src)
|
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 {
|
reference operator*() const {
|
||||||
return _node->value; }
|
return _node->value; }
|
||||||
@@ -175,8 +175,7 @@ public:
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
node<value_type>* getNode() const {
|
node<value_type>* get_node() const { return _node; }
|
||||||
return _node; }
|
|
||||||
|
|
||||||
friend bool operator==(const self &lhs, const self &rhs) {
|
friend bool operator==(const self &lhs, const self &rhs) {
|
||||||
return lhs._node == rhs._node; }
|
return lhs._node == rhs._node; }
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ MP_TPL MP::
|
|||||||
, _comp(comp)
|
, _comp(comp)
|
||||||
, _allocator(alloc) {
|
, _allocator(alloc) {
|
||||||
|
|
||||||
_init_sentinel();
|
_sentinel = _allocator_sentinel.allocate(1);
|
||||||
|
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
|
||||||
}
|
}
|
||||||
MP_TPL template < typename InputIt > MP::
|
MP_TPL template < typename InputIt > MP::
|
||||||
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
|
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)
|
, _comp(comp)
|
||||||
, _allocator(alloc) {
|
, _allocator(alloc) {
|
||||||
|
|
||||||
_init_sentinel();
|
_sentinel = _allocator_sentinel.allocate(1);
|
||||||
|
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
|
||||||
insert(first, last);
|
insert(first, last);
|
||||||
}
|
}
|
||||||
MP_TPL MP::
|
MP_TPL MP::
|
||||||
@@ -35,7 +37,8 @@ MP_TPL MP::
|
|||||||
, _comp(src._comp)
|
, _comp(src._comp)
|
||||||
, _allocator(src._allocator) {
|
, _allocator(src._allocator) {
|
||||||
|
|
||||||
_init_sentinel();
|
_sentinel = _allocator_sentinel.allocate(1);
|
||||||
|
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
|
||||||
*this = src;
|
*this = src;
|
||||||
}
|
}
|
||||||
// destructor --------------------------------
|
// destructor --------------------------------
|
||||||
@@ -128,7 +131,7 @@ MP_TPL typename MP::mapped_type& MP::
|
|||||||
return (n->value.second);
|
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);
|
return (n->value.second);
|
||||||
}
|
}
|
||||||
@@ -193,33 +196,33 @@ MP_TPL template < typename InputIt > void MP::
|
|||||||
MP_TPL void MP::
|
MP_TPL void MP::
|
||||||
erase(iterator pos) {
|
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>* n_del = NULL;
|
||||||
node<value_type>* next;
|
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();
|
next = n->right->min();
|
||||||
|
|
||||||
if (next->up != n)
|
if (next->up != n)
|
||||||
{
|
{
|
||||||
_subtree_shift(next, next->right);
|
_swap_nodes(next, next->right);
|
||||||
next->right = n->right;
|
next->right = n->right;
|
||||||
next->right->up = next;
|
next->right->up = next;
|
||||||
}
|
}
|
||||||
n_del = _subtree_shift(n, next);
|
n_del = _swap_nodes(n, next);
|
||||||
next->left = n->left;
|
next->left = n->left;
|
||||||
next->left->up = next;
|
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.destroy(n);
|
||||||
_allocator_node.deallocate(n, 1);
|
_allocator_node.deallocate(n, 1);
|
||||||
@@ -403,14 +406,8 @@ MP_TPL typename MP::allocator_type MP::
|
|||||||
/*********************
|
/*********************
|
||||||
* private functions :
|
* 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::
|
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;
|
node<value_type>* p = n_old->up;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user