map pair ok

This commit is contained in:
hugogogo
2022-06-20 15:40:35 +02:00
parent 8a404b0839
commit 8c181b6407
7 changed files with 287 additions and 67 deletions

View File

@@ -2,52 +2,68 @@
#ifndef MAP_HPP
# define MAP_HPP
//# include "colors.h"
//# include <iostream>
//# include <string>
//# include <memory> // std::allocator
//# include <algorithm> // std::min, std::max
//# include <stdexcept> // out_of_range, length_error, logic_error
//# include <cstddef> // NULL, std::size_t, std::ptrdiff_t
//
//# include "enable_if.hpp"
//# include "is_integral.hpp"
//# include "reverse_iterator.hpp"
//# include "equal.hpp"
//# include "lexicographical_compare.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 T,
// class Allocator = std::allocator<T> >
class map {
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 T value_type;
// typedef Allocator allocator_type;
// typedef std::size_t size_type;
// typedef std::ptrdiff_t difference_type;
//
// typedef T * iterator;
// typedef T const * const_iterator;
// typedef ft::reverse_iterator<iterator> reverse_iterator;
// typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
//
// // dependent qualified name :
// // https://en.cppreference.com/w/cpp/keyword/typename
// typedef typename Allocator::reference reference;
// typedef typename Allocator::const_reference const_reference;
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
// 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());
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(),
@@ -148,16 +164,38 @@ public:
private:
// size_type _size;
// size_type _capacity;
// value_type * _mem_ptr;
// allocator_type _allocator;
//
// void _destroy(iterator first, iterator last);
// void _increment_capacity(size_type n);
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"

89
headers/pair.hpp Normal file
View File

@@ -0,0 +1,89 @@
#ifndef PAIR_HPP
# define PAIR_HPP
# define PR_TPL template < class T1, class T2 >
# define PR pair<T1, T2>
namespace ft {
PR_TPL
struct pair {
typedef T1 first_type;
typedef T2 second_type;
pair();
template< typename U, typename V >
pair(const pair<U,V>& pr);
pair(const first_type& a, const second_type& b);
T1 first;
T2 second;
};
/************
* copliens :
************/
PR_TPL PR::
pair() {
}
PR_TPL template< typename U, typename V > PR::
pair(const pair<U,V>& pr) : first(pr.first), second(pr.second) {
}
PR_TPL PR::
pair(const first_type& a, const second_type& b) : first(a), second(b) {
}
/************************
* non-member functions :
************************/
PR_TPL
bool operator==(const PR& lhs, const PR& rhs) {
return (lhs.first == rhs.first) && (lhs.second == rhs.second);
}
PR_TPL
bool operator<(const PR& lhs, const PR& rhs) {
return (
lhs.first < rhs.first
|| ( !(rhs.first < lhs.first) && (lhs.second < rhs.second) )
);
}
PR_TPL
bool operator!=(const PR& lhs, const PR& rhs) {
return !(lhs == rhs);
}
PR_TPL
bool operator>(const PR& lhs, const PR& rhs) {
return (rhs < lhs);
}
PR_TPL
bool operator<=(const PR& lhs, const PR& rhs) {
return !(lhs > rhs);
}
PR_TPL
bool operator>=(const PR& lhs, const PR& rhs) {
return !(lhs < rhs);
}
PR_TPL
PR make_pair(T1 x, T2 y) {
return PR(x, y) ;
}
} // namespace ft
# undef PR
# undef PR_TPL
#endif