stack almost good
This commit is contained in:
@@ -21,96 +21,135 @@ template <
|
||||
typename T,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Allocator = std::allocator< ft::pair<const Key, T> >
|
||||
> class Bst
|
||||
{
|
||||
public:
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Allocator allocator_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
> class Bst {
|
||||
|
||||
typedef typename Allocator::pointer pointer;
|
||||
typedef typename Allocator::const_pointer const_pointer;
|
||||
typedef typename Allocator::reference reference;
|
||||
typedef typename Allocator::const_reference const_reference;
|
||||
public:
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Allocator allocator_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
typedef Bst_iterator<Key, T, Compare, Allocator> iterator;
|
||||
typedef Bst_const_iterator<Key, T, Compare, Allocator> const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
|
||||
typedef Bst_iterator<Key, T, Compare, Allocator> iterator;
|
||||
typedef Bst_const_iterator<Key, T, Compare, Allocator> const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
// constructors ------------------------------
|
||||
explicit Bst(
|
||||
const key_compare& comp = Compare(),
|
||||
const Allocator& alloc = Allocator()
|
||||
);
|
||||
template < typename InputIt >
|
||||
Bst(
|
||||
InputIt first, InputIt last,
|
||||
const key_compare& comp = Compare(),
|
||||
const allocator_type& alloc = Allocator()
|
||||
);
|
||||
Bst(const Bst& src);
|
||||
// destructor --------------------------------
|
||||
~Bst();
|
||||
// operator= ---------------------------------
|
||||
Bst& operator=(const Bst& rhs);
|
||||
|
||||
|
||||
// Member functions
|
||||
explicit Bst(const Compare& comp = Compare(), const Allocator& alloc = Allocator() );
|
||||
template < typename InputIt >
|
||||
Bst(InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator());
|
||||
Bst(const Bst& src);
|
||||
~Bst();
|
||||
/*************
|
||||
* iterators :
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
// end ---------------------------------------
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
// rbegin ------------------------------------
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
// rend --------------------------------------
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
|
||||
Bst& operator=(const Bst& rhs);
|
||||
|
||||
// Element access
|
||||
T& operator[](const Key& key);
|
||||
/************
|
||||
* capacity :
|
||||
************/
|
||||
// empty -------------------------------------
|
||||
bool empty() const;
|
||||
// size --------------------------------------
|
||||
size_type size() const;
|
||||
// max_size ----------------------------------
|
||||
size_type max_size() const;
|
||||
|
||||
// Iterators
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
|
||||
// Capacity
|
||||
bool empty() const;
|
||||
size_type size() const;
|
||||
size_type max_size() const;
|
||||
/******************
|
||||
* element access :
|
||||
******************/
|
||||
// operator[] --------------------------------
|
||||
mapped_type& operator[](const key_type& key);
|
||||
|
||||
// Modifiers
|
||||
void clear();
|
||||
pair<iterator, bool> insert(const value_type& value);
|
||||
iterator insert(iterator hint, const value_type& value);
|
||||
template < typename InputIt >
|
||||
void insert(InputIt first, InputIt last);
|
||||
void erase(iterator pos);
|
||||
void erase(iterator first, iterator last);
|
||||
size_type erase(const Key& key);
|
||||
void swap(Bst& other);
|
||||
|
||||
// Lookup
|
||||
iterator find(const Key& key);
|
||||
const_iterator find(const Key& key) const;
|
||||
size_type count(const Key& key) const;
|
||||
/*************
|
||||
* modifiers :
|
||||
*************/
|
||||
// insert ------------------------------------
|
||||
pair<iterator, bool> insert(const value_type& value);
|
||||
iterator insert(iterator hint, const value_type& value);
|
||||
template < typename InputIt >
|
||||
void insert(InputIt first, InputIt last);
|
||||
// erase -------------------------------------
|
||||
void erase(iterator pos);
|
||||
size_type erase(const key_type& key);
|
||||
void erase(iterator first, iterator last);
|
||||
// swap --------------------------------------
|
||||
void swap(Bst& other);
|
||||
// clear -------------------------------------
|
||||
void clear();
|
||||
|
||||
private:
|
||||
size_type _size;
|
||||
node<value_type>* _root;
|
||||
node_sentinel<value_type>* _sentinel;
|
||||
Compare _comp;
|
||||
Allocator _allocator;
|
||||
/**************
|
||||
* operations :
|
||||
**************/
|
||||
// find --------------------------------------
|
||||
iterator find(const key_type& key);
|
||||
const_iterator find(const key_type& key) const;
|
||||
// count -------------------------------------
|
||||
size_type count(const key_type& key) const;
|
||||
|
||||
// TODO : rebind syntaxe pas clair.
|
||||
typename Allocator::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
||||
typename Allocator::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe
|
||||
private:
|
||||
size_type _size;
|
||||
node<value_type>* _root;
|
||||
node_sentinel<value_type>* _sentinel;
|
||||
Compare _comp;
|
||||
Allocator _allocator;
|
||||
|
||||
void _init_sentinel();
|
||||
pair<iterator, bool> _insert(const value_type& value);
|
||||
node<value_type>* _erase(iterator pos);
|
||||
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
||||
// https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
|
||||
typename Allocator::template
|
||||
rebind< node<value_type> >::other _allocator_node;
|
||||
typename Allocator::template
|
||||
rebind< node_sentinel<value_type> >::other _allocator_node_sentinel;
|
||||
|
||||
// AVL Balancing
|
||||
void _insert_rebalancing(node<value_type>* n);
|
||||
void _erase_rebalancing(node<value_type>* n);
|
||||
void _init_sentinel();
|
||||
pair<iterator, bool> _insert(const value_type& value);
|
||||
node<value_type>* _erase(iterator pos);
|
||||
node<value_type>* _subtree_shift(
|
||||
node<value_type>* st_old,
|
||||
node<value_type>* st_new
|
||||
);
|
||||
|
||||
short _compute_height(node<value_type>* n);
|
||||
short _bf(node<value_type>* n); // balance factor
|
||||
node<value_type>* _rotate_left(node<value_type>* n);
|
||||
node<value_type>* _rotate_right(node<value_type>* n);
|
||||
// BBST
|
||||
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||
void _insert_rebalancing(node<value_type>* n);
|
||||
void _erase_rebalancing(node<value_type>* n);
|
||||
|
||||
short _compute_height(node<value_type>* n);
|
||||
short _balance_factor(node<value_type>* n);
|
||||
node<value_type>* _rotate_left(node<value_type>* n);
|
||||
node<value_type>* _rotate_right(node<value_type>* n);
|
||||
};
|
||||
|
||||
// Non-member functions
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#ifndef MAP_HPP
|
||||
# define MAP_HPP
|
||||
|
||||
# include "colors.h"
|
||||
# include <memory> // std::allocator
|
||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||
# include <functional> // std::less, std::binary_function
|
||||
@@ -29,11 +28,6 @@ public:
|
||||
typedef Compare key_compare;
|
||||
typedef Alloc allocator_type;
|
||||
|
||||
// typedef typename allocator_type::reference reference;
|
||||
// typedef typename allocator_type::const_reference const_reference;
|
||||
// typedef typename allocator_type::pointer pointer;
|
||||
// typedef typename allocator_type::const_pointer const_pointer;
|
||||
|
||||
typedef Bst<Key,T,Compare,Alloc> bst_map;
|
||||
|
||||
typedef typename bst_map::iterator iterator;
|
||||
@@ -203,5 +197,11 @@ template< class Key, class T, class Compare, class Alloc > void swap
|
||||
|
||||
# include "map.tpp"
|
||||
|
||||
// banlanced binary search tree :
|
||||
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||
|
||||
// entinel node :
|
||||
// https://en.wikipedia.org/wiki/Sentinel_node
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
267
headers/map.hpp
267
headers/map.hpp
@@ -4,7 +4,7 @@
|
||||
|
||||
# include <memory> // std::allocator
|
||||
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
|
||||
# include <algorithm> // max()
|
||||
# include <algorithm> // std::max()
|
||||
# include <functional> // std::less, std::binary_function
|
||||
|
||||
# include "reverse_iterator.hpp"
|
||||
@@ -22,27 +22,29 @@ template <
|
||||
class T, // map::mapped_type
|
||||
class Compare = std::less<Key>, // map::key_compare
|
||||
class Alloc = std::allocator< ft::pair<const Key, T> > // map::allocator_type
|
||||
> class map
|
||||
{
|
||||
public:
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Alloc allocator_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
> class map {
|
||||
|
||||
// typedef typename Alloc::pointer pointer;
|
||||
// typedef typename Alloc::const_pointer const_pointer;
|
||||
// typedef typename Alloc::reference reference;
|
||||
// typedef typename Alloc::const_reference const_reference;
|
||||
public:
|
||||
|
||||
typedef map_iterator<Key, T, Compare, Alloc> iterator;
|
||||
typedef map_const_iterator<Key, T, Compare, Alloc> const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
typedef Key key_type;
|
||||
typedef T mapped_type;
|
||||
typedef pair<const Key, T> value_type;
|
||||
typedef std::size_t size_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef Compare key_compare;
|
||||
typedef Alloc allocator_type;
|
||||
|
||||
typedef map_iterator<Key, T, Compare, Alloc> iterator;
|
||||
typedef map_const_iterator<Key, T, Compare, Alloc> const_iterator;
|
||||
typedef ft::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
|
||||
/****************
|
||||
* member class :
|
||||
****************/
|
||||
// https://en.cppreference.com/w/cpp/container/map/value_compare
|
||||
// https://stackoverflow.com/questions/4571355/why-would-one-use-nested-classes-in-c
|
||||
class value_compare : public std::binary_function<value_type, value_type, bool> {
|
||||
|
||||
friend class map;
|
||||
@@ -54,105 +56,172 @@ template <
|
||||
{ return comp(x.first, y.first); }
|
||||
};
|
||||
|
||||
// Member functions
|
||||
explicit map(const Compare& comp = Compare(), const Alloc& alloc = Alloc() );
|
||||
template < typename InputIt >
|
||||
map(InputIt first, InputIt last, const Compare& comp = Compare(), const Alloc& alloc = Alloc());
|
||||
map(const map& src);
|
||||
~map();
|
||||
|
||||
map& operator=(const map& rhs);
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
// constructors ------------------------------
|
||||
explicit map (const key_compare& comp = key_compare(),
|
||||
const allocator_type& alloc = allocator_type());
|
||||
template <class InputIterator>
|
||||
map (InputIterator first, InputIterator last,
|
||||
const key_compare& comp = key_compare(),
|
||||
const allocator_type& alloc = allocator_type());
|
||||
map(const map& x);
|
||||
// destructor --------------------------------
|
||||
~map();
|
||||
// operator= ---------------------------------
|
||||
map& operator= (const map& x);
|
||||
|
||||
// Element access
|
||||
T& operator[](const Key& key);
|
||||
|
||||
// Iterators
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
/*************
|
||||
* iterators :
|
||||
*************/
|
||||
// begin -------------------------------------
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
// end ---------------------------------------
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
// rbegin ------------------------------------
|
||||
reverse_iterator rbegin();
|
||||
const_reverse_iterator rbegin() const;
|
||||
// rend --------------------------------------
|
||||
reverse_iterator rend();
|
||||
const_reverse_iterator rend() const;
|
||||
|
||||
// Capacity
|
||||
bool empty() const;
|
||||
size_type size() const;
|
||||
size_type max_size() const;
|
||||
|
||||
// Modifiers
|
||||
void clear();
|
||||
pair<iterator, bool> insert(const value_type& value);
|
||||
iterator insert(iterator hint, const value_type& value);
|
||||
template < typename InputIt >
|
||||
void insert(InputIt first, InputIt last);
|
||||
void erase(iterator pos);
|
||||
void erase(iterator first, iterator last);
|
||||
size_type erase(const Key& key);
|
||||
void swap(map& other);
|
||||
/************
|
||||
* capacity :
|
||||
************/
|
||||
// empty -------------------------------------
|
||||
bool empty() const;
|
||||
// size --------------------------------------
|
||||
size_type size() const;
|
||||
// max_size ----------------------------------
|
||||
size_type max_size() const;
|
||||
|
||||
// Lookup
|
||||
iterator find(const Key& key);
|
||||
const_iterator find(const Key& key) const;
|
||||
size_type count(const Key& key) const;
|
||||
|
||||
private:
|
||||
size_type _size;
|
||||
node<value_type>* _root;
|
||||
node_sentinel<value_type>* _sentinel;
|
||||
Compare _comp;
|
||||
Alloc _allocator;
|
||||
/******************
|
||||
* element access :
|
||||
******************/
|
||||
// operator[] --------------------------------
|
||||
mapped_type & operator[] (const key_type& k);
|
||||
|
||||
// TODO : rebind syntaxe pas clair.
|
||||
typename Alloc::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
||||
typename Alloc::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe
|
||||
|
||||
void _init_sentinel();
|
||||
pair<iterator, bool> _insert(const value_type& value);
|
||||
node<value_type>* _erase(iterator pos);
|
||||
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
||||
/*************
|
||||
* modifiers :
|
||||
*************/
|
||||
// insert ------------------------------------
|
||||
pair<iterator,bool> insert (const value_type& val);
|
||||
iterator insert (iterator position, const value_type& val);
|
||||
template <class InputIterator>
|
||||
void insert (InputIterator first, InputIterator last);
|
||||
// erase -------------------------------------
|
||||
void erase (iterator position);
|
||||
size_type erase (const key_type& k);
|
||||
void erase (iterator first, iterator last);
|
||||
// swap --------------------------------------
|
||||
void swap (map& x);
|
||||
// clear -------------------------------------
|
||||
void clear();
|
||||
|
||||
// AVL Balancing
|
||||
void _insert_rebalancing(node<value_type>* n);
|
||||
void _erase_rebalancing(node<value_type>* n);
|
||||
|
||||
short _compute_height(node<value_type>* n);
|
||||
short _bf(node<value_type>* n); // balance factor
|
||||
node<value_type>* _rotate_left(node<value_type>* n);
|
||||
node<value_type>* _rotate_right(node<value_type>* n);
|
||||
/*************
|
||||
* observers :
|
||||
*************/
|
||||
// key_comp ----------------------------------
|
||||
key_compare key_comp() const;
|
||||
// value_comp --------------------------------
|
||||
value_compare value_comp() const;
|
||||
|
||||
|
||||
/**************
|
||||
* operations :
|
||||
**************/
|
||||
// find --------------------------------------
|
||||
iterator find (const key_type& k);
|
||||
const_iterator find (const key_type& k) const;
|
||||
// count -------------------------------------
|
||||
size_type count (const key_type& k) const;
|
||||
// lower_bound -------------------------------
|
||||
iterator lower_bound (const key_type& k);
|
||||
const_iterator lower_bound (const key_type& k) const;
|
||||
// upper_bound -------------------------------
|
||||
iterator upper_bound (const key_type& k);
|
||||
const_iterator upper_bound (const key_type& k) const;
|
||||
// equal_range -------------------------------
|
||||
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
|
||||
pair<iterator,iterator> equal_range (const key_type& k);
|
||||
|
||||
|
||||
/*************
|
||||
* allocator :
|
||||
*************/
|
||||
// get_allocator -----------------------------
|
||||
allocator_type get_allocator() const;
|
||||
|
||||
private:
|
||||
size_type _size;
|
||||
node<value_type>* _root;
|
||||
node_sentinel<value_type>* _sentinel;
|
||||
Compare _comp;
|
||||
Alloc _allocator;
|
||||
|
||||
// https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
|
||||
typename Alloc::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
|
||||
typename Alloc::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe // SENTINELL
|
||||
|
||||
void _init_sentinel();
|
||||
pair<iterator, bool> _insert(const value_type& value);
|
||||
node<value_type>* _erase(iterator pos);
|
||||
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
|
||||
|
||||
// BBST
|
||||
void _insert_rebalancing(node<value_type>* n);
|
||||
void _erase_rebalancing(node<value_type>* n);
|
||||
|
||||
short _compute_height(node<value_type>* n);
|
||||
short _balance_factor(node<value_type>* n);
|
||||
node<value_type>* _rotate_left(node<value_type>* n);
|
||||
node<value_type>* _rotate_right(node<value_type>* n);
|
||||
};
|
||||
|
||||
// Non-member functions
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator==(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator!=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator<(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator>(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator<=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
bool operator>=(const map<Key,T,Compare,Alloc>& lhs, const map<Key,T,Compare,Alloc>& rhs);
|
||||
|
||||
template < typename Key, typename T, typename Compare, typename Alloc >
|
||||
void swap(map<Key,T,Compare,Alloc>& lhs, map<Key,T,Compare,Alloc>& rhs);
|
||||
/************************
|
||||
* non-member functions :
|
||||
************************/
|
||||
// operator == -------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator==
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// operator != -------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator!=
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// operator < --------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator<
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// operator <= -------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator<=
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// operator > --------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator>
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// operator >= -------------------------------
|
||||
template< class K, class T, class Comp, class Alloc > bool operator>=
|
||||
( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
|
||||
// swap (map) -----------------------------
|
||||
template< class Key, class T, class Compare, class Alloc > void swap
|
||||
( std::map<Key,T,Compare,Alloc>& lhs, std::map<Key,T,Compare,Alloc>& rhs );
|
||||
|
||||
} // namespace ft
|
||||
|
||||
# include "bst.tpp"
|
||||
# include "map.tpp"
|
||||
|
||||
// https://en.wikipedia.org/wiki/Binary_search_tree
|
||||
// https://en.wikipedia.org/wiki/AVL_tree
|
||||
|
||||
// https://fr.wikipedia.org/wiki/Arbre_binaire_de_recherche
|
||||
// https://fr.wikipedia.org/wiki/Arbre_AVL
|
||||
|
||||
// https://visualgo.net/en/bst
|
||||
// https://visualgo.net/en/bst?slide=14-8 // --> to 14-13
|
||||
// banlanced binary search tree :
|
||||
// https://www.youtube.com/watch?v=vRwi_UcZGjU
|
||||
|
||||
// entinel node :
|
||||
// https://en.wikipedia.org/wiki/Sentinel_node
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ template <
|
||||
typedef value_type& reference;
|
||||
|
||||
map_iterator() : _node(), _sentinel() {}
|
||||
map_iterator(node<value_type>* n, node_sentinel<value_type>* sentinel) : _node(n), _sentinel(sentinel) {}
|
||||
//map_iterator(const map_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
|
||||
map_iterator(node<value_type>* n, node_sentinel<value_type>* sentinel) : _node(n), _sentinel(sentinel) {} // SENTINELL
|
||||
//map_iterator(node<value_type>* n, node<value_type>* sentinel) : _node(n), _sentinel(sentinel) {}
|
||||
|
||||
reference operator*() const
|
||||
{ return _node->value; }
|
||||
@@ -38,7 +38,8 @@ template <
|
||||
Self& operator++()
|
||||
{
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->min();
|
||||
_node = _sentinel->child->min(); // SENTINELL
|
||||
//_node = _sentinel->min();
|
||||
else if (_node->right)
|
||||
_node = _node->right->min();
|
||||
else
|
||||
@@ -57,7 +58,8 @@ template <
|
||||
Self& operator--()
|
||||
{
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->max();
|
||||
_node = _sentinel->child->max(); // SENTINELL
|
||||
//_node = _sentinel->max();
|
||||
else if (_node->left)
|
||||
_node = _node->left->max();
|
||||
else
|
||||
@@ -93,7 +95,8 @@ template <
|
||||
{ return _node; }
|
||||
const node<value_type>* getNode() const
|
||||
{ return _node; }
|
||||
const node_sentinel<value_type>* getSentinel() const
|
||||
const node_sentinel<value_type>* getSentinel() const // SENTINELL
|
||||
//const node<value_type>* getSentinel() const
|
||||
{ return _sentinel; }
|
||||
|
||||
// TODO : friend Non-member functions syntaxe pas clair.
|
||||
@@ -104,7 +107,8 @@ template <
|
||||
|
||||
private:
|
||||
node<value_type>* _node;
|
||||
node_sentinel<value_type>* _sentinel;
|
||||
node_sentinel<value_type>* _sentinel; // SENTINELL
|
||||
//node<value_type>* _sentinel;
|
||||
};
|
||||
|
||||
template <
|
||||
@@ -125,8 +129,8 @@ template <
|
||||
typedef const value_type& reference;
|
||||
|
||||
map_const_iterator() : _node(), _sentinel() {}
|
||||
map_const_iterator(const node<value_type>* node, const node_sentinel<value_type>* sentinel) : _node(node), _sentinel(sentinel) {}
|
||||
//map_const_iterator(const map_const_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
|
||||
map_const_iterator(const node<value_type>* node, const node_sentinel<value_type>* sentinel) : _node(node), _sentinel(sentinel) {} // SENTINELL
|
||||
//map_const_iterator(const node<value_type>* nodee, const node<value_type>* sentinel) : _node(nodee), _sentinel(sentinel) {}
|
||||
map_const_iterator(const map_iterator<Key, T, Compare, Allocator>& src) : _node(src.getNode()), _sentinel(src.getSentinel()) {}
|
||||
|
||||
reference operator*() const
|
||||
@@ -137,7 +141,8 @@ template <
|
||||
Self& operator++()
|
||||
{
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->min();
|
||||
_node = _sentinel->child->min(); // SENTINELL
|
||||
//_node = _sentinel->min();
|
||||
else if (_node->right)
|
||||
_node = _node->right->min();
|
||||
else
|
||||
@@ -156,7 +161,8 @@ template <
|
||||
Self& operator--()
|
||||
{
|
||||
if (_node == NULL)
|
||||
_node = _sentinel->child->max();
|
||||
_node = _sentinel->child->max(); // SENTINELL
|
||||
//_node = _sentinel->max();
|
||||
else if (_node->left)
|
||||
_node = _node->left->max();
|
||||
else
|
||||
@@ -174,7 +180,6 @@ template <
|
||||
|
||||
Self operator++(int)
|
||||
{
|
||||
//Self old(*this);
|
||||
Self old = *this;
|
||||
++(*this);
|
||||
return old;
|
||||
@@ -182,7 +187,6 @@ template <
|
||||
|
||||
Self operator--(int)
|
||||
{
|
||||
//Self old(*this);
|
||||
Self old = *this;
|
||||
--(*this);
|
||||
return old;
|
||||
@@ -198,7 +202,8 @@ template <
|
||||
|
||||
private:
|
||||
const node<value_type>* _node;
|
||||
const node_sentinel<value_type>* _sentinel;
|
||||
const node_sentinel<value_type>* _sentinel; // SENTINELL
|
||||
//const node<value_type>* _sentinel;
|
||||
};
|
||||
|
||||
} // namespace ft
|
||||
|
||||
@@ -7,18 +7,24 @@
|
||||
namespace ft {
|
||||
|
||||
template < typename ValueType >
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
|
||||
ValueType value;
|
||||
node *up;
|
||||
node *left;
|
||||
node *right;
|
||||
short height;
|
||||
|
||||
node(const ValueType& val) : value(val), up(NULL), left(NULL), right(NULL), height(1) {}
|
||||
node(const ValueType& val)
|
||||
: value(val)
|
||||
, up(NULL)
|
||||
, left(NULL)
|
||||
, right(NULL)
|
||||
, height(1)
|
||||
{}
|
||||
|
||||
node* min() {
|
||||
|
||||
node* min()
|
||||
{
|
||||
node* n = this;
|
||||
|
||||
while (n->left)
|
||||
@@ -26,8 +32,8 @@ struct node
|
||||
return n;
|
||||
}
|
||||
|
||||
node* max()
|
||||
{
|
||||
node* max() {
|
||||
|
||||
node* n = this;
|
||||
|
||||
while (n->right)
|
||||
@@ -37,8 +43,8 @@ struct node
|
||||
};
|
||||
|
||||
template < typename ValueType >
|
||||
struct node_sentinel
|
||||
{
|
||||
struct node_sentinel {
|
||||
|
||||
node<ValueType> *child;
|
||||
|
||||
node_sentinel() : child(NULL) {}
|
||||
|
||||
94
headers/stack.hpp
Normal file
94
headers/stack.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
|
||||
#ifndef STACK_HPP
|
||||
# define STACK_HPP
|
||||
|
||||
# include "vector.hpp"
|
||||
|
||||
namespace ft {
|
||||
|
||||
template <
|
||||
typename T,
|
||||
typename Container = ft::vector<T>
|
||||
> class stack
|
||||
{
|
||||
public:
|
||||
typedef Container container_type;
|
||||
typedef typename Container::value_type value_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
|
||||
|
||||
/************
|
||||
* copliens :
|
||||
************/
|
||||
// constructors ------------------------------
|
||||
explicit stack(const container_type& cont = Container()) : c(cont) {}
|
||||
explicit stack(stack const &other): c(other.c) {}
|
||||
|
||||
|
||||
/**********************
|
||||
* overload functions :
|
||||
**********************/
|
||||
// empty -------------------------------------
|
||||
bool empty() const { return c.empty(); }
|
||||
// size --------------------------------------
|
||||
size_type size() const { return c.size(); }
|
||||
// top ---------------------------------------
|
||||
value_type& top() { return c.back(); }
|
||||
const value_type& top() const { return c.back(); }
|
||||
// push --------------------------------------
|
||||
void push(const value_type& value) { c.push_back(value); }
|
||||
// pop ---------------------------------------
|
||||
void pop() { c.pop_back(); }
|
||||
|
||||
// Relational Operators (friend)
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator==(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator!=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator<(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator>(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator<=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
template < typename T2, typename C2 >
|
||||
friend bool operator>=(const stack<T2,C2>& lhs, const stack<T2,C2>& rhs);
|
||||
|
||||
protected:
|
||||
Container c;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/************************
|
||||
* non-member functions :
|
||||
************************/
|
||||
// operator == -------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator==(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c == rhs.c; }
|
||||
// operator != -------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator!=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c != rhs.c; }
|
||||
// operator < --------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator<(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c < rhs.c; }
|
||||
// operator > --------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator>(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c > rhs.c; }
|
||||
// operator <= -------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator<=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c <= rhs.c; }
|
||||
// operator >= -------------------------------
|
||||
template < typename T, typename Container >
|
||||
bool operator>=(const stack<T, Container>& lhs, const stack<T, Container>& rhs)
|
||||
{ return lhs.c >= rhs.c; }
|
||||
|
||||
} // namespace ft
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user