When performance is not the main priority I always love to use Linq. I would define a generic class that contains three members. Your generic object, the priority a and the priority b.
Then you make a list of this class and get the elements with the highest priority a or b via Linq.
I could post a quick and dirty solution, but i don't want to spoil to much if you want to figure it our yourself.
It can be very deceptive. Like most powerful tools it offers a lot but to get there it also does a lot. If you don't pay attention you can start performing a lot of expensive operations quickly.
I'd start with some simple brute forcing. Definitely I would combine the triad of Item, PriorityA, and PriorityB together into a single class. Then start by wrapping a very basic collection with poor performance. Build out the public API and make some very basic unit tests (and probably a test that checks performance).
So, with that in mind, here's a very basic, very brute force approach. Basically just wraps a List<PrioritizedItem<T>> and whenever you dequeue, it does a quick ordering, finds the highest priority, then removes that item and returns it.
public class PriorityQueue<T>
{
private List<PrioritizedItem<T>> Items = new List<PrioritizedItem<T>>();
public void Enqueue(T item, double priorityA, double priorityB)
{
Items.Add(new PrioritizedItem<T>(item, priorityA, priorityB));
}
public T DequeueA()
{
var prioritizedItem = Items.OrderByDescending(i => i.PriorityA).First();
Items.Remove(prioritizedItem);
return prioritizedItem.Item;
}
public T DequeueB()
{
var prioritizedItem = Items.OrderByDescending(i => i.PriorityB).First();
Items.Remove(prioritizedItem);
return prioritizedItem.Item;
}
}
internal class PrioritizedItem<T>
{
public T Item { get; private set; }
public double PriorityA { get; private set; }
public double PriorityB { get; private set; }
public PrioritizedItem(T item, double priorityA, double priorityB)
{
this.Item = item;
this.PriorityA = priorityA;
this.PriorityB = priorityB;
}
}
Some basic usage code:
var priorityQueue = new PriorityQueue<string>();
priorityQueue.Enqueue("asdf", 10, 2);
priorityQueue.Enqueue("qwerty", 1, 5);
Console.WriteLine(priorityQueue.DequeueB()); //outputs "qwerty"
From here you can implement the Count and Clear methods easily. Once you have the basic tests, you can then start dinking around with the underlying List<PrioritizedItem<T>> Items collection with various pre-sorted collections to try and optimize performance. Hopefully this will give you a starting point.
EDIT: Then of course, you can attempt the extension. This being C#, you can also remove the hard-coded double values for priority and instead use some kind of IComparable interface instead. Then you can use other types to dictate priority.
Yeah thats almost word by word the solution I have, but I think the learining effect for OP would have been bigger if you don't just post an almost complete solution.
Firstly you only need one generic parameter instead of three, because the priorities will always be double or something like that.
Secondly i dont know what problems you've been experiencing, but whatever you do you most certainly need to initialize your List with the "new"-Keyword. In your example you only declared a variable "_list" but it refers to no actual List.
You also don't need a "_size" field, because pretty much all functionality you need is already implemented in "List" or possible via Linq.
4
u/[deleted] Feb 14 '15
Here's my solution:
1
u/cactus_bodyslam Feb 13 '15
When performance is not the main priority I always love to use Linq. I would define a generic class that contains three members. Your generic object, the priority a and the priority b.
Then you make a list of this class and get the elements with the highest priority a or b via Linq.
I could post a quick and dirty solution, but i don't want to spoil to much if you want to figure it our yourself.
1
u/FizixMan Feb 13 '15
I'd start with some simple brute forcing. Definitely I would combine the triad of
Item,PriorityA, andPriorityBtogether into a single class. Then start by wrapping a very basic collection with poor performance. Build out the public API and make some very basic unit tests (and probably a test that checks performance).So, with that in mind, here's a very basic, very brute force approach. Basically just wraps a
List<PrioritizedItem<T>>and whenever you dequeue, it does a quick ordering, finds the highest priority, then removes that item and returns it.Some basic usage code:
From here you can implement the
CountandClearmethods easily. Once you have the basic tests, you can then start dinking around with the underlyingList<PrioritizedItem<T>> Itemscollection with various pre-sorted collections to try and optimize performance. Hopefully this will give you a starting point.EDIT: Then of course, you can attempt the extension. This being C#, you can also remove the hard-coded
doublevalues for priority and instead use some kind ofIComparableinterface instead. Then you can use other types to dictate priority.1
u/circly Feb 13 '15
SortedDictionary might help.