public void UnitRdx_RemoveRange() { var rd = new RankedDictionary <int, int> { Capacity = 7 }; for (int ii = 0; ii < 20; ++ii) { rd.Add(ii, -ii); } rd.RemoveRange(20, 0); Assert.AreEqual(20, rd.Count); rd.RemoveRange(12, 4); Assert.AreEqual(16, rd.Count); #if DEBUG rd.SanityCheck(); #endif }
static void Main() { int reps = 5000000; Console.WriteLine("LINQ's Last extension method will enumerate over the *entire* collection giving"); Console.WriteLine("a time complexity of O(n) regardless of the data structure. This is due to the"); Console.WriteLine("\"one size fits all\" approach of LINQ. SortedDictionary supplies no optimized"); Console.WriteLine("implementation of Last. Also, SortedDictionary does not supply any optimized"); Console.WriteLine("way of performing queries based on a key range. Again, the time complexity of"); Console.WriteLine("such an operation is O(n).\n"); var sd = new SortedDictionary <int, int>(); Console.Write("Loading SortedDictionary with " + reps + " elements:\n\nLoad time = "); Stopwatch watch1 = new Stopwatch(); watch1.Reset(); watch1.Start(); for (int i = 0; i < reps; ++i) { sd.Add(i, -i); } var time11 = watch1.ElapsedMilliseconds; var last1 = sd.Last(); var time12 = watch1.ElapsedMilliseconds; Console.WriteLine(time11 + "ms"); Console.WriteLine("Last time = " + (time12 - time11) + "ms"); //// Console.WriteLine("\nRankedDictionary has its own implementation of Last() which does not suffer the"); Console.WriteLine("performance hit that SortedDictionary.Last() does. RankedDictionary also"); Console.WriteLine("supports optimized range queries with its ElementsFrom(TKey) and"); Console.WriteLine("ElementsBetween(TKey,TKey) enumerators.\n"); var bt = new RankedDictionary <int, int>(); Console.Write("Loading RankedDictionary with " + reps + " elements:\n\nLoad time = "); Stopwatch watch2 = new Stopwatch(); watch2.Reset(); watch2.Start(); for (int i = 0; i < reps; ++i) { bt.Add(i, -i); } var time21 = watch2.ElapsedMilliseconds; var lastKV = bt.Last(); var time22 = watch2.ElapsedMilliseconds; // Range query: Sum the middle 100 values. var rangeVals = bt.ElementsBetween(reps / 2 - 50, reps / 2 + 50).Sum(x => x.Value); var time23 = watch2.ElapsedMilliseconds; Console.WriteLine(time21 + "ms"); Console.WriteLine("Last time = " + (time22 - time21) + "ms"); Console.WriteLine("Range time = " + (time23 - time22) + "ms"); #if DEBUG bt.SanityCheck(); Console.WriteLine(); Console.Write("---- height = " + bt.GetHeight()); Console.Write(", branch fill = " + bt.BranchSlotsUsed * 100 / bt.BranchSlotCount + "%"); Console.WriteLine(", leaf fill = " + bt.LeafSlotsUsed * 100 / bt.LeafSlotCount + "% ----"); #endif }
static void Main() { var dary = new RankedDictionary <int, int>() { Capacity = 4 }; for (int w = 1; w < 21; ++w) { foreach (Permutation permAdd in new Permutation(w).GetRows()) { foreach (Permutation permDel in permAdd.GetRows()) { for (int m = 0; m < permAdd.Choices; ++m) { dary.Add(permAdd[m], permAdd[m] + 100); #if DEBUG if (permDel.Rank == 0) { try { dary.SanityCheck(); } catch (DataMisalignedException ex) { Console.WriteLine("Insanity found: {0}", ex.Message); Console.WriteLine("Width={0} add.Rank={1} m={2}", w, permAdd.Rank, m); throw; } } #endif } for (int m = 0; m < permDel.Choices; ++m) { dary.Remove(permDel[m]); #if DEBUG try { dary.SanityCheck(); } catch (DataMisalignedException ex) { Console.WriteLine("Insanity found: {0}", ex.Message); Console.WriteLine("Width={0} add.Rank={1} del.Rank={2} m={3}", w, permAdd.Rank, permDel.Rank, m); throw; } #endif } if (dary.Count != 0) { throw new DataMisalignedException("Count should be zero"); } dary.Clear(); } } Console.WriteLine("{2} - Completed Width {0} = {1}", w, new Permutation(w), DateTime.Now); Thread.Sleep(250); } }