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> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
{
|
||||
template<class _T1, class _T2>
|
||||
template<class T1, class T2>
|
||||
struct pair{
|
||||
typedef _T1 first_type;
|
||||
typedef _T2 second_type;
|
||||
typedef T1 first_type;
|
||||
typedef T2 second_type;
|
||||
|
||||
_T1 _first;
|
||||
_T2 _second;
|
||||
T1 first;
|
||||
T2 second;
|
||||
|
||||
pair(): _first(), _second() {}
|
||||
pair( const T1& x, const T2& y ); : _first(x), _second(y) {}
|
||||
pair(): first(), second() {}
|
||||
pair( const T1& x, const T2& y ) : first(x), second(y) {}
|
||||
|
||||
template<class _U1, class _U2>
|
||||
pair(const pair<_U1, _U2>& p): _first(p._first), _second(p._second) { }
|
||||
template<class U1, class U2>
|
||||
pair(const pair<U1, U2>& p): first(p.first), second(p.second) { }
|
||||
|
||||
pair& operator=(pair& other)
|
||||
{
|
||||
_first = other.first;
|
||||
_second = other.second;
|
||||
first = other.first;
|
||||
second = other.second;
|
||||
return (*this);
|
||||
}
|
||||
};
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -131,8 +131,20 @@ namespace ft
|
||||
return (*tmp);
|
||||
}
|
||||
|
||||
pointer operator ->() { return (_Ite.getPointer()); }
|
||||
const pointer operator ->() const { return (_Ite.getPointer()); }
|
||||
pointer operator ->()
|
||||
{
|
||||
iterator_type tmp(_Ite);
|
||||
tmp--;
|
||||
return (&(*tmp));
|
||||
}
|
||||
|
||||
const pointer operator ->() const
|
||||
{
|
||||
iterator_type tmp(_Ite);
|
||||
tmp--;
|
||||
return (&(*tmp));
|
||||
}
|
||||
|
||||
reference operator [](difference_type nbr) const { return (*(_Ite - nbr - 1)); }
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
@ -5,13 +5,20 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/12 19:46:25 by apommier #+# #+# */
|
||||
/* Updated: 2022/11/18 11:58:52 by apommier ### ########.fr */
|
||||
/* Created: 2022/11/24 08:50:18 by apommier #+# #+# */
|
||||
/* Updated: 2022/11/24 17:45:57 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MAP_HPP
|
||||
# define MAP_HPP
|
||||
#pragma once
|
||||
|
||||
#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
|
||||
{
|
||||
@ -29,6 +36,7 @@ class map
|
||||
//-----------------------------
|
||||
//---------MEMBER TYPE---------
|
||||
//-----------------------------
|
||||
struct node;
|
||||
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
@ -37,42 +45,46 @@ class map
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Allocator allocator_type;
|
||||
typedef typename Allocator::template rebind<node>::other node_allocator_type;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
typedef Allocator::pointer pointer;
|
||||
typedef Allocator::const_pointer const_pointer;
|
||||
typedef LegacyBidirectionalIterator iterator;
|
||||
typedef LegacyBidirectionalIterator const_iterator;
|
||||
typedef typename Allocator::pointer pointer;
|
||||
typedef typename Allocator::const_pointer const_pointer;
|
||||
typedef ft::bidirectionnal_iterator iterator;
|
||||
typedef ft::bidirectionnal_iterator const_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 :
|
||||
|
||||
pointer _start;
|
||||
pointer _end;
|
||||
size_type _size;
|
||||
size_type _capacity;
|
||||
key_compare _comp;
|
||||
allocator_type _alloc;
|
||||
node_allocator_type _node_alloc;
|
||||
|
||||
node *_root;
|
||||
node *_end;
|
||||
size_type _size;
|
||||
|
||||
public :
|
||||
|
||||
//---------------------------------------
|
||||
//---------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--------
|
||||
//------------------------
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
|
||||
if (!_size)
|
||||
return (1);
|
||||
return(0);
|
||||
}
|
||||
|
||||
size_type size() const
|
||||
{
|
||||
|
||||
return (_size);
|
||||
}
|
||||
|
||||
size_type max_size() const
|
||||
{
|
||||
|
||||
return (_alloc.max_size());
|
||||
}
|
||||
|
||||
|
||||
@ -187,7 +202,7 @@ class map
|
||||
//-------------------------
|
||||
//--------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--------
|
||||
//-------------------------
|
||||
|
||||
key_compare key_comp() const
|
||||
{
|
||||
|
||||
@ -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