From 4d2fdff3b2ceccc05bf8f82e68d83dda5a92bf57 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 9 May 2019 02:43:47 +0700 Subject: [PATCH] remove Allocator template param --- README.md | 188 +++++++++++++++++----------------- headers/flat_hpp/flat_map.hpp | 137 ++++++++++++------------- headers/flat_hpp/flat_set.hpp | 113 +++++++++----------- untests/flat_map_tests.cpp | 16 +-- untests/flat_set_tests.cpp | 14 +-- 5 files changed, 214 insertions(+), 254 deletions(-) diff --git a/README.md b/README.md index 506af96..8f66e97 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,7 @@ target_link_libraries(your_project_target flat.hpp) ```cpp template < typename Key , typename Compare = std::less - , typename Allocator = std::allocator - , typename Container = std::vector > + , typename Container = std::vector > class flat_set; ``` @@ -71,7 +70,6 @@ class flat_set; | `difference_type` | `Container::difference_type` | | `key_compare` | `Compare` | | `value_compare` | `Compare` | -| `allocator_type` | `Allocator` | | `container_type` | `Container` | | `reference` | `Container::reference` | | `const_reference` | `Container::const_reference` | @@ -85,43 +83,51 @@ class flat_set; ### Member functions ```cpp -explicit flat_set( - const Allocator& a); +flat_set(); -explicit flat_set( - const Compare& c = Compare(), - const Allocator& a = Allocator()); +explicit flat_set(const Compare& c); + +template < typename Allocator > +explicit flat_set(const Allocator& a); + +template < typename Allocator > +flat_set(const Compare& c, const Allocator& a); template < typename InputIter > -flat_set( - InputIter first, - InputIter last, - const Allocator& a); +flat_set(InputIter first, InputIter last); template < typename InputIter > -flat_set( - InputIter first, - InputIter last, - const Compare& c = Compare(), - const Allocator& a = Allocator()); +flat_set(InputIter first, InputIter last, const Compare& c); -flat_set( - std::initializer_list ilist, - const Allocator& a); +template < typename InputIter, typename Allocator > +flat_set(InputIter first, InputIter last, const Allocator& a); -flat_set( - std::initializer_list ilist, - const Compare& c = Compare(), - const Allocator& a = Allocator()); +template < typename InputIter, typename Allocator > +flat_set(InputIter first, InputIter last, const Compare& c, const Allocator& a); + +flat_set(std::initializer_list ilist); + +flat_set(std::initializer_list ilist, const Compare& c); + +template < typename Allocator > +flat_set(std::initializer_list ilist, const Allocator& a); + +template < typename Allocator > +flat_set(std::initializer_list ilist, const Compare& c, const Allocator& a); + +template < typename Allocator > +flat_set(flat_set&& other, const Allocator& a); + +template < typename Allocator > +flat_set(const flat_set& other, const Allocator& a); flat_set(flat_set&& other); -flat_set(const flat_set& other) +flat_set(const flat_set& other); flat_set& operator=(flat_set&& other); flat_set& operator=(const flat_set& other); -flat_set& operator=(std::initializer_list ilist); -allocator_type get_allocator() const; +flat_set& operator=(std::initializer_list ilist); ``` ### Iterators @@ -211,59 +217,52 @@ value_compare value_comp() const; ```cpp template < typename Key , typename Compare - , typename Allocator , typename Container > void swap( - flat_set& l, - flat_set& r); + flat_set& l, + flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator==( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator!=( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator<( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator>( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator<=( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator>=( - const flat_set& l, - const flat_set& r); + const flat_set& l, + const flat_set& r); ``` ## Flat Map @@ -272,8 +271,7 @@ bool operator>=( template < typename Key , typename Value , typename Compare = std::less - , typename Allocator = std::allocator> - , typename Container = std::vector, Allocator> > + , typename Container = std::vector> > class flat_map; ``` @@ -287,7 +285,6 @@ class flat_map; | `size_type` | `Container::size_type` | | `difference_type` | `Container::difference_type` | | `key_compare` | `Compare` | -| `allocator_type` | `Allocator` | | `container_type` | `Container` | | `reference` | `Container::reference` | | `const_reference` | `Container::const_reference` | @@ -307,43 +304,51 @@ class value_compare; ### Member functions ```cpp -explicit flat_map( - const Allocator& a); +flat_map(); -explicit flat_map( - const Compare& c = Compare(), - const Allocator& a = Allocator()); +explicit flat_map(const Compare& c); + +template < typename Allocator > +explicit flat_map(const Allocator& a); + +template < typename Allocator > +flat_map(const Compare& c, const Allocator& a); template < typename InputIter > -flat_map( - InputIter first, - InputIter last, - const Allocator& a); +flat_map(InputIter first, InputIter last); template < typename InputIter > -flat_map( - InputIter first, - InputIter last, - const Compare& c = Compare(), - const Allocator& a = Allocator()); +flat_map(InputIter first, InputIter last, const Compare& c); -flat_map( - std::initializer_list ilist, - const Allocator& a); +template < typename InputIter, typename Allocator > +flat_map(InputIter first, InputIter last, const Allocator& a); -flat_map( - std::initializer_list ilist, - const Compare& c = Compare(), - const Allocator& a = Allocator()); +template < typename InputIter , typename Allocator > +flat_map(InputIter first, InputIter last, const Compare& c, const Allocator& a); + +flat_map(std::initializer_list ilist); + +flat_map(std::initializer_list ilist, const Compare& c); + +template < typename Allocator > +flat_map(std::initializer_list ilist, const Allocator& a); + +template < typename Allocator > +flat_map(std::initializer_list ilist, const Compare& c, const Allocator& a); + +template < typename Allocator > +flat_map(flat_map&& other, const Allocator& a); + +template < typename Allocator > +flat_map(const flat_map& other, const Allocator& a); flat_map(flat_map&& other); flat_map(const flat_map& other); flat_map& operator=(flat_map&& other); flat_map& operator=(const flat_map& other); -flat_map& operator=(std::initializer_list ilist); -allocator_type get_allocator() const; +flat_map& operator=(std::initializer_list ilist); ``` ### Iterators @@ -444,65 +449,58 @@ value_compare value_comp() const; template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > void swap( - flat_map& l, - flat_map& r); + flat_map& l, + flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator==( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator!=( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator<( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator>( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator<=( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator>=( - const flat_map& l, - const flat_map& r); + const flat_map& l, + const flat_map& r); ``` ## Flat Multiset diff --git a/headers/flat_hpp/flat_map.hpp b/headers/flat_hpp/flat_map.hpp index f914b62..b531731 100644 --- a/headers/flat_hpp/flat_map.hpp +++ b/headers/flat_hpp/flat_map.hpp @@ -20,8 +20,7 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare = std::less - , typename Allocator = std::allocator> - , typename Container = std::vector, Allocator> > + , typename Container = std::vector> > class flat_map final { class uber_comparer_type : public Compare { public: @@ -39,6 +38,10 @@ namespace flat_hpp bool operator()(typename Container::const_reference l, const Key& r) const { return Compare::operator()(l.first, r); } + + bool operator()(typename Container::const_reference l, typename Container::const_reference r) const { + return Compare::operator()(l.first, r.first); + } }; public: using key_type = Key; @@ -49,7 +52,6 @@ namespace flat_hpp using difference_type = typename Container::difference_type; using key_compare = Compare; - using allocator_type = Allocator; using container_type = Container; using reference = typename Container::reference; @@ -67,78 +69,80 @@ namespace flat_hpp bool operator()(const value_type& l, const value_type& r) const { return compare_(l.first, r.first); } - private: + protected: friend class flat_map; - explicit value_compare(const key_compare& compare) - : compare_(compare) {} + explicit value_compare(key_compare compare) + : compare_(std::move(compare)) {} private: key_compare compare_; }; - - static_assert( - std::is_same::value, - "Allocator::value_type must be same type as value_type"); - - static_assert( - std::is_same::value, - "Container::value_type must be same type as value_type"); - - static_assert( - std::is_same::value, - "Container::allocator_type must be same type as allocator_type"); public: flat_map() {} - explicit flat_map( - const Allocator& a) + explicit flat_map(const Compare& c) + : compare_(c) {} + + template < typename Allocator > + explicit flat_map(const Allocator& a) : data_(a) {} - explicit flat_map( - const Compare& c, - const Allocator& a = Allocator()) + template < typename Allocator > + flat_map(const Compare& c, const Allocator& a) : data_(a) , compare_(c) {} template < typename InputIter > - flat_map( - InputIter first, - InputIter last, - const Allocator& a) - : data_(a) { + flat_map(InputIter first, InputIter last) { insert(first, last); } template < typename InputIter > - flat_map( - InputIter first, - InputIter last, - const Compare& c = Compare(), - const Allocator& a = Allocator()) + flat_map(InputIter first, InputIter last, const Compare& c) + : compare_(c) { + insert(first, last); + } + + template < typename InputIter, typename Allocator > + flat_map(InputIter first, InputIter last, const Allocator& a) + : data_(a) { + insert(first, last); + } + + template < typename InputIter , typename Allocator > + flat_map(InputIter first, InputIter last, const Compare& c, const Allocator& a) : data_(a) , compare_(c) { insert(first, last); } - flat_map( - std::initializer_list ilist, - const Allocator& a) + flat_map(std::initializer_list ilist) { + insert(ilist); + } + + flat_map(std::initializer_list ilist, const Compare& c) + : compare_(c) { + insert(ilist); + } + + template < typename Allocator > + flat_map(std::initializer_list ilist, const Allocator& a) : data_(a) { insert(ilist); } - flat_map( - std::initializer_list ilist, - const Compare& c = Compare(), - const Allocator& a = Allocator()) + template < typename Allocator > + flat_map(std::initializer_list ilist, const Compare& c, const Allocator& a) : data_(a) , compare_(c) { insert(ilist); } + template < typename Allocator > flat_map(flat_map&& other, const Allocator& a) : data_(std::move(other.data_), a) , compare_(std::move(other.compare_)) {} + template < typename Allocator > flat_map(const flat_map& other, const Allocator& a) : data_(other.data_, a) , compare_(other.compare_) {} @@ -154,10 +158,6 @@ namespace flat_hpp return *this; } - allocator_type get_allocator() const { - return data_.get_allocator(); - } - iterator begin() noexcept { return data_.begin(); } const_iterator begin() const noexcept { return data_.begin(); } const_iterator cbegin() const noexcept { return data_.cbegin(); } @@ -230,28 +230,28 @@ namespace flat_hpp std::pair insert(value_type&& value) { const iterator iter = lower_bound(value.first); - return iter == end() || compare_(value.first, iter->first) + return iter == end() || compare_(value, *iter) ? std::make_pair(data_.insert(iter, std::move(value)), true) : std::make_pair(iter, false); } std::pair insert(const value_type& value) { const iterator iter = lower_bound(value.first); - return iter == end() || compare_(value.first, iter->first) + return iter == end() || compare_(value, *iter) ? std::make_pair(data_.insert(iter, value), true) : std::make_pair(iter, false); } iterator insert(const_iterator hint, value_type&& value) { - return (hint == begin() || compare_((hint - 1)->first, value.first)) - && (hint == end() || compare_(value.first, hint->first)) + return (hint == begin() || compare_(*(hint - 1), value)) + && (hint == end() || compare_(value, *hint)) ? data_.insert(hint, std::move(value)) : insert(std::move(value)).first; } iterator insert(const_iterator hint, const value_type& value) { - return (hint == begin() || compare_((hint - 1)->first, value.first)) - && (hint == end() || compare_(value.first, hint->first)) + return (hint == begin() || compare_(*(hint - 1), value)) + && (hint == end() || compare_(value, *hint)) ? data_.insert(hint, value) : insert(value).first; } @@ -303,7 +303,7 @@ namespace flat_hpp } size_type count(const key_type& key) const { - const auto iter = find(key); + const const_iterator iter = find(key); return iter != end() ? 1 : 0; } @@ -363,11 +363,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > void swap( - flat_map& l, - flat_map& r) + flat_map& l, + flat_map& r) { l.swap(r); } @@ -375,11 +374,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator==( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return l.size() == r.size() && std::equal(l.begin(), l.end(), r.begin()); @@ -388,11 +386,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator!=( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return !(l == r); } @@ -400,11 +397,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator<( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); } @@ -412,11 +408,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator>( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return r < l; } @@ -424,11 +419,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator<=( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return !(r < l); } @@ -436,11 +430,10 @@ namespace flat_hpp template < typename Key , typename Value , typename Compare - , typename Allocator , typename Container > bool operator>=( - const flat_map& l, - const flat_map& r) + const flat_map& l, + const flat_map& r) { return !(l < r); } diff --git a/headers/flat_hpp/flat_set.hpp b/headers/flat_hpp/flat_set.hpp index 95ab531..c8fd349 100644 --- a/headers/flat_hpp/flat_set.hpp +++ b/headers/flat_hpp/flat_set.hpp @@ -19,8 +19,7 @@ namespace flat_hpp { template < typename Key , typename Compare = std::less - , typename Allocator = std::allocator - , typename Container = std::vector > + , typename Container = std::vector > class flat_set final { public: using key_type = Key; @@ -31,7 +30,6 @@ namespace flat_hpp using key_compare = Compare; using value_compare = Compare; - using allocator_type = Allocator; using container_type = Container; using reference = typename Container::reference; @@ -43,71 +41,73 @@ namespace flat_hpp using const_iterator = typename Container::const_iterator; using reverse_iterator = typename Container::reverse_iterator; using const_reverse_iterator = typename Container::const_reverse_iterator; - - static_assert( - std::is_same::value, - "Allocator::value_type must be same type as value_type"); - - static_assert( - std::is_same::value, - "Container::value_type must be same type as value_type"); - - static_assert( - std::is_same::value, - "Container::allocator_type must be same type as allocator_type"); public: flat_set() {} - explicit flat_set( - const Allocator& a) + explicit flat_set(const Compare& c) + : compare_(c) {} + + template < typename Allocator > + explicit flat_set(const Allocator& a) : data_(a) {} - explicit flat_set( - const Compare& c, - const Allocator& a = Allocator()) + template < typename Allocator > + flat_set(const Compare& c, const Allocator& a) : data_(a) , compare_(c) {} template < typename InputIter > - flat_set( - InputIter first, - InputIter last, - const Allocator& a) - : data_(a) { + flat_set(InputIter first, InputIter last) { insert(first, last); } template < typename InputIter > - flat_set( - InputIter first, - InputIter last, - const Compare& c = Compare(), - const Allocator& a = Allocator()) + flat_set(InputIter first, InputIter last, const Compare& c) + : compare_(c) { + insert(first, last); + } + + template < typename InputIter, typename Allocator > + flat_set(InputIter first, InputIter last, const Allocator& a) + : data_(a) { + insert(first, last); + } + + template < typename InputIter, typename Allocator > + flat_set(InputIter first, InputIter last, const Compare& c, const Allocator& a) : data_(a) , compare_(c) { insert(first, last); } - flat_set( - std::initializer_list ilist, - const Allocator& a) + flat_set(std::initializer_list ilist) { + insert(ilist); + } + + flat_set(std::initializer_list ilist, const Compare& c) + : compare_(c) { + insert(ilist); + } + + template < typename Allocator > + flat_set(std::initializer_list ilist, const Allocator& a) : data_(a) { insert(ilist); } - flat_set( - std::initializer_list ilist, - const Compare& c = Compare(), - const Allocator& a = Allocator()) + template < typename Allocator > + flat_set(std::initializer_list ilist, const Compare& c, const Allocator& a) : data_(a) , compare_(c) { insert(ilist); } + template < typename Allocator > flat_set(flat_set&& other, const Allocator& a) : data_(std::move(other.data_), a) , compare_(std::move(other.compare_)) {} + template < typename Allocator > flat_set(const flat_set& other, const Allocator& a) : data_(other.data_, a) , compare_(other.compare_) {} @@ -123,10 +123,6 @@ namespace flat_hpp return *this; } - allocator_type get_allocator() const { - return data_.get_allocator(); - } - iterator begin() noexcept { return data_.begin(); } const_iterator begin() const noexcept { return data_.begin(); } const_iterator cbegin() const noexcept { return data_.cbegin(); } @@ -301,22 +297,20 @@ namespace flat_hpp { template < typename Key , typename Compare - , typename Allocator , typename Container > void swap( - flat_set& l, - flat_set& r) + flat_set& l, + flat_set& r) { l.swap(r); } template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator==( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return l.size() == r.size() && std::equal(l.begin(), l.end(), r.begin()); @@ -324,55 +318,50 @@ namespace flat_hpp template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator!=( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return !(l == r); } template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator<( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); } template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator>( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return r < l; } template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator<=( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return !(r < l); } template < typename Key , typename Compare - , typename Allocator , typename Container > bool operator>=( - const flat_set& l, - const flat_set& r) + const flat_set& l, + const flat_set& r) { return !(l < r); } diff --git a/untests/flat_map_tests.cpp b/untests/flat_map_tests.cpp index 2d6b976..eacca10 100644 --- a/untests/flat_map_tests.cpp +++ b/untests/flat_map_tests.cpp @@ -119,16 +119,16 @@ TEST_CASE("flat_map") { int, unsigned, std::less, - alloc_t>; + std::vector, alloc_t>>; using map2_t = flat_map< int, unsigned, std::greater, - alloc_t>; + std::vector, alloc_t>>; using vec_t = std::vector< - std::pair>; + std::pair, alloc_t>; { auto s0 = map_t(); @@ -162,13 +162,6 @@ TEST_CASE("flat_map") { REQUIRE(vec_t(s3.begin(), s3.end()) == vec_t({{0,1},{1,2}})); } - { - auto s0 = map_t(); - auto s1 = map_t(alloc_t(42)); - REQUIRE(s0.get_allocator().i == 0); - REQUIRE(s1.get_allocator().i == 42); - } - { auto s0 = map_t{{0,1}, {1,2}}; auto s1 = s0; @@ -179,8 +172,6 @@ TEST_CASE("flat_map") { REQUIRE(s2 == map_t{{0,1}, {1,2}}); auto s3 = map_t(s2, alloc_t(42)); REQUIRE(s2 == s3); - REQUIRE(s2.get_allocator().i == 0); - REQUIRE(s3.get_allocator().i == 42); auto s4 = map_t(std::move(s3), alloc_t(21)); REQUIRE(s3.empty()); REQUIRE(s4 == map_t{{0,1}, {1,2}}); @@ -251,7 +242,6 @@ TEST_CASE("flat_map") { int, unsigned, std::less, - alloc2_t, std::deque, alloc2_t>>; map2_t s1; diff --git a/untests/flat_set_tests.cpp b/untests/flat_set_tests.cpp index ea6b6af..ad7c508 100644 --- a/untests/flat_set_tests.cpp +++ b/untests/flat_set_tests.cpp @@ -110,8 +110,8 @@ TEST_CASE("flat_set") { } SECTION("ctors") { using alloc_t = dummy_allocator; - using set_t = flat_set, alloc_t>; - using set2_t = flat_set, alloc_t>; + using set_t = flat_set, std::vector>; + using set2_t = flat_set, std::vector>; using vec_t = std::vector; { @@ -146,13 +146,6 @@ TEST_CASE("flat_set") { REQUIRE(vec_t(s3.begin(), s3.end()) == vec_t({2,1,0})); } - { - auto s0 = set_t(); - auto s1 = set_t(alloc_t(42)); - REQUIRE(s0.get_allocator().i == 0); - REQUIRE(s1.get_allocator().i == 42); - } - { auto s0 = set_t{0,1,2}; auto s1 = s0; @@ -163,8 +156,6 @@ TEST_CASE("flat_set") { REQUIRE(s2 == set_t{0,1,2}); auto s3 = set_t(s2, alloc_t(42)); REQUIRE(s2 == s3); - REQUIRE(s2.get_allocator().i == 0); - REQUIRE(s3.get_allocator().i == 42); auto s4 = set_t(std::move(s3), alloc_t(21)); REQUIRE(s3.empty()); REQUIRE(s4 == set_t{0,1,2}); @@ -233,7 +224,6 @@ TEST_CASE("flat_set") { using set2_t = flat_set< int, std::less, - alloc2_t, std::deque>; set2_t s1;