English 中文(简体)
Why doesn t the .Net framework have a priority queue class?
原标题:

There are some threads on Stack Overflow dealing with implementing priority queues in .Net and C#.

My issue is of a more basic nature: Why isn t there a priority queue out of the box in the .Net framework? Even the C++ Standard Library has one.

最佳回答

There was a question a while ago (why C# does allow non-member functions like C++) which prompted Eric Lippert to write a blog post about the reasons why. In it, he explains:

I am asked "why doesn t C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature. All six of those things are necessary to make a feature happen. All of them cost huge amounts of time, effort and money. Features are not cheap, and we try very hard to make sure that we are only shipping those features which give the best possible benefits to our users given our constrained time, effort and money budgets.

I suspect that is probably the answer to why .Net does not ship with a priority queue - there was just not enough time, effort, money, demand(?) to implement one.

问题回答

.NET 4.0 introduces a SortedSet<T> class, along with the ISet<T> interface which is implemented by SortedSet<T> and HashSet<T>. This will obviously make it simpler to implement your own PriorityQueue<T> class.

However, there is still no IQueue<T> interface, which would at least admit the need for priority queues or any other implementation than the basic BCL Queue<T>. Similarly, there s no IStack<T>.

Personally I find this lack of some of these most basic interfaces disappointing and short-sighted, especially as the design/specification/implementation/testing/documentation cost of extracting a simple interface from an existing class should be very low indeed.

public interface IQueue<T> : IEnumerable<T>, ICollection, IEnumerable
{
    T Dequeue();
    void Enqueue(T item);
    T Peek();
}

There, see? I ve done it.

This has been officially announced and is in the .NET 6 preview.

PriorityQueue<TElement, TPriority> (System.Collections.Generic) is a new collection that enables adding new items with a value and a priority. On dequeue the PriorityQueue returns the element with the lowest priority value. You can think of this new collection as similar to Queue but that each enqueued element has a priority value that affects the behavior of dequeue.

The following sample demonstrates the behavior of PriorityQueue<string, int>.

// creates a priority queue of strings with integer priorities
var pq = new PriorityQueue<string, int>();

// enqueue elements with associated priorities
pq.Enqueue("A", 3);
pq.Enqueue("B", 1);
pq.Enqueue("C", 2);
pq.Enqueue("D", 3);

pq.Dequeue(); // returns "B"
pq.Dequeue(); // returns "C"
pq.Dequeue(); // either "A" or "D", stability is not guaranteed.

As of January 2021, .Net Core added a PriorityQueue implementation. The actual commit to the repo and the API can be found here: https://github.com/dotnet/runtime/commit/826aa4f7844fd3d48784025ec6d47010867baab4

It s available as part of .NET6 now. Check out the following blog post for implementation.

https://dotnetcoretutorials.com/2021/03/17/priorityqueue-in-net/

See this: Add PriorityQueue to Collections, they take farking 5 years, still no PriorityQueue.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签