priority-queue icon indicating copy to clipboard operation
priority-queue copied to clipboard

Add a pop_lowest_priority API

Open Luni-4 opened this issue 2 years ago • 2 comments

It would be helpful to add a pop API that extracts the lowest priority from the queue and not only the greatest one (as documented here)

Luni-4 avatar Jul 20 '22 08:07 Luni-4

If you need a PriorityQueue that can extract at any time the greatest or the lowest priority, you can use the DoublePriorityQueue data structure. It features two methods: pop_max() and pop_min().

If you always want to extract the minimum priority item, you can wrap your priorities in a standard wrapper called Reverse<T>

garro95 avatar Jul 23 '22 08:07 garro95

Oh, thank you! Could you please provide some examples in README or inside the examples directory about these two patterns you just mentioned? That would be really helpful! Thank you!

Luni-4 avatar Jul 23 '22 10:07 Luni-4

@Luni-4 I needed this too - here's an example:

let mut pq: PriorityQueue<String, Reverse<u64>> = PriorityQueue::new();

pq.push("1".into(), Reverse(10));
pq.push("2".into(), Reverse(15));

rikonor avatar Dec 02 '22 13:12 rikonor