Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libcxx/include/__flat_map/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ class flat_map {
__compare_(std::forward<_CompArg>(__comp)...) {}

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_sorted_and_unique(auto&& __key_container) const {
auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); };
auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) -> bool { return !__compare_(__x, __y); };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's expected that decltype(__compare_(__x, __y)) already model boolean-testable (or even is exactly bool in most normal usages).

If the standard were already so requiring, we wouldn't need to change these lambdas. But I haven't found such a requirement...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was likely to be wrong. But are such conversions already performed in algorithms?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was likely to be wrong. But are such conversions already performed in algorithms?

https://godbolt.org/z/nbG9v4zqY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. It's the nature of lambda expression without trailing return type that unfortunately decay-copies the result.

Perhaps there should be some test cases for this, with types in boolean_testable.h reused?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there should be some test cases for this, with types in boolean_testable.h reused?

I don't have a local repository for llvm, co-working is welcome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recalled that the current standard wording doesn't actually require the result type of Predicate of erase_if for flat_meow to model boolean-testable. The boolean-testablity requirements seem only imposed for erase_if for sequence containers.

For flat_meow, the current wording uses explicit cast to bool. So should we use bool(__compare_(...)) instead, or open an LWG issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recalled that the current standard wording doesn't actually require the result type of Predicate of erase_if for flat_meow to model boolean-testable. The boolean-testablity requirements seem only imposed for erase_if for sequence containers.

I mainly referred to https://eel.is/c++draft/algorithms#requirements-6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recalled that the current standard wording doesn't actually require the result type of Predicate of erase_if for flat_meow to model boolean-testable. The boolean-testablity requirements seem only imposed for erase_if for sequence containers.

I mainly referred to https://eel.is/c++draft/algorithms#requirements-6

I meant that such requirement doesn't seem properly propagated to erase_if for non-sequence containers and container adaptors. (And possibly hive?)

  • For basic_string, deque, inplace_vector, and vector, the requirements are propagated via equivalent-to code that uses std::remove_if.
  • For forward_list and list, the requirements are propagated via [forward.list.ops], [list.ops] and equivalent-to code that uses member remove_if.

But for other containers and container adaptors, there doesn't seem such requirement propagation. For hive it's quite weird that [hive.operations] propagates the requirements, but erase_if isn't in that subclause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for other containers and container adaptors, there doesn't seem such requirement propagation. For hive it's quite weird that [hive.operations] propagates the requirements, but erase_if isn't in that subclause.

In any case, std::erase_if can be argued to be an algorithm.
"When not otherwise constrained, the Predicate parameter is used whenever an algorithm expects a function object..." should also apply IMHO.

return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container);
}

Expand Down Expand Up @@ -954,7 +954,7 @@ class flat_map {
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
auto __append_start_offset = __containers_.keys.size() - __num_of_appended;
auto __end = __zv.end();
auto __compare_key = [this](const auto& __p1, const auto& __p2) {
auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool {
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
};
if constexpr (!_WasSorted) {
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__flat_map/flat_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ class flat_multimap {
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
auto __append_start_offset = __containers_.keys.size() - __num_appended;
auto __end = __zv.end();
auto __compare_key = [this](const auto& __p1, const auto& __p2) {
auto __compare_key = [this](const auto& __p1, const auto& __p2) -> bool {
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
};
if constexpr (!_WasSorted) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: std-at-least-c++23

// <flat_map>
//
// flat_map should support comparator that return a non-boolean
// value as long as the returned type is implicitly convertible to bool.

#include <flat_map>
#include <vector>
#include <ranges>

#include "boolean_testable.h"

void test() {
using Key = StrictComparable<int>;
using Value = StrictComparable<int>;
std::flat_map<Key, Value> m1;
std::flat_map m2(std::from_range, m1, StrictBinaryPredicate);
std::flat_map m3(std::sorted_unique, m1.keys(), m1.values(), StrictBinaryPredicate);
std::flat_map m4(m1.begin(), m1.end(), StrictBinaryPredicate);
m2.insert(m1.begin(), m1.end());
m2.insert(std::sorted_unique, m1.begin(), m1.end());
m2.insert_range(m1);
(void)m2.at(2);
m3[1] = 2;
m3.insert_or_assign(1, 2);
m4.try_emplace(1, 2);
m2.emplace(1, 2);
m2.emplace_hint(m2.begin(), 1, 2);
for (const auto& [k, v] : m2) {
(void)k;
(void)v;
}
(void)m2.find(Key{1});
(void)m2.equal_range(Key{1});
(void)(m2 == m2);
m2.erase(m2.begin());
m2.erase(m2.begin(), m2.end());
std::erase_if(
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& {
return yes;
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// <flat_map>
//
// flat_multimap should support comparator that return a non-boolean
// value as long as the returned type is implicitly convertible to bool.

#include <flat_map>
#include <ranges>
#include <vector>

#include "boolean_testable.h"

void test() {
using Key = StrictComparable<int>;
using Value = StrictComparable<int>;
std::flat_multimap<Key, Value> m1;
std::flat_multimap m2(std::from_range, m1, StrictBinaryPredicate);
std::flat_multimap m3(std::sorted_equivalent, m1.keys(), m1.values(), StrictBinaryPredicate);
std::flat_multimap m4(m1.begin(), m1.end(), StrictBinaryPredicate);
m2.insert(m1.begin(), m1.end());
m2.insert(std::sorted_equivalent, m1.begin(), m1.end());
m2.insert_range(m1);
m2.emplace(1, 2);
m2.emplace_hint(m2.begin(), 1, 2);
for (const auto& [k, v] : m2) {
(void)k;
(void)v;
}
(void)m2.find(Key{1});
(void)m2.equal_range(Key{1});
(void)(m2 == m2);
m2.erase(m2.begin());
m2.erase(m2.begin(), m2.end());
std::erase_if(
m2, []<class T>(std::pair<const StrictComparable<T>&, const StrictComparable<T>&>) -> BooleanTestable const& {
return yes;
});
}
Loading