From 8376286aa6409db33b12d188cefc68f4afea06c1 Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Fri, 7 Jun 2019 11:52:44 +0200 Subject: [PATCH 1/6] With heterogeneous lookup, `equal_range` can result in a range with length greater than 1. --- include/boost/container/flat_set.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/container/flat_set.hpp b/include/boost/container/flat_set.hpp index 080beb5..4d4ae47 100644 --- a/include/boost/container/flat_set.hpp +++ b/include/boost/container/flat_set.hpp @@ -1031,7 +1031,7 @@ class flat_set //! Complexity: Logarithmic template std::pair equal_range(const K& x) - { return this->tree_t::lower_bound_range(x); } + { return this->tree_t::equal_range(x); } //! Requires: This overload is available only if //! key_compare::is_transparent exists. @@ -1041,7 +1041,7 @@ class flat_set //! Complexity: Logarithmic template std::pair equal_range(const K& x) const - { return this->tree_t::lower_bound_range(x); } + { return this->tree_t::equal_range(x); } #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED) From 699b1e966d3f6124a4ad6fd7a528bbcc251d5a73 Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Fri, 7 Jun 2019 11:55:15 +0200 Subject: [PATCH 2/6] With heterogeneous lookup, `equal_range` can result in a range with length greater than 1. --- include/boost/container/flat_map.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/container/flat_map.hpp b/include/boost/container/flat_map.hpp index 2b52e14..7ef8c5f 100644 --- a/include/boost/container/flat_map.hpp +++ b/include/boost/container/flat_map.hpp @@ -1465,7 +1465,7 @@ class flat_map //! Complexity: Logarithmic. template BOOST_CONTAINER_FORCEINLINE std::pair equal_range(const K& x) - { return dtl::force_copy >(m_flat_tree.lower_bound_range(x)); } + { return dtl::force_copy >(m_flat_tree.equal_range(x)); } //! Requires: This overload is available only if //! key_compare::is_transparent exists. @@ -1475,7 +1475,7 @@ class flat_map //! Complexity: Logarithmic. template BOOST_CONTAINER_FORCEINLINE std::pair equal_range(const K& x) const - { return dtl::force_copy >(m_flat_tree.lower_bound_range(x)); } + { return dtl::force_copy >(m_flat_tree.equal_range(x)); } //! Effects: Extracts the internal sequence container. //! From 1aa15ead3513bd72deaac26d79240abe6ab62686 Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Tue, 11 Jun 2019 10:23:13 +0200 Subject: [PATCH 3/6] Add testcases for heterogeneous lookup with partial keys. --- test/flat_map_test.cpp | 35 +++++++++++++++++++++++++++++++++++ test/flat_set_test.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index 9497ee2..ca4ce1a 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -22,6 +22,7 @@ #include "../../intrusive/test/iterator_test.hpp" #include +#include using namespace boost::container; @@ -557,6 +558,37 @@ bool test_heterogeneous_lookups() return true; } +// An ordered sequence of std:pair is also ordered by std::pair::first. +struct with_lookup_by_first +{ + typedef void is_transparent; + inline bool operator()(std::pair a, std::pair b) const + { + return a < b; + } + inline bool operator()(std::pair a, int first) const + { + return a.first < first; + } + inline bool operator()(int first, std::pair b) const + { + return first < b.first; + } +}; + +bool test_heterogeneous_lookup_by_partial_key() +{ + flat_set, with_lookup_by_first> const set1 + { + {{0, 1}, 3}, + {{0, 2}, 3}, + }; + + auto const first_0_range = uut.equal_range(0); + + return 2 == first_0_range.second - first_0_range.first; +} + }}} //namespace boost::container::test int main() @@ -617,6 +649,9 @@ int main() if (!test_heterogeneous_lookups()) return 1; + if (!test_heterogeneous_lookup_by_partial_key()) + return 1; + //////////////////////////////////// // Testing allocator implementations //////////////////////////////////// diff --git a/test/flat_set_test.cpp b/test/flat_set_test.cpp index 7e561f7..0215a69 100644 --- a/test/flat_set_test.cpp +++ b/test/flat_set_test.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -574,6 +575,37 @@ bool test_heterogeneous_lookups() return true; } +// An ordered sequence of std:pair is also ordered by std::pair::first. +struct with_lookup_by_first +{ + typedef void is_transparent; + inline bool operator()(std::pair a, std::pair b) const + { + return a < b; + } + inline bool operator()(std::pair a, int first) const + { + return a.first < first; + } + inline bool operator()(int first, std::pair b) const + { + return first < b.first; + } +}; + +bool test_heterogeneous_lookup_by_partial_key() +{ + flat_set, with_lookup_by_first> const set1 + { + {0, 1}, + {0, 2}, + }; + + auto const first_0_range = uut.equal_range(0); + + return 2 == first_0_range.second - first_0_range.first; +} + }}} template @@ -715,6 +747,10 @@ int main() return 1; } + if(!test_heterogeneous_lookup_by_partial_key()){ + return 1; + } + //////////////////////////////////// // Testing allocator implementations //////////////////////////////////// From a6b6f97a5000694f460ab0bf9773f1721c306f7d Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Tue, 11 Jun 2019 11:17:01 +0200 Subject: [PATCH 4/6] Solve copy-paste errors. --- test/flat_map_test.cpp | 4 ++-- test/flat_set_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index cbf9c41..e3c3347 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -581,13 +581,13 @@ struct with_lookup_by_first bool test_heterogeneous_lookup_by_partial_key() { - flat_set, with_lookup_by_first> const set1 + flat_map, with_lookup_by_first> const map1 { {{0, 1}, 3}, {{0, 2}, 3}, }; - auto const first_0_range = uut.equal_range(0); + auto const first_0_range = map1.equal_range(0); return 2 == first_0_range.second - first_0_range.first; } diff --git a/test/flat_set_test.cpp b/test/flat_set_test.cpp index 054be9e..c74cb78 100644 --- a/test/flat_set_test.cpp +++ b/test/flat_set_test.cpp @@ -603,7 +603,7 @@ bool test_heterogeneous_lookup_by_partial_key() {0, 2}, }; - auto const first_0_range = uut.equal_range(0); + auto const first_0_range = set1.equal_range(0); return 2 == first_0_range.second - first_0_range.first; } From 628289cb00b40f7c6750abaf98a7176bf4568d60 Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Tue, 11 Jun 2019 11:51:28 +0200 Subject: [PATCH 5/6] Solve copy-paste errors. --- test/flat_map_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index e3c3347..ef1e3a4 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -581,7 +581,7 @@ struct with_lookup_by_first bool test_heterogeneous_lookup_by_partial_key() { - flat_map, with_lookup_by_first> const map1 + flat_map,int, with_lookup_by_first> const map1 { {{0, 1}, 3}, {{0, 2}, 3}, From 3f09061d783064f546ba9c014eeeae0a5a369eea Mon Sep 17 00:00:00 2001 From: Jan Eisenhauer <44572464+JanEisenhauer@users.noreply.github.com> Date: Tue, 11 Jun 2019 12:04:43 +0200 Subject: [PATCH 6/6] Remove usage of C++11 features. --- test/flat_map_test.cpp | 12 ++++++------ test/flat_set_test.cpp | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index ef1e3a4..d01598b 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -581,13 +581,13 @@ struct with_lookup_by_first bool test_heterogeneous_lookup_by_partial_key() { - flat_map,int, with_lookup_by_first> const map1 - { - {{0, 1}, 3}, - {{0, 2}, 3}, - }; + typedef flat_map,int, with_lookup_by_first> map_t; - auto const first_0_range = map1.equal_range(0); + map_t map1; + map1[std::pair(0, 1)] = 3; + map1[std::pair(0, 2)] = 3; + + std::pair const first_0_range = map1.equal_range(0); return 2 == first_0_range.second - first_0_range.first; } diff --git a/test/flat_set_test.cpp b/test/flat_set_test.cpp index c74cb78..693305f 100644 --- a/test/flat_set_test.cpp +++ b/test/flat_set_test.cpp @@ -597,13 +597,13 @@ struct with_lookup_by_first bool test_heterogeneous_lookup_by_partial_key() { - flat_set, with_lookup_by_first> const set1 - { - {0, 1}, - {0, 2}, - }; + typedef flat_set, with_lookup_by_first> set_t; - auto const first_0_range = set1.equal_range(0); + set_t set1; + set1.insert(std::pair(0, 1)); + set1.insert(std::pair(0, 2)); + + std::pair const first_0_range = set1.equal_range(0); return 2 == first_0_range.second - first_0_range.first; }