From 8c181b6407bcafaa6a35478bf65561267046d3ff Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 20 Jun 2022 15:40:35 +0200 Subject: [PATCH] map pair ok --- Makefile | 6 +- headers/map.hpp | 118 ++++++++++++++++++++++----------- headers/pair.hpp | 89 +++++++++++++++++++++++++ templates/map.tpp | 45 ++++++++++--- tests/includes/tests_utils.hpp | 12 ++-- tests/main.cpp | 4 +- tests/tests_map.cpp | 80 +++++++++++++++++++--- 7 files changed, 287 insertions(+), 67 deletions(-) create mode 100644 headers/pair.hpp diff --git a/Makefile b/Makefile index 4892a68..ba89a80 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ RESET = "\e[0m" NAME = containers -CC = g++ +CC = clang++ EXT = cpp CFLAGS = -Wall -Wextra -Werror $(INCLUDES) @@ -66,11 +66,13 @@ HEADERS = colors.h \ reverse_iterator.hpp \ equal.hpp \ lexicographical_compare.hpp \ + pair.hpp \ \ vector.hpp D_TEMPLATES = ./templates -TEMPLATES = vector.tpp +TEMPLATES = vector.tpp \ + map.tpp D_TESTS = ./tests/includes TESTS = main.hpp \ diff --git a/headers/map.hpp b/headers/map.hpp index 80001fc..b2625c1 100644 --- a/headers/map.hpp +++ b/headers/map.hpp @@ -2,52 +2,68 @@ #ifndef MAP_HPP # define MAP_HPP -//# include "colors.h" -//# include -//# include -//# include // std::allocator -//# include // std::min, std::max -//# include // out_of_range, length_error, logic_error -//# include // 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 // std::allocator +# include // NULL, std::size_t, std::ptrdiff_t +# include // std::less, std::binary_function + +# include "pair.hpp" +//# include "bst.hpp" namespace ft { -//template < -// class T, -// class Allocator = std::allocator > -class map { +template < + class Key, // map::key_type + class T, // map::mapped_type + class Compare = std::less, // map::key_compare + class Alloc = std::allocator< ft::pair > // 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 reverse_iterator; -// typedef ft::reverse_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 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 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 +// { +// 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 // 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& lhs, const std::map& rhs ); +//// operator != ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator!= +// ( const std::map& lhs, const std::map& rhs ); +//// operator < -------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator< +// ( const std::map& lhs, const std::map& rhs ); +//// operator <= ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator<= +// ( const std::map& lhs, const std::map& rhs ); +//// operator > -------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator> +// ( const std::map& lhs, const std::map& rhs ); +//// operator >= ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator>= +// ( const std::map& lhs, const std::map& rhs ); +//// swap (map) ----------------------------- +//template< class Key, class T, class Compare, class Alloc > void swap +// ( std::map& lhs, std::map& rhs ); + + } // namespace ft # include "map.tpp" diff --git a/headers/pair.hpp b/headers/pair.hpp new file mode 100644 index 0000000..d246eb5 --- /dev/null +++ b/headers/pair.hpp @@ -0,0 +1,89 @@ +#ifndef PAIR_HPP +# define PAIR_HPP + +# define PR_TPL template < class T1, class T2 > +# define PR pair + +namespace ft { + +PR_TPL + struct pair { + + typedef T1 first_type; + typedef T2 second_type; + + pair(); + template< typename U, typename V > + pair(const pair& 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& 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 + diff --git a/templates/map.tpp b/templates/map.tpp index e0a451d..c584390 100644 --- a/templates/map.tpp +++ b/templates/map.tpp @@ -1,9 +1,7 @@ -//#define VT_TPL template -//#define VT vector -//#define MP_TPL template < typename Key, typename T, typename Compare, typename Allocator > -//#define MP map +#define MP_TPL template < typename Key, typename T, typename Compare, typename Alloc > +#define MP map namespace ft { @@ -13,8 +11,13 @@ namespace ft { * copliens : ************/ //// constructors ------------------------------ -// explicit map (const key_compare& comp = key_compare(), -// const allocator_type& alloc = allocator_type()); +MP_TPL MP:: + map (const key_compare & comp, const allocator_type & alloc) + : _allocator(alloc) + , _comp(comp) { + + return; +} // template // map (InputIterator first, InputIterator last, // const key_compare& comp = key_compare(), @@ -113,8 +116,34 @@ namespace ft { //// get_allocator ----------------------------- // allocator_type get_allocator() const; + + /************************ + * non-member functions : + ************************/ +//// operator == ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator== +// ( const std::map& lhs, const std::map& rhs ); +//// operator != ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator!= +// ( const std::map& lhs, const std::map& rhs ); +//// operator < -------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator< +// ( const std::map& lhs, const std::map& rhs ); +//// operator <= ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator<= +// ( const std::map& lhs, const std::map& rhs ); +//// operator > -------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator> +// ( const std::map& lhs, const std::map& rhs ); +//// operator >= ------------------------------- +//template< class K, class T, class Comp, class Alloc > bool operator>= +// ( const std::map& lhs, const std::map& rhs ); +//// swap (map) ----------------------------- +//template< class Key, class T, class Compare, class Alloc > void swap +// ( std::map& lhs, std::map& rhs ); + } // namespace ft -//#undef VT -//#undef VT_TPL +#undef VT +#undef VT_TPL diff --git a/tests/includes/tests_utils.hpp b/tests/includes/tests_utils.hpp index 77aabb7..170c866 100644 --- a/tests/includes/tests_utils.hpp +++ b/tests/includes/tests_utils.hpp @@ -20,6 +20,7 @@ namespace ft = std; #else #include "vector.hpp" + #include "map.hpp" #include "reverse_iterator.hpp" #endif @@ -82,9 +83,6 @@ extern std::vector< mystruct* > mem_list; { void func(); };\ void f_name () {\ add_to_list(#f_name, "int, int", new(s_ ## f_name ));\ - add_to_list("", "char, int", new(s_ ## f_name ));\ - add_to_list("", "std::string, int", new(s_ ## f_name ));\ - add_to_list("", "mystruct*, int", new(s_ ## f_name ));\ }\ template \ void s_ ## f_name ::func() @@ -104,15 +102,15 @@ template std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n"; } template - void print(ft::map ma) { + void print(ft::map mp) { int i = 0; typename ft::map::iterator it; - typename ft::map::iterator it_end = ma.end(); + typename ft::map::iterator it_end = mp.end(); - for (it = ma.begin(); it != it_end; ++it, i++) + for (it = mp.begin(); it != it_end; ++it, i++) std::cout << "[" << i << "]" << it->first << ":" << it->second << " "; - std::cout << "\nsize:" << ma.size() << "\n"; + std::cout << "\nsize:" << mp.size() << "\n"; } // templates get value diff --git a/tests/main.cpp b/tests/main.cpp index 544fcbb..17a98e1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -5,7 +5,7 @@ int main() { // VECTOR - tests_vector_constructor(); +// tests_vector_constructor(); // tests_vector_operator_assignation(); // tests_vector_begin(); // tests_vector_end(); @@ -57,6 +57,8 @@ int main() { // tests_map_upper_bound(); // tests_map_equal_range(); // tests_map_get_allocator(); +// tests_map_relational_operators(); +// tests_map_swap_non_member(); // STACK diff --git a/tests/tests_map.cpp b/tests/tests_map.cpp index 2f440ae..d257f0f 100644 --- a/tests/tests_map.cpp +++ b/tests/tests_map.cpp @@ -9,15 +9,12 @@ TEST_M(tests_map_simple) // title TITLE(simple test) - typename std::map first; - typename std::map::iterator it; - - first[VALT('a')]=VALU(10); - first[VALT('b')]=VALU(30); - first[VALT('c')]=VALU(50); - first[VALT('d')]=VALU(70); - - PRINT(first) + typename ft::map mp; +// typename std::map::iterator it; +// +// mp[VALT('a')] = VALU(10); +// +// PRINT(mp) DELETE } @@ -543,6 +540,71 @@ TEST_M(tests_map_get_allocator) DELETE } +TEST_M(tests_map_relational_operators) +{ + // title + TITLE(cplusplus.com reference) + + std::map alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; + std::map bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; + std::map eve{{1, 'a'}, {2, 'b'}, {3, 'c'}}; + + std::cout << std::boolalpha; + + // Compare non equal containers + std::cout << "alice == bob returns " << (alice == bob) << '\n'; + std::cout << "alice != bob returns " << (alice != bob) << '\n'; + std::cout << "alice < bob returns " << (alice < bob) << '\n'; + std::cout << "alice <= bob returns " << (alice <= bob) << '\n'; + std::cout << "alice > bob returns " << (alice > bob) << '\n'; + std::cout << "alice >= bob returns " << (alice >= bob) << '\n'; + + std::cout << '\n'; + + // Compare equal containers + std::cout << "alice == eve returns " << (alice == eve) << '\n'; + std::cout << "alice != eve returns " << (alice != eve) << '\n'; + std::cout << "alice < eve returns " << (alice < eve) << '\n'; + std::cout << "alice <= eve returns " << (alice <= eve) << '\n'; + std::cout << "alice > eve returns " << (alice > eve) << '\n'; + std::cout << "alice >= eve returns " << (alice >= eve) << '\n'; + + DELETE +} + + +TEST_M(tests_map_swap_non_member) +{ + // title + TITLE(cplusplus.com reference) + + std::map alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; + std::map bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; + + auto print = [](std::pair& n) { + std::cout << " " << n.first << '(' << n.second << ')'; + }; + + // Print state before swap + std::cout << "alice:"; + std::for_each(alice.begin(), alice.end(), print); + std::cout << "\n" "bob :"; + std::for_each(bob.begin(), bob.end(), print); + std::cout << '\n'; + + std::cout << "-- SWAP\n"; + std::swap(alice, bob); + + // Print state after swap + std::cout << "alice:"; + std::for_each(alice.begin(), alice.end(), print); + std::cout << "\n" "bob :"; + std::for_each(bob.begin(), bob.end(), print); + std::cout << '\n'; + + DELETE +} + */ #endif