Files
42_INT_11_ft_containers/headers/map.hpp
2022-06-22 00:22:04 +02:00

159 lines
5.3 KiB
C++

#ifndef MAP_HPP
# define MAP_HPP
# include <memory> // std::allocator
# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
# include <algorithm> // max()
# include <functional> // std::less, std::binary_function
# include "reverse_iterator.hpp"
# include "equal.hpp"
# include "lexicographical_compare.hpp"
# include "pair.hpp"
# include "map_node.hpp"
# include "map_iterator.hpp"
namespace ft {
template <
class Key, // map::key_type
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;
// typedef typename Alloc::pointer pointer;
// typedef typename Alloc::const_pointer const_pointer;
// typedef typename Alloc::reference reference;
// typedef typename Alloc::const_reference const_reference;
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;
class value_compare : public std::binary_function<value_type, value_type, bool> {
friend class map;
protected:
Compare comp;
value_compare(Compare c) : comp(c) {}
public:
bool operator() (const value_type& x, const value_type& y) const
{ 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);
// 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;
// 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);
// 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;
// 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);
// 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);
};
// 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);
} // namespace ft
# include "bst.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
#endif