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 flexibility, BuildComparator #38886

Closed
leonardo-m opened this issue Jan 6, 2017 · 2 comments
Closed

BinaryHeap flexibility, BuildComparator #38886

leonardo-m opened this issue Jan 6, 2017 · 2 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

leonardo-m commented Jan 6, 2017

In D language when you create a binary heap you can optionally specify the comparison function:

void main() {
    import std.stdio, std.container;
    auto heap1 = [10, 2, 5, 3].heapify;
    writeln(heap1); // Output: [10, 5, 3, 2]
    auto heap2 = [10, 2, 5, 3].heapify!q{b < a};
    writeln(heap2); // Output: [2, 3, 5, 10]
}

This is handy. Similar code in Rust:

fn main() {
    use std::collections::BinaryHeap;
    let heap1: BinaryHeap<_> = [10, 2, 5, 3].iter().cloned().collect();
    println!("{:?}", heap1); // [10, 3, 5, 2]
}

If you just have to reverse the ordering (a min-heap) you can sometimes wrap your items with this:

https://docs.rs/revord/0.0.2/revord/struct.RevOrd.html

But in general the cmp function can be more complex (or it can involve just one field of each item). Currently to solve this problem you need to write a lot of boilerplate code, a newtype and implement the four Eq/Ord traits on it (but a binary heap doesn't need to test the equality of the the items).

So can we add something more handy to BinaryHeap to specify a different comparison function?

A comment by cuviper:

To support collect, I think you'd have to incorporate it into the type, just like HashMap<K, V, S: BuildHasher>. So this would have some kind of BuildComparator, and it could be added without a breaking change by defaulting to a MaxComparator.

@cuviper
Copy link
Member

cuviper commented Jan 6, 2017

The general case might still feel like annoying boiler plate with what I suggested, having to implement a BuildComparator and corresponding Comparator. But maybe a few provided basics can ease most cases, like MaxComparator, MinComparator, and generally FnMutComparator.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 26, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2017

I would love to support this better. That D code is really slick! I believe this is going to require an RFC in which someone proposes a concrete API for how this would work in Rust. The BuildComparator approach sounds promising.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants