Add BinaryHeap::retain - #668
Conversation
87e788b to
64e77df
Compare
How so? |
The implementation doesn't preserve the order invariant of the binary heap. |
Huh? Is it? 🤔 https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=98dfdde25b2a102d7146761f6d5294db |
|
I will add some tests for |
Your example is wrong because you called |
Ah right. That was from the std doc example I started off from. |
4c41991 to
d8e3a5c
Compare
985fee8 to
beb1189
Compare
58678fe to
1535316
Compare
|
Ready for review! |
zeenix
left a comment
There was a problem hiding this comment.
Only tiny nits and then it's good from my side.
|
@zeenix Is there any concerns preventing merging this feature? |
Not from my side but I was hoping for another review from someone else. |
sgued
left a comment
There was a problem hiding this comment.
LGTM besides two small nits.
Thank you!
| fn sift_down_to_bottom(&mut self, mut pos: usize) { | ||
| let end = self.len(); | ||
| let start = pos; | ||
| // Moves the element down to a leaf. |
There was a problem hiding this comment.
Please make this a // SAFETY: comment, that way we can progressively work towards enabling #![deny(clippy::undocumented_unsafe_blocks)]
|
|
||
| #[test] | ||
| fn remove_unchecked() { | ||
| // This test depends on implementation details. |
There was a problem hiding this comment.
| // 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.
Implement #666
This PR adds the
BinaryHeap::retainmethod.I didn't base my work on #345 because the implementation seems wrong to me.
I added an internal method
BinaryHeap::removethat is now used by bothBinaryHeap::retainandBinaryHeap::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
retainremoves multiple elements. However, this would make the implementation much more complex.