Skip to content

Add BinaryHeap::retain - #668

Merged
sgued merged 8 commits into
rust-embedded:mainfrom
Conaclos:binaryheap_retain
Aug 8, 2026
Merged

Add BinaryHeap::retain#668
sgued merged 8 commits into
rust-embedded:mainfrom
Conaclos:binaryheap_retain

Conversation

@Conaclos

@ConaclosConaclos commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Implement #666

This PR adds the BinaryHeap::retain method.
I didn't base my work on #345 because the implementation seems wrong to me.

I added an internal method BinaryHeap::remove that is now used by both BinaryHeap::retain and BinaryHeap::pop_unchecked.
In the process I improved its implementation.

One alternative implementation could be to traverse the internal vector in reverse order. This could reduce the number of shifted elements if retain removes multiple elements. However, this would make the implementation much more complex.

@ConaclosConaclos changed the title Add BinaryHeap::retainAdd BinaryHeap::retainJul 8, 2026
@Conaclos
Conaclosforce-pushed the binaryheap_retain branch 4 times, most recently from 87e788b to 64e77dfCompareJuly 8, 2026 15:17
@zeenix

Copy link
Copy Markdown
Contributor

I didn't base my work on #345 because the implementation seems wrong to me.

How so?

@Conaclos

Conaclos commented Jul 8, 2026

Copy link
Copy Markdown
ContributorAuthor

How so?

The implementation doesn't preserve the order invariant of the binary heap.
For example, if you take the binary max-heap [8, 2, 5] and ask to retain only the numbers that are different of 8, then you end up with [2, 5], which is incorrect.

@zeenix

Copy link
Copy Markdown
Contributor

For example, if you take the binary max-heap [8, 2, 5] and ask to retain only the numbers that are different of 8, then you end up with [2, 5], which is incorrect.

Huh? Is it? 🤔 https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=98dfdde25b2a102d7146761f6d5294db

@Conaclos

Copy link
Copy Markdown
ContributorAuthor

I will add some tests for sift_down_to_bottom because the function seems untested with arguments (pos) greater than 0.

@Conaclos

Copy link
Copy Markdown
ContributorAuthor

Huh? Is it? 🤔 https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=98dfdde25b2a102d7146761f6d5294db

Your example is wrong because you called into_sorted_vec() in the assertion. It will always return [2, 5].
Instead, try: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=dccba979ca3526f18a2a2dea5f93130f

@zeenix

Copy link
Copy Markdown
Contributor

Your example is wrong because you called into_sorted_vec() in the assertion. It will always return [2, 5].
Instead, try: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=dccba979ca3526f18a2a2dea5f93130f

Ah right. That was from the std doc example I started off from.

@Conaclos
Conaclosforce-pushed the binaryheap_retain branch 6 times, most recently from 58678fe to 1535316CompareJuly 21, 2026 20:12
@Conaclos

Copy link
Copy Markdown
ContributorAuthor

Ready for review!

@zeenixzeenix left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only tiny nits and then it's good from my side.

Comment threadCHANGELOG.md Outdated
Comment threadsrc/binary_heap.rs
Comment threadsrc/binary_heap.rs
zeenix
zeenix previously approved these changes Jul 22, 2026
@Conaclos

Copy link
Copy Markdown
ContributorAuthor

@zeenix Is there any concerns preventing merging this feature?

@zeenix

Copy link
Copy Markdown
Contributor

@zeenix Is there any concerns preventing merging this feature?

Not from my side but I was hoping for another review from someone else.

@zeenix
zeenix requested a review from a teamJuly 27, 2026 11:59

@sguedsgued left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM besides two small nits.

Thank you!

Comment threadsrc/binary_heap.rs Outdated
Comment threadsrc/binary_heap.rs Outdated
@Conaclos

Conaclos commented Aug 1, 2026

Copy link
Copy Markdown
ContributorAuthor

@sgued Fixed.

I took the opportunity of making unsafe the relevant functions, adding missing unsafe blocks and SAFETY comments. This improves compatibility with clippy::undocumented_unsafe_blocks and clippy::multiple_unsafe_ops_per_block.

I added PeekMutInner::new that allows checking the type invariant.
Ideally, this type should be moved to a dedicated module to protect its instantiation.
I renamed PeekMutInner::sift into PeekMutInner::removed (inverting the boo logic) to make clearer its purpose.

Last, I turned some conditionals into early returns to make them cold paths.

@Conaclos
Conaclos requested a review from sguedAugust 3, 2026 16:22
@sgued
sguedforce-pushed the binaryheap_retain branch from 7de39e5 to 4d515c9CompareAugust 5, 2026 16:00
sgued
sgued previously approved these changes Aug 5, 2026
@sgued
sgued requested a review from zeenixAugust 5, 2026 16:57
@sgued

sgued commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

I rebased it and force pushed a typo fix and CI fixes

@sgued
sgued added this pull request to the merge queueAug 8, 2026
Merged via the queue into rust-embedded:main with commit f008da8Aug 8, 2026
21 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@Conaclos@zeenix@sgued