为什么.Net框架没有优先级队列类?
Stack Overflow 上有一些线程涉及在 . NET 和 C#。
我的问题具有更基本的性质:为什么 .Net 框架中没有开箱即用的优先级队列?甚至 C++ 标准库也有一个。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不久前有一个问题(为什么 C# 允许像 C++ 这样的非成员函数),这促使 Eric Lippert 写了一个 关于原因的博客文章。他在其中解释道:
我怀疑这可能就是 .Net 不附带优先级队列的原因 - 只是没有足够的时间、精力、金钱、需求(?)来实现优先级队列。
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 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 引入了。这显然会使您自己的
SortedSet
类,以及由SortedSet
和ISet
接口实现的代码>HashSetPriorityQueue
类的实现变得更加简单。但是,仍然没有
IQueue
接口,该接口至少承认需要优先级队列或除基本 BCLQueue
之外的任何其他实现。同样,也没有IStack
。就我个人而言,我发现缺乏一些最基本的接口令人失望和短视,特别是因为从现有类中提取简单接口的设计/规范/实现/测试/文档成本确实应该非常低。
在那里,看到了吗?我已经做到了。
.NET 4.0 introduces a
SortedSet<T>
class, along with theISet<T>
interface which is implemented bySortedSet<T>
andHashSet<T>
. This will obviously make it simpler to implement your ownPriorityQueue<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 BCLQueue<T>
. Similarly, there's noIStack<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.
There, see? I've done it.
这已正式宣布,并且位于 . NET 6 预览。
This has been officially announced and is in the .NET 6 preview.
截至 2021 年 1 月,.Net Core 添加了 PriorityQueue 实现。可以在此处找到对存储库和 API 的实际提交: https://github.com/ dotnet/runtime/commit/826aa4f7844fd3d48784025ec6d47010867baab4
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
它现在作为 .NET6 的一部分提供。查看以下博客文章以了解实施情况。
https://dotnetcoretutorials.com/2021/03/17/priorityqueue-in -net/
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/