Uh oh!
There was an error while loading. Please reload this page.
Add BinaryHeap::contains and BinaryHeap::remove - #82002
Conversation
rust-highfive
commented
Feb 11, 2021
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I think incorrect (or malicious) `PartialEq` and `PartialOrd` implementations for `T` could define `x == y` and `x < y` to both be conditionally true with interior mutability in such a way that `pos == len - 1` (implying `data[pos] == data[len - 1]`) but we end up in the `data[pos] < data[len - 1]` branch. `sift_up()` and `sift_down()` assume that `pos` is in-bounds which would be violated in this case.
m-ou-se
commented
Mar 3, 2021
Thanks for your PR. I'm afraid that adding these functions will be misleading, as they do nothing special with the structure of a binary heap, making them O(n) just like on a Vec. For For What do you think? |
Thomasdezeeuw
commented
Mar 3, 2021
@m-ou-se I'm mostly interesting interest in the |
scottmcm
commented
Mar 4, 2021
Maybe |
billyrieger
commented
Mar 4, 2021
@m-ou-se I agree that Initially I had an implementation that used the max-heap property to search for elements as @Thomasdezeeuw suggested but I'm not sure that it's any better than just iterating over the underlying data. The average- and worst-case time complexity is still the same, at least. impl<T:Ord>BinaryHeap<T>{fnfind(&self,item:&T) -> Option<usize>{self.find_recursive(item,0)}fnfind_recursive(&self,item:&T,pos:usize) -> Option<usize>{if pos >= self.data.len(){None}else{matchself.data[pos].cmp(item){// If `self.data[pos] == item`, return the item's position.Ordering::Equal => Some(pos),// If `self.data[pos] < item`, the item cannot be in either of// the child branches (because this is a max heap).Ordering::Less => None,// If `self.data[pos] > item`, we need to search both child branches.Ordering::Greater => {let left_child = 2* pos + 1;let right_child = 2* pos + 2;self.find_recursive(item, left_child).or_else(|| self.find_recursive(item, right_child))}}}}} |
billyrieger
commented
Mar 4, 2021
This relies on the fact that |
bors
commented
Mar 4, 2021
☔ The latest upstream changes (presumably #82756) made this pull request unmergeable. Please resolve the merge conflicts. |
Triage: there's merge conflicts now, and it would be best if you could start a discussion on Zulip to make progress on this. |
JohnCSimon
commented
Apr 4, 2021
@billyrieger ping from triage: can you please fix the merge conflicts? Thank you. |
billyrieger
commented
Apr 19, 2021
I'm going to close this - #82331 covers some of the same functionality and at this point it's unclear what the implementation details for |
Tracking issue: #82001.