stack almost good

This commit is contained in:
hugogogo
2022-06-24 02:23:53 +02:00
parent 58d417742b
commit 6617d6cdf5
23 changed files with 3137 additions and 1081 deletions

View File

@@ -8,55 +8,49 @@ namespace ft {
//////////////////////
// Member functions //
BST_TEMPLATE
BST::
Bst(const Compare& comp, const Allocator& alloc) :
_size(0),
_root(NULL),
_comp(comp),
_allocator(alloc)
{
BST_TEMPLATE BST::
Bst(const Compare& comp, const Allocator& alloc)
: _size(0)
, _root(NULL)
, _comp(comp)
, _allocator(alloc) {
_init_sentinel();
}
BST_TEMPLATE
template < typename InputIt >
BST::
Bst(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc) :
_size(0),
_root(NULL),
_comp(comp),
_allocator(alloc)
{
BST_TEMPLATE template < typename InputIt > BST::
Bst(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc)
: _size(0)
, _root(NULL)
, _comp(comp)
, _allocator(alloc) {
_init_sentinel();
insert(first, last);
}
BST_TEMPLATE
BST::
Bst(const Bst& src) :
_size(0),
_root(NULL),
_comp(src._comp),
_allocator(src._allocator)
{
BST_TEMPLATE BST::
Bst(const Bst& src)
: _size(0)
, _root(NULL)
, _comp(src._comp)
, _allocator(src._allocator) {
_init_sentinel();
*this = src;
}
BST_TEMPLATE
BST::
~Bst()
{
BST_TEMPLATE BST::
~Bst() {
clear();
_allocator_node_sentinel.destroy(_sentinel);
_allocator_node_sentinel.deallocate(_sentinel, 1);
}
BST_TEMPLATE
BST& BST::
operator=(const Bst& rhs)
{
BST_TEMPLATE BST& BST::
operator=(const Bst& rhs) {
if (this == &rhs)
return (*this);
Bst new_bst(rhs.begin(), rhs.end());
@@ -68,10 +62,9 @@ BST& BST::
////////////////////
// Element access //
BST_TEMPLATE
T& BST::
operator[](const Key& key)
{
BST_TEMPLATE T& BST::
operator[](const Key& key) {
node<value_type>* n = _root;
//node<value_type>* prev = NULL;
@@ -96,64 +89,53 @@ T& BST::
///////////////
// Iterators //
BST_TEMPLATE
typename BST::iterator BST::
begin()
{
BST_TEMPLATE typename BST::iterator BST::
begin() {
if (_root)
return iterator(_root->min(), _sentinel);
else
return end();
}
BST_TEMPLATE
typename BST::const_iterator BST::
begin() const
{
BST_TEMPLATE typename BST::const_iterator BST::
begin() const {
if (_root)
return const_iterator(_root->min(), _sentinel);
else
return end();
}
BST_TEMPLATE
typename BST::iterator BST::
BST_TEMPLATE typename BST::iterator BST::
end() { return iterator(NULL, _sentinel); }
BST_TEMPLATE
typename BST::const_iterator BST::
BST_TEMPLATE typename BST::const_iterator BST::
end() const { return const_iterator(NULL, _sentinel); }
BST_TEMPLATE
typename BST::reverse_iterator BST::
BST_TEMPLATE typename BST::reverse_iterator BST::
rbegin() { return reverse_iterator(end()); }
BST_TEMPLATE
typename BST::const_reverse_iterator BST::
BST_TEMPLATE typename BST::const_reverse_iterator BST::
rbegin() const { return const_reverse_iterator(end()); }
BST_TEMPLATE
typename BST::reverse_iterator BST::
BST_TEMPLATE typename BST::reverse_iterator BST::
rend() { return reverse_iterator(begin()); }
BST_TEMPLATE
typename BST::const_reverse_iterator BST::
BST_TEMPLATE typename BST::const_reverse_iterator BST::
rend() const { return const_reverse_iterator(begin()); }
//////////////
// Capacity //
BST_TEMPLATE
bool BST::
BST_TEMPLATE bool BST::
empty() const { return (_size == 0); }
BST_TEMPLATE
typename BST::size_type BST::
BST_TEMPLATE typename BST::size_type BST::
size() const { return (_size); }
BST_TEMPLATE
typename BST::size_type BST::
BST_TEMPLATE typename BST::size_type BST::
max_size() const
{
return ( _allocator_node.max_size() );
@@ -419,19 +401,19 @@ void BST::
{
n->height = _compute_height(n);
if (_bf(n) > 1) // Left Heavy
if (_balance_factor(n) > 1) // Left Heavy
{
parent = n->up;
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
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 (_bf(n) < -1) // Right Heavy
else if (_balance_factor(n) < -1) // Right Heavy
{
parent = n->up;
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
n->right = _rotate_right(n->right);
// Right-Right Case
n = _rotate_left(n);
@@ -468,19 +450,19 @@ void BST::
{
n->height = _compute_height(n);
if (_bf(n) > 1) // Left Heavy
if (_balance_factor(n) > 1) // Left Heavy
{
parent = n->up;
if (_bf(n->left) < 0) // Left-Right Case (BF == -1)
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 (_bf(n) < -1) // Right Heavy
else if (_balance_factor(n) < -1) // Right Heavy
{
parent = n->up;
if (_bf(n->right) > 0) // Right-Left Case (BF == 1)
if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1)
n->right = _rotate_right(n->right);
// Right-Right Case
n = _rotate_left(n);
@@ -514,10 +496,9 @@ short BST::
return 1;
}
BST_TEMPLATE
short BST::
_bf(node<value_type>* n) // optimisation possible if assume n have at least one child ?
{
BST_TEMPLATE short BST::
_balance_factor(node<value_type>* n) {
if (n->left && n->right)
return n->left->height - n->right->height;
else if (n->left)
@@ -528,10 +509,9 @@ short BST::
return 0;
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_rotate_left(node<value_type>* n) // assume n->right != NULL
{
BST_TEMPLATE node<typename BST::value_type>* BST::
_rotate_left(node<value_type>* n) {
node<value_type>* ori_right = n->right;
ori_right->up = n->up;
@@ -554,10 +534,9 @@ node<typename BST::value_type>* BST::
return ori_right; // return new sub-tree root
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_rotate_right(node<value_type>* n) // assume n->left != NULL
{
BST_TEMPLATE node<typename BST::value_type>* BST::
_rotate_right(node<value_type>* n) {
node<value_type>* ori_left = n->left;
ori_left->up = n->up;

268
templates/bak__map.tpp Normal file
View File

@@ -0,0 +1,268 @@
#define MP_TPL template < typename Key, typename T, typename Compare, typename Alloc >
#define MP map<Key, T, Compare, Alloc>
namespace ft {
/************
* copliens :
************/
// constructors ------------------------------
MP_TPL MP::
map (const key_compare & comp, const allocator_type & alloc)
: _bst()
, _allocator(alloc)
, _comp(comp) {
return;
}
MP_TPL template <class InputIt> MP::
map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
: _bst(first, last)
, _allocator(alloc)
, _comp(comp) {
}
MP_TPL MP::
map (const map& x)
: _bst()
, _allocator(x._allocator)
, _comp(x._comp) {
*this = x;
}
// destructor --------------------------------
MP_TPL MP::
~map() { clear(); }
// operator= ---------------------------------
MP_TPL MP& MP::
operator= (const map& x) {
if (this == &x)
return (*this);
map new_map(x.begin(), x.end());
swap(new_map);
return (*this);
}
/*************
* iterators :
*************/
// begin -------------------------------------
MP_TPL typename MP::iterator MP::
begin() { return (_bst.begin()); }
MP_TPL typename MP::const_iterator MP::
begin() const { return (_bst.begin()); }
// end ---------------------------------------
MP_TPL typename MP::iterator MP::
end() { return (_bst.end()); }
MP_TPL typename MP::const_iterator MP::
end() const { return (_bst.end()); }
// rbegin ------------------------------------
MP_TPL typename MP::reverse_iterator MP::
rbegin() { return (_bst.rbegin()); }
MP_TPL typename MP::const_reverse_iterator MP::
rbegin() const { return (_bst.rbegin()); }
// rend --------------------------------------
MP_TPL typename MP::reverse_iterator MP::
rend() { return (_bst.rend()); }
MP_TPL typename MP::const_reverse_iterator MP::
rend() const { return (_bst.rend()); }
/************
* capacity :
************/
// empty -------------------------------------
MP_TPL bool MP::
empty() const { return (_bst.empty()); }
// size --------------------------------------
MP_TPL typename MP::size_type MP::
size() const { return (_bst.size()); }
// max_size ----------------------------------
MP_TPL typename MP::size_type MP::
max_size() const { return (_bst.max_size()); }
/******************
* element access :
******************/
// operator[] --------------------------------
MP_TPL typename MP::mapped_type& MP::
operator[] (const key_type& k) { return _bst[k]; }
/*************
* modifiers :
*************/
// insert ------------------------------------
MP_TPL pair<typename MP::iterator, bool> MP::
insert (const value_type& val) { return (_bst.insert(val)); }
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)); }
// 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)); }
MP_TPL void MP::
erase (iterator first, iterator last) { return (_bst.erase(first, last)); }
// swap --------------------------------------
MP_TPL void MP::
swap (map& x) {
bst_map tmp;
tmp.swap(_bst);
_bst.swap(x._bst);
x._bst.swap(tmp);
}
// clear -------------------------------------
MP_TPL void MP::
clear() {
_bst.clear();
}
/*************
* observers :
*************/
// key_comp ----------------------------------
MP_TPL typename MP::key_compare MP::
key_comp() const { return (value_compare(_comp).comp); }
// value_comp --------------------------------
MP_TPL typename MP::value_compare MP::
value_comp() const { return (value_compare(_comp)); }
/**************
* operations :
**************/
// find --------------------------------------
MP_TPL typename MP::iterator MP::
find (const key_type& k) { return (_bst.find(k)); }
MP_TPL typename MP::const_iterator MP::
find (const key_type& k) const { return (_bst.find(k)); }
// count -------------------------------------
MP_TPL typename MP::size_type MP::
count (const key_type& k) const { return (_bst.count(k)); }
// lower_bound -------------------------------
MP_TPL typename MP::iterator MP::
lower_bound (const key_type& k) {
iterator it = begin();
iterator it_end = end();
while (it != it_end)
{
if (_comp(it->first, k) == false)
return (it);
++it;
}
return (it_end);
}
MP_TPL typename MP::const_iterator MP::
lower_bound (const key_type& k) const {
const_iterator it = begin();
const_iterator it_end = end();
while (it != it_end)
{
if (_comp(it->first, k) == false)
return (it);
++it;
}
return (it_end);
}
// upper_bound -------------------------------
MP_TPL typename MP::iterator MP::
upper_bound (const key_type& k) {
iterator it = begin();
iterator it_end = end();
while (it != it_end)
{
if (_comp(k, it->first))
return (it);
++it;
}
return (it_end);
}
MP_TPL typename MP::const_iterator MP::
upper_bound (const key_type& k) const {
const_iterator it = begin();
const_iterator it_end = end();
while (it != it_end)
{
if (_comp(k, it->first))
return (it);
++it;
}
return (it_end);
}
// equal_range -------------------------------
MP_TPL pair<typename MP::const_iterator, typename MP::const_iterator> MP::
equal_range (const key_type& k) const {
return ft::make_pair( lower_bound(k), upper_bound(k) );
}
MP_TPL pair<typename MP::iterator, typename MP::iterator> MP::
equal_range (const key_type& k) {
return ft::make_pair( lower_bound(k), upper_bound(k) );
}
/*************
* allocator :
*************/
// get_allocator -----------------------------
MP_TPL typename MP::allocator_type MP::
get_allocator() const { return (_allocator); }
/************************
* 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);
}
// operator < --------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator<
( const MP& lhs, const MP& rhs ) {
return (lhs._bst < rhs._bst);
}
// operator != -------------------------------
template< class Key, class T, class Compare, class Alloc > 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); }
// operator > --------------------------------
template< class Key, class T, class Compare, class Alloc > 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); }
// swap (map) -----------------------------
template< class Key, class T, class Compare, class Alloc > void swap
( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }
} // namespace ft
#undef VT
#undef VT_TPL

View File

@@ -1,628 +0,0 @@
#define BST_TEMPLATE template < typename Key, typename T, typename Compare, typename Allocator >
#define BST map<Key, T, Compare, Allocator>
namespace ft {
//////////////////////
// Member functions //
BST_TEMPLATE
BST::
map(const Compare& comp, const Allocator& alloc) :
_size(0),
_root(NULL),
_comp(comp),
_allocator(alloc)
{
_init_sentinel();
}
BST_TEMPLATE
template < typename InputIt >
BST::
map(InputIt first, InputIt last, const Compare& comp, const Allocator& alloc) :
_size(0),
_root(NULL),
_comp(comp),
_allocator(alloc)
{
_init_sentinel();
insert(first, last);
}
BST_TEMPLATE
BST::
map(const map& src) :
_size(0),
_root(NULL),
_comp(src._comp),
_allocator(src._allocator)
{
_init_sentinel();
*this = src;
}
BST_TEMPLATE
BST::
~map()
{
clear();
_allocator_node_sentinel.destroy(_sentinel);
_allocator_node_sentinel.deallocate(_sentinel, 1);
}
BST_TEMPLATE
BST& BST::
operator=(const map& rhs)
{
if (this == &rhs)
return (*this);
map new_bst(rhs.begin(), rhs.end());
swap(new_bst);
return (*this);
}
////////////////////
// Element access //
BST_TEMPLATE
T& BST::
operator[](const Key& key)
{
node<value_type>* n = _root;
//node<value_type>* prev = NULL;
while (n)
{
//prev = 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);
}
// TODO : Call insert with hint (prev)
n = insert( ft::make_pair(key, mapped_type()) ).first.getNode();
return (n->value.second);
}
///////////////
// Iterators //
BST_TEMPLATE
typename BST::iterator BST::
begin()
{
if (_root)
return iterator(_root->min(), _sentinel);
else
return end();
}
BST_TEMPLATE
typename BST::const_iterator BST::
begin() const
{
if (_root)
return const_iterator(_root->min(), _sentinel);
else
return end();
}
BST_TEMPLATE
typename BST::iterator BST::
end() { return iterator(NULL, _sentinel); }
BST_TEMPLATE
typename BST::const_iterator BST::
end() const { return const_iterator(NULL, _sentinel); }
BST_TEMPLATE
typename BST::reverse_iterator BST::
rbegin() { return reverse_iterator(end()); }
BST_TEMPLATE
typename BST::const_reverse_iterator BST::
rbegin() const { return const_reverse_iterator(end()); }
BST_TEMPLATE
typename BST::reverse_iterator BST::
rend() { return reverse_iterator(begin()); }
BST_TEMPLATE
typename BST::const_reverse_iterator BST::
rend() const { return const_reverse_iterator(begin()); }
//////////////
// Capacity //
BST_TEMPLATE
bool BST::
empty() const { return (_size == 0); }
BST_TEMPLATE
typename BST::size_type BST::
size() const { return (_size); }
BST_TEMPLATE
typename BST::size_type BST::
max_size() const
{
return ( _allocator_node.max_size() );
}
///////////////
// Modifiers //
BST_TEMPLATE
void BST::
clear()
{
// TODO : optimisation jouable ?
erase(begin(), end());
//_size = 0;
}
BST_TEMPLATE
pair<typename BST::iterator, bool> BST::
insert(const value_type& value)
{
pair<typename BST::iterator, bool> ret;
ret = _insert(value);
if (ret.second == true)
_insert_rebalancing(ret.first.getNode()->up);
return (ret);
}
BST_TEMPLATE
typename BST::iterator BST::
insert(iterator hint, const value_type& value)
{
// TODO : optimise with hint
(void)hint;
return insert(value).first;
}
BST_TEMPLATE
template < typename InputIt >
void BST::
insert(InputIt first, InputIt last)
{
//static int i = 0; // Debug
while (first != last)
{
insert(*first);
++first;
//std::cout << "c|" << i << "\n";
//++i;
}
}
BST_TEMPLATE
void BST::
erase(iterator pos)
{
node<value_type>* delete_point;
delete_point = _erase(pos);
_erase_rebalancing(delete_point);
}
BST_TEMPLATE
void BST::
erase(iterator first, iterator last)
{
while (first != last)
erase(first++);
}
BST_TEMPLATE
typename BST::size_type BST::
erase(const Key& key)
{
iterator pos = find(key);
if (pos == end())
return (0);
else
{
erase(pos);
return (1);
}
}
BST_TEMPLATE
void BST::
swap(map& other)
{
node<value_type>* tmp_root = _root;
node_sentinel<value_type>* tmp_sentinel = _sentinel;
size_type tmp_size = _size;
_root = other._root;
_sentinel = other._sentinel;
_size = other._size;
other._root = tmp_root;
other._sentinel = tmp_sentinel;
other._size = tmp_size;
}
////////////
// Lookup //
BST_TEMPLATE
typename BST::iterator BST::
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());
}
BST_TEMPLATE
typename BST::const_iterator BST::
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());
}
BST_TEMPLATE
typename BST::size_type BST::
count(const Key& key) const
{
if (find(key) != end())
return (1);
else
return (0);
}
///////////////////////
// Private functions //
BST_TEMPLATE
void BST::
_init_sentinel()
{
_sentinel = _allocator_node_sentinel.allocate(1);
_allocator_node_sentinel.construct(_sentinel, node_sentinel<value_type>());
}
BST_TEMPLATE
pair<typename BST::iterator, bool> BST::
_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) // if (_size == 0)
{
_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);
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_erase(iterator pos)
{
node<value_type>* n = pos.getNode();
node<value_type>* delete_point = NULL;
if (n->left && n->right) // 2 child
{
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) // no child (leaf)
delete_point = _subtree_shift(n, NULL); // bug ?
else if (n->left) // 1 child
delete_point = _subtree_shift(n, n->left);
else if (n->right) // 1 child
delete_point = _subtree_shift(n, n->right); // bug ?
_allocator_node.destroy(n);
_allocator_node.deallocate(n, 1);
--_size;
return (delete_point);
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_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); // return deletion point
st_new->up = p;
return (st_new); // return deletion point
}
BST_TEMPLATE
void BST::
_insert_rebalancing(node<value_type>* n)
{
node<value_type>* old_n;
node<value_type>* parent = NULL;
while (n)
{
n->height = _compute_height(n);
if (_bf(n) > 1) // Left Heavy
{
parent = n->up;
if (_bf(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 (_bf(n) < -1) // Right Heavy
{
parent = n->up;
if (_bf(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;
break;
}
n = n->up;
}
while (n)
{
n->height = _compute_height(n);
n = n->up;
}
}
BST_TEMPLATE
void BST::
_erase_rebalancing(node<value_type>* n)
{
node<value_type>* old_n;
node<value_type>* parent = NULL;
while (n)
{
n->height = _compute_height(n);
if (_bf(n) > 1) // Left Heavy
{
parent = n->up;
if (_bf(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 (_bf(n) < -1) // Right Heavy
{
parent = n->up;
if (_bf(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;
}
}
BST_TEMPLATE
short BST::
_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;
}
BST_TEMPLATE
short BST::
_bf(node<value_type>* n) // optimisation possible if assume n have at least one child ?
{
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;
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_rotate_left(node<value_type>* n) // assume n->right != NULL
{
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; // return new sub-tree root
}
BST_TEMPLATE
node<typename BST::value_type>* BST::
_rotate_right(node<value_type>* n) // assume n->left != NULL
{
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; // return new sub-tree root
}
//////////////////////////
// Non-member functions //
BST_TEMPLATE
bool operator==(const BST& lhs, const BST& rhs)
{
if (lhs.size() != rhs.size())
return false;
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
BST_TEMPLATE
bool operator!=(const BST& lhs, const BST& rhs)
{ return !(lhs == rhs); }
BST_TEMPLATE
bool operator<(const BST& lhs, const BST& rhs)
{
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
BST_TEMPLATE
bool operator>(const BST& lhs, const BST& rhs)
{ return (rhs < lhs); }
BST_TEMPLATE
bool operator<=(const BST& lhs, const BST& rhs)
{ return !(lhs > rhs); }
BST_TEMPLATE
bool operator>=(const BST& lhs, const BST& rhs)
{ return !(lhs < rhs); }
BST_TEMPLATE
void swap(BST& lhs, BST& rhs)
{ lhs.swap(rhs); }
} // namespace ft
#undef BST
#undef BST_TEMPLATE

View File

@@ -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