stack almost good
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
|
||||
|
||||
#define MP_TPL template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
#define MP map<Key, T, Compare, Alloc>
|
||||
|
||||
#define MP_TPL template < typename Key, typename T, typename Compare, typename Allocator >
|
||||
#define MP map<Key, T, Compare, Allocator>
|
||||
|
||||
namespace ft {
|
||||
|
||||
@@ -13,38 +11,49 @@ namespace ft {
|
||||
// constructors ------------------------------
|
||||
MP_TPL MP::
|
||||
map (const key_compare & comp, const allocator_type & alloc)
|
||||
: _bst()
|
||||
, _allocator(alloc)
|
||||
, _comp(comp) {
|
||||
: _size(0)
|
||||
, _root(NULL)
|
||||
, _comp(comp)
|
||||
, _allocator(alloc) {
|
||||
|
||||
return;
|
||||
_init_sentinel();
|
||||
}
|
||||
MP_TPL template <class InputIt> MP::
|
||||
MP_TPL template < typename InputIt > MP::
|
||||
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
|
||||
: _bst(first, last)
|
||||
, _allocator(alloc)
|
||||
, _comp(comp) {
|
||||
: _size(0)
|
||||
, _root(NULL)
|
||||
, _comp(comp)
|
||||
, _allocator(alloc) {
|
||||
|
||||
_init_sentinel();
|
||||
insert(first, last);
|
||||
}
|
||||
MP_TPL MP::
|
||||
map (const map& x)
|
||||
: _bst()
|
||||
, _allocator(x._allocator)
|
||||
, _comp(x._comp) {
|
||||
map(const map& src)
|
||||
: _size(0)
|
||||
, _root(NULL)
|
||||
, _comp(src._comp)
|
||||
, _allocator(src._allocator) {
|
||||
|
||||
*this = x;
|
||||
_init_sentinel();
|
||||
*this = src;
|
||||
}
|
||||
// destructor --------------------------------
|
||||
MP_TPL MP::
|
||||
~map() { clear(); }
|
||||
~map() {
|
||||
|
||||
clear();
|
||||
_allocator_node_sentinel.destroy(_sentinel);
|
||||
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
||||
}
|
||||
// operator= ---------------------------------
|
||||
MP_TPL MP& MP::
|
||||
operator= (const map& x) {
|
||||
operator=(const map& rhs) {
|
||||
|
||||
if (this == &x)
|
||||
if (this == &rhs)
|
||||
return (*this);
|
||||
|
||||
map new_map(x.begin(), x.end());
|
||||
swap(new_map);
|
||||
map new_bst(rhs.begin(), rhs.end());
|
||||
swap(new_bst);
|
||||
return (*this);
|
||||
}
|
||||
|
||||
@@ -54,38 +63,50 @@ MP_TPL MP& MP::
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
MP_TPL typename MP::iterator MP::
|
||||
begin() { return (_bst.begin()); }
|
||||
begin() {
|
||||
|
||||
if (_root)
|
||||
return iterator(_root->min(), _sentinel);
|
||||
else
|
||||
return end();
|
||||
}
|
||||
MP_TPL typename MP::const_iterator MP::
|
||||
begin() const { return (_bst.begin()); }
|
||||
begin() const {
|
||||
|
||||
if (_root)
|
||||
return const_iterator(_root->min(), _sentinel);
|
||||
else
|
||||
return end();
|
||||
}
|
||||
// end ---------------------------------------
|
||||
MP_TPL typename MP::iterator MP::
|
||||
end() { return (_bst.end()); }
|
||||
end() { return iterator(NULL, _sentinel); }
|
||||
MP_TPL typename MP::const_iterator MP::
|
||||
end() const { return (_bst.end()); }
|
||||
end() const { return const_iterator(NULL, _sentinel); }
|
||||
// rbegin ------------------------------------
|
||||
MP_TPL typename MP::reverse_iterator MP::
|
||||
rbegin() { return (_bst.rbegin()); }
|
||||
rbegin() { return reverse_iterator(end()); }
|
||||
MP_TPL typename MP::const_reverse_iterator MP::
|
||||
rbegin() const { return (_bst.rbegin()); }
|
||||
rbegin() const { return const_reverse_iterator(end()); }
|
||||
// rend --------------------------------------
|
||||
MP_TPL typename MP::reverse_iterator MP::
|
||||
rend() { return (_bst.rend()); }
|
||||
rend() { return reverse_iterator(begin()); }
|
||||
MP_TPL typename MP::const_reverse_iterator MP::
|
||||
rend() const { return (_bst.rend()); }
|
||||
rend() const { return const_reverse_iterator(begin()); }
|
||||
|
||||
|
||||
/************
|
||||
* capacity :
|
||||
************/
|
||||
// empty -------------------------------------
|
||||
MP_TPL bool MP::
|
||||
empty() const { return (_bst.empty()); }
|
||||
MP_TPL bool MP::
|
||||
empty() const { return (_size == 0); }
|
||||
// size --------------------------------------
|
||||
MP_TPL typename MP::size_type MP::
|
||||
size() const { return (_bst.size()); }
|
||||
size() const { return (_size); }
|
||||
// max_size ----------------------------------
|
||||
MP_TPL typename MP::size_type MP::
|
||||
max_size() const { return (_bst.max_size()); }
|
||||
max_size() const { return ( _allocator_node.max_size() ); }
|
||||
|
||||
|
||||
/******************
|
||||
@@ -93,7 +114,24 @@ MP_TPL typename MP::size_type MP::
|
||||
******************/
|
||||
// operator[] --------------------------------
|
||||
MP_TPL typename MP::mapped_type& MP::
|
||||
operator[] (const key_type& k) { return _bst[k]; }
|
||||
operator[](const Key& key) {
|
||||
|
||||
node<value_type>* n = _root;
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (_comp(key, n->value.first))
|
||||
n = n->left;
|
||||
else if (_comp(n->value.first, key))
|
||||
n = n->right;
|
||||
else
|
||||
return (n->value.second);
|
||||
}
|
||||
|
||||
n = insert( ft::make_pair(key, mapped_type()) ).first.getNode();
|
||||
|
||||
return (n->value.second);
|
||||
}
|
||||
|
||||
|
||||
/*************
|
||||
@@ -101,35 +139,75 @@ MP_TPL typename MP::mapped_type& MP::
|
||||
*************/
|
||||
// insert ------------------------------------
|
||||
MP_TPL pair<typename MP::iterator, bool> MP::
|
||||
insert (const value_type& val) { return (_bst.insert(val)); }
|
||||
insert(const value_type& value) {
|
||||
|
||||
pair<typename MP::iterator, bool> ret;
|
||||
|
||||
ret = _insert(value);
|
||||
if (ret.second == true)
|
||||
_insert_rebalancing(ret.first.getNode()->up);
|
||||
return (ret);
|
||||
}
|
||||
MP_TPL typename MP::iterator MP::
|
||||
insert (iterator pos, const value_type& val) { return (_bst.insert(pos, val)); }
|
||||
MP_TPL template <class InputIt> void MP::
|
||||
insert (InputIt first, InputIt last) { return (_bst.insert(first, last)); }
|
||||
insert(iterator hint, const value_type& value) {
|
||||
|
||||
(void)hint;
|
||||
return insert(value).first;
|
||||
}
|
||||
MP_TPL template < typename InputIt > void MP::
|
||||
insert(InputIt first, InputIt last) {
|
||||
|
||||
while (first != last)
|
||||
{
|
||||
insert(*first);
|
||||
++first;
|
||||
}
|
||||
}
|
||||
// erase -------------------------------------
|
||||
MP_TPL void MP::
|
||||
erase (iterator pos) { return (_bst.erase(pos)); }
|
||||
MP_TPL typename MP::size_type MP::
|
||||
erase (const key_type& k) { return (_bst.erase(k)); }
|
||||
erase(iterator pos) {
|
||||
|
||||
node<value_type>* delete_point;
|
||||
delete_point = _erase(pos);
|
||||
_erase_rebalancing(delete_point);
|
||||
}
|
||||
MP_TPL void MP::
|
||||
erase (iterator first, iterator last) { return (_bst.erase(first, last)); }
|
||||
erase(iterator first, iterator last) {
|
||||
|
||||
while (first != last)
|
||||
erase(first++);
|
||||
}
|
||||
MP_TPL typename MP::size_type MP::
|
||||
erase(const Key& key) {
|
||||
|
||||
iterator pos = find(key);
|
||||
if (pos == end())
|
||||
return (0);
|
||||
else
|
||||
{
|
||||
erase(pos);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
// swap --------------------------------------
|
||||
MP_TPL void MP::
|
||||
swap (map& x) {
|
||||
swap(map& other) {
|
||||
|
||||
bst_map tmp;
|
||||
node<value_type>* tmp_root = _root;
|
||||
node_sentinel<value_type>* tmp_sentinel = _sentinel;
|
||||
size_type tmp_size = _size;
|
||||
|
||||
tmp.swap(_bst);
|
||||
_bst.swap(x._bst);
|
||||
x._bst.swap(tmp);
|
||||
_root = other._root;
|
||||
_sentinel = other._sentinel;
|
||||
_size = other._size;
|
||||
|
||||
other._root = tmp_root;
|
||||
other._sentinel = tmp_sentinel;
|
||||
other._size = tmp_size;
|
||||
}
|
||||
|
||||
// clear -------------------------------------
|
||||
MP_TPL void MP::
|
||||
clear() {
|
||||
|
||||
_bst.clear();
|
||||
}
|
||||
clear() { erase(begin(), end()); }
|
||||
|
||||
|
||||
/*************
|
||||
@@ -148,12 +226,46 @@ MP_TPL typename MP::value_compare MP::
|
||||
**************/
|
||||
// find --------------------------------------
|
||||
MP_TPL typename MP::iterator MP::
|
||||
find (const key_type& k) { return (_bst.find(k)); }
|
||||
find(const Key& key) {
|
||||
|
||||
node<value_type>* n = _root;
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (_comp(key, n->value.first))
|
||||
n = n->left;
|
||||
else if (_comp(n->value.first, key))
|
||||
n = n->right;
|
||||
else
|
||||
return (iterator(n, _sentinel));
|
||||
}
|
||||
return (end());
|
||||
}
|
||||
MP_TPL typename MP::const_iterator MP::
|
||||
find (const key_type& k) const { return (_bst.find(k)); }
|
||||
find(const Key& key) const {
|
||||
|
||||
node<value_type>* n = _root;
|
||||
|
||||
while (n)
|
||||
{
|
||||
if (_comp(key, n->value.first))
|
||||
n = n->left;
|
||||
else if (_comp(n->value.first, key))
|
||||
n = n->right;
|
||||
else
|
||||
return (const_iterator(n, _sentinel));
|
||||
}
|
||||
return (end());
|
||||
}
|
||||
// count -------------------------------------
|
||||
MP_TPL typename MP::size_type MP::
|
||||
count (const key_type& k) const { return (_bst.count(k)); }
|
||||
count(const Key& key) const {
|
||||
|
||||
if (find(key) != end())
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
// lower_bound -------------------------------
|
||||
MP_TPL typename MP::iterator MP::
|
||||
lower_bound (const key_type& k) {
|
||||
@@ -233,39 +345,300 @@ MP_TPL typename MP::allocator_type MP::
|
||||
get_allocator() const { return (_allocator); }
|
||||
|
||||
|
||||
/*********************
|
||||
* private functions :
|
||||
*********************/
|
||||
MP_TPL void MP::
|
||||
_init_sentinel() {
|
||||
|
||||
_sentinel = _allocator_node_sentinel.allocate(1);
|
||||
_allocator_node_sentinel.construct(_sentinel, node_sentinel<value_type>());
|
||||
}
|
||||
MP_TPL pair<typename MP::iterator, bool> MP::
|
||||
_insert(const value_type& value) {
|
||||
|
||||
node<value_type>* n = _root;
|
||||
node<value_type>* prev = NULL;
|
||||
|
||||
while (n)
|
||||
{
|
||||
prev = n;
|
||||
if (_comp(value.first, n->value.first))
|
||||
n = n->left;
|
||||
else if (_comp(n->value.first, value.first))
|
||||
n = n->right;
|
||||
else
|
||||
return ft::make_pair(iterator(n, _sentinel), false);
|
||||
}
|
||||
|
||||
n = _allocator_node.allocate(1);
|
||||
_allocator_node.construct(n, node<value_type>(value));
|
||||
if (_root == NULL)
|
||||
{
|
||||
_root = n;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
else if (_comp(value.first, prev->value.first))
|
||||
prev->left = n;
|
||||
else
|
||||
prev->right = n;
|
||||
n->up = prev;
|
||||
++_size;
|
||||
return ft::make_pair(iterator(n, _sentinel), true);
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_erase(iterator pos) {
|
||||
|
||||
node<value_type>* n = pos.getNode();
|
||||
node<value_type>* delete_point = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
{
|
||||
node<value_type>* next = n->right->min();
|
||||
|
||||
if (next->up != n)
|
||||
{
|
||||
_subtree_shift(next, next->right);
|
||||
next->right = n->right;
|
||||
next->right->up = next;
|
||||
}
|
||||
delete_point = _subtree_shift(n, next);
|
||||
next->left = n->left;
|
||||
next->left->up = next;
|
||||
}
|
||||
else if (!n->left && !n->right)
|
||||
delete_point = _subtree_shift(n, NULL);
|
||||
else if (n->left)
|
||||
delete_point = _subtree_shift(n, n->left);
|
||||
else if (n->right)
|
||||
delete_point = _subtree_shift(n, n->right);
|
||||
|
||||
_allocator_node.destroy(n);
|
||||
_allocator_node.deallocate(n, 1);
|
||||
--_size;
|
||||
return (delete_point);
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_subtree_shift(node<value_type>* st_old, node<value_type>* st_new) {
|
||||
|
||||
node<value_type>* p = st_old->up;
|
||||
|
||||
if (st_old == _root)
|
||||
{
|
||||
_root = st_new;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
else if (st_old == p->left)
|
||||
p->left = st_new;
|
||||
else
|
||||
p->right = st_new;
|
||||
|
||||
if (st_new == NULL)
|
||||
return (p);
|
||||
st_new->up = p;
|
||||
return (st_new);
|
||||
}
|
||||
MP_TPL void MP::
|
||||
_insert_rebalancing(node<value_type>* n) {
|
||||
|
||||
node<value_type>* old_n;
|
||||
node<value_type>* parent = NULL;
|
||||
|
||||
while (n)
|
||||
{
|
||||
n->height = _compute_height(n);
|
||||
|
||||
if (_balance_factor(n) > 1) // Left Heavy
|
||||
{
|
||||
parent = n->up;
|
||||
if (_balance_factor(n->left) < 0) // Left-Right Case
|
||||
n->left = _rotate_left(n->left);
|
||||
// Left-Left Case
|
||||
n = _rotate_right(n);
|
||||
old_n = n->right;
|
||||
}
|
||||
else if (_balance_factor(n) < -1) // Right Heavy
|
||||
{
|
||||
parent = n->up;
|
||||
if (_balance_factor(n->right) > 0) // Right-Left Case
|
||||
n->right = _rotate_right(n->right);
|
||||
// Right-Right Case
|
||||
n = _rotate_left(n);
|
||||
old_n = n->left;
|
||||
}
|
||||
|
||||
if (parent)
|
||||
{
|
||||
if (parent->left == old_n)
|
||||
parent->left = n;
|
||||
else
|
||||
parent->right = n;
|
||||
break;
|
||||
}
|
||||
|
||||
n = n->up;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
n->height = _compute_height(n);
|
||||
n = n->up;
|
||||
}
|
||||
}
|
||||
MP_TPL void MP::
|
||||
_erase_rebalancing(node<value_type>* n) {
|
||||
|
||||
node<value_type>* old_n;
|
||||
node<value_type>* parent = NULL;
|
||||
|
||||
while (n)
|
||||
{
|
||||
n->height = _compute_height(n);
|
||||
|
||||
if (_balance_factor(n) > 1) // Left Heavy
|
||||
{
|
||||
parent = n->up;
|
||||
if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1)
|
||||
n->left = _rotate_left(n->left);
|
||||
// Left-Left Case
|
||||
n = _rotate_right(n);
|
||||
old_n = n->right;
|
||||
}
|
||||
else if (_balance_factor(n) < -1) // Right Heavy
|
||||
{
|
||||
parent = n->up;
|
||||
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
|
||||
n->right = _rotate_right(n->right);
|
||||
// Right-Right Case
|
||||
n = _rotate_left(n);
|
||||
old_n = n->left;
|
||||
}
|
||||
|
||||
if (parent)
|
||||
{
|
||||
if (parent->left == old_n)
|
||||
parent->left = n;
|
||||
else
|
||||
parent->right = n;
|
||||
parent = NULL;
|
||||
}
|
||||
|
||||
n = n->up;
|
||||
}
|
||||
}
|
||||
MP_TPL short MP::
|
||||
_compute_height(node<value_type>* n) {
|
||||
|
||||
if (n->left && n->right)
|
||||
return std::max(n->left->height, n->right->height) + 1;
|
||||
else if (n->left)
|
||||
return n->left->height + 1;
|
||||
else if (n->right)
|
||||
return n->right->height + 1;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
MP_TPL short MP::
|
||||
_balance_factor(node<value_type>* n) {
|
||||
|
||||
if (n->left && n->right)
|
||||
return n->left->height - n->right->height;
|
||||
else if (n->left)
|
||||
return n->left->height;
|
||||
else if (n->right)
|
||||
return (-(n->right->height));
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_rotate_left(node<value_type>* n) {
|
||||
|
||||
node<value_type>* ori_right = n->right;
|
||||
|
||||
ori_right->up = n->up;
|
||||
n->up = ori_right;
|
||||
|
||||
n->right = ori_right->left;
|
||||
if (n->right != NULL)
|
||||
n->right->up = n;
|
||||
ori_right->left = n;
|
||||
|
||||
n->height = _compute_height(n);
|
||||
ori_right->height = _compute_height(ori_right);
|
||||
|
||||
if (n == _root)
|
||||
{
|
||||
_root = ori_right;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
|
||||
return ori_right;
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_rotate_right(node<value_type>* n) {
|
||||
|
||||
node<value_type>* ori_left = n->left;
|
||||
|
||||
ori_left->up = n->up;
|
||||
n->up = ori_left;
|
||||
|
||||
n->left = ori_left->right;
|
||||
if (n->left != NULL)
|
||||
n->left->up = n;
|
||||
ori_left->right = n;
|
||||
|
||||
n->height = _compute_height(n);
|
||||
ori_left->height = _compute_height(ori_left);
|
||||
|
||||
if (n == _root)
|
||||
{
|
||||
_root = ori_left;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
|
||||
return ori_left;
|
||||
}
|
||||
|
||||
|
||||
/************************
|
||||
* non-member functions :
|
||||
************************/
|
||||
// operator == -------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator==
|
||||
( const MP& lhs, const MP& rhs ) {
|
||||
|
||||
return (lhs._bst == rhs._bst);
|
||||
MP_TPL bool operator== (const MP& lhs, const MP& rhs) {
|
||||
|
||||
if (lhs.size() != rhs.size())
|
||||
return false;
|
||||
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||
}
|
||||
// operator < --------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator<
|
||||
( const MP& lhs, const MP& rhs ) {
|
||||
|
||||
return (lhs._bst < rhs._bst);
|
||||
MP_TPL bool operator< (const MP& lhs, const MP& rhs) {
|
||||
|
||||
return ft::lexicographical_compare(
|
||||
lhs.begin(),
|
||||
lhs.end(),
|
||||
rhs.begin(),
|
||||
rhs.end()
|
||||
);
|
||||
}
|
||||
// operator != -------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator!=
|
||||
( const MP& lhs, const MP& rhs ) { return !(lhs == rhs); }
|
||||
MP_TPL bool operator!= (const MP& lhs, const MP& rhs) {
|
||||
return !(lhs == rhs); }
|
||||
// operator <= -------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator<=
|
||||
( const MP& lhs, const MP& rhs ) { return !(lhs > rhs); }
|
||||
MP_TPL bool operator<= (const MP& lhs, const MP& rhs) {
|
||||
return !(lhs > rhs); }
|
||||
// operator > --------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator>
|
||||
( const MP& lhs, const MP& rhs ) { return (rhs < lhs); }
|
||||
MP_TPL bool operator> (const MP& lhs, const MP& rhs) {
|
||||
return (rhs < lhs); }
|
||||
// operator >= -------------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > bool operator>=
|
||||
( const MP& lhs, const MP& rhs ) { return !(lhs < rhs); }
|
||||
MP_TPL bool operator>= (const MP& lhs, const MP& rhs) {
|
||||
return !(lhs < rhs); }
|
||||
// swap (map) -----------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > void swap
|
||||
( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }
|
||||
MP_TPL void swap(MP& lhs, MP& rhs) {
|
||||
lhs.swap(rhs); }
|
||||
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#undef VT
|
||||
#undef VT_TPL
|
||||
#undef MP
|
||||
#undef MP_TPL
|
||||
|
||||
|
||||
Reference in New Issue
Block a user