Add BinaryHeap::into_iter_sorted()

Existing BinaryHeap::iter() returns an iterator that visits underlying data in arbitrary order. I think it’s worth adding a sorted version. BinaryHeap already has into_sorted_vec() method, but using the iterator should be more performant in some cases.

I couldn’t find any previous discussion.

One question is that in what order the sorted iterator should visit the underlying data. BinaryHeap::pop() returns the greatest item, while into_sorted_vec() sorts the data in ascending order.

Given that one can now just do std::iter::from_fn(move || my_heap.pop()), I’m less confident that this is worth it in std, since the api discussions are non-obvious.

5 Likes

Ah, I didn’t come up with using from_fn(). That’s a concise and good way. Thanks!

And I just found a discussion about the sorted version of the iterator. Sorry.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.