en fait nan map pas finis

This commit is contained in:
kinou-p 2022-11-26 17:55:16 +01:00
parent 49051b5571
commit c8d919bd05
2 changed files with 23 additions and 81 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/26 15:23:32 by apommier #+# #+# */ /* Created: 2022/11/26 15:23:32 by apommier #+# #+# */
/* Updated: 2022/11/26 17:03:16 by apommier ### ########.fr */ /* Updated: 2022/11/26 17:36:15 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,7 +67,6 @@ class map
node_allocator_type _node_alloc; node_allocator_type _node_alloc;
node *_root; node *_root;
//node *_end;
size_type _size; size_type _size;
public : public :
@ -80,11 +79,9 @@ class map
node *left; node *left;
bool color; bool color;
//node() : parent(0), right(map::_end), left(map::_end), color(0) {}
node(key_type const &key, mapped_type const &val) node(key_type const &key, mapped_type const &val)
: data(ft::make_pair(key, val)), parent(0), right(_end), left(_end), color(0) : data(ft::make_pair(key, val)), parent(0), right(_end), left(_end), color(0)
{} {}
//std::cout << "end in construct= " << _end << std::endl;
}; };
//----------------------------- //-----------------------------
//-----PRIVATE MEMBER TYPE----- //-----PRIVATE MEMBER TYPE-----
@ -99,8 +96,6 @@ class map
explicit map( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) explicit map( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end)
{ {
//_end = _node_alloc.allocate(1);
//_node_alloc.construct(_end, node());
_size = 0; _size = 0;
} }
@ -108,8 +103,6 @@ class map
map( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end) map( InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator() ) : _comp(comp), _alloc(alloc), _root(_end)
{ {
_size = 0; _size = 0;
//_end = _node_alloc.allocate(1);
//_node_alloc.construct(_end, node());
this->insert(first, last); this->insert(first, last);
} }
@ -129,7 +122,6 @@ class map
_alloc = x._alloc; _alloc = x._alloc;
_node_alloc = x._node_alloc; _node_alloc = x._node_alloc;
_root = x._root; _root = x._root;
//_end = x._end;
_size = x._size; _size = x._size;
return (*this); return (*this);
} }
@ -244,8 +236,6 @@ class map
//------------------------- //-------------------------
ft::pair<iterator,bool> insert (const value_type& val) ft::pair<iterator,bool> insert (const value_type& val)
{ {
// if (this->insert_node(val.first, val.second))
// _size++;
node *pt = new_node(val.first, val.second); node *pt = new_node(val.first, val.second);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
@ -254,8 +244,6 @@ class map
iterator insert (iterator position, const value_type& val) iterator insert (iterator position, const value_type& val)
{ {
(void)position; (void)position;
// if (this->insert_node(val.first, val.second))
// _size++;
node *pt = new_node(val.first, val.second); node *pt = new_node(val.first, val.second);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
@ -264,13 +252,8 @@ class map
template <class InputIterator> template <class InputIterator>
void insert (InputIterator first, InputIterator last) void insert (InputIterator first, InputIterator last)
{ {
int i = 0;
while (first != last) while (first != last)
{ {
// std::cout << "i === " << i++ << std::endl;
// if (this->insert_node(_root, new_node((*first).first, (*first).second)))
// _size++;
// first++;
node *pt = new_node((*first).first, (*first).second); node *pt = new_node((*first).first, (*first).second);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
@ -306,14 +289,12 @@ class map
tmp->_alloc = _alloc; tmp->_alloc = _alloc;
tmp->_node_alloc = _node_alloc; tmp->_node_alloc = _node_alloc;
tmp->_root = _root; tmp->_root = _root;
//tmp->_end = _end;
tmp->_size = _size; tmp->_size = _size;
_comp = x->_comp; _comp = x->_comp;
_alloc = x->_alloc; _alloc = x->_alloc;
_node_alloc = x->_node_alloc; _node_alloc = x->_node_alloc;
_root = x->_root; _root = x->_root;
//_end = x->_end;
_size = x->_size; _size = x->_size;
x->_comp = tmp-> _comp; x->_comp = tmp-> _comp;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/26 15:23:32 by apommier #+# #+# */ /* Created: 2022/11/26 15:23:32 by apommier #+# #+# */
/* Updated: 2022/11/26 17:04:29 by apommier ### ########.fr */ /* Updated: 2022/11/26 17:32:44 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#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 "vector.hpp"
#define RED 1 #define RED 1
#define BLACK 0 #define BLACK 0
@ -28,9 +28,8 @@ namespace ft
template< template<
class Key, class Key,
class T,
class Compare = std::less<Key>, class Compare = std::less<Key>,
class Allocator = std::allocator<ft::pair<const Key, T> > > class Allocator = std::allocator<Key> >
class set class set
{ {
@ -42,8 +41,7 @@ class set
struct node; struct node;
typedef Key key_type; typedef Key key_type;
typedef T mapped_type; typedef Key value_type;
typedef ft::pair<const Key, T> value_type;
typedef std::size_t size_type; typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef Compare key_compare; typedef Compare key_compare;
@ -80,8 +78,8 @@ class set
bool color; bool color;
//node() : parent(0), right(set::_end), left(set::_end), color(0) {} //node() : parent(0), right(set::_end), left(set::_end), color(0) {}
node(key_type const &key, mapped_type const &val) node(key_type const &key)
: data(ft::make_pair(key, val)), parent(0), right(_end), left(_end), color(0) : data(key), parent(0), right(_end), left(_end), color(0)
{} {}
//std::cout << "end in construct= " << _end << std::endl; //std::cout << "end in construct= " << _end << std::endl;
}; };
@ -202,42 +200,6 @@ class set
return (_alloc.max_size()); return (_alloc.max_size());
} }
//------------------------------
//--------Element access--------
//------------------------------
mapped_type& operator[] (const key_type& k)
{
iterator tmp = this->find(k);
//ft::pair<> new_pair = ft::make_pair(k, mapped_type());
value_type new_pair = ft::make_pair(k, mapped_type());
if (tmp.base() == _end)
return this->insert_node(new_pair.first, new_pair.second)->data.second; //??????
else
return ((*tmp).second);
}
mapped_type& at (const key_type& k)
{
iterator tmp = this->find(k);
if (tmp->m == _end)
throw (std::out_of_range("ft::set::at"));
else
return (*tmp.pair.second);
}
const mapped_type& at (const key_type& k) const
{
iterator tmp = this->find(k);
if (tmp->m == _end)
throw (std::out_of_range("ft::set::at"));
else
return (*tmp.pair.second);
}
//------------------------- //-------------------------
//--------Modifiers-------- //--------Modifiers--------
//------------------------- //-------------------------
@ -245,7 +207,7 @@ class set
{ {
// if (this->insert_node(val.first, val.second)) // if (this->insert_node(val.first, val.second))
// _size++; // _size++;
node *pt = new_node(val.first, val.second); node *pt = new_node(val);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
} }
@ -255,7 +217,7 @@ class set
(void)position; (void)position;
// if (this->insert_node(val.first, val.second)) // if (this->insert_node(val.first, val.second))
// _size++; // _size++;
node *pt = new_node(val.first, val.second); node *pt = new_node(val);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
} }
@ -263,14 +225,13 @@ class set
template <class InputIterator> template <class InputIterator>
void insert (InputIterator first, InputIterator last) void insert (InputIterator first, InputIterator last)
{ {
int i = 0;
while (first != last) while (first != last)
{ {
// std::cout << "i === " << i++ << std::endl; // std::cout << "i === " << i++ << std::endl;
// if (this->insert_node(_root, new_node((*first).first, (*first).second))) // if (this->insert_node(_root, new_node((*first).first, (*first).second)))
// _size++; // _size++;
// first++; // first++;
node *pt = new_node((*first).first, (*first).second); node *pt = new_node(*first);
_root = insert_node(_root, pt); _root = insert_node(_root, pt);
fixViolation(_root, pt); fixViolation(_root, pt);
first++; first++;
@ -388,7 +349,7 @@ class set
{ {
iterator it = begin(), ite = end(); iterator it = begin(), ite = end();
while (it != ite && !(_comp((*it).first, k))) while (it != ite && !(_comp((*it), k)))
it++; it++;
return (it); return (it);
} }
@ -397,7 +358,7 @@ class set
{ {
const_iterator it = begin(), ite = end(); const_iterator it = begin(), ite = end();
while (it != ite && !(_comp((*it).first, k))) while (it != ite && !(_comp((*it), k)))
it++; it++;
return (it); return (it);
} }
@ -406,7 +367,7 @@ class set
{ {
iterator it = begin(), ite = end(); iterator it = begin(), ite = end();
while (it != ite && !(_comp((*it).first, k))) while (it != ite && !(_comp((*it), k)))
it++; it++;
return (it); return (it);
} }
@ -415,7 +376,7 @@ class set
{ {
const_iterator it = begin(), ite = end(); const_iterator it = begin(), ite = end();
while (it != ite && _comp((*it).first, k)) while (it != ite && _comp((*it), k))
it++; it++;
return (it); return (it);
} }
@ -777,15 +738,15 @@ class set
} }
//template<typename T_node> //template<typename T_node>
node *new_node(key_type key, mapped_type val) node *new_node(key_type key)
{ {
node *ret; node *ret;
ret = _node_alloc.allocate(1); ret = _node_alloc.allocate(1);
_node_alloc.construct(ret, node(key, val)); _node_alloc.construct(ret, node(key));
//ret = _node_alloc::allocate(1); //ret = _node_alloc::allocate(1);
//_node_alloc::construct(ret, node(key, val)); //_node_alloc::construct(ret, node(key));
return (ret); return (ret);
} }
@ -795,8 +756,8 @@ class set
//----------COMPARE CLASS----------- //----------COMPARE CLASS-----------
//---------------------------------- //----------------------------------
template <class Key, class T, class Compare, class Alloc> template <class Key, class Compare, class Alloc>
class set<Key,T,Compare,Alloc>::value_compare //man set::value_compare class set<Key,Compare,Alloc>::value_compare //man set::value_compare
{ // in C++98, it is required to inherit binary_function<value_type,value_type,bool> { // in C++98, it is required to inherit binary_function<value_type,value_type,bool>
friend class set; friend class set;
protected: protected:
@ -808,7 +769,7 @@ class set<Key,T,Compare,Alloc>::value_compare //man set::value_compare
typedef value_type second_argument_type; typedef value_type second_argument_type;
bool operator() (const value_type& x, const value_type& y) const bool operator() (const value_type& x, const value_type& y) const
{ {
return comp(x.first, y.first); return comp(x, y);
} }
}; };