Skip to content

Conversation

@CaseyCarter
Copy link
Contributor

@CaseyCarter CaseyCarter commented Jul 25, 2020

Instead of assuming that the _Val argument to the heap helper functions is an rvalue of some type, perfect-forward it all the way into _Push_heap_by_index where it's finally written into the hole in the heap. This allows partial_sort_copy to pass in a reference to an element in its input range which is then correctly written into the output range in _Push_heap_by_index.

I update the std::ranges heap helpers similarly, and make their constraints explicit. The helpers with a _Val argument now accept a distinct projection to be applied to _Val, which will enable the implementation of std::ranges::partial_sort_copy.

Drive-by: Replace _unchecked in the names of ranges::_Make_heap_fn::_Make_heap_unchecked and ranges::_Sort_heap_fn::_Sort_heap_unchecked with _common to be consistent with the names of other internal helpers that require the sentinel and iterator to have the same type. Promote both functions from class scope to namespace scope as needed to support implementation of partial_sort and partial_sort_copy.

Fixes #1086.

Instead of assuming that the `_Val` argument to the heap helper functions is an rvalue of some type, perfect-forward it all the way into `_Push_heap_by_index` where it's finally written into the hole in the heap. This allows `partial_sort_copy` to pass in ` reference to an element in its input range which is then correctly written into the output range in `_Push_heap_by_index`.

I update the `std::ranges` heap helpers similarly, and make their constraints explicit. The helpers with a `_Val` argument now accept a distinct projection to be applied to `_Val`, which will enable the implementation of `std::ranges::partial_sort_copy`.

Drive-by: Replace `_unchecked` in the names of `ranges::_Make_heap_fn::_Make_heap_unchecked` and `ranges::_Sort_heap_fn::_Sort_heap_unchecked` with `_common` to be consistent with the names of other internal helpers that require the sentinel and iterator to have the same type. Promote both functions from class scope to namespace scope as need to support implementation of `partial_sort` and `partial_sort_copy`.

Fixes microsoft#1086.
@CaseyCarter CaseyCarter added the bug Something isn't working label Jul 25, 2020
@CaseyCarter CaseyCarter requested a review from a team as a code owner July 25, 2020 20:00
@CaseyCarter CaseyCarter mentioned this pull request Jul 27, 2020
Copy link
Member

@StephanTLavavej StephanTLavavej left a comment

Choose a reason for hiding this comment

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

I'll push a micro-change, then this looks good to me!

@cbezault cbezault removed their assignment Aug 5, 2020
@StephanTLavavej StephanTLavavej self-assigned this Aug 8, 2020
@StephanTLavavej StephanTLavavej merged commit af3db68 into microsoft:master Aug 9, 2020
@StephanTLavavej
Copy link
Member

Thanks for finding and fixing this bug! 🪲

@CaseyCarter CaseyCarter deleted the partial_sort_copy_fix branch August 10, 2020 01:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<algorithm>: std::partial_sort_copy performs an unconstrained operation

4 participants