Skip to content

Commit 8d879ed

Browse files
hewillkhuixie90frederick-vs-ja
authored
[libc++] Make sure flat_{multi}map::key_compare handle boolean-testable correctly (#132621)
This is sibling of [#69378](#69378). --------- Co-authored-by: Hui Xie <[email protected]> Co-authored-by: A. Jiang <[email protected]>
1 parent 19358ca commit 8d879ed

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

libcxx/include/__flat_map/flat_map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ class flat_map {
934934
__compare_(std::forward<_CompArg>(__comp)...) {}
935935

936936
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_sorted_and_unique(auto&& __key_container) const {
937-
auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); };
937+
auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) -> bool { return !__compare_(__x, __y); };
938938
return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container);
939939
}
940940

@@ -967,7 +967,7 @@ class flat_map {
967967
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
968968
auto __append_start_offset = __containers_.keys.size() - __num_of_appended;
969969
auto __end = __zv.end();
970-
auto __compare_key = [this](const auto& __p1, const auto& __p2) {
970+
auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool {
971971
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
972972
};
973973
if constexpr (!_WasSorted) {

libcxx/include/__flat_map/flat_multimap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ class flat_multimap {
854854
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
855855
auto __append_start_offset = __containers_.keys.size() - __num_appended;
856856
auto __end = __zv.end();
857-
auto __compare_key = [this](const auto& __p1, const auto& __p2) {
857+
auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool {
858858
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
859859
};
860860
if constexpr (!_WasSorted) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++23
10+
11+
// <flat_map>
12+
//
13+
// flat_map should support comparator that return a non-boolean
14+
// value as long as the returned type is implicitly convertible to bool.
15+
16+
#include <flat_map>
17+
#include <vector>
18+
#include <ranges>
19+
20+
#include "boolean_testable.h"
21+
22+
void test() {
23+
using Key = StrictComparable<int>;
24+
using Value = StrictComparable<int>;
25+
std::flat_map<Key, Value> m1;
26+
std::flat_map m2(std::from_range, m1, StrictBinaryPredicate);
27+
std::flat_map m3(std::sorted_unique, m1.keys(), m1.values(), StrictBinaryPredicate);
28+
std::flat_map m4(m1.begin(), m1.end(), StrictBinaryPredicate);
29+
m2.insert(m1.begin(), m1.end());
30+
m2.insert(std::sorted_unique, m1.begin(), m1.end());
31+
m2.insert_range(m1);
32+
(void)m2.at(2);
33+
m3[1] = 2;
34+
m3.insert_or_assign(1, 2);
35+
m4.try_emplace(1, 2);
36+
m2.emplace(1, 2);
37+
m2.emplace_hint(m2.begin(), 1, 2);
38+
for (const auto& [k, v] : m2) {
39+
(void)k;
40+
(void)v;
41+
}
42+
(void)m2.find(Key{1});
43+
(void)m2.equal_range(Key{1});
44+
(void)(m2 == m2);
45+
m2.erase(m2.begin());
46+
m2.erase(m2.begin(), m2.end());
47+
std::erase_if(
48+
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& {
49+
return yes;
50+
});
51+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++23
10+
11+
// <flat_map>
12+
//
13+
// flat_multimap should support comparator that return a non-boolean
14+
// value as long as the returned type is implicitly convertible to bool.
15+
16+
#include <flat_map>
17+
#include <ranges>
18+
#include <vector>
19+
20+
#include "boolean_testable.h"
21+
22+
void test() {
23+
using Key = StrictComparable<int>;
24+
using Value = StrictComparable<int>;
25+
std::flat_multimap<Key, Value> m1;
26+
std::flat_multimap m2(std::from_range, m1, StrictBinaryPredicate);
27+
std::flat_multimap m3(std::sorted_equivalent, m1.keys(), m1.values(), StrictBinaryPredicate);
28+
std::flat_multimap m4(m1.begin(), m1.end(), StrictBinaryPredicate);
29+
m2.insert(m1.begin(), m1.end());
30+
m2.insert(std::sorted_equivalent, m1.begin(), m1.end());
31+
m2.insert_range(m1);
32+
m2.emplace(1, 2);
33+
m2.emplace_hint(m2.begin(), 1, 2);
34+
for (const auto& [k, v] : m2) {
35+
(void)k;
36+
(void)v;
37+
}
38+
(void)m2.find(Key{1});
39+
(void)m2.equal_range(Key{1});
40+
(void)(m2 == m2);
41+
m2.erase(m2.begin());
42+
m2.erase(m2.begin(), m2.end());
43+
std::erase_if(
44+
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& {
45+
return yes;
46+
});
47+
}

0 commit comments

Comments
 (0)