map start
This commit is contained in:
parent
1808fc6e96
commit
5ee48e352f
27
containers/iterators/bidirectionnal_iterator.hpp
Normal file
27
containers/iterators/bidirectionnal_iterator.hpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* bidirectionnal_iterator.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/22 14:50:53 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/11/22 14:53:19 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ft
|
||||||
|
{
|
||||||
|
|
||||||
|
class bidirectionnal_iterator
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
public :
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@ -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/22 14:11:41 by apommier ### ########.fr */
|
/* Updated: 2022/11/24 17:38:32 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,24 +14,24 @@
|
|||||||
|
|
||||||
namespace ft
|
namespace ft
|
||||||
{
|
{
|
||||||
template<class _T1, class _T2>
|
template<class T1, class T2>
|
||||||
struct pair{
|
struct pair{
|
||||||
typedef _T1 first_type;
|
typedef T1 first_type;
|
||||||
typedef _T2 second_type;
|
typedef T2 second_type;
|
||||||
|
|
||||||
_T1 _first;
|
T1 first;
|
||||||
_T2 _second;
|
T2 second;
|
||||||
|
|
||||||
pair(): _first(), _second() {}
|
pair(): first(), second() {}
|
||||||
pair( const T1& x, const T2& y ); : _first(x), _second(y) {}
|
pair( const T1& x, const T2& y ) : first(x), second(y) {}
|
||||||
|
|
||||||
template<class _U1, class _U2>
|
template<class U1, class U2>
|
||||||
pair(const pair<_U1, _U2>& p): _first(p._first), _second(p._second) { }
|
pair(const pair<U1, U2>& p): first(p.first), second(p.second) { }
|
||||||
|
|
||||||
pair& operator=(pair& other)
|
pair& operator=(pair& other)
|
||||||
{
|
{
|
||||||
_first = other.first;
|
first = other.first;
|
||||||
_second = other.second;
|
second = other.second;
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -39,19 +39,19 @@ namespace ft
|
|||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
||||||
{
|
{
|
||||||
return (lhs.first==rhs.first && lhs.second==rhs.second);
|
return (lhs.first == rhs.first && lhs.second == rhs.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
||||||
{
|
{
|
||||||
return (!(lhs==rhs));
|
return (!(lhs == rhs));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
|
||||||
{
|
{
|
||||||
return (lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second));
|
return (lhs.first < rhs.first || (!(rhs.first < lhs.first) && lhs.second < rhs.second));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/19 05:14:31 by apommier #+# #+# */
|
/* Created: 2022/11/19 05:14:31 by apommier #+# #+# */
|
||||||
/* Updated: 2022/11/22 11:41:22 by apommier ### ########.fr */
|
/* Updated: 2022/11/23 10:59:47 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -123,16 +123,28 @@ namespace ft
|
|||||||
tmp--;
|
tmp--;
|
||||||
return (*tmp);
|
return (*tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const reference operator *() const
|
const reference operator *() const
|
||||||
{
|
{
|
||||||
iterator_type tmp(_Ite);
|
iterator_type tmp(_Ite);
|
||||||
tmp--;
|
tmp--;
|
||||||
return (*tmp);
|
return (*tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pointer operator ->()
|
||||||
|
{
|
||||||
|
iterator_type tmp(_Ite);
|
||||||
|
tmp--;
|
||||||
|
return (&(*tmp));
|
||||||
|
}
|
||||||
|
|
||||||
|
const pointer operator ->() const
|
||||||
|
{
|
||||||
|
iterator_type tmp(_Ite);
|
||||||
|
tmp--;
|
||||||
|
return (&(*tmp));
|
||||||
|
}
|
||||||
|
|
||||||
pointer operator ->() { return (_Ite.getPointer()); }
|
|
||||||
const pointer operator ->() const { return (_Ite.getPointer()); }
|
|
||||||
reference operator [](difference_type nbr) const { return (*(_Ite - nbr - 1)); }
|
reference operator [](difference_type nbr) const { return (*(_Ite - nbr - 1)); }
|
||||||
|
|
||||||
//---------------------------------------
|
//---------------------------------------
|
||||||
|
|||||||
@ -5,22 +5,29 @@
|
|||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/12 19:46:25 by apommier #+# #+# */
|
/* Created: 2022/11/24 08:50:18 by apommier #+# #+# */
|
||||||
/* Updated: 2022/11/18 11:58:52 by apommier ### ########.fr */
|
/* Updated: 2022/11/24 17:45:57 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef MAP_HPP
|
#pragma once
|
||||||
# define MAP_HPP
|
|
||||||
|
#include "./iterators/bidirectionnal_iterator.hpp"
|
||||||
|
#include "./iterators/pair.hpp"
|
||||||
|
|
||||||
|
#define RED 1
|
||||||
|
#define BLACK 0
|
||||||
|
|
||||||
|
//typedef typename Alloc::template rebind<s_node<T> >::other
|
||||||
|
|
||||||
namespace ft
|
namespace ft
|
||||||
{
|
{
|
||||||
|
|
||||||
template<
|
template<
|
||||||
class Key,
|
class Key,
|
||||||
class T,
|
class T,
|
||||||
class Compare = std::less<Key>,
|
class Compare = std::less<Key>,
|
||||||
class Allocator = std::allocator<std::pair<const Key, T>>>
|
class Allocator = std::allocator<std::pair<const Key, T>>>
|
||||||
|
|
||||||
class map
|
class map
|
||||||
{
|
{
|
||||||
@ -29,50 +36,55 @@ class map
|
|||||||
//-----------------------------
|
//-----------------------------
|
||||||
//---------MEMBER TYPE---------
|
//---------MEMBER TYPE---------
|
||||||
//-----------------------------
|
//-----------------------------
|
||||||
|
struct node;
|
||||||
|
|
||||||
typedef Key key_type;
|
typedef Key key_type;
|
||||||
typedef T mapped_type;
|
typedef T mapped_type;
|
||||||
typedef std::pair<const Key, T> value_type;
|
typedef std::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;
|
||||||
typedef Allocator allocator_type;
|
typedef Allocator allocator_type;
|
||||||
typedef value_type& reference;
|
typedef typename Allocator::template rebind<node>::other node_allocator_type;
|
||||||
typedef const value_type& const_reference;
|
typedef value_type& reference;
|
||||||
typedef Allocator::pointer pointer;
|
typedef const value_type& const_reference;
|
||||||
typedef Allocator::const_pointer const_pointer;
|
typedef typename Allocator::pointer pointer;
|
||||||
typedef LegacyBidirectionalIterator iterator;
|
typedef typename Allocator::const_pointer const_pointer;
|
||||||
typedef LegacyBidirectionalIterator const_iterator;
|
typedef ft::bidirectionnal_iterator iterator;
|
||||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
typedef ft::bidirectionnal_iterator const_iterator;
|
||||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
|
||||||
|
struct node{
|
||||||
|
bool color;
|
||||||
|
node *parent;
|
||||||
|
node *right;
|
||||||
|
node *left;
|
||||||
|
value_type pair;
|
||||||
|
|
||||||
|
node(key_type &key, mapped_type &val)
|
||||||
|
: pair(make_pair(key, val)), parent(0), right(this->_end), left(this->_end), color(0) {}
|
||||||
|
};
|
||||||
//-----------------------------
|
//-----------------------------
|
||||||
//-----PRIVATE MEMBER TYPE-----
|
//-----PRIVATE MEMBER TYPE-----
|
||||||
//-----------------------------
|
//-----------------------------
|
||||||
private :
|
private :
|
||||||
|
|
||||||
pointer _start;
|
key_compare _comp;
|
||||||
pointer _end;
|
allocator_type _alloc;
|
||||||
size_type _size;
|
node_allocator_type _node_alloc;
|
||||||
size_type _capacity;
|
|
||||||
allocator_type _alloc;
|
node *_root;
|
||||||
|
node *_end;
|
||||||
public :
|
size_type _size;
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
//---------------------------------------
|
//---------------------------------------
|
||||||
//---------COPLIEN FORM FUNCTION---------
|
//---------COPLIEN FORM FUNCTION---------
|
||||||
//---------------------------------------
|
//---------------------------------------
|
||||||
map()
|
|
||||||
{
|
|
||||||
_start = 0;
|
|
||||||
_end = 0;
|
|
||||||
_size = 0;
|
|
||||||
_capacity = 0;
|
|
||||||
_alloc = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit map( const Compare& comp, const Allocator& alloc = Allocator() )
|
explicit map( const Compare& comp = Compare(), const Allocator& alloc = Allocator() )
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -149,19 +161,22 @@ class map
|
|||||||
//------------------------
|
//------------------------
|
||||||
//--------Capacity--------
|
//--------Capacity--------
|
||||||
//------------------------
|
//------------------------
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const
|
||||||
{
|
{
|
||||||
|
if (!_size)
|
||||||
|
return (1);
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_type size() const
|
size_type size() const
|
||||||
{
|
{
|
||||||
|
return (_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_type max_size() const
|
size_type max_size() const
|
||||||
{
|
{
|
||||||
|
return (_alloc.max_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -187,7 +202,7 @@ class map
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
//--------Modifiers--------
|
//--------Modifiers--------
|
||||||
//-------------------------
|
//-------------------------
|
||||||
pair<iterator,bool> insert (const value_type& val)
|
ft::pair<iterator,bool> insert (const value_type& val)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -230,6 +245,7 @@ class map
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
//--------Observers--------
|
//--------Observers--------
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
key_compare key_comp() const
|
key_compare key_comp() const
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -243,7 +259,7 @@ class map
|
|||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//-------Operations--------
|
//-------Operations--------
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
iterator find (const key_type& k)
|
iterator find (const key_type& k)
|
||||||
{
|
{
|
||||||
@ -280,18 +296,167 @@ class map
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pair<const_iterator,const_iterator> equal_range (const key_type& k) const
|
ft::pair<const_iterator,const_iterator> equal_range (const key_type& k) const
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pair<iterator,iterator> equal_range (const key_type& k)
|
ft::pair<iterator,iterator> equal_range (const key_type& k)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* ******************************TREE FUNCTIONS****************************** */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
template<typename T_node>
|
||||||
|
node *new_node(key_type key, mapped_type val)
|
||||||
|
{
|
||||||
|
node *ret;
|
||||||
|
|
||||||
|
ret = _node_alloc::allocate(1);
|
||||||
|
_node_alloc::construct(ret, node(key, val));
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void left_rotate(node *elem) //x == elem
|
||||||
|
{
|
||||||
|
node *pt_right = elem->right;
|
||||||
|
|
||||||
|
elem->right = pt_right->left;
|
||||||
|
if (elem->right != NULL)
|
||||||
|
elem->right->parent = elem;
|
||||||
|
pt_right->parent = elem->parent;
|
||||||
|
if (elem->parent == NULL)
|
||||||
|
_root = pt_right;
|
||||||
|
else if (elem == elem->parent->left)
|
||||||
|
elem->parent->left = pt_right;
|
||||||
|
else
|
||||||
|
elem->parent->right = pt_right;
|
||||||
|
pt_right->left = elem;
|
||||||
|
elem->parent = pt_right;
|
||||||
|
}
|
||||||
|
|
||||||
|
void right_rotate(node *elem)
|
||||||
|
{
|
||||||
|
node *pt_left = elem->left;
|
||||||
|
|
||||||
|
elem->left = pt_left->right;
|
||||||
|
if (elem->left != NULL)
|
||||||
|
elem->left->parent = elem;
|
||||||
|
pt_left->parent = elem->parent;
|
||||||
|
if (elem->parent == NULL)
|
||||||
|
_root = pt_left;
|
||||||
|
else if (elem == elem->parent->left)
|
||||||
|
elem->parent->left = pt_left;
|
||||||
|
else
|
||||||
|
elem->parent->right = pt_left;
|
||||||
|
pt_left->right = elem;
|
||||||
|
elem->parent = pt_left;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_fix()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_node()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert_fix(node *new_elem, node *parent)
|
||||||
|
{
|
||||||
|
node *uncle = NULL;
|
||||||
|
|
||||||
|
while (new_elem->parent->color == RED)
|
||||||
|
{
|
||||||
|
if (new_elem->parent->parent && new_elem->parent == new_elem->parent->parent->left )
|
||||||
|
{
|
||||||
|
uncle = new_elem->parent->parent->right;
|
||||||
|
if (uncle != _end && uncle->color == RED) //uncle is red == only recoloring
|
||||||
|
{
|
||||||
|
new_elem->parent->color = BLACK; //parent
|
||||||
|
uncle->color = BLACK;
|
||||||
|
new_elem->parent->parent->color = RED; //grandparent
|
||||||
|
new_elem = new_elem->parent->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (new_elem == new_elem->parent->right) //if elem is right child
|
||||||
|
{
|
||||||
|
new_elem = new_elem->parent;
|
||||||
|
left_rotate(new_elem);
|
||||||
|
}
|
||||||
|
new_elem->parent->color = BLACK;
|
||||||
|
new_elem->parent->parent->color = RED;
|
||||||
|
right_rotate(new_elem->parent->parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uncle = new_elem->parent->parent->left;
|
||||||
|
if (uncle != _end && uncle->color == RED) //uncle is red == only recoloring
|
||||||
|
{
|
||||||
|
new_elem->parent->color = BLACK; //parent
|
||||||
|
uncle->color = BLACK;
|
||||||
|
new_elem->parent->parent->color = RED; //grandparent
|
||||||
|
new_elem = new_elem->parent->parent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (new_elem == new_elem->parent->left) //if elem is right child
|
||||||
|
{
|
||||||
|
new_elem = new_elem->parent;
|
||||||
|
right_rotate(new_elem);
|
||||||
|
}
|
||||||
|
new_elem->parent->color = BLACK;
|
||||||
|
new_elem->parent->parent->color = RED;
|
||||||
|
left_rotate(new_elem->parent->parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_root->color = BLACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
node *insert(key_type key, mapped_type val)
|
||||||
|
{
|
||||||
|
node *x = _root;
|
||||||
|
node *new_parent;
|
||||||
|
|
||||||
|
if (this->empty())
|
||||||
|
{
|
||||||
|
_root = new_node(key, val);
|
||||||
|
_end = _root;
|
||||||
|
_size = 1;
|
||||||
|
return _root;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (x != _end)
|
||||||
|
{
|
||||||
|
new_parent = x;
|
||||||
|
if (key < x->key)
|
||||||
|
x = x->right;
|
||||||
|
else if (key > x->key)
|
||||||
|
x = x->left;
|
||||||
|
else
|
||||||
|
return (x);
|
||||||
|
}
|
||||||
|
x = new_node(key, val);
|
||||||
|
x->parent = new_parent;
|
||||||
|
if (key < new_parent.key)
|
||||||
|
new_parent.left = x;
|
||||||
|
else
|
||||||
|
new_parent.right = x;
|
||||||
|
x->color = RED;
|
||||||
|
return (x);
|
||||||
|
}
|
||||||
|
this->insert_fix();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
Loading…
Reference in New Issue
Block a user