There are other, and in my opinion better ways, of modeling your required data structure and required sort behavior than the Dictionary examples shown here.
The primary reason I would not use a Dictionary is that there is no sorting functionality. While you could use a SortedList, which is a list of KeyValuePairs, the sorting that will be performed automatically (or by using the custom IComparer implementation you provide), will sort on the Keys only: I doubt you want to have Keys be the arrival time !
Well, yes, you could write your own custom sort routine to re-arrange the indexes of all the KeyValuePairs in a Dictionary, perhaps sub-classing Dictionary (?): and good luck with that :)
Since I am currently studying the use of Tuples[
^] in .NET 4.0, your scenario seemed like an interesting one to try to solve using Tuples: so, I wrote an experimental solution using Tuples that I'll share with you: but be aware:
-2. I was made aware by a CP member of a good article on Tuples in .NET 4: "C# 4 - Tuples" by fmsalmeida"[
^].
-1. this is experimental code: there are a lot of opinions that using Tuples in the way shown here is not a good idea: just Google on: "Mutable Tuple"[
^]
0. I've implemented this as a static class, another reason I call this code "experimental." There was no particular reason I did that, it just seemed somehow appropriate in the light of the fact that Tuple is, inherently, a static object, a kind of "factory class" (which is why we have to use Tuple.Create).
1. the hack that I do here to get around the fact that Tuple items are immutable (read-only): is not pretty ! adding to the complexity here is that I am using a nested Tuple: it is a Tuple with two Items: the first Item is a DateTime (arrivalTime); the second Item is a Tuple with two Items: its first Item is a string (processName); its second Item is a TimeSpan (burstTime).
2. Tuples in a generic List, with a DateTime as the first Item, can be sorted by the DateTime by the generic List basic Sort method.
The code: this assumes "bursttime" is a TimeSpan
public static class PCB
{
public static Tuple<DateTime, Tuple<string, TimeSpan>> CurrentProcess;
public static List<Tuple<DateTime, Tuple<string, TimeSpan>>> ProcessList = new List<Tuple<DateTime, Tuple<string, TimeSpan>>>();
static PCB()
{
}
public static void add(DateTime arrivalTime, string procName, TimeSpan burstTime)
{
CurrentProcess = Tuple.Create(arrivalTime, Tuple.Create(procName, burstTime));
ProcessList.Add(CurrentProcess);
}
public static void SortTupleList()
{
if (ProcessList.Count >= 2) ProcessList.Sort();
}
public static void ChangeBurstTime(string procName, TimeSpan timeToAdd)
{
Tuple<DateTime, Tuple<string, TimeSpan>> targetInstance = ProcessList.Find
(
delegate(Tuple<DateTime, Tuple<string, TimeSpan>> pcbMatch)
{
return pcbMatch.Item2.Item1 == procName;
}
);
if (targetInstance != null)
{
int ndx = ProcessList.IndexOf(targetInstance);
targetInstance = Tuple.Create(targetInstance.Item1, Tuple.Create(targetInstance.Item2.Item1, targetInstance.Item2.Item2.Add(timeToAdd)));
ProcessList.RemoveAt(ndx);
ProcessList.Insert(ndx, targetInstance);
}
}
}
Using the code:
PCB.add(DateTime.Now.AddHours(2.3), "arrival two", TimeSpan.FromHours(3.4));
PCB.add(DateTime.Now.AddHours(1), "arrival one", TimeSpan.FromHours(0.0));
PCB.SortTupleList();
PCB.ChangeBurstTime("arrival two", TimeSpan.FromHours(4));
Once you've built the List of Tuples, constructing lists sorted on various criteria is relatively easy: suppose you want a List of all Processes sorted by bursttime that includes processname:
List<Tuple<TimeSpan, string>> SortedBurstTimeArrivalList =
(
from tpl in PCB.ProcessList
select
new Tuple<TimeSpan, string>(tpl.Item2.Item2, tpl.Item2.Item1)
).ToList();
SortedBurstTimeArrivalList.Sort();
Suggest you try this out, and set breakpoints on each call into the static class PCB, and then examine the internal contents of 'ProcessList: verify that its contents match what you expect to happen every step of the way, as entries are added, as they are sorted, and, finally, as the "bursttime" of one "entry" is modified.
Or, add some code like this, and examine the output:
foreach(var process in PCB.ProcessList){ Console.WriteLine(process);}
Last, I hope someone will come along and show a (much simpler) solution here that uses a custom collection class, and does some real magic with Linq !