c# - .NET 中是否存在排序队列?

标签 c# collections

我需要一个相当专业的集合 .NET,我不认为 BCL 可以帮助我,但我想如果有人知道类似的东西,我会把它扔在那里。

基本上,我的要求是:

  • 我有一个值对列表,例如:(3, 10), (5, 10), (3, 7), (5, 5)
  • 顺序很重要,即。 (3, 10) != (10, 3)
  • 单个值的重复是可以的,但应该删除重复的对(最好是默默地)。
  • 关键是,我需要一直对这个列表进行排序。在任何时候,我只对排序算法定义的列表中的第一个值感兴趣。

因此,一些我希望能够执行的示例代码(正如我所设想的那样,它可能会被实现,其他符合上述内容的实现也可以):

public class Pair
{
    public Pair(int first, int second)
    { First = first; Second = second; }
    public int First { get; set; }
    public int Second { get; set; }
}

SortedQueue<Pair> foo = new SortedQueue<Pair>((left, right) => {
    return right.First - left.First;
});

foo.Add(new Pair(10, 3));
foo.Add(new Pair(4, 6));
foo.Add(new Pair(6, 15));
foo.Add(new Pair(6, 13)); // This shouldn't cause a problem

Pair current = foo.Shift(); // current = (4, 6)

最佳答案

我引用:

I need this list sorted all the time. I'm only ever interested in the first value in the list as defined by the sort algorithm at any one time.

这听起来像是您想要一个排序队列而是一个Priority Queue .如果性能是一个问题,那么 PQ 肯定会更快,O(log n) vs O(n)。但是删除重复项的问题需要您同时保留一个并行的 HashSet<>。

关于c# - .NET 中是否存在排序队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1447832/

相关文章:

c# - 接口(interface)扩展 - 基类或扩展方法

c# - WP7邮件客户端是否使用ActiveSync

java - 如何在java中对另一个 map 中的一组 map 进行排序?

c# - 如何解决错误CS1525无效表达词 '='?

c# - Ninject OnePerRequestBehaviour 似乎无法正常工作?

c# - 使用 mathdotnet 的 Matlab 等效函数

java - 空列表解码 Java 对象

Java 集合 : Is Object Added Directly Added To Collection?

Java集合列表: Converting from List '<Column <String1, String2>>' to 'List <String1>'

java - 如何在 ArrayBlockingQueue 中使用谓词来使用方法 "removeIf"