views:

3868

answers:

6

I am looking for a .Net (preferably C#) implementation of a priority queue or heap.

Unless I am looking in the wrong place, there isn't one in the framework. Is anyone aware of a good one, or should I roll my own?

A: 

Pretty trivial to roll your own if the priorities are pre-defined.

Joel Coehoorn
Right, but if the priority can take any value, like a Double "milliseconds left before execution" - to get good performance there, you'd really need an implementation based on a heap or a self-balancing binary search tree.
Johann Gerell
+3  A: 

I found one at CodeProject that looks like it fits well in the .NET framework. I didn't test it, though.

http://www.codeproject.com/KB/recipes/priorityqueue.aspx

You may want to write your own, though. It could be fun.

EndangeredMassa
+3  A: 

I like using the OrderedBag and OrderedSet classes in PowerCollections as priority queues.

Ben Hoffstein
OrderedBag/OrderedSet do more work than necessary, they use a red-black tree instead of a heap.
Dan Berindei
+1  A: 

Use a Java to C# translator on the Java implementation (java.util.PriorityQueue) in the Java Collections framework, or more intelligently use the algorithm and core code and plug it into a C# class of your own making that adheres to the C# Collections framework API for Queues, or at least Collections.

JeeBee
+3  A: 

I found one by Julian Bucknall on his blog here - http://www.boyet.com/Articles/PriorityQueueCSharp3.html

We modified it slightly so that low-priority items on the queue would eventually 'bubble-up' to the top over time, so they wouldn't suffer starvation.

Duncan
+8  A: 

Check out the "C5 Generic Collection Library" http://www.itu.dk/research/c5/

there you can use the IntervalHeap

jaras