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.
Assuming I’m looping ten times, each time printing the value from 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 low.
Any ideas?
the simplest approach is to have 3 collections behind one interface. When calling
MoveNextcheck 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.
Urgenthas5/10 = 0.5Mediumhas0.3Lowhas0.2At each turn gerate random number in range
[0; 1]. if value falls into[0; 0,5]then pick fromUrgentqueue, if into[0,5; 0,8]thenMedium,[0,8; 1]->Low;