map filled luky style

This commit is contained in:
hugogogo
2022-06-21 01:50:41 +02:00
parent c7114c1db2
commit 28acc5ef51
3 changed files with 301 additions and 189 deletions

View File

@@ -36,137 +36,138 @@ public:
typedef Bst<Key,T,Compare,Alloc> bst_map; typedef Bst<Key,T,Compare,Alloc> bst_map;
// typedef typename bst_map::iterator iterator; typedef typename bst_map::iterator iterator;
// typedef typename bst_map::const_iterator const_iterator; typedef typename bst_map::const_iterator const_iterator;
// typedef typename bst_map::reverse_iterator reverse_iterator; typedef typename bst_map::reverse_iterator reverse_iterator;
// typedef typename bst_map::const_reverse_iterator const_reverse_iterator; typedef typename bst_map::const_reverse_iterator const_reverse_iterator;
/**************** /****************
* member class : * member class :
****************/ ****************/
// // https://en.cppreference.com/w/cpp/container/map/value_compare // https://en.cppreference.com/w/cpp/container/map/value_compare
// class value_compare : public std::binary_function<value_type, value_type, bool> // 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: friend class map;
// Compare comp; protected:
// value_compare(Compare c) : comp(c) {} Compare comp;
// public: value_compare(Compare c) : comp(c) {}
// bool operator() (const value_type& x, const value_type& y) const public:
// { return comp(x.first, y.first); } bool operator() (const value_type& x, const value_type& y) const
// }; { return comp(x.first, y.first); }
};
/************ /************
* copliens : * copliens :
************/ ************/
//// constructors ------------------------------ // constructors ------------------------------
explicit map (const key_compare& comp = key_compare(), explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); const allocator_type& alloc = allocator_type());
// template <class InputIterator> template <class InputIterator>
// map (InputIterator first, InputIterator last, map (InputIterator first, InputIterator last,
// const key_compare& comp = key_compare(), const key_compare& comp = key_compare(),
// const allocator_type& alloc = allocator_type()); const allocator_type& alloc = allocator_type());
// map (const map& x); map (const map& x);
//// destructor -------------------------------- // destructor --------------------------------
// ~map(); ~map();
//// operator= --------------------------------- // operator= ---------------------------------
// map& operator= (const map& x); map& operator= (const map& x);
/************* /*************
* iterators : * iterators :
*************/ *************/
//// begin ------------------------------------- // begin -------------------------------------
// iterator begin(); iterator begin();
// const_iterator begin() const; const_iterator begin() const;
//// end --------------------------------------- // end ---------------------------------------
// iterator end(); iterator end();
// const_iterator end() const; const_iterator end() const;
//// rbegin ------------------------------------ // rbegin ------------------------------------
// reverse_iterator rbegin(); reverse_iterator rbegin();
// const_reverse_iterator rbegin() const; const_reverse_iterator rbegin() const;
//// rend -------------------------------------- // rend --------------------------------------
// reverse_iterator rend(); reverse_iterator rend();
// const_reverse_iterator rend() const; const_reverse_iterator rend() const;
/************ /************
* capacity : * capacity :
************/ ************/
//// empty ------------------------------------- // empty -------------------------------------
// bool empty() const; bool empty() const;
//// size -------------------------------------- // size --------------------------------------
// size_type size() const; size_type size() const;
//// max_size ---------------------------------- // max_size ----------------------------------
// size_type max_size() const; size_type max_size() const;
/****************** /******************
* element access : * element access :
******************/ ******************/
//// operator[] -------------------------------- // operator[] --------------------------------
mapped_type & operator[] (const key_type& k); mapped_type & operator[] (const key_type& k);
/************* /*************
* modifiers : * modifiers :
*************/ *************/
//// insert ------------------------------------ // insert ------------------------------------
// pair<iterator,bool> insert (const value_type& val); pair<iterator,bool> insert (const value_type& val);
// iterator insert (iterator position, const value_type& val); iterator insert (iterator position, const value_type& val);
// template <class InputIterator> template <class InputIterator>
// void insert (InputIterator first, InputIterator last); void insert (InputIterator first, InputIterator last);
//// erase ------------------------------------- // erase -------------------------------------
// void erase (iterator position); void erase (iterator position);
// size_type erase (const key_type& k); size_type erase (const key_type& k);
// void erase (iterator first, iterator last); void erase (iterator first, iterator last);
//// swap -------------------------------------- // swap --------------------------------------
// void swap (map& x); void swap (map& x);
//// clear ------------------------------------- // clear -------------------------------------
// void clear(); void clear();
/************* /*************
* observers : * observers :
*************/ *************/
//// key_comp ---------------------------------- // key_comp ----------------------------------
// key_compare key_comp() const; key_compare key_comp() const;
//// value_comp -------------------------------- // value_comp --------------------------------
// value_compare value_comp() const; value_compare value_comp() const;
/************** /**************
* operations : * operations :
**************/ **************/
//// find -------------------------------------- // find --------------------------------------
// iterator find (const key_type& k); iterator find (const key_type& k);
// const_iterator find (const key_type& k) const; const_iterator find (const key_type& k) const;
//// count ------------------------------------- // count -------------------------------------
// size_type count (const key_type& k) const; size_type count (const key_type& k) const;
//// lower_bound ------------------------------- // lower_bound -------------------------------
// iterator lower_bound (const key_type& k); iterator lower_bound (const key_type& k);
// const_iterator lower_bound (const key_type& k) const; const_iterator lower_bound (const key_type& k) const;
//// upper_bound ------------------------------- // upper_bound -------------------------------
// iterator upper_bound (const key_type& k); iterator upper_bound (const key_type& k);
// const_iterator upper_bound (const key_type& k) const; const_iterator upper_bound (const key_type& k) const;
//// equal_range ------------------------------- // equal_range -------------------------------
// pair<const_iterator,const_iterator> equal_range (const key_type& k) const; pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
// pair<iterator,iterator> equal_range (const key_type& k); pair<iterator,iterator> equal_range (const key_type& k);
/************* /*************
* allocator : * allocator :
*************/ *************/
//// get_allocator ----------------------------- // get_allocator -----------------------------
// allocator_type get_allocator() const; allocator_type get_allocator() const;
private: private:
bst_map _bst;
allocator_type _allocator; allocator_type _allocator;
key_compare _comp; key_compare _comp;
bst_map _bst;
}; };
@@ -174,27 +175,27 @@ private:
/************************ /************************
* non-member functions : * non-member functions :
************************/ ************************/
//// operator == ------------------------------- // operator == -------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// operator != ------------------------------- // operator != -------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// operator < -------------------------------- // operator < --------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// operator <= ------------------------------- // operator <= -------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// operator > -------------------------------- // operator > --------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// operator >= ------------------------------- // operator >= -------------------------------
//template< class K, class T, class Comp, class Alloc > bool 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 ); ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs );
//// swap (map) ----------------------------- // swap (map) -----------------------------
//template< class Key, class T, class Compare, class Alloc > void swap 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 ); ( std::map<Key,T,Compare,Alloc>& lhs, std::map<Key,T,Compare,Alloc>& rhs );
} // namespace ft } // namespace ft

View File

@@ -10,141 +10,253 @@ namespace ft {
/************ /************
* copliens : * copliens :
************/ ************/
//// constructors ------------------------------ // constructors ------------------------------
MP_TPL MP:: MP_TPL MP::
map (const key_compare & comp, const allocator_type & alloc) map (const key_compare & comp, const allocator_type & alloc)
: _allocator(alloc) : _bst()
, _allocator(alloc)
, _comp(comp) { , _comp(comp) {
return; return;
} }
// template <class InputIterator> MP_TPL template <class InputIt> MP::
// map (InputIterator first, InputIterator last, map (InputIt first, InputIt last, const key_compare& comp, const allocator_type& alloc)
// const key_compare& comp = key_compare(), : _bst(first, last)
// const allocator_type& alloc = allocator_type()); , _allocator(alloc)
// map (const map& x); , _comp(comp) {
//// destructor -------------------------------- }
// ~map(); MP_TPL MP::
//// operator= --------------------------------- map (const map& x)
// map& operator= (const map& x); : _bst()
, _allocator(x._allocator)
, _comp(x._comp) {
*this = x;
}
// destructor --------------------------------
MP_TPL MP::
~map() { clear(); }
// operator= ---------------------------------
MP_TPL MP& MP::
operator= (const map& x) {
if (this == &x)
return (*this);
map new_map(x.begin(), x.end());
swap(new_map);
return (*this);
}
/************* /*************
* iterators : * iterators :
*************/ *************/
//// begin ------------------------------------- // begin -------------------------------------
// iterator begin(); MP_TPL typename MP::iterator MP::
// const_iterator begin() const; begin() { return (_bst.begin()); }
//// end --------------------------------------- MP_TPL typename MP::const_iterator MP::
// iterator end(); begin() const { return (_bst.begin()); }
// const_iterator end() const; // end ---------------------------------------
//// rbegin ------------------------------------ MP_TPL typename MP::iterator MP::
// reverse_iterator rbegin(); end() { return (_bst.end()); }
// const_reverse_iterator rbegin() const; MP_TPL typename MP::const_iterator MP::
//// rend -------------------------------------- end() const { return (_bst.end()); }
// reverse_iterator rend(); // rbegin ------------------------------------
// const_reverse_iterator rend() const; MP_TPL typename MP::reverse_iterator MP::
rbegin() { return (_bst.rbegin()); }
MP_TPL typename MP::const_reverse_iterator MP::
rbegin() const { return (_bst.rbegin()); }
// rend --------------------------------------
MP_TPL typename MP::reverse_iterator MP::
rend() { return (_bst.rend()); }
MP_TPL typename MP::const_reverse_iterator MP::
rend() const { return (_bst.rend()); }
/************ /************
* capacity : * capacity :
************/ ************/
//// empty ------------------------------------- // empty -------------------------------------
// bool empty() const; MP_TPL bool MP::
//// size -------------------------------------- empty() const { return (_bst.empty()); }
// size_type size() const; // size --------------------------------------
//// max_size ---------------------------------- MP_TPL typename MP::size_type MP::
// size_type max_size() const; size() const { return (_bst.size()); }
// max_size ----------------------------------
MP_TPL typename MP::size_type MP::
max_size() const { return (_bst.max_size()); }
/****************** /******************
* element access : * element access :
******************/ ******************/
//// operator[] -------------------------------- // operator[] --------------------------------
MP_TPL typename MP::mapped_type& MP:: MP_TPL typename MP::mapped_type& MP::
operator[] (const key_type& k) { operator[] (const key_type& k) { return _bst[k]; }
return _bst[k];
}
/************* /*************
* modifiers : * modifiers :
*************/ *************/
//// insert ------------------------------------ // insert ------------------------------------
// pair<iterator,bool> insert (const value_type& val); MP_TPL pair<typename MP::iterator, bool> MP::
// iterator insert (iterator position, const value_type& val); insert (const value_type& val) { return (_bst.insert(val)); }
// template <class InputIterator> MP_TPL typename MP::iterator MP::
// void insert (InputIterator first, InputIterator last); insert (iterator pos, const value_type& val) { return (_bst.insert(pos, val)); }
//// erase ------------------------------------- MP_TPL template <class InputIt> void MP::
// void erase (iterator position); insert (InputIt first, InputIt last) { return (_bst.insert(first, last)); }
// size_type erase (const key_type& k); // erase -------------------------------------
// void erase (iterator first, iterator last); MP_TPL void MP::
//// swap -------------------------------------- erase (iterator pos) { return (_bst.erase(pos)); }
// void swap (map& x); MP_TPL typename MP::size_type MP::
//// clear ------------------------------------- erase (const key_type& k) { return (_bst.erase(k)); }
// void clear(); MP_TPL void MP::
erase (iterator first, iterator last) { return (_bst.erase(first, last)); }
// swap --------------------------------------
MP_TPL void MP::
swap (map& x) {
bst_map tmp;
tmp.swap(_bst);
_bst.swap(x._bst);
x._bst.swap(tmp);
}
// clear -------------------------------------
MP_TPL void MP::
clear() {
_bst.clear();
}
/************* /*************
* observers : * observers :
*************/ *************/
//// key_comp ---------------------------------- // key_comp ----------------------------------
// key_compare key_comp() const; MP_TPL typename MP::key_compare MP::
//// value_comp -------------------------------- key_comp() const { return (value_compare(_comp).comp); }
// value_compare value_comp() const; // value_comp --------------------------------
MP_TPL typename MP::value_compare MP::
value_comp() const { return (value_compare(_comp)); }
/************** /**************
* operations : * operations :
**************/ **************/
//// find -------------------------------------- // find --------------------------------------
// iterator find (const key_type& k); MP_TPL typename MP::iterator MP::
// const_iterator find (const key_type& k) const; find (const key_type& k) { return (_bst.find(k)); }
//// count ------------------------------------- MP_TPL typename MP::const_iterator MP::
// size_type count (const key_type& k) const; find (const key_type& k) const { return (_bst.find(k)); }
//// lower_bound ------------------------------- // count -------------------------------------
// iterator lower_bound (const key_type& k); MP_TPL typename MP::size_type MP::
// const_iterator lower_bound (const key_type& k) const; count (const key_type& k) const { return (_bst.count(k)); }
//// upper_bound ------------------------------- // lower_bound -------------------------------
// iterator upper_bound (const key_type& k); MP_TPL typename MP::iterator MP::
// const_iterator upper_bound (const key_type& k) const; lower_bound (const key_type& k) {
//// equal_range -------------------------------
// pair<const_iterator,const_iterator> equal_range (const key_type& k) const; iterator it = begin();
// pair<iterator,iterator> equal_range (const key_type& k); iterator it_end = end();
while (it != it_end)
{
if (_comp(it->first, k) == false)
return (it);
++it;
}
return (it_end);
}
MP_TPL typename MP::const_iterator MP::
lower_bound (const key_type& k) const {
const_iterator it = begin();
const_iterator it_end = end();
while (it != it_end)
{
if (_comp(it->first, k) == false)
return (it);
++it;
}
return (it_end);
}
// upper_bound -------------------------------
MP_TPL typename MP::iterator MP::
upper_bound (const key_type& k) {
iterator it = begin();
iterator it_end = end();
while (it != it_end)
{
if (_comp(k, it->first))
return (it);
++it;
}
return (it_end);
}
MP_TPL typename MP::const_iterator MP::
upper_bound (const key_type& k) const {
const_iterator it = begin();
const_iterator it_end = end();
while (it != it_end)
{
if (_comp(k, it->first))
return (it);
++it;
}
return (it_end);
}
// equal_range -------------------------------
MP_TPL pair<typename MP::const_iterator, typename MP::const_iterator> MP::
equal_range (const key_type& k) const {
return ft::make_pair( lower_bound(k), upper_bound(k) );
}
MP_TPL pair<typename MP::iterator, typename MP::iterator> MP::
equal_range (const key_type& k) {
return ft::make_pair( lower_bound(k), upper_bound(k) );
}
/************* /*************
* allocator : * allocator :
*************/ *************/
//// get_allocator ----------------------------- // get_allocator -----------------------------
// allocator_type get_allocator() const; MP_TPL typename MP::allocator_type MP::
get_allocator() const { return (_allocator); }
/************************ /************************
* non-member functions : * non-member functions :
************************/ ************************/
//// operator == ------------------------------- // operator == -------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator== template< class Key, class T, class Compare, class Alloc > bool operator==
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst == rhs._bst); }
//// operator != ------------------------------- // operator != -------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator!= template< class Key, class T, class Compare, class Alloc > bool operator!=
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst != rhs._bst); }
//// operator < -------------------------------- // operator < --------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator< template< class Key, class T, class Compare, class Alloc > bool operator<
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst < rhs._bst); }
//// operator <= ------------------------------- // operator <= -------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator<= template< class Key, class T, class Compare, class Alloc > bool operator<=
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst <= rhs._bst); }
//// operator > -------------------------------- // operator > --------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator> template< class Key, class T, class Compare, class Alloc > bool operator>
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst > rhs._bst); }
//// operator >= ------------------------------- // operator >= -------------------------------
//template< class K, class T, class Comp, class Alloc > bool operator>= template< class Key, class T, class Compare, class Alloc > bool operator>=
// ( const std::map<K,T,Comp,Alloc>& lhs, const std::map<K,T,Comp,Alloc>& rhs ); ( const MP& lhs, const MP& rhs ) { return (lhs._bst >= rhs._bst); }
//// swap (map) ----------------------------- // swap (map) -----------------------------
//template< class Key, class T, class Compare, class Alloc > void swap 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 ); ( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }
} // namespace ft } // namespace ft

View File

@@ -14,8 +14,7 @@ TEST_M(tests_map_simple)
mp[VALT('a')] = VALU(10); mp[VALT('a')] = VALU(10);
// it = mp.begin(); PRINT(mp)
// PRINT(mp)
DELETE DELETE
} }