Skip to content

[libc++] replace stable_sort with sort in flat_map #121431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
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
8 changes: 3 additions & 5 deletions libcxx/include/__flat_map/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <__algorithm/ranges_inplace_merge.h>
#include <__algorithm/ranges_lower_bound.h>
#include <__algorithm/ranges_partition_point.h>
#include <__algorithm/ranges_stable_sort.h>
#include <__algorithm/ranges_sort.h>
#include <__algorithm/ranges_unique.h>
#include <__algorithm/ranges_upper_bound.h>
#include <__algorithm/remove_if.h>
Expand Down Expand Up @@ -853,9 +853,7 @@ class flat_map {
// is no invariant state to preserve
_LIBCPP_HIDE_FROM_ABI void __sort_and_unique() {
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
// To be consistent with std::map's behaviour, we use stable_sort instead of sort.
// As a result, if there are duplicated keys, the first value in the original order will be taken.
ranges::stable_sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });
ranges::sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });
auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin();
auto __dist = ranges::distance(__zv.begin(), __dup_start);
__containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());
Expand Down Expand Up @@ -886,7 +884,7 @@ class flat_map {
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
};
if constexpr (!_WasSorted) {
ranges::stable_sort(__zv.begin() + __append_start_offset, __end, __compare_key);
ranges::sort(__zv.begin() + __append_start_offset, __end, __compare_key);
} else {
_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(
__is_sorted_and_unique(__containers_.keys | ranges::views::drop(__append_start_offset)),
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ module std [system] {
module ranges_sort {
header "__algorithm/ranges_sort.h"
export std.functional.ranges_operations
export std.algorithm.sort
export std.algorithm.make_projected
Comment on lines +762 to +763
Copy link
Member Author

Choose a reason for hiding this comment

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

I have no idea why but this is the minimum I need to export from ranges_sort in order to resolve the linker error in the CI of the clang module build. I don't see any missing includes

Copy link
Member Author

Choose a reason for hiding this comment

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

this is a reproducer of the CI issue I mentioned. see ranges::sort has linker error

https://godbolt.org/z/68Gdcxrnc

}
module ranges_stable_partition {
header "__algorithm/ranges_stable_partition.h"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading