public void MinLimitHeap_Works_Correctly(int heapSize, int itemCount) { var items = new string[itemCount]; for (var i = 0; i < itemCount; i++) { items[i] = Guid.NewGuid().ToString("N"); } var heap = new MinLimitHeap <string>(heapSize); Assert.AreEqual(heap.AddAll(items), itemCount); Array.Sort(items); var minItems = new HashSet <string>(); var itemCounter = 0; for (var i = 0; i < Math.Min(heapSize, itemCount); i++) { minItems.Add(items[itemCounter++]); } itemCounter = 0; foreach (var current in heap.PopAll()) { Assert.IsTrue(minItems.Contains(current)); itemCounter++; } Assert.AreEqual(itemCounter, Math.Min(heapSize, itemCount)); }
/// <summary> /// Enumerates over given <paramref name="itemCollection"/> and finds the /// minimums (up to <paramref name="totalMinimums"/>) items using <paramref name="comparer"/>. /// Returns another enumerable based of required <paramref name="sorting"/>. /// </summary> /// <typeparam name="T">Type of the items in the input collection</typeparam> /// <param name="itemCollection">Input collection</param> /// <param name="totalMinimums">Count of total number of minimum to find. /// Error is thrown if value is negative or zero (0).</param> /// <param name="comparer">Comparer instance. If not provided, then <seealso cref="Comparer{T}.Default"/> will be used.</param> /// <param name="sorting">Output sorting (it uses <paramref name="comparer"/>). As the interest is only to find all the required /// number of minimums. It is best to use <seealso cref="SortOrder.None"/> as sorting value /// otherwise, an additional latency might be observed.</param> public static IEnumerable <T> FindMinimums <T>(this IEnumerable <T> itemCollection, int totalMinimums, IComparer <T> comparer = null, SortOrder sorting = SortOrder.None) { var heap = new MinLimitHeap <T>(totalMinimums, comparer); heap.AddAll(itemCollection); return(heap.GetSorted(comparer, sorting, false)); }
public void MinLimitHeap_Ctor_Throws_Error_For_Invalid_Argument(int size) { var ex = Assert.Throws <DdnDfException>(() => { var _ = new MinLimitHeap <int>(size); }); Assert.NotNull(ex); Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); }