Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a sorted queue exist in .NET?

Tags:

c#

collections

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)
like image 557
Matthew Scharley Avatar asked Sep 19 '09 05:09

Matthew Scharley


People also ask

Is there Priorityqueue in C#?

Priority Queue in C# is a very useful data structure that has so many real-world applications. In this article, we are going to cover the main concepts of Priority Queue in C#.

Is HashSet sorted C#?

A HashSet<T> collection is not sorted and cannot contain duplicate elements.

What is sorted list in C#?

In C#, SortedList is a collection of key/value pairs which are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic type of collection. The generic SortedList is defined in System.

Why there is no priority queue in C#?

The . NET team did not see value in writing such a class, hence why it doesn't exist. They've got limited time, and budget, for each release cycle that they have. Obviously, priority-based queue and stacks were not on the list of items they felt were necessary for them to supply.


4 Answers

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.

like image 142
Henk Holterman Avatar answered Sep 30 '22 15:09

Henk Holterman


Have you looked at SortedDictionary or SortedList?

like image 29
Vadim Avatar answered Sep 30 '22 14:09

Vadim


Check out the C5 Generic Collections Library which already has an implementation just like you're looking for, called an IntervalHeap or Priority Queue. Here's some documentation on it from the C5 Book: IPriorityQueue<T>

like image 28
Marcus Griep Avatar answered Sep 30 '22 15:09

Marcus Griep


There is nothing built into .NET at this time that meets all of your requirements. In .NET 4.0 there is a SortedSet class. I realize that it probably does not do you much good now.

SortedList comes close if you implement IComparable. You would just use the Pair as the key and the value. You would only be storing the reference to your Pair twice so it isn't a huge memory overhead. But this will not take care of duplicates.

There are numerous ways to write it yourself, but nothing built in that fully matches what you need. There are a few open source SortedSet implementations (there is one in Spring.Net for example). That might be your best bet right now.

like image 20
Mike Two Avatar answered Sep 30 '22 16:09

Mike Two