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

View File

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