From edc5d786884e95a3315ef9bdf64b643b054a109a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Michal?= Date: Tue, 6 Jan 2026 14:34:27 +0100 Subject: [PATCH 1/4] Simplify iterator operations in _Flat_map_base::_Erase_if and similar. --- stl/inc/flat_map | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index a8a72666874..d434dd7d5ab 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -1024,16 +1024,13 @@ private: template _NODISCARD size_type _Erase_if(_Predicate _Pred) { - auto _View = _View_to_mutate(); - auto _Mut_first = _View.begin(); - const auto _Mut_last = _View.end(); - const auto _Old_size = size(); - _Clear_guard _Guard{this}; - _STD _Seek_wrapped(_Mut_first, _RANGES remove_if(_View, _Pred).begin()); - _Data.keys.erase(_Mut_first._Key_it, _Mut_last._Key_it); - _Data.values.erase(_Mut_first._Mapped_it, _Mut_last._Mapped_it); + const auto _New_last = _RANGES remove_if(_View_to_mutate(), _Pred).begin(); + const auto _Old_size = size(); + + _Data.keys.erase(_New_last._Key_it, _Data.keys.end()); + _Data.values.erase(_New_last._Mapped_it, _Data.values.end()); _Guard._Target = nullptr; return _Old_size - size(); @@ -1071,17 +1068,13 @@ private: _Clear_guard _Guard{this}; // O(N) if already sorted. - const auto _Begin_unsorted = - _STD is_sorted_until(_STD cbegin(_Data.keys), _STD cend(_Data.keys), _Pass_key_comp()); - const difference_type _Num_sorted = _Begin_unsorted - _STD cbegin(_Data.keys); + const auto _Num_sorted = _RANGES is_sorted_until(_Data.keys, _Pass_key_comp()) - _Data.keys.begin(); auto _View = _View_to_mutate(); - const auto _Begin = _STD begin(_View); - const auto _Begin_unsorted_values = _Begin + _Num_sorted; - const auto _End = _STD end(_View); + auto const _Begin_unsorted_values = _STD begin(_View) + _Num_sorted; - _RANGES sort(_Begin_unsorted_values, _End, value_compare{_Key_compare}); - _RANGES inplace_merge(_Begin, _Begin_unsorted_values, _End, value_compare{_Key_compare}); + _RANGES sort(_Begin_unsorted_values, _STD end(_View), value_compare{_Key_compare}); + _RANGES inplace_merge(_View, _Begin_unsorted_values, value_compare{_Key_compare}); _Erase_dupes_if_not_multi(); @@ -1093,16 +1086,12 @@ private: void _Erase_dupes_if_not_multi() { if constexpr (_IsUnique) { auto _Sorted_view = _View_to_mutate(); - const auto _Subrange = _RANGES unique(_Sorted_view, [this](const_reference _Left, const_reference _Right) { + const auto _New_last = _RANGES unique(_Sorted_view, [this](const_reference _Left, const_reference _Right) { return this->_Key_equal(_Left.first, _Right.first); - }); - const auto _Remaining_count = _Subrange.begin() - _Sorted_view.begin(); - _Data.keys.erase( - _Data.keys.begin() + static_cast<_RANGES range_difference_t<_KeyContainer>>(_Remaining_count), - _Data.keys.end()); - _Data.values.erase( - _Data.values.begin() + static_cast<_RANGES range_difference_t<_MappedContainer>>(_Remaining_count), - _Data.values.end()); + }).begin(); + + _Data.keys.erase(_New_last._Key_it, _Data.keys.end()); + _Data.values.erase(_New_last._Mapped_it, _Data.values.end()); } } From 201af88179c5bd42f2dc02f1eede485a0732eed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Michal?= Date: Wed, 7 Jan 2026 02:00:36 +0100 Subject: [PATCH 2/4] Small cleanup in _Flat_map_base::_Insert_range --- stl/inc/flat_map | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index d434dd7d5ab..07e85d58522 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -1120,9 +1120,7 @@ private: // Sort the newly inserted elements auto _Sorted_view = _View_to_mutate(); if constexpr (_NeedSorting) { - auto _Sorted_new_elements = _Sorted_view; - _Sorted_new_elements.advance(_Old_distance); - _RANGES sort(_Sorted_new_elements, value_compare{_Key_compare}); + _RANGES sort(_STD begin(_Sorted_view) + _Old_distance, _STD end(_Sorted_view), value_compare{_Key_compare}); } else { _STL_ASSERT(_Is_sorted_and_unique( _Data.keys.begin() + static_cast(_Old_distance), From 94761b63ff5affa14596b52740921d485a5b985d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Michal?= Date: Wed, 7 Jan 2026 14:39:15 +0100 Subject: [PATCH 3/4] Rename _Flat_map_base::_Sort_and_erase_dupes_... for consistency with flat_set. --- stl/inc/flat_map | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index 07e85d58522..1df43ce01bc 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -352,7 +352,7 @@ public: key_container_type _Key_cont, mapped_container_type _Mapped_cont, const key_compare& _Comp = key_compare()) : _Data{.keys = _STD move(_Key_cont), .values = _STD move(_Mapped_cont)}, _Key_compare(_Comp) { _STL_ASSERT(_Data.keys.size() == _Data.values.size(), "key_cont.size() != mapped_cont.size()"); - _Sort_and_erase_dupes_if_not_multi(); + _Make_invariants_fulfilled(); } template <_Usable_allocator_for _Allocator> @@ -367,7 +367,7 @@ public: .values = _STD make_obj_using_allocator(_Alloc, _Mapped_cont)}, _Key_compare(_Comp) { _STL_ASSERT(_Data.keys.size() == _Data.values.size(), "key_cont.size() != mapped_cont.size()"); - _Sort_and_erase_dupes_if_not_multi(); + _Make_invariants_fulfilled(); } _Flat_map_base(_Sorted_t, key_container_type _Key_cont, mapped_container_type _Mapped_cont, @@ -1064,8 +1064,10 @@ private: return _Is_sorted_and_unique(_Cont.begin(), _Cont.end()); } - void _Sort_and_erase_dupes_if_not_multi() { - _Clear_guard _Guard{this}; + void _Make_invariants_fulfilled() { + if (empty()) { // FIXME maybe consider if (begin() == end()) for consistency with flat_set + return; + } // O(N) if already sorted. const auto _Num_sorted = _RANGES is_sorted_until(_Data.keys, _Pass_key_comp()) - _Data.keys.begin(); @@ -1079,8 +1081,6 @@ private: _Erase_dupes_if_not_multi(); _STL_INTERNAL_CHECK(_Is_sorted_and_unique()); - - _Guard._Target = nullptr; } void _Erase_dupes_if_not_multi() { From a8344f286c134b5b6f7508a01594113358fc1db8 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 8 Jan 2026 12:18:38 -0800 Subject: [PATCH 4/4] West const. --- stl/inc/flat_map | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/flat_map b/stl/inc/flat_map index 1df43ce01bc..b2294bb805c 100644 --- a/stl/inc/flat_map +++ b/stl/inc/flat_map @@ -1073,7 +1073,7 @@ private: const auto _Num_sorted = _RANGES is_sorted_until(_Data.keys, _Pass_key_comp()) - _Data.keys.begin(); auto _View = _View_to_mutate(); - auto const _Begin_unsorted_values = _STD begin(_View) + _Num_sorted; + const auto _Begin_unsorted_values = _STD begin(_View) + _Num_sorted; _RANGES sort(_Begin_unsorted_values, _STD end(_View), value_compare{_Key_compare}); _RANGES inplace_merge(_View, _Begin_unsorted_values, value_compare{_Key_compare});