做了排序队列在.NET存在吗? [英] Does a sorted queue exist in .NET?

查看:110
本文介绍了做了排序队列在.NET存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个相当专门收集.NET,我不认为BCL可以帮助我,但我想我把它扔出去那里是否有人知道的类似的东西。



基本上,我的要求是这样的:




  • 我对值的列表,例如为:(3,10),(5,10),(3,7),(5,5)

  • 顺序很重要,即(3,10)!=(10,3)

  • 个人价值的副本都很好,但重复的双应被丢弃(最好默默)。

  • 起脚的是,我需要这个名单排序的所有的时间。我只对在列表中的第一个值有史以来感兴趣由排序算法在任何一个时间限定。



所以,有些什么,我希望能够做到(我设想它可能会被执行,其他适合以上的罚款来实现)示例代码:

 公共类对
{
公众对(诠释第一,诠释秒)
{首先=第一;其次=秒; }
公众诠释一是{搞定;组; }
公众诠释二{搞定;组; }
}

SortedQueue<对GT&;富=新SortedQueue<对GT&((左,右)= GT; {
返回right.First - left.First;
});

foo.Add(新对(10,3));
foo.Add(新对(4,6));
foo.Add(新对(6,15));
foo.Add(新对(6,13)); //这不会导致出现问题

对电流= foo.Shift(); //电流=(4,6)


解决方案

我引述




我需要这个名单排序的所有的时间。
我只对在列表中的第
值为曾经有意通过在任一时刻的
排序算法定义




这听起来像你这样的不可以要排序的队列中,但一个优先级队列。如果性能是一个问题,那么一个PQ肯定会更快,O(log n)的VS为O(n)。但是,下拉式的重复问题将要求你保持一个并行的HashSet<>以及


I have a need for a fairly specialised collection .NET, and I don't think that the BCL can help me, but I thought I'd throw it out there for if anyone knew of something similar.

Basically, my requirements are thus:

  • I have a list of pairs of values, such as: (3, 10), (5, 10), (3, 7), (5, 5)
  • Order is important, ie. (3, 10) != (10, 3)
  • Duplicates of individual values are fine, but duplicate pairs should be dropped (preferably silently).
  • The kicker is, 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.

So, some example code of what I want to be able to do (as I envision it would probably be implemented, other implementations that fit the above are fine to):

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 quote:

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.

This sounds like you do not want a Sorted Queue but a Priority Queue. If performance is an issue then a PQ will certainly be faster, O(log n) vs O(n). But the drop-duplicates issue would require you to keep a parallel HashSet<> as well.

这篇关于做了排序队列在.NET存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆