stack almost good

This commit is contained in:
hugogogo
2022-06-24 02:23:53 +02:00
parent 58d417742b
commit 6617d6cdf5
23 changed files with 3137 additions and 1081 deletions

View File

@@ -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