I need to implement a prioritised collection.
Assuming we have the following three values in the collection (and their priorities):
thisIsUrgent = Priority.High
thisIsImportant = Priority.Medium
thisIsBoring = Priority.Low
I want to use MoveNext()
to go through the collection to get another value.
MoveNext()
, the desired output is:
thisIsUrgent
thisIsImportant
thisIsUrgent
thisIsUrgent
thisIsBoring
thisIsImportant
thisIsUrgent
thisIsUrgent
thisIsImportant
thisIsBoring
So basically, I get five high priority values, three normal and one开发者_如何学Python low.
Any ideas?
the simplest approach is to have 3 collections behind one interface. When calling MoveNext
check the one with highest priority, if there are messges return them until queue gets empty. Then lower and lower. Then you can improve algorithm of picking next queue, for example implement probabilistic one.
In your particular case you should use probabilistic scheduling.
Urgent
has5/10 = 0.5
Medium
has0.3
Low
has0.2
At each turn gerate random number in range [0; 1]
. if value falls into [0; 0,5]
then pick from Urgent
queue, if into [0,5; 0,8]
then Medium
, [0,8; 1]
-> Low
;
What you need is:
http://en.wikipedia.org/wiki/Priority_queue
Use 3 collections, one for each priority as Andrey says.
Then, when you want to get the next task, pick a random number between 1 and 9.
Retrieve the next task from the relevant collection as follows:
1 to 5: High priority
6 to 8: Normal priority
9 : Low priority
Perhaps you can clarify your question a little better; it's not immediately obvious why you need your output to be in that particular order. Is the data in one collection, or are there multiple collections?
But if you're looking for a data structure that implements priorities, I suggest going with the tried-and-true Priority Queue.
精彩评论