Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`BinaryHeap` owned iterators should yield items in heap order #59278

Open
jonhoo opened this Issue Mar 18, 2019 · 4 comments

Comments

Projects
None yet
5 participants
@jonhoo
Copy link
Contributor

jonhoo commented Mar 18, 2019

Currently, both BinaryHeap::into_iter and BinaryHeap::drain yield the heap elements in an arbitrary order. This seems counter-intuitive given the nature of a heap. Sadly, we can't simply change their behavior behind the scenes, as we would have to remove the implementation of DoubleEndedIterator. Should we perhaps add into_ordered_iter and drain_ordered? The implementation is pretty straightforward so I'd be happy to submit a PR.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented Mar 30, 2019

Isn't into_sorted_vec().into_iter() sufficient?

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Apr 1, 2019

@clarfon No, because getting the largest k items from that is O(n lg n + k), whereas with into_ordered_iter() it would be O(k lg n).

@ckaran

This comment has been minimized.

Copy link

ckaran commented Apr 2, 2019

@clarfon In addition to what @scottmcm said, into_sorted_vec().into_iter() takes up space proportional to the number of items in the heap because you have to create the Vec first. That means that you've roughly doubled the amount of memory you need, which could be a concern either when you are on more limited systems, or when you're dealing with very large amounts of data.

@clarfon

This comment has been minimized.

Copy link
Contributor

clarfon commented Apr 3, 2019

@ckaran into_sorted_vec actually doesn't take up any space at all; it reuses the same buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.