no homemade tester, work well ? no leak

This commit is contained in:
kinou-p 2022-11-28 21:38:08 +01:00
parent 4ff8386a85
commit ea089eab3d
6 changed files with 587 additions and 713 deletions

View File

@ -70,7 +70,7 @@ class bidirectionnal_iterator
return (true); return (true);
else if (!lhs._node || !rhs._node) else if (!lhs._node || !rhs._node)
return (false); return (false);
return (lhs._node->data == rhs._node->data); return (lhs._node == rhs._node);
// if (lhs._node && rhs._node) // if (lhs._node && rhs._node)
// return (lhs._node->data == rhs._node->data); // return (lhs._node->data == rhs._node->data);
// if (!lhs._node && !rhs._node) // if (!lhs._node && !rhs._node)
@ -85,7 +85,7 @@ class bidirectionnal_iterator
return (false); return (false);
else if (!lhs._node || !rhs._node) else if (!lhs._node || !rhs._node)
return (true); return (true);
return (lhs._node->data != rhs._node->data); return (lhs._node != rhs._node);
// if (lhs._node && rhs._node) // if (lhs._node && rhs._node)
// return (lhs._node->data != rhs._node->data); // return (lhs._node->data != rhs._node->data);
// if (!lhs._node && !rhs._node) // if (!lhs._node && !rhs._node)
@ -171,159 +171,8 @@ class bidirectionnal_iterator
return y; return y;
} }
// bidirectionnal_iterator &operator ++()
// {
// if (_node != NULL)
// _node = successor(_node);
// return (*this);
// }
// bidirectionnal_iterator operator ++(int)
// {
// bidirectionnal_iterator tmp(*this);
// ++(*this);
// return (tmp);
// }
// bidirectionnal_iterator &operator --()
// {
// if (_node == _end)
// _node = maximum(_root);
// else
// _node = predecessor(_node);
// return (*this);
// }
// bidirectionnal_iterator operator --(int)
// {
// bidirectionnal_iterator tmp(*this);
// --(*this);
// return (tmp);
// }
// NodePtr maximum(NodePtr ptr)
// {
// while (ptr && ptr->right != _end)
// ptr = ptr->right;
// return (ptr);
// }
// NodePtr minimum(NodePtr ptr)
// {
// while (ptr && ptr->left != _end)
// ptr = ptr->left;
// return (ptr);
// }
// NodePtr minimum(NodePtr node)
// {
// if (!node)
// return NULL;
// while (node->left->parent != NULL)
// node = node->left;
// return node;
// }
// NodePtr maximum(NodePtr node)
// {
// if (!node)
// return NULL;
// while (node->right->parent != NULL)
// node = node->right;
// return node;
// }
private : private :
// NodePtr predecessor(NodePtr x)
// {
// if (x->left != _end)
// {
// return maximum(x->left);
// }
// NodePtr y = x->parent;
// while (y != NULL && x == y->left)
// {
// x = y;
// y = y->parent;
// }
// return y;
// }
// NodePtr successor(NodePtr x)
// {
// if (!x)
// return (0);
// //std::cout << "_node: " << this->base() << std::endl;
// //std::cout << "succkey: " << x->data.first << " | succvalue: " << x->data.second << std::endl;
// if (x->right != _end)
// return minimum(x->right);
// NodePtr y = x->parent;
// while (y != NULL && x == y->right)
// {
// x = y;
// y = y->parent;
// }
// if (y == NULL)
// return _end;
// return y;
// }
// NodePtr successor(NodePtr x) {
// if (x->right != NULL) {
// return minimum(x->right);
// }
// NodePtr y = x->parent;
// while (y != NULL && x == y->right) {
// x = y;
// y = y->parent;
// }
// return y;
// }
// NodePtr predecessor(NodePtr x) {
// if (x->left != NULL) {
// return maximum(x->left);
// }
// NodePtr y = x->parent;
// while (y != NULL && x == y->left) {
// x = y;
// y = y->parent;
// }
// return y;
// }
}; };
} }

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexicographical_compare.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 14:58:42 by apommier #+# #+# */
/* Updated: 2022/11/28 15:46:54 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
namespace ft
{
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
while (first1!=last1)
{
if (first2 == last2 || *first2 < *first1) return false;
else if (*first1 < *first2) return true;
++first1; ++first2;
}
return (first2 != last2);
}
template <class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
while (first1 != last1)
{
std::cout << "i serve\n" << std::endl;
if (comp(*first1, *first2))
return true;
if (comp(*first2, *first1))
return false;
++first1;
++first2;
}
return (first2 != last2);
}
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/22 13:39:29 by apommier #+# #+# */ /* Created: 2022/11/22 13:39:29 by apommier #+# #+# */
/* Updated: 2022/11/27 12:00:33 by apommier ### ########.fr */ /* Updated: 2022/11/28 15:59:35 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -15,11 +15,12 @@
#include "./iterators/bidirectionnal_iterator.hpp" #include "./iterators/bidirectionnal_iterator.hpp"
#include "./iterators/pair.hpp" #include "./iterators/pair.hpp"
#include "./iterators/make_pair.hpp" #include "./iterators/make_pair.hpp"
#include "./iterators/equal.hpp"
#include "./iterators/lexicographical_compare.hpp"
#include "vector.hpp" #include "vector.hpp"
#define RED 1 //red = 1
#define BLACK 0 //black = 0
namespace ft namespace ft
{ {
@ -60,6 +61,9 @@ class map
class value_compare; class value_compare;
//-----------------------------
//-----PRIVATE MEMBER TYPE-----
//-----------------------------
protected : protected :
key_compare _comp; key_compare _comp;
@ -86,12 +90,6 @@ class map
: data(ft::make_pair(key, val)), parent(end), right(end), left(end), color(1) : data(ft::make_pair(key, val)), parent(end), right(end), left(end), color(1)
{} {}
}; };
//-----------------------------
//-----PRIVATE MEMBER TYPE-----
//-----------------------------
public :
//--------------------------------------- //---------------------------------------
//---------COPLIEN FORM FUNCTION--------- //---------COPLIEN FORM FUNCTION---------
@ -117,36 +115,35 @@ class map
map( const map& x) : _comp(x._comp), _alloc(x._alloc), _node_alloc(x._node_alloc) map( const map& x) : _comp(x._comp), _alloc(x._alloc), _node_alloc(x._node_alloc)
{ {
_size = 0; _size = 0;
_end = _node_alloc.allocate(1); _end = _node_alloc.allocate(1);
_node_alloc.construct(_end, node()); _node_alloc.construct(_end, node());
_root = _end; _root = _end;
insert(x.begin(), x.end()); insert(x.begin(), x.end());
//*this = x;
} }
~map() ~map()
{ {
// if (_size && _root && _root != _end)
// this->clear(); // this->clear();
//if (_size)
//{
this->destructTree(_root);
this->destructNode(_end);
//}
} }
map& operator=(const map& x) map& operator=(const map& x)
{ {
this->clear(); this->clear();
// _comp = x._comp;
// _alloc = x._alloc;
// _node_alloc = x._node_alloc;
// _root = _end;
// _size = 0;
// insert(x.begin(), x.end());
_comp = (x._comp); _comp = (x._comp);
_alloc = (x._alloc); _alloc = (x._alloc);
_node_alloc = (x._node_alloc); _node_alloc = (x._node_alloc);
//destructNode(_end);
//_end = x._end;
_size = 0; _size = 0;
_end = _node_alloc.allocate(1); //_end = _node_alloc.allocate(1);
_node_alloc.construct(_end, node()); //_node_alloc.construct(_end, node());
_root = _end; _root = _end;
insert(x.begin(), x.end()); insert(x.begin(), x.end());
return (*this); return (*this);
@ -165,12 +162,6 @@ class map
if (_root == _end) if (_root == _end)
return (it); return (it);
iterator ret(_root, _end, it.minimum(_root)); iterator ret(_root, _end, it.minimum(_root));
//std::cout << "root key: " << _root->data.first << " | value: " << _root->data.second << std::endl;
//std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl;
//ret++;
//std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl;
//return iterator(_root, it.minimum(_root));
return ret; return ret;
} }
@ -180,12 +171,6 @@ class map
if (_root == _end) if (_root == _end)
return (it); return (it);
const_iterator ret(_root, _end, it.minimum(_root)); const_iterator ret(_root, _end, it.minimum(_root));
//std::cout << "root key: " << _root->data.first << " | value: " << _root->data.second << std::endl;
//std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl;
//ret++;
//std::cout << "ret key: " << ret->first << " | value: " << ret->second << std::endl;
//return iterator(_root, it.minimum(_root));
return ret; return ret;
} }
@ -219,7 +204,6 @@ class map
return const_reverse_iterator(this->begin()); return const_reverse_iterator(this->begin());
} }
//------------------------ //------------------------
//--------Capacity-------- //--------Capacity--------
//------------------------ //------------------------
@ -247,20 +231,14 @@ class map
//------------------------------ //------------------------------
mapped_type& operator[] (const key_type& k) mapped_type& operator[] (const key_type& k)
{ {
//std::cout << "here\n";
iterator tmp = this->find(k); iterator tmp = this->find(k);
//std::cout << "here3\n";
NodePtr pt; NodePtr pt;
//ft::pair<> new_pair = ft::make_pair(k, mapped_type());
//value_type new_pair = ft::make_pair(k, mapped_type());
//std::cout << "base== " << tmp.base() << std::endl;
if (tmp.base() == _end) if (tmp.base() == _end)
{ {
pt = new_node(k, mapped_type()); pt = newNode(k, mapped_type());
insert(pt); insert(pt);
_size++; _size++;
return (pt->data.second); return (pt->data.second);
//return (this->insert_node(_root, new_node(k, mapped_type()))->data.second); //??????
} }
else else
return ((*tmp).second); return ((*tmp).second);
@ -294,9 +272,9 @@ class map
iterator find = this->find(val.first); iterator find = this->find(val.first);
if (find.base() != _end) if (find.base() != _end)
return (ft::make_pair(find, false)); return (ft::make_pair(find, false));
NodePtr pt = new_node(val.first, val.second); NodePtr pt = newNode(val.first, val.second);
insert(pt); insert(pt);
//fixViolation(_root, pt); //iterator ret = find(val.first);
_size++; _size++;
return (ft::make_pair(iterator(_root, _end, pt), true)); return (ft::make_pair(iterator(_root, _end, pt), true));
} }
@ -304,20 +282,20 @@ class map
iterator insert (iterator position, const value_type& val) iterator insert (iterator position, const value_type& val)
{ {
(void)position; (void)position;
NodePtr pt = new_node(val.first, val.second); NodePtr pt = newNode(val.first, val.second);
insert(pt); insert(pt);
//fixViolation(_root, pt);
_size++; _size++;
return (iterator(_root, _end, pt)); iterator ret = find(val.first);
return (ret);
//return (iterator(_root, _end, pt));
} }
template <class InputIterator> template <class InputIterator>
void insert (InputIterator first, InputIterator last) void insert (InputIterator first, InputIterator last)
{ {
while (first != last) while (first != last)
{ {
NodePtr pt = new_node(first->first, first->second); NodePtr pt = newNode(first->first, first->second);
insert(pt); insert(pt);
first++; first++;
_size++; _size++;
@ -326,16 +304,13 @@ class map
void erase (iterator position) void erase (iterator position)
{ {
//std::cout << "position"<< std::endl;
deleteNode(_root, position->first); deleteNode(_root, position->first);
_size--; _size--;
} }
size_type erase (const key_type& k) size_type erase (const key_type& k)
{ {
//std::cout << "key== "<< k << std::endl;
iterator test = find(k); iterator test = find(k);
//std::cout << "test-base= " << test.base() << std::endl;
if (test.base() != _end) if (test.base() != _end)
{ {
deleteNode(_root, test->first); deleteNode(_root, test->first);
@ -347,85 +322,56 @@ class map
void erase (iterator first, iterator last) void erase (iterator first, iterator last)
{ {
// printHelper(_root, "", true);
// std::cout << "range"<< std::endl;
// std::cout << "_end== "<< _end << std::endl;
// std::cout << "first== "<< first.base() << std::endl;
// std::cout << "first-r== "<< first.base()->right << std::endl;
// std::cout << "first-f== "<< first.base()->left << std::endl;
//int i = 0;
//std::cout << "== "<< _end << std::endl;
while (first != last) while (first != last)
{ {
//std::cout << "i = "<< i++ << std::endl; deleteNode(_root, (first++)->first);
//last--;
//NodePtr pt = new_node(first->first, first->second);
deleteNode(_root, (first++)->first);//insert(pt);
//std::cout << "fitsrbase before== "<< first.base() << std::endl;
//first++;
//std::cout << "fitsrbase== "<< first.base() << std::endl;
//std::cout << "lastbase== "<< last.base() << std::endl;
_size--; _size--;
} }
// //last--;
// while (first != last)
// {
// //_size--;
// erase(first->first);
// first++;
// }
} }
void swap (map& x) void swap (map& x)
{ {
map tmp; //map tmp;
tmp->_comp = _comp; // key_compare _comp;
tmp->_alloc = _alloc; // allocator_type _alloc;
tmp->_node_alloc = _node_alloc; // node_allocator_type _node_alloc;
tmp->_root = _root;
tmp->_size = _size;
_comp = x->_comp; // NodePtr _root;
_alloc = x->_alloc; // NodePtr _end;
_node_alloc = x->_node_alloc; // size_type _size;
_root = x->_root;
_size = x->_size;
x->_comp = tmp-> _comp; // //destructNode(tmp._end);
x->_alloc = tmp->_alloc; key_compare tmp_comp = _comp;
x->_node_alloc = tmp->_node_alloc; allocator_type tmp_alloc = _alloc;
x->_root = tmp->_root; node_allocator_type tmp_node_alloc = _node_alloc;
//x->_end = tmp->_end; NodePtr tmp_root = _root;
x->_size = tmp->_size; NodePtr tmp_end = _end;
size_type tmp_size = _size;
_comp = x._comp;
_alloc = x._alloc;
_node_alloc = x._node_alloc;
_root = x._root;
_size = x._size;
_end = x._end;
//x = tmp;
x._comp = tmp_comp;
x._alloc = tmp_alloc;
x._node_alloc = tmp_node_alloc;
x._root = tmp_root;
x._size = tmp_size;
x._end = tmp_end;
//tmp._end = 0;
//tmp._size = 0;
} }
void clear() void clear()
{ {
//int i = 0; this->destructTree(_root);
//std::cout << "size = " << _size << std::endl; // this->destructNode(_end);
//if(!_size)
// std::cout << "s = " << std::endl;
///if(_size - 1 > 0)
// std::cout << "sizerdtgserg = " << std::endl;
iterator it;
while (_size && _size - 1 > 0)
{
//
it = this->begin();
//std::cout << "_root key = " << _root->data.first << "_root value = " << _root->data.second << std::endl;
//std::cout << "key = " << it->first << "value = " << it->second << std::endl;
deleteNode(_root, it->first/**/);
_size--;
//std::cout << "size === " << _size << std::endl;
//printHelper(_root, "", true);
//it++;
}
destruct_node(_root);
_size = 0; _size = 0;
_root = _end; _root = _end;
} }
@ -444,7 +390,6 @@ class map
return (value_compare(_comp)); return (value_compare(_comp));
} }
//------------------------- //-------------------------
//-------Operations-------- //-------Operations--------
//------------------------- //-------------------------
@ -472,28 +417,6 @@ class map
} }
} }
return iterator(_root, _end, temp); return iterator(_root, _end, temp);
// NodePtr temp = _root;
// while (temp != NULL)
// {
// if (k < temp->data.first)
// {
// //if (temp->left == NULL)
// // break;
// //else
// temp = temp->left;
// }
// else if (k == temp->data.first)
// break;
// else
// {
// //if (temp->right == NULL)
// // break;
// //else
// temp = temp->right;
// }
// }
// return iterator(_root, _end, temp);
} }
const_iterator find(const key_type& k) const const_iterator find(const key_type& k) const
@ -597,24 +520,32 @@ class map
/* ************************************************************************** */ /* ************************************************************************** */
/* ************************************************************************** */ /* ************************************************************************** */
/* ************************************************************************** */ /* ************************************************************************** */
private :
void deleteFix(NodePtr x) { void deleteFix(NodePtr x)
{
NodePtr s; NodePtr s;
while (x != _root && x->color == 0) { while (x != _root && x->color == 0)
if (x == x->parent->left) { {
if (x == x->parent->left) //x is left child
{
s = x->parent->right; s = x->parent->right;
if (s->color == 1) { if (s->color == 1)
{
s->color = 0; s->color = 0;
x->parent->color = 1; x->parent->color = 1;
leftRotate(x->parent); leftRotate(x->parent);
s = x->parent->right; s = x->parent->right;
} }
if (s->left->color == 0 && s->right->color == 0)
if (s->left->color == 0 && s->right->color == 0) { {
s->color = 1; s->color = 1;
x = x->parent; x = x->parent;
} else { }
if (s->right->color == 0) { else
{
if (s->right->color == 0)
{
s->left->color = 0; s->left->color = 0;
s->color = 1; s->color = 1;
rightRotate(s); rightRotate(s);
@ -627,26 +558,31 @@ class map
leftRotate(x->parent); leftRotate(x->parent);
x = _root; x = _root;
} }
} else { }
else //x is right child same but with right and left inversed
{
s = x->parent->left; s = x->parent->left;
if (s->color == 1) { if (s->color == 1)
{
s->color = 0; s->color = 0;
x->parent->color = 1; x->parent->color = 1;
rightRotate(x->parent); rightRotate(x->parent);
s = x->parent->left; s = x->parent->left;
} }
if (s->right->color == 0 && s->right->color == 0)
if (s->right->color == 0 && s->right->color == 0) { {
s->color = 1; s->color = 1;
x = x->parent; x = x->parent;
} else { }
if (s->left->color == 0) { else
{
if (s->left->color == 0)
{
s->right->color = 0; s->right->color = 0;
s->color = 1; s->color = 1;
leftRotate(s); leftRotate(s);
s = x->parent->left; s = x->parent->left;
} }
s->color = x->parent->color; s->color = x->parent->color;
x->parent->color = 0; x->parent->color = 0;
s->left->color = 0; s->left->color = 0;
@ -658,81 +594,89 @@ class map
x->color = 0; x->color = 0;
} }
void rbTransplant(NodePtr u, NodePtr v) { void transplant(NodePtr u, NodePtr v)
if (u->parent == NULL) { {
if (u->parent == NULL)
_root = v; _root = v;
} else if (u == u->parent->left) { else if (u == u->parent->left)
u->parent->left = v; u->parent->left = v;
} else { else
u->parent->right = v; u->parent->right = v;
}
v->parent = u->parent; v->parent = u->parent;
} }
void deleteNode(NodePtr node, int key) { void deleteNode(NodePtr node, int key)
{
NodePtr z = _end; NodePtr z = _end;
NodePtr x, y; NodePtr x, y;
while (node != _end) { while (node != _end)
if (node->data.first == key) { {
if (node->data.first == key)
z = node; z = node;
} if (node->data.first <= key)
if (node->data.first <= key) {
node = node->right; node = node->right;
} else { else
node = node->left; node = node->left;
} }
} if (z == _end)
{
if (z == _end) {
_size++; _size++;
//std::cout << "Key not found in the tree" << std::endl;
return; return;
} }
y = z; y = z;
int y_original_color = y->color; int y_original_color = y->color;
if (z->left == _end) { if (z->left == _end)
{
x = z->right; x = z->right;
rbTransplant(z, z->right); transplant(z, z->right);
} else if (z->right == _end) { }
else if (z->right == _end)
{
x = z->left; x = z->left;
rbTransplant(z, z->left); transplant(z, z->left);
} else { }
else
{
y = minimum(z->right); y = minimum(z->right);
y_original_color = y->color; y_original_color = y->color;
x = y->right; x = y->right;
if (y->parent == z) { if (y->parent == z)
x->parent = y; x->parent = y;
} else { else
rbTransplant(y, y->right); {
transplant(y, y->right);
y->right = z->right; y->right = z->right;
y->right->parent = y; y->right->parent = y;
} }
transplant(z, y);
rbTransplant(z, y);
y->left = z->left; y->left = z->left;
y->left->parent = y; y->left->parent = y;
y->color = z->color; y->color = z->color;
} }
delete z; destructNode(z);
if (y_original_color == 0) { if (y_original_color == 0)
deleteFix(x); deleteFix(x);
} }
}
void insertFix(NodePtr k) { void insertFix(NodePtr k)
{
NodePtr u; NodePtr u;
while (k->parent->color == 1) { while (k->parent->color == 1)
if (k->parent == k->parent->parent->right) { {
if (k->parent == k->parent->parent->right)
{
u = k->parent->parent->left; u = k->parent->parent->left;
if (u->color == 1) { if (u->color == 1)
{
u->color = 0; u->color = 0;
k->parent->color = 0; k->parent->color = 0;
k->parent->parent->color = 1; k->parent->parent->color = 1;
k = k->parent->parent; k = k->parent->parent;
} else { }
if (k == k->parent->left) { else
{
if (k == k->parent->left)
{
k = k->parent; k = k->parent;
rightRotate(k); rightRotate(k);
} }
@ -740,16 +684,21 @@ class map
k->parent->parent->color = 1; k->parent->parent->color = 1;
leftRotate(k->parent->parent); leftRotate(k->parent->parent);
} }
} else { }
else//same but left become right..
{
u = k->parent->parent->right; u = k->parent->parent->right;
if (u->color == 1)
if (u->color == 1) { {
u->color = 0; u->color = 0;
k->parent->color = 0; k->parent->color = 0;
k->parent->parent->color = 1; k->parent->parent->color = 1;
k = k->parent->parent; k = k->parent->parent;
} else { }
if (k == k->parent->right) { else
{
if (k == k->parent->right)
{
k = k->parent; k = k->parent;
leftRotate(k); leftRotate(k);
} }
@ -758,139 +707,124 @@ class map
rightRotate(k->parent->parent); rightRotate(k->parent->parent);
} }
} }
if (k == _root) { if (k == _root)
break; break;
} }
}
_root->color = 0; _root->color = 0;
} }
NodePtr minimum(NodePtr node) { void insert(NodePtr node)
while (node->left != _end) { {
NodePtr y = NULL;
NodePtr x = _root;
while (x != _end)
{
y = x;
if (node->data.first == x->data.first)
{
_size--;
destructNode(node);
return ;
}
else if (_comp(node->data.first, x->data.first))
x = x->left;
else
x = x->right;
}
node->parent = y;
if (y == NULL)
_root = node;
else if (_comp(node->data.first, y->data.first))
y->left = node;
else
y->right = node;
if (node->parent == NULL)
{
node->color = 0;
return;
}
if (node->parent->parent == NULL)
return;
insertFix(node);
}
NodePtr minimum(NodePtr node)
{
while (node->left != _end)
node = node->left; node = node->left;
}
return node; return node;
} }
NodePtr maximum(NodePtr node) { NodePtr maximum(NodePtr node)
while (node->right != _end) { {
while (node->right != _end)
node = node->right; node = node->right;
}
return node; return node;
} }
NodePtr successor(NodePtr x) { NodePtr successor(NodePtr x)
if (x->right != _end) { {
if (x->right != _end)
return minimum(x->right); return minimum(x->right);
}
NodePtr y = x->parent; NodePtr y = x->parent;
while (y != _end && x == y->right) { while (y != _end && x == y->right)
{
x = y; x = y;
y = y->parent; y = y->parent;
} }
return y; return y;
} }
NodePtr predecessor(NodePtr x) { NodePtr predecessor(NodePtr x)
if (x->left != _end) { {
if (x->left != _end)
return maximum(x->left); return maximum(x->left);
}
NodePtr y = x->parent; NodePtr y = x->parent;
while (y != _end && x == y->left) { while (y != _end && x == y->left)
{
x = y; x = y;
y = y->parent; y = y->parent;
} }
return y; return y;
} }
void leftRotate(NodePtr x) { void leftRotate(NodePtr x)
{
NodePtr y = x->right; NodePtr y = x->right;
x->right = y->left; x->right = y->left;
if (y->left != _end) { if (y->left != _end)
y->left->parent = x; y->left->parent = x;
}
y->parent = x->parent; y->parent = x->parent;
if (x->parent == NULL) { if (x->parent == NULL)
_root = y; _root = y;
} else if (x == x->parent->left) { else if (x == x->parent->left)
x->parent->left = y; x->parent->left = y;
} else { else
x->parent->right = y; x->parent->right = y;
}
y->left = x; y->left = x;
x->parent = y; x->parent = y;
} }
void rightRotate(NodePtr x) { void rightRotate(NodePtr x)
{
NodePtr y = x->left; NodePtr y = x->left;
x->left = y->right; x->left = y->right;
if (y->right != _end) { if (y->right != _end)
y->right->parent = x; y->right->parent = x;
}
y->parent = x->parent; y->parent = x->parent;
if (x->parent == NULL) { if (x->parent == NULL)
_root = y; _root = y;
} else if (x == x->parent->right) { else if (x == x->parent->right)
x->parent->right = y; x->parent->right = y;
} else { else
x->parent->left = y; x->parent->left = y;
}
y->right = x; y->right = x;
x->parent = y; x->parent = y;
} }
// Inserting a node NodePtr newNode(key_type key, mapped_type val)
void insert(NodePtr node) {
// NodePtr node = new Node;
// node->parent = NULL;
// node->data = key;
// node->left = _end;
// node->right = _end;
// node->color = 1;
NodePtr y = NULL;
NodePtr x = _root;
//std::cout << "root key= " << _root->data.first << " root value= " << _root->data.second << "end== " << _end << std::endl;
while (x != _end)
{
y = x;
if (node->data.first < x->data.first)
x = x->left;
else if (node->data.first > x->data.first)
x = x->right;
else
{
_size--;
return ;
}
}
node->parent = y;
if (y == NULL) {
_root = node;
} else if (node->data.first < y->data.first) {
y->left = node;
} else {
y->right = node;
}
if (node->parent == NULL) {
node->color = 0;
return;
}
if (node->parent->parent == NULL) {
return;
}
insertFix(node);
}
NodePtr new_node(key_type key, mapped_type val)
{ {
NodePtr ret; NodePtr ret;
@ -899,11 +833,24 @@ class map
return (ret); return (ret);
} }
void destruct_node(node *x) void destructTree(node *x)//recursive function to clear tree
{
if (x == _end)
return;
destructTree(x->left);
destructTree(x->right);
_node_alloc.destroy(x);
_node_alloc.deallocate(x, 1);
}
void destructNode(node *x)
{
if (x)
{ {
_node_alloc.destroy(x); _node_alloc.destroy(x);
_node_alloc.deallocate(x, 1); _node_alloc.deallocate(x, 1);
} }
}
void printHelper(NodePtr _root, std::string indent, bool last) void printHelper(NodePtr _root, std::string indent, bool last)
{ {
@ -927,7 +874,8 @@ class map
} }
} }
}; }; //end of map class
//---------------------------------- //----------------------------------
//----------COMPARE CLASS----------- //----------COMPARE CLASS-----------
//---------------------------------- //----------------------------------
@ -949,4 +897,46 @@ class map<Key,T,Compare,Alloc>::value_compare //man map::value_compare
} }
}; };
//----------------------------------
//-------NON-MEMBER OPERATOR--------
//----------------------------------
template< class Key, class T, class Compare, class Alloc >
bool operator==( const ft::map<Key, T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
if (lhs.size() != rhs.size())
return false;
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template< class Key, class T, class Compare, class Alloc >
bool operator!=( const ft::map<Key, T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
return (!(rhs == lhs));
}
template< class Key, class T, class Compare, class Alloc >
bool operator<( const ft::map<Key, T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template< class Key, class T, class Compare, class Alloc >
bool operator<=( const ft::map<Key ,T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
return (!(rhs < lhs));
}
template< class Key, class T, class Compare, class Alloc >
bool operator>( const ft::map<Key, T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
return (rhs < lhs);
}
template< class Key, class T, class Compare, class Alloc >
bool operator>=( const ft::map<Key, T, Compare, Alloc>& lhs, const ft::map<Key,T,Compare,Alloc>& rhs )
{
return (!(lhs < rhs));
}
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:30 by apommier #+# #+# */ /* Created: 2022/10/12 19:46:30 by apommier #+# #+# */
/* Updated: 2022/10/21 13:43:05 by apommier ### ########.fr */ /* Updated: 2022/11/28 15:56:04 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -76,7 +76,7 @@ class stack
void push (const value_type& val) void push (const value_type& val)
{ {
return (c.push_back()); return (c.push_back(val));
} }
void pop() void pop()
@ -84,47 +84,58 @@ class stack
return (c.pop_back()); return (c.pop_back());
} }
template <class type, class C>
friend bool operator==(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
template <class type, class C>
friend bool operator!=(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
template <class type, class C>
friend bool operator<(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
template <class type, class C>
friend bool operator<=(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
template <class type, class C>
friend bool operator>(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
template <class type, class C>
friend bool operator>=(const ft::stack<type,C>& lhs, const ft::stack<type,C>& rhs );
};
//--------------------------------------------- //---------------------------------------------
//---------OPERATOR OVERLOAD FUNCTION---------- //---------OPERATOR OVERLOAD FUNCTION----------
//--------------------------------------------- //---------------------------------------------
// template <class T, class Container> template< class T, class Container >
// bool operator== (const stack<T,Container>& lhs, const stack<T,Container>& rhs) bool operator==(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
// { {
return (lhs.c == rhs.c);
}
// } template< class T, class Container >
bool operator!=(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
{
return (!(lhs.c == rhs.c));
}
// template <class T, class Container> template< class T, class Container >
// bool operator!= (const stack<T,Container>& lhs, const stack<T,Container>& rhs) bool operator<(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
// { {
return (lhs.c < rhs.c);
}
// } template< class T, class Container >
bool operator<=(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
{
return (!(rhs.c < lhs.c));
}
// template <class T, class Container> template< class T, class Container >
// bool operator< (const stack<T,Container>& lhs, const stack<T,Container>& rhs) bool operator>(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
// { {
return (rhs.c < lhs.c);
}
// } template< class T, class Container >
bool operator>=(const ft::stack<T,Container>& lhs, const ft::stack<T,Container>& rhs )
// template <class T, class Container> {
// bool operator<= (const stack<T,Container>& lhs, const stack<T,Container>& rhs) return (!(lhs.c < rhs.c));
// { }
// }
// template <class T, class Container>
// bool operator> (const stack<T,Container>& lhs, const stack<T,Container>& rhs)
// {
// }
// template <class T, class Container>
// bool operator>= (const stack<T,Container>& lhs, const stack<T,Container>& rhs)
// {
// }
};
} }
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:32 by apommier #+# #+# */ /* Created: 2022/10/12 19:46:32 by apommier #+# #+# */
/* Updated: 2022/11/25 15:54:52 by apommier ### ########.fr */ /* Updated: 2022/11/28 21:36:36 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@
# include "./iterators/enable_if.hpp" # include "./iterators/enable_if.hpp"
# include "./iterators/is_integral.hpp" # include "./iterators/is_integral.hpp"
# include "./iterators/reverse_iterator.hpp" # include "./iterators/reverse_iterator.hpp"
//# include "./iterators/iterator_traits.hpp"
# include <cstddef> # include <cstddef>
# include <memory> # include <memory>
@ -46,8 +45,6 @@ class vector
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator; typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type; typedef std::size_t size_type;
//typedef random_access_iterator iterator_category;
//----------------------------- //-----------------------------
//-----PRIVATE MEMBER TYPE----- //-----PRIVATE MEMBER TYPE-----
@ -59,10 +56,6 @@ class vector
size_type _capacity; size_type _capacity;
allocator_type _alloc; allocator_type _alloc;
//ft::random_access_iterator<value_type> _end;
//ft::random_access_iterator<value_type> _start;
//pointer _end_capacity;
public: public:
@ -82,29 +75,52 @@ class vector
_tab = _alloc.allocate(n); _tab = _alloc.allocate(n);
_size = n; _size = n;
_capacity = n; _capacity = n;
while (n--) for (size_type i = 0; i < n ; i++)
_alloc.construct(_tab + n, val); _alloc.construct(_tab + i, val);
}
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type(), typename ft::enable_if<!ft::is_integral<InputIterator>::value, InputIterator>::type* = NULL) : _alloc(alloc) //range constructor
{
_tab = 0;
difference_type diff = 0;
InputIterator tmp = first;
while(tmp != last)
{
diff++;
tmp++;
}
_size = diff;
_capacity = diff;
_tab = _alloc.allocate(_capacity);
for (size_type i = 0; i < _size; i++)
_alloc.construct(_tab + i, *first++);
} }
vector (const vector& x)//copy constructor vector (const vector& x)//copy constructor
{ {
_tab = 0;
_size = 0;
_capacity = 0;
*this = x; *this = x;
} }
~vector() ~vector()
{ {
//std::cout << "destct" << std::endl; if (_capacity)
if (_capacity == 0)
{ {
_alloc.deallocate(_tab, _capacity); _alloc.deallocate(_tab, _capacity);
_capacity = 0; _capacity = 0;
} }
//std::cout << "destct end" << std::endl;
} }
vector& operator= (const vector& x)//assignation operator vector& operator= (const vector& x)//assignation operator
{ {
//this->~vector(); if (_capacity)
{
_alloc.deallocate(_tab, _capacity);
_capacity = 0;
}
_alloc = x._alloc; _alloc = x._alloc;
_size = x._size; _size = x._size;
_capacity = x._size; _capacity = x._size;
@ -176,12 +192,10 @@ class vector
void resize (size_type n, value_type val = value_type()) //Resizes the container so that it contains n elements. void resize (size_type n, value_type val = value_type()) //Resizes the container so that it contains n elements.
{ {
//std::cout << "resize------val = " << val << std::endl;
if (n < _size) if (n < _size)
{ {
while (n < _size) while (n < _size)
{ {
//_end--;
_size--; _size--;
_alloc.destroy(_tab + _size); _alloc.destroy(_tab + _size);
} }
@ -193,11 +207,9 @@ class vector
{ {
_alloc.construct(_tab + _size, val); _alloc.construct(_tab + _size, val);
_size++; _size++;
//_end++;
} }
} }
//_size = n;
} }
size_type capacity() const size_type capacity() const
@ -219,9 +231,10 @@ class vector
else if (n > _capacity) else if (n > _capacity)
{ {
value_type *tmp; value_type *tmp;
tmp = _alloc.allocate(n, 0); tmp = _alloc.allocate(n);
for (int i = 0; _tab + i != _tab + _size; i++) for (size_type i = 0; i < _size; i++)
*(tmp + i) = *(_tab + i); _alloc.construct(tmp + i, *(_tab + i));
if (_capacity)
_alloc.deallocate(_tab, _capacity); _alloc.deallocate(_tab, _capacity);
_tab = tmp; _tab = tmp;
_capacity = n; _capacity = n;
@ -243,7 +256,6 @@ class vector
reference at (size_type n) reference at (size_type n)
{ {
//exception
if (n >= _size) if (n >= _size)
throw (std::out_of_range("ft::vector::at")); throw (std::out_of_range("ft::vector::at"));
return (*(_tab + n)); return (*(_tab + n));
@ -251,7 +263,6 @@ class vector
const_reference at (size_type n) const const_reference at (size_type n) const
{ {
//exception
if (n >= _size) if (n >= _size)
throw (std::out_of_range("ft::vector::at")); throw (std::out_of_range("ft::vector::at"));
return (*(_tab + n)); return (*(_tab + n));
@ -292,7 +303,6 @@ class vector
//------------------------- //-------------------------
template <class InputIterator> template <class InputIterator>
//void assign (InputIterator first, InputIterator last) //range
void assign (InputIterator first, InputIterator last, typename ft::enable_if<!ft::is_integral<InputIterator>::value, InputIterator>::type* = 0) void assign (InputIterator first, InputIterator last, typename ft::enable_if<!ft::is_integral<InputIterator>::value, InputIterator>::type* = 0)
{ {
this->clear(); this->clear();
@ -319,7 +329,6 @@ class vector
this->reserve(1); this->reserve(1);
else if (_size == _capacity) else if (_size == _capacity)
this->reserve(_size * 2); this->reserve(_size * 2);
//std::cout << "coucou1\n";
_alloc.construct(_tab + _size, val); _alloc.construct(_tab + _size, val);
_size++; _size++;
} }
@ -335,68 +344,54 @@ class vector
iterator insert (iterator position, const value_type& val) //single element iterator insert (iterator position, const value_type& val) //single element
{ {
this->insert(position, 1, val); difference_type index = position - begin();
return (position);
insert(position, 1, val);
return (iterator(begin() + index));
} }
void insert (iterator position, size_type n, const value_type& val) //fill void insert (iterator position, size_type n, const value_type& val) //fill
{ {
int j = 0; difference_type const pos_diff = position - begin();
int i; difference_type const old_end_diff = end() - begin();
value_type *tmp; iterator old_end;
iterator new_end;
if (_size + n > this->max_size()) resize(this->_size + n);
throw (std::length_error("vector::resize")); new_end = end();
tmp = _alloc.allocate(_size + n); position = begin() + pos_diff;
for (i = 0; i < position; i++) old_end = begin() + old_end_diff;
tmp[i] = _tab[i]; while (old_end != position)
while (n - j) *--new_end = *--old_end;
{ while (n-- > 0)
tmp[i + j] = val; *position++ = val;
j++;
}
while (_size - i)
tmp[i + j] = _tab[i];
this->clear();
_tab = tmp;
_size += n;
_capacity = _size;
} }
template <class InputIterator> template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last) //range void insert (iterator position, InputIterator first, InputIterator last, typename ft::enable_if<!ft::is_integral<InputIterator>::value, InputIterator>::type* = NULL)
{ {
int j = 0; size_type n = 0;
int i; for (InputIterator tmp = first; tmp != last; tmp++) {
value_type *tmp; n++;
if (_size + (last - first) > this->max_size())
throw (std::length_error("vector::resize"));
tmp = _alloc.allocate(_size + (last - first));
for (i = 0; i < position - _tab; i++)
tmp[i] = _tab[i];
while (first + j != last)
{
tmp[i + j] = *(j + first);
j++;
} }
while (_size - i) difference_type const pos_diff = position - begin();
tmp[i + j] = _tab[i]; difference_type const old_end_diff = end() - begin();
this->clear(); iterator old_end;
_tab = tmp; iterator new_end;
_size += (last - first);
_capacity = _size; resize(_size + n);
new_end = end();
position = begin() + pos_diff;
old_end = begin() + old_end_diff;
while (old_end != position)
*--new_end = *--old_end;
while (first != last)
*position++ = *first++;
} }
iterator erase (iterator position) iterator erase (iterator position)
{ {
_alloc.destroy(&(*position)); _alloc.destroy(&(*position));
// while (_tab + _size - 1 > _tab + (position - _tab))
// {
// *position = *(position + 1);
// position++;
// _alloc.destroy(&(*position));
// }
for (size_type i = 0; _tab + _size - 1 > _tab + (position - _tab + i); i++) for (size_type i = 0; _tab + _size - 1 > _tab + (position - _tab + i); i++)
{ {
_alloc.construct(&(*(position + i)), *(position + i + 1)); _alloc.construct(&(*(position + i)), *(position + i + 1));
@ -409,7 +404,6 @@ class vector
iterator erase (iterator first, iterator last) iterator erase (iterator first, iterator last)
{ {
difference_type lenght = last - first; difference_type lenght = last - first;
//std::cout << "diff = " << lenght << std::endl;
int i; int i;
for (i = 0; lenght - i - 1; i++) for (i = 0; lenght - i - 1; i++)
@ -419,11 +413,6 @@ class vector
_alloc.construct(&(*(first + i)), *(first + lenght + i)); _alloc.construct(&(*(first + i)), *(first + lenght + i));
_alloc.destroy(&(*(first + lenght + i ))); _alloc.destroy(&(*(first + lenght + i )));
} }
// for (; _size - lenght - i; i++)
// {
// _alloc.construct(&(*(first + i)), *(first + lenght + i));
// _alloc.destroy(&(*(first + _size - lenght + i )));
// }
_size -= lenght; _size -= lenght;
return (first); return (first);
} }
@ -464,13 +453,6 @@ class vector
//-------Comparaison------- //-------Comparaison-------
//------------------------- //-------------------------
// bool operator ==(const random_access_iterator &b) { return (this->_tab == b); }
// bool operator !=(const random_access_iterator &b) { return (this->_tab != b); }
// bool operator <(const random_access_iterator &b) { return (this->_tab < b); }
// bool operator >(const random_access_iterator &b) { return (this->_tab > b); }
// bool operator <=(const random_access_iterator &b) { return (this->_tab <= b); }
// bool operator >=(const random_access_iterator &b) { return (this->_tab >= b); }
template <class Temp, class Alloc> template <class Temp, class Alloc>
friend bool operator== (const vector<Temp,Alloc>& lhs, const vector<Temp,Alloc>& rhs); friend bool operator== (const vector<Temp,Alloc>& lhs, const vector<Temp,Alloc>& rhs);
template <class Temp, class Alloc> template <class Temp, class Alloc>