Skip to content

Commit 162397f

Browse files
authored
[libc++] Replace stable_sort with sort in flat_map (#121431)
Fixes #120788
1 parent 2a551ab commit 162397f

File tree

5 files changed

+5
-202
lines changed

5 files changed

+5
-202
lines changed

libcxx/include/__flat_map/flat_map.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <__algorithm/ranges_inplace_merge.h>
1818
#include <__algorithm/ranges_lower_bound.h>
1919
#include <__algorithm/ranges_partition_point.h>
20-
#include <__algorithm/ranges_stable_sort.h>
20+
#include <__algorithm/ranges_sort.h>
2121
#include <__algorithm/ranges_unique.h>
2222
#include <__algorithm/ranges_upper_bound.h>
2323
#include <__algorithm/remove_if.h>
@@ -853,9 +853,7 @@ class flat_map {
853853
// is no invariant state to preserve
854854
_LIBCPP_HIDE_FROM_ABI void __sort_and_unique() {
855855
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
856-
// To be consistent with std::map's behaviour, we use stable_sort instead of sort.
857-
// As a result, if there are duplicated keys, the first value in the original order will be taken.
858-
ranges::stable_sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });
856+
ranges::sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });
859857
auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin();
860858
auto __dist = ranges::distance(__zv.begin(), __dup_start);
861859
__containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());
@@ -886,7 +884,7 @@ class flat_map {
886884
return __compare_(std::get<0>(__p1), std::get<0>(__p2));
887885
};
888886
if constexpr (!_WasSorted) {
889-
ranges::stable_sort(__zv.begin() + __append_start_offset, __end, __compare_key);
887+
ranges::sort(__zv.begin() + __append_start_offset, __end, __compare_key);
890888
} else {
891889
_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(
892890
__is_sorted_and_unique(__containers_.keys | ranges::views::drop(__append_start_offset)),

libcxx/include/module.modulemap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@ module std [system] {
760760
module ranges_sort {
761761
header "__algorithm/ranges_sort.h"
762762
export std.functional.ranges_operations
763+
export std.algorithm.sort
764+
export std.algorithm.make_projected
763765
}
764766
module ranges_stable_partition {
765767
header "__algorithm/ranges_stable_partition.h"

libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.pass.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.pass.cpp

Lines changed: 0 additions & 66 deletions
This file was deleted.

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range_stability.pass.cpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)