[llvm-branch-commits] [libcxx] release/22.x: [libc++] Fix multi{map, set}::extract not returning the first matching element (#199703) (PR #200270)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu May 28 13:54:54 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: llvmbot
<details>
<summary>Changes</summary>
Backport 72871f6fa1f1edc3df45d01b67f5093ff9d8e8b5
Requested by: @<!-- -->philnik777
---
Full diff: https://github.com/llvm/llvm-project/pull/200270.diff
3 Files Affected:
- (modified) libcxx/include/__tree (+2-2)
- (modified) libcxx/test/std/containers/associative/multimap/multimap.modifiers/extract_key.pass.cpp (+13)
- (modified) libcxx/test/std/containers/associative/multiset/extract_key.pass.cpp (+13)
``````````diff
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index eb17f7d36936c..ddeb82f91b8ad 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -2026,8 +2026,8 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __
template <class _Tp, class _Compare, class _Allocator>
template <class _NodeHandle>
_LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key) {
- iterator __it = find(__key);
- if (__it == end())
+ iterator __it = __lower_bound_multi(__key);
+ if (__it == end() || __value_comp_(__key, *__it))
return _NodeHandle();
return __node_handle_extract<_NodeHandle>(__it);
}
diff --git a/libcxx/test/std/containers/associative/multimap/multimap.modifiers/extract_key.pass.cpp b/libcxx/test/std/containers/associative/multimap/multimap.modifiers/extract_key.pass.cpp
index b3027b3b0f727..42184edbdb8b6 100644
--- a/libcxx/test/std/containers/associative/multimap/multimap.modifiers/extract_key.pass.cpp
+++ b/libcxx/test/std/containers/associative/multimap/multimap.modifiers/extract_key.pass.cpp
@@ -50,6 +50,19 @@ int main(int, char**) {
test(m, std::begin(keys), std::end(keys));
}
+ { // Check that the first element is returned
+ std::multimap<int, int> m = {{1, 1}, {1, 2}, {1, 3}};
+ auto ptr = std::addressof(m.begin()->first);
+ auto res = m.extract(1);
+ assert(std::addressof(res.key()) == ptr);
+ }
+
+ { // Check that no element is returned if there is no match
+ std::multimap<int, int> m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}};
+ auto res = m.extract(0);
+ assert(!res);
+ }
+
{
std::multimap<Counter<int>, Counter<int>> m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}};
{
diff --git a/libcxx/test/std/containers/associative/multiset/extract_key.pass.cpp b/libcxx/test/std/containers/associative/multiset/extract_key.pass.cpp
index 24d98cfaaf31a..ec75a6c5d835c 100644
--- a/libcxx/test/std/containers/associative/multiset/extract_key.pass.cpp
+++ b/libcxx/test/std/containers/associative/multiset/extract_key.pass.cpp
@@ -48,6 +48,19 @@ int main(int, char**) {
test(m, std::begin(keys), std::end(keys));
}
+ { // Check that the first element is returned
+ std::multiset<int> m = {1, 1, 1};
+ auto ptr = std::addressof(*m.begin());
+ auto res = m.extract(1);
+ assert(std::addressof(res.value()) == ptr);
+ }
+
+ { // Check that no element is returned if there is no match
+ std::multiset<int> m = {1, 2, 3};
+ auto res = m.extract(0);
+ assert(!res);
+ }
+
{
std::multiset<Counter<int>> m = {1, 2, 3, 4, 5, 6};
{
``````````
</details>
https://github.com/llvm/llvm-project/pull/200270
More information about the llvm-branch-commits
mailing list