C# .NET 排序不佳的容器

发布于 2024-10-02 06:14:21 字数 379 浏览 0 评论 0原文

我要用 C# 编写一个基于事件的模拟器。我需要一个具有以下功能的调度程序排序容器:

  • 键 - 值对按键排序存储(时间、委托对)
  • 按键高效插入和删除(删除最小的项目,插入是任意的)
  • 最小的项目(键值对)可以查询。

我需要的实际上是一个非常基本的二叉树或排序队列或类似的东西。但我在 .NET 中的选项 - SortedList 和 SortedDictionary - 并不令人满意。第一个存在插入和删除的效率问题,第二个存在查询最小项目的问题。

我应该开始实现自己的容器还是我会错过一些东西?令人难以置信的是,没有一个内置容器可以满足我的需求。

谢谢!

(更新:我正在寻找.NET 2.0下的解决方案)

I'm to write an event based simulator in C#. I need a sorted container for the scheduler that has the following capabilities:

  • Key - Value pairs are stored sorted by the key (Time, Delegate pairs)
  • Efficient inserts and removes by key (Smallest items are removed, inserts are arbitrary)
  • The smallest item (key value pair) can be queried.

What I need is actually a very basic binary tree or a sorted queue or something similar. But the options I have in .NET - SortedList and SortedDictionary - are unsatisfactory. The first has efficiency issues with inserts and removes, the second has problems with querying the smallest items.

Should I start implementing my own container or I miss something? It is so unbelievable that there is no built in container that fits my needs.

Thanks!

(update: Im looking for a solution under .NET 2.0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-10-09 06:14:21

如果您自己跟踪最小的项目,您仍然可以使用 SortedDictionary

但我和 @Frédéric 有同样的问题 - sortedDictionary.First() 有什么问题吗?

编辑:根据 Frédéric 的建议,.NET 2.0 的 Enumerable.First 实现:

public static T First(IEnumerable<T> items)
{
    foreach (T item in items)
        return item;

    throw new InvalidOperationException("The source sequence is empty.");
}

You could still use a SortedDictionary if you kept track of the smallest item yourself.

But I have the same question as @Frédéric - what's wrong with sortedDictionary.First()?

Edit: As per Frédéric's suggestion, an Enumerable.First implementation for .NET 2.0:

public static T First(IEnumerable<T> items)
{
    foreach (T item in items)
        return item;

    throw new InvalidOperationException("The source sequence is empty.");
}
画骨成沙 2024-10-09 06:14:21

如果 .NET 集合没有提供您正在寻找的功能和性能,您可能需要考虑 C5 库

If the .NET collections don't provide the functionality and performance that you're looking for, you might want to consider the C5 library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文