Skip to content

Add BinaryHeap::retain - #668

Open
Conaclos wants to merge 5 commits into
rust-embedded:mainfrom
Conaclos:binaryheap_retain
Open

Add BinaryHeap::retain#668
Conaclos wants to merge 5 commits into
rust-embedded:mainfrom
Conaclos:binaryheap_retain

Conversation

@Conaclos

@Conaclos Conaclos commented Jul 8, 2026

Copy link
Copy Markdown

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.

@Conaclos Conaclos changed the title Add BinaryHeap::retain Add BinaryHeap::retain Jul 8, 2026
@Conaclos
Conaclos force-pushed the binaryheap_retain branch 4 times, most recently from 87e788b to 64e77df Compare July 8, 2026 15:17
@zeenix

zeenix commented Jul 8, 2026

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
Author

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

zeenix commented Jul 8, 2026

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

Conaclos commented Jul 8, 2026

Copy link
Copy Markdown
Author

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

@Conaclos

Conaclos commented Jul 8, 2026

Copy link
Copy Markdown
Author

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

zeenix commented Jul 8, 2026

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
Conaclos force-pushed the binaryheap_retain branch 3 times, most recently from 4c41991 to d8e3a5c Compare July 21, 2026 20:02
@Conaclos
Conaclos force-pushed the binaryheap_retain branch from 985fee8 to beb1189 Compare July 21, 2026 20:10
@Conaclos
Conaclos force-pushed the binaryheap_retain branch 2 times, most recently from 58678fe to 1535316 Compare July 21, 2026 20:12
@Conaclos

Copy link
Copy Markdown
Author

Ready for review!

@zeenix zeenix 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 thread CHANGELOG.md Outdated
Comment thread src/binary_heap.rs
Comment thread src/binary_heap.rs
@Conaclos

Copy link
Copy Markdown
Author

@zeenix Is there any concerns preventing merging this feature?

@zeenix

zeenix commented Jul 27, 2026

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 team July 27, 2026 11:59

@sgued sgued 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 thread src/binary_heap.rs
fn sift_down_to_bottom(&mut self, mut pos: usize) {
let end = self.len();
let start = pos;
// Moves the element down to a leaf.

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.

Please make this a // SAFETY: comment, that way we can progressively work towards enabling #![deny(clippy::undocumented_unsafe_blocks)]

Comment thread src/binary_heap.rs

#[test]
fn remove_unchecked() {
// This test depends on implementation details.

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.

Suggested change
// This test depends on implementation details.
// This test depends on private APIs.

I spent some times figuring out which implementation detail regarding order or something was impactful, I think "private APIs" is clearer and is already used in another comment.

Sign up for free to 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