From d8da03aa1f3819f35839ad1c576eeaa8b1ad61b9 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 14 Feb 2017 21:17:11 +0000 Subject: [PATCH] PR78273 fix count to work with partitioning function Backport from mainline 2017-01-11 Jonathan Wakely PR libstdc++/78273 * include/bits/stl_map.h (map::count<_Kt>(const _Kt&)): Don't assume the heterogeneous comparison can only find one match. * include/bits/stl_set.h (set::count<_Kt>(const _Kt&)): Likewise. * testsuite/23_containers/map/operations/2.cc: Test count works with comparison function that just partitions rather than sorting. * testsuite/23_containers/set/operations/2.cc: Likewise. From-SVN: r245454 --- libstdc++-v3/ChangeLog | 13 +++++++++++ libstdc++-v3/include/bits/stl_map.h | 2 +- libstdc++-v3/include/bits/stl_set.h | 2 +- .../23_containers/map/operations/2.cc | 22 ++++++++++++++++++ .../23_containers/set/operations/2.cc | 23 +++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0734d2156910..76d2dfcd1faf 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2017-02-14 Jonathan Wakely + + Backport from mainline + 2017-01-11 Jonathan Wakely + + PR libstdc++/78273 + * include/bits/stl_map.h (map::count<_Kt>(const _Kt&)): Don't assume + the heterogeneous comparison can only find one match. + * include/bits/stl_set.h (set::count<_Kt>(const _Kt&)): Likewise. + * testsuite/23_containers/map/operations/2.cc: Test count works with + comparison function that just partitions rather than sorting. + * testsuite/23_containers/set/operations/2.cc: Likewise. + 2017-02-01 Jonathan Wakely PR libstdc++/78346 diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index 04345c58101b..89ba1d7a2ff8 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -896,7 +896,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template auto count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x)) - { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; } + { return _M_t._M_count_tr(__x); } #endif //@} diff --git a/libstdc++-v3/include/bits/stl_set.h b/libstdc++-v3/include/bits/stl_set.h index 805fd0788f7a..7c1c1c60a83b 100644 --- a/libstdc++-v3/include/bits/stl_set.h +++ b/libstdc++-v3/include/bits/stl_set.h @@ -673,7 +673,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER auto count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x)) - { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; } + { return _M_t._M_count_tr(__x); } #endif //@} diff --git a/libstdc++-v3/testsuite/23_containers/map/operations/2.cc b/libstdc++-v3/testsuite/23_containers/map/operations/2.cc index ef301ef136c3..cbfd22322660 100644 --- a/libstdc++-v3/testsuite/23_containers/map/operations/2.cc +++ b/libstdc++-v3/testsuite/23_containers/map/operations/2.cc @@ -133,6 +133,27 @@ test05() VERIFY( Cmp::count == 0); } +void +test06() +{ + // PR libstdc++/78273 + + struct C { + bool operator()(int l, int r) const { return l < r; } + + struct Partition { }; + + bool operator()(int l, Partition) const { return l < 2; } + bool operator()(Partition, int r) const { return 4 < r; } + + using is_transparent = void; + }; + + std::map m{ {1,0}, {2,0}, {3,0}, {4, 0}, {5, 0} }; + + auto n = m.count(C::Partition{}); + VERIFY( n == 3 ); +} int main() @@ -142,4 +163,5 @@ main() test03(); test04(); test05(); + test06(); } diff --git a/libstdc++-v3/testsuite/23_containers/set/operations/2.cc b/libstdc++-v3/testsuite/23_containers/set/operations/2.cc index 6a68453ec7bf..30355d4afafa 100644 --- a/libstdc++-v3/testsuite/23_containers/set/operations/2.cc +++ b/libstdc++-v3/testsuite/23_containers/set/operations/2.cc @@ -150,6 +150,28 @@ test06() s.find(i); } +void +test07() +{ + // PR libstdc++/78273 + + struct C { + bool operator()(int l, int r) const { return l < r; } + + struct Partition { }; + + bool operator()(int l, Partition) const { return l < 2; } + bool operator()(Partition, int r) const { return 4 < r; } + + using is_transparent = void; + }; + + std::set s{ 1, 2, 3, 4, 5 }; + + auto n = s.count(C::Partition{}); + VERIFY( n == 3 ); +} + int main() { @@ -159,4 +181,5 @@ main() test04(); test05(); test06(); + test07(); } -- 2.47.2