separate make pair
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
# CONTAINERS #
|
||||
|
||||
### Compilation
|
||||
@@ -53,3 +54,4 @@ saires. Cependant, vous devez éviter le problème de la double inclusion en les
|
||||
* Vous devez créer deux binaires faisant tourner les mêmes tests : l’un avec vos containers et l’autre avec les containers standards.
|
||||
* Comparez les sorties et les performances / temps (vos containers peuvent être jusqu’à 20 fois plus lents que les originaux).
|
||||
* Pour tester vos containers : ft::<container>.
|
||||
|
||||
|
||||
23
headers/make_pair.hpp
Normal file
23
headers/make_pair.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef MAKE_PAIR_HPP
|
||||
# define MAKE_PAIR_HPP
|
||||
|
||||
# define PR_TPL template < class T1, class T2 >
|
||||
# define PR pair<T1, T2>
|
||||
|
||||
#include "pair.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
PR_TPL PR
|
||||
make_pair(T1 x, T2 y) {
|
||||
|
||||
return PR(x, y) ;
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# undef PR
|
||||
# undef PR_TPL
|
||||
|
||||
#endif
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
# include "lexicographical_compare.hpp"
|
||||
|
||||
# include "pair.hpp"
|
||||
# include "make_pair.hpp"
|
||||
# include "map_node.hpp"
|
||||
# include "map_iterator.hpp"
|
||||
|
||||
@@ -174,8 +175,6 @@ private:
|
||||
|
||||
// BBST
|
||||
enum {INSERT, ERASE};
|
||||
node<value_type>* _swap_nodes(node<value_type>* st_old, node<value_type>* st_new);
|
||||
node<value_type>* _shift_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);
|
||||
|
||||
@@ -26,13 +26,16 @@ PR_TPL
|
||||
* copliens :
|
||||
************/
|
||||
PR_TPL PR::
|
||||
pair() {
|
||||
}
|
||||
pair() {}
|
||||
PR_TPL template< typename U, typename V > PR::
|
||||
pair(const pair<U,V>& pr) : first(pr.first), second(pr.second) {
|
||||
pair(const pair<U,V>& pr)
|
||||
: first(pr.first)
|
||||
, second(pr.second) {
|
||||
}
|
||||
PR_TPL PR::
|
||||
pair(const first_type& a, const second_type& b) : first(a), second(b) {
|
||||
pair(const first_type& a, const second_type& b)
|
||||
: first(a)
|
||||
, second(b) {
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +51,7 @@ PR_TPL
|
||||
PR_TPL
|
||||
bool operator<(const PR& lhs, const PR& rhs) {
|
||||
|
||||
return (
|
||||
lhs.first < rhs.first
|
||||
return (lhs.first < rhs.first
|
||||
|| ( !(rhs.first < lhs.first) && (lhs.second < rhs.second) )
|
||||
);
|
||||
}
|
||||
@@ -74,11 +76,6 @@ PR_TPL
|
||||
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
PR_TPL
|
||||
PR make_pair(T1 x, T2 y) {
|
||||
|
||||
return PR(x, y) ;
|
||||
}
|
||||
|
||||
} // namespace ft
|
||||
|
||||
|
||||
@@ -1,607 +0,0 @@
|
||||
|
||||
#define BST_TEMPLATE template < typename Key, typename T, typename Compare, typename Allocator >
|
||||
#define BST Bst<Key, T, Compare, Allocator>
|
||||
|
||||
namespace ft {
|
||||
|
||||
|
||||
//////////////////////
|
||||
// Member functions //
|
||||
|
||||
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) {
|
||||
|
||||
_init_sentinel();
|
||||
insert(first, last);
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
clear();
|
||||
_allocator_node_sentinel.destroy(_sentinel);
|
||||
_allocator_node_sentinel.deallocate(_sentinel, 1);
|
||||
}
|
||||
|
||||
BST_TEMPLATE BST& BST::
|
||||
operator=(const Bst& rhs) {
|
||||
|
||||
if (this == &rhs)
|
||||
return (*this);
|
||||
Bst 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(Bst& 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 (_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;
|
||||
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 (_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;
|
||||
}
|
||||
}
|
||||
|
||||
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::
|
||||
_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;
|
||||
}
|
||||
|
||||
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;
|
||||
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) {
|
||||
|
||||
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
|
||||
|
||||
@@ -1,268 +0,0 @@
|
||||
|
||||
#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
|
||||
|
||||
@@ -196,27 +196,6 @@ MP_TPL template < typename InputIt > void MP::
|
||||
MP_TPL void MP::
|
||||
erase(iterator pos) {
|
||||
|
||||
/* version 1 -- NOT WORKING
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* n_del = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
_swap_nodes(n, n->right->min());
|
||||
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);
|
||||
|
||||
_allocator_node.destroy(n_del);
|
||||
_allocator_node.deallocate(n_del, 1);
|
||||
_size--;
|
||||
|
||||
_balance(n_del, ERASE);
|
||||
*/
|
||||
|
||||
/* version 2 -- WORKING
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* replace = NULL;
|
||||
|
||||
@@ -224,52 +203,13 @@ MP_TPL void MP::
|
||||
{
|
||||
replace = n->right->min();
|
||||
|
||||
if (replace->up != n)
|
||||
{
|
||||
_shift_nodes(replace, replace->right);
|
||||
replace->right = n->right;
|
||||
replace->right->up = replace;
|
||||
}
|
||||
_shift_nodes(n, replace);
|
||||
replace->left = n->left;
|
||||
replace->left->up = replace;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->left)
|
||||
replace = n->left;
|
||||
else if (n->right)
|
||||
replace = n->right;
|
||||
else
|
||||
replace = NULL;
|
||||
_shift_nodes(n, replace);
|
||||
}
|
||||
|
||||
_allocator_node.destroy(n);
|
||||
_allocator_node.deallocate(n, 1);
|
||||
_size--;
|
||||
|
||||
_balance(replace, ERASE);
|
||||
*/
|
||||
|
||||
node<value_type>* n = pos.get_node();
|
||||
node<value_type>* replace = NULL;
|
||||
|
||||
if (n->left && n->right)
|
||||
{
|
||||
replace = n->right->min();
|
||||
|
||||
// change connection replace->left with n->left
|
||||
replace->left = n->left;
|
||||
replace->left->up = replace;
|
||||
// if replace and n are not connexes
|
||||
if (replace->up != n)
|
||||
{
|
||||
// connecte replace->right with replace->up
|
||||
replace->up->left = replace->right;
|
||||
if (replace->right != NULL)
|
||||
replace->right->up = replace->up;
|
||||
// change connection replace->right with n->right
|
||||
replace->right = n->right;
|
||||
replace->right->up = replace;
|
||||
}
|
||||
@@ -299,44 +239,6 @@ MP_TPL void MP::
|
||||
_size--;
|
||||
|
||||
_balance(replace, ERASE);
|
||||
/* version 3 -- WIP
|
||||
*/
|
||||
|
||||
/* version 0 -- WORKING
|
||||
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_del = _shift_nodes(n, n->left);
|
||||
else if (n->right)
|
||||
n_del = _shift_nodes(n, n->right);
|
||||
else
|
||||
n_del = _shift_nodes(n, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
next = n->right->min();
|
||||
|
||||
if (next->up != n)
|
||||
{
|
||||
_shift_nodes(next, next->right);
|
||||
next->right = n->right;
|
||||
next->right->up = next;
|
||||
}
|
||||
n_del = _shift_nodes(n, next);
|
||||
next->left = n->left;
|
||||
next->left->up = next;
|
||||
}
|
||||
|
||||
_allocator_node.destroy(n);
|
||||
_allocator_node.deallocate(n, 1);
|
||||
_size--;
|
||||
|
||||
_balance(n_del, ERASE);
|
||||
*/
|
||||
}
|
||||
MP_TPL void MP::
|
||||
erase(iterator first, iterator last) {
|
||||
@@ -514,81 +416,6 @@ MP_TPL typename MP::allocator_type MP::
|
||||
/*********************
|
||||
* private functions :
|
||||
*********************/
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_swap_nodes(node<value_type>* n_old, node<value_type>* n_new) {
|
||||
|
||||
node<value_type>* tmp;
|
||||
// tmp = _allocator_node.allocate(1);
|
||||
// _allocator_node.construct(tmp, node<value_type>(n_old->value));
|
||||
|
||||
tmp = n_old->up;
|
||||
n_old->up = n_new->up;
|
||||
n_new->up = tmp;
|
||||
|
||||
tmp = n_old->right;
|
||||
n_old->right = n_new->right;
|
||||
n_new->right = tmp;
|
||||
|
||||
tmp = n_old->left;
|
||||
n_old->left = n_new->left;
|
||||
n_new->left = tmp;
|
||||
|
||||
// n_old
|
||||
tmp = n_old->up;
|
||||
if (tmp)
|
||||
{
|
||||
if (tmp->left == n_new)
|
||||
tmp->left = n_old;
|
||||
else
|
||||
tmp->right = n_old;
|
||||
}
|
||||
tmp = n_old->left;
|
||||
if (tmp)
|
||||
tmp->up = n_old;
|
||||
tmp = n_old->right;
|
||||
if (tmp)
|
||||
tmp->up = n_old;
|
||||
|
||||
// n_new
|
||||
tmp = n_new->up;
|
||||
if (tmp)
|
||||
{
|
||||
if (tmp->left == n_old)
|
||||
tmp->left = n_new;
|
||||
else
|
||||
tmp->right = n_new;
|
||||
}
|
||||
tmp = n_new->left;
|
||||
if (tmp)
|
||||
tmp->up = n_new;
|
||||
tmp = n_new->right;
|
||||
if (tmp)
|
||||
tmp->up = n_new;
|
||||
|
||||
// _allocator_node.destroy(tmp);
|
||||
// _allocator_node.deallocate(tmp, 1);
|
||||
return (n_old);
|
||||
}
|
||||
MP_TPL node<typename MP::value_type>* MP::
|
||||
_shift_nodes(node<value_type>* n_old, node<value_type>* n_new) {
|
||||
|
||||
node<value_type>* p = n_old->up;
|
||||
|
||||
if (n_old == _root)
|
||||
{
|
||||
_root = n_new;
|
||||
_sentinel->child = _root;
|
||||
}
|
||||
else if (n_old == p->left)
|
||||
p->left = n_new;
|
||||
else
|
||||
p->right = n_new;
|
||||
|
||||
if (n_new == NULL)
|
||||
return (p);
|
||||
n_new->up = p;
|
||||
return (n_new);
|
||||
}
|
||||
MP_TPL void MP::
|
||||
_balance(node<value_type>* n, bool action) {
|
||||
|
||||
|
||||
112
tests/main.cpp
112
tests/main.cpp
@@ -4,68 +4,68 @@
|
||||
int main() {
|
||||
|
||||
// VECTOR
|
||||
// tests_vector_constructor();
|
||||
// tests_vector_operator_assignation();
|
||||
// tests_vector_begin();
|
||||
// tests_vector_end();
|
||||
// tests_vector_rbegin();
|
||||
// tests_vector_rend();
|
||||
// tests_vector_size();
|
||||
// tests_vector_max_size();
|
||||
// tests_vector_resize();
|
||||
// tests_vector_capacity();
|
||||
// tests_vector_empty();
|
||||
// tests_vector_reserve();
|
||||
// tests_vector_operator_access();
|
||||
// tests_vector_at();
|
||||
// tests_vector_front();
|
||||
// tests_vector_back();
|
||||
// tests_vector_assign();
|
||||
// tests_vector_push_back();
|
||||
// tests_vector_pop_back();
|
||||
// tests_vector_insert();
|
||||
// tests_vector_erase();
|
||||
// tests_vector_swap();
|
||||
// tests_vector_clear();
|
||||
// tests_vector_get_allocator();
|
||||
// tests_vector_swap_non_member();
|
||||
// tests_vector_reverse_iterators();
|
||||
// tests_vector_relational_operators();
|
||||
tests_vector_constructor();
|
||||
tests_vector_operator_assignation();
|
||||
tests_vector_begin();
|
||||
tests_vector_end();
|
||||
tests_vector_rbegin();
|
||||
tests_vector_rend();
|
||||
tests_vector_size();
|
||||
tests_vector_max_size();
|
||||
tests_vector_resize();
|
||||
tests_vector_capacity();
|
||||
tests_vector_empty();
|
||||
tests_vector_reserve();
|
||||
tests_vector_operator_access();
|
||||
tests_vector_at();
|
||||
tests_vector_front();
|
||||
tests_vector_back();
|
||||
tests_vector_assign();
|
||||
tests_vector_push_back();
|
||||
tests_vector_pop_back();
|
||||
tests_vector_insert();
|
||||
tests_vector_erase();
|
||||
tests_vector_swap();
|
||||
tests_vector_clear();
|
||||
tests_vector_get_allocator();
|
||||
tests_vector_swap_non_member();
|
||||
tests_vector_reverse_iterators();
|
||||
tests_vector_relational_operators();
|
||||
|
||||
// MAP
|
||||
// tests_map_simple();
|
||||
// tests_map_constructor();
|
||||
// tests_map_operator_assignation();
|
||||
// tests_map_begin();
|
||||
// tests_map_end();
|
||||
// tests_map_rbegin();
|
||||
// tests_map_rend();
|
||||
// tests_map_empty();
|
||||
// tests_map_size();
|
||||
// tests_map_max_size();
|
||||
// tests_map_operator_access();
|
||||
tests_map_simple();
|
||||
tests_map_constructor();
|
||||
tests_map_operator_assignation();
|
||||
tests_map_begin();
|
||||
tests_map_end();
|
||||
tests_map_rbegin();
|
||||
tests_map_rend();
|
||||
tests_map_empty();
|
||||
tests_map_size();
|
||||
tests_map_max_size();
|
||||
tests_map_operator_access();
|
||||
tests_map_insert();
|
||||
tests_map_erase();
|
||||
// tests_map_swap();
|
||||
// tests_map_clear();
|
||||
// tests_map_key_comp();
|
||||
// tests_map_value_comp();
|
||||
// tests_map_find();
|
||||
// tests_map_count();
|
||||
// tests_map_lower_bound();
|
||||
// tests_map_upper_bound();
|
||||
// tests_map_equal_range();
|
||||
// tests_map_get_allocator();
|
||||
// tests_map_swap_non_member();
|
||||
// tests_map_relational_operators();
|
||||
tests_map_swap();
|
||||
tests_map_clear();
|
||||
tests_map_key_comp();
|
||||
tests_map_value_comp();
|
||||
tests_map_find();
|
||||
tests_map_count();
|
||||
tests_map_lower_bound();
|
||||
tests_map_upper_bound();
|
||||
tests_map_equal_range();
|
||||
tests_map_get_allocator();
|
||||
tests_map_swap_non_member();
|
||||
tests_map_relational_operators();
|
||||
|
||||
// STACK
|
||||
// tests_stack_constructor();
|
||||
// tests_stack_empty();
|
||||
// tests_stack_size();
|
||||
// tests_stack_top();
|
||||
// tests_stack_push();
|
||||
// tests_stack_pop();
|
||||
tests_stack_constructor();
|
||||
tests_stack_empty();
|
||||
tests_stack_size();
|
||||
tests_stack_top();
|
||||
tests_stack_push();
|
||||
tests_stack_pop();
|
||||
|
||||
// execute tests and print them :
|
||||
int size = test_list.size();
|
||||
|
||||
@@ -9,10 +9,10 @@ OUTPUT_FT="output_ft.log"
|
||||
make -j
|
||||
|
||||
echo -e "\nstl :"
|
||||
time ./containers_ft > tests/$OUTPUT_STL
|
||||
time ./containers_stl > tests/$OUTPUT_STL
|
||||
|
||||
echo -e "\nft :"
|
||||
time ./containers_stl > tests/$OUTPUT_FT
|
||||
time ./containers_ft > tests/$OUTPUT_FT
|
||||
|
||||
diff --context=0 --color=always tests/$OUTPUT_STL tests/$OUTPUT_FT
|
||||
|
||||
|
||||
@@ -403,6 +403,18 @@ TEST_M(tests_map_erase)
|
||||
|
||||
PRINT(mymap)
|
||||
|
||||
|
||||
// title
|
||||
TITLE(big tree)
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 10000; i++)
|
||||
mymap[VALT(i)]=VALU(i);
|
||||
while (i)
|
||||
mymap.erase (VAL(i--));
|
||||
|
||||
PRINT(mymap)
|
||||
|
||||
DELETE
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user