diff --git a/containers/map.hpp b/containers/map.hpp index d504a71..d2c51cf 100644 --- a/containers/map.hpp +++ b/containers/map.hpp @@ -6,7 +6,7 @@ /* 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 *_root; - //node *_end; size_type _size; public : @@ -80,11 +79,9 @@ class map node *left; bool color; - //node() : parent(0), right(map::_end), left(map::_end), color(0) {} node(key_type const &key, mapped_type const &val) : 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----- @@ -99,8 +96,6 @@ class map 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; } @@ -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) { _size = 0; - //_end = _node_alloc.allocate(1); - //_node_alloc.construct(_end, node()); this->insert(first, last); } @@ -129,7 +122,6 @@ class map _alloc = x._alloc; _node_alloc = x._node_alloc; _root = x._root; - //_end = x._end; _size = x._size; return (*this); } @@ -244,8 +236,6 @@ class map //------------------------- ft::pair insert (const value_type& val) { - // if (this->insert_node(val.first, val.second)) - // _size++; node *pt = new_node(val.first, val.second); _root = insert_node(_root, pt); fixViolation(_root, pt); @@ -254,8 +244,6 @@ class map iterator insert (iterator position, const value_type& val) { (void)position; - // if (this->insert_node(val.first, val.second)) - // _size++; node *pt = new_node(val.first, val.second); _root = insert_node(_root, pt); fixViolation(_root, pt); @@ -264,13 +252,8 @@ class map template void insert (InputIterator first, InputIterator last) { - int i = 0; 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); _root = insert_node(_root, pt); fixViolation(_root, pt); @@ -306,14 +289,12 @@ class map tmp->_alloc = _alloc; tmp->_node_alloc = _node_alloc; tmp->_root = _root; - //tmp->_end = _end; tmp->_size = _size; _comp = x->_comp; _alloc = x->_alloc; _node_alloc = x->_node_alloc; _root = x->_root; - //_end = x->_end; _size = x->_size; x->_comp = tmp-> _comp; diff --git a/containers/set.hpp b/containers/set.hpp index 251cfe7..0073367 100644 --- a/containers/set.hpp +++ b/containers/set.hpp @@ -6,7 +6,7 @@ /* 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/pair.hpp" #include "./iterators/make_pair.hpp" -#include "vector.hpp" + #define RED 1 #define BLACK 0 @@ -27,10 +27,9 @@ namespace ft { template< - class Key, - class T, - class Compare = std::less, - class Allocator = std::allocator > > + class Key, + class Compare = std::less, + class Allocator = std::allocator > class set { @@ -42,8 +41,7 @@ class set struct node; typedef Key key_type; - typedef T mapped_type; - typedef ft::pair value_type; + typedef Key value_type; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef Compare key_compare; @@ -80,8 +78,8 @@ class set bool color; //node() : parent(0), right(set::_end), left(set::_end), color(0) {} - node(key_type const &key, mapped_type const &val) - : data(ft::make_pair(key, val)), parent(0), right(_end), left(_end), color(0) + node(key_type const &key) + : data(key), parent(0), right(_end), left(_end), color(0) {} //std::cout << "end in construct= " << _end << std::endl; }; @@ -201,51 +199,15 @@ class set { 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-------- //------------------------- - ft::pair insert (const value_type& val) + ft::pair 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); _root = insert_node(_root, pt); fixViolation(_root, pt); } @@ -255,7 +217,7 @@ class set (void)position; // if (this->insert_node(val.first, val.second)) // _size++; - node *pt = new_node(val.first, val.second); + node *pt = new_node(val); _root = insert_node(_root, pt); fixViolation(_root, pt); } @@ -263,14 +225,13 @@ class set template void insert (InputIterator first, InputIterator last) { - int i = 0; 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); _root = insert_node(_root, pt); fixViolation(_root, pt); first++; @@ -388,7 +349,7 @@ class set { iterator it = begin(), ite = end(); - while (it != ite && !(_comp((*it).first, k))) + while (it != ite && !(_comp((*it), k))) it++; return (it); } @@ -397,7 +358,7 @@ class set { const_iterator it = begin(), ite = end(); - while (it != ite && !(_comp((*it).first, k))) + while (it != ite && !(_comp((*it), k))) it++; return (it); } @@ -406,7 +367,7 @@ class set { iterator it = begin(), ite = end(); - while (it != ite && !(_comp((*it).first, k))) + while (it != ite && !(_comp((*it), k))) it++; return (it); } @@ -415,7 +376,7 @@ class set { const_iterator it = begin(), ite = end(); - while (it != ite && _comp((*it).first, k)) + while (it != ite && _comp((*it), k)) it++; return (it); } @@ -777,15 +738,15 @@ class set } //template - node *new_node(key_type key, mapped_type val) + node *new_node(key_type key) { node *ret; ret = _node_alloc.allocate(1); - _node_alloc.construct(ret, node(key, val)); + _node_alloc.construct(ret, node(key)); //ret = _node_alloc::allocate(1); - //_node_alloc::construct(ret, node(key, val)); + //_node_alloc::construct(ret, node(key)); return (ret); } @@ -795,8 +756,8 @@ class set //----------COMPARE CLASS----------- //---------------------------------- -template -class set::value_compare //man set::value_compare +template +class set::value_compare //man set::value_compare { // in C++98, it is required to inherit binary_function friend class set; protected: @@ -808,7 +769,7 @@ class set::value_compare //man set::value_compare typedef value_type second_argument_type; bool operator() (const value_type& x, const value_type& y) const { - return comp(x.first, y.first); + return comp(x, y); } };