public void GenerationalDictionary2() { GenerationalDictionary <string, string> gd = new GenerationalDictionary <string, string>(); gd["one"] = "g0-one"; gd["two"] = "g0-two"; gd.NextGeneration(); gd["two"] = "g1-two"; gd.NextGeneration(); gd["one"] = "g2-one"; gd.NextGeneration(); gd["three"] = "g3-three"; var onekey = gd.GetHistoryOfKey("one"); Check.That(onekey.Count).Equals(2); var twokey = gd.GetHistoryOfKey("two"); Check.That(twokey.Count).Equals(2); var oneg2 = gd.GetHistoryOfKey(2, "one"); Check.That(oneg2.Count).Equals(2); var oneg1 = gd.GetHistoryOfKey(1, "one"); Check.That(oneg1.Count).Equals(1); var threeg1 = gd.GetHistoryOfKey(1, "three"); Check.That(threeg1.Count).Equals(0); var threeg10 = gd.GetHistoryOfKey(10, "three"); Check.That(threeg10.Count).Equals(1); }
private FactionInfo Clone(string faction, bool incrgen = true) // clone both FactionInformation structure and a faction { if (faction.HasChars() && faction != "$faction_none;") { FactionInfo newfi = history.GetLast(faction); // get the last one, or null newfi = newfi != null ? new FactionInfo(newfi) : new FactionInfo(faction); // make a new copy, or an empty copy if (incrgen) { history.NextGeneration(); } history[faction] = newfi; // add this new one to the history list return(newfi); } else { return(null); } }
public uint Process(JournalEntry je, JournalEntry lastentry, bool insrv) { if (je is ICommodityJournalEntry || je is IMaterialJournalEntry || je is IMicroResourceJournalEntry) { items.NextGeneration(); // increment number, its cheap operation even if nothing gets changed //System.Diagnostics.Debug.WriteLine("***********************" + je.EventTimeUTC + " GENERATION " + items.Generation); if (je is ICommodityJournalEntry) { ICommodityJournalEntry e = je as ICommodityJournalEntry; e.UpdateCommodities(this, insrv); } if (je is IMaterialJournalEntry) { IMaterialJournalEntry e = je as IMaterialJournalEntry; e.UpdateMaterials(this); } if (je is IMicroResourceJournalEntry) { IMicroResourceJournalEntry e = je as IMicroResourceJournalEntry; e.UpdateMicroResource(this, lastentry); } if (items.UpdatesAtThisGeneration == 0) // if nothing changed, abandon it. { // System.Diagnostics.Debug.WriteLine("*********************** {0} {1} No changes for Generation {2} Abandon", je.EventTimeUTC.ToString(), je.EventTypeStr, items.Generation); items.AbandonGeneration(); } else { // System.Diagnostics.Debug.WriteLine("*********************** {0} {1} Generation {2} Changes {3}",je.EventTimeUTC.ToString(), je.EventTypeStr, items.Generation,items.UpdatesAtThisGeneration); } } return(items.Generation); // return the generation we are on. }
public void GenerationalDictionary() { GenerationalDictionary <int, uint> gd = new GenerationalDictionary <int, uint>(); Random rnd = new Random(1001); const int generations = 1004; const int depth = 10000; const int modulo = 2; int[] genskip = new int[depth]; for (int i = 0; i < depth; i++) { genskip[i] = rnd.Next(23) + modulo + 1; // need to be bigger than modulo } for (uint g = 0; g < generations; g++) { gd.NextGeneration(); for (int i = 0; i < depth; i++) { if (g % genskip[i] == modulo) { // System.Diagnostics.Debug.WriteLine("{0} Add {1}", (g+1), i); gd[i] = g; } } } Stopwatch sw = new Stopwatch(); sw.Start(); long time = sw.ElapsedMilliseconds; //File.WriteAllText(@"c:\code\time.txt", "Time taken " + time); for (uint g = 0; g < generations; g++) { var dict = gd.Get(g + 1); // System.Diagnostics.Debug.WriteLine("At gen {0} get {1} {2}", g + 1, dict.Count, string.Join(",",dict.Values)); for (int i = 0; i < depth; i++) { bool present = g % genskip[i] == modulo; if (present) { Check.That(dict[i]).Equals(g); } else if (g < modulo) { Check.That(dict.ContainsKey(i)).IsFalse(); } else { Check.That(dict[i]).IsNotEqualTo(g); } } //foreach( var kvp in dict) { System.Diagnostics.Debug.WriteLine("{0} {1}={2}", g+1, kvp.Key, kvp.Value); } //System.Diagnostics.Debug.WriteLine(""); } for (uint g = 0; g < generations; g++) { var values = gd.GetValues(g + 1); for (int i = 0; i < depth; i++) { bool present = g % genskip[i] == modulo; if (present) { Check.That(values[i]).Equals(g); } else if (g < modulo) { Check.That(values.Contains((uint)i)).IsFalse(); } else { Check.That(values[i]).IsNotEqualTo(g); } } //foreach( var kvp in dict) { System.Diagnostics.Debug.WriteLine("{0} {1}={2}", g+1, kvp.Key, kvp.Value); } //System.Diagnostics.Debug.WriteLine(""); } //1004x10000 = release 3035 }