Add BinaryHeap::retain#668
Conversation
f8167da to
87e788b
Compare
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. |
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.