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

208 lines
6.5 KiB
C++

#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
# include "pair.hpp"
# include "bst.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 std::size_t size_type;
typedef std::ptrdiff_t difference_type;
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;
typedef typename bst_map::const_iterator const_iterator;
typedef typename bst_map::reverse_iterator reverse_iterator;
typedef typename bst_map::const_reverse_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;
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); }
};
/************
* 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);
/*************
* 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 :
************/
// empty -------------------------------------
bool empty() const;
// size --------------------------------------
size_type size() const;
// max_size ----------------------------------
size_type max_size() const;
/******************
* element access :
******************/
// operator[] --------------------------------
mapped_type & operator[] (const key_type& k);
/*************
* 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();
/*************
* 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;
// TMP non privat
bst_map _bst;
private:
allocator_type _allocator;
key_compare _comp;
};
/************************
* 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 "map.tpp"
#endif