Priority Queue in .Net

Possible Duplicate:
Priority Queue in .Net

This question is similar, but I want to know for sure:

Is there any class / struct / ... in .Net for the priority queue? Like in STL, for this priority_queue. It supports comparsion function to support custom sorts.

The best I've found on .Net is a SortedList <Key, Value> , which sorts values ​​by key. Thus, one solution is to implement the Compare user interface for the Key class. But I can’t split my elements into key / value pairs. I have atomic elements that should be queued according to their values ​​using a special function.

So, is there any collection class in .Net that accepts a Compare function to sort its elements?

Is there a way to get a .Net class (possibly a HashSet ) that supports this feature?


Note:

  • I know that many proponents have implemented really good classes for this. Perhaps a good example is PowerCollections . But I want a quick and easy solution using existing classes in .Net
  • I am using .Net Framework 3.5 and as C #;)
+3
source share
2 answers

You can use the SortedDictionary class , which is shared.

You can specify a mapping object with a constructor that should handle the priority comparison of your objects:

public class DataComparer : IComparer<Data>
{
    public Int32 Compare(Data a, Data b)
    {
        if (a == null && b == null)
            return 0;
        if (a == null)
            return -1;
        if (b == null)
            return +1;
        return a.Priority.CompareTo(b.Priority);
    }
}

SortedDictionary<Data, Data> priQueue = new SortedDictionary<Data, Data>(
    new DataComparer());
+3
source

IComparable , IList.Sort()?

+1

Source: https://habr.com/ru/post/1721071/


All Articles