diff --git a/containers_ft b/containers_ft index 2f16785..a72348c 100755 Binary files a/containers_ft and b/containers_ft differ diff --git a/headers/map.hpp b/headers/map.hpp index 77bbe86..e54e199 100644 --- a/headers/map.hpp +++ b/headers/map.hpp @@ -172,11 +172,9 @@ private: typename Alloc::template rebind< node >::other _allocator_node; typename Alloc::template rebind< sentinel >::other _allocator_sentinel; - void _init_sentinel(); - node* _subtree_shift(node* st_old, node* st_new); - // BBST enum {INSERT, ERASE}; + node* _swap_nodes(node* st_old, node* st_new); void _balance(node* n, bool action); short _compute_height(node* n); short _balance_factor(node* n); diff --git a/headers/map_iterator.hpp b/headers/map_iterator.hpp index 9903529..98a729c 100644 --- a/headers/map_iterator.hpp +++ b/headers/map_iterator.hpp @@ -83,8 +83,8 @@ public: return old; } - node* getNode() { return _node; } - const node* getNode() const { return _node; } + node* get_node() { return _node; } + const node* get_node() const { return _node; } const sentinel* getSentinel() const { return _sentinel; } friend bool operator==(const self &lhs, const self &rhs) { @@ -120,7 +120,7 @@ public: const sentinel* 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* getNode() const { - return _node; } + node* get_node() const { return _node; } friend bool operator==(const self &lhs, const self &rhs) { return lhs._node == rhs._node; } diff --git a/templates/map.tpp b/templates/map.tpp index 37838a0..d9aff63 100644 --- a/templates/map.tpp +++ b/templates/map.tpp @@ -16,7 +16,8 @@ MP_TPL MP:: , _comp(comp) , _allocator(alloc) { - _init_sentinel(); + _sentinel = _allocator_sentinel.allocate(1); + _allocator_sentinel.construct(_sentinel, sentinel()); } 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()); 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()); *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* n = pos.getNode(); + node* n = pos.get_node(); node* n_del = NULL; node* 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()); -} MP_TPL node* MP:: - _subtree_shift(node* n_old, node* n_new) { + _swap_nodes(node* n_old, node* n_new) { node* p = n_old->up;