/// <summary> /// Prints the heap statistics. /// </summary> public void PrintStats() { Stats.PrintStats(); long totalAgesSum = 0; int oldestElement = 0; int numberOfElementsGreaterThanExtractionLimit = 0; while (InternalHeap.GetSize() > 0) { var element = InternalHeap.RemoveMin(); var age = TimeStamp - element.TimeStamp; if (oldestElement < age) { oldestElement = age; } totalAgesSum += age; if (element.Key > Stats.HighestExtractedElement) { numberOfElementsGreaterThanExtractionLimit++; } } totalAgesSum += Stats.ExtractedElementsAgeTotalSum; Console.WriteLine("\t\tAverage element's age (all):\t" + totalAgesSum / (Stats.ElementsAges.Count + Stats.CurrentHeapSize)); Console.WriteLine("\t\tOldest extracted:\t\t" + Stats.OldestExtractedElement); Console.WriteLine("\t\tOldest not extracted:\t\t" + oldestElement); Console.WriteLine("\t\tElements beyond extraction limit:\t" + numberOfElementsGreaterThanExtractionLimit); }
/// <summary> /// Gets the value item with the minimal key and deletes the element from the collection. /// </summary> /// <returns>Value item with the minimal key.</returns> public Value RemoveMin() { var result = InternalHeap.RemoveMin(); Logger?.WriteLine("r\t" + result.Key); Stats.UpdateAfterRemove(result); return(result.Value); }
/// <summary> /// Adds a new key-value pair into the collection. /// </summary> /// <param name="key">Key item.</param> /// <param name="value">Value item.</param> public void Add(int key, Value value) { var newElement = new HeapNode(key, value, TimeStamp++); InternalHeap.Add(newElement.Key, newElement); Logger?.WriteLine("i\t" + key); Stats.UpdateAfterInsert(newElement); }
/// <summary> /// Clears the collection. /// </summary> public void Clear() { InternalHeap.Clear(); Stats.Clear(); TimeStamp = 0; }
/// <summary> /// Gets the size of the collection, i.e. number of included elements. /// </summary> /// <returns>Collection size.</returns> public int GetSize() { return(InternalHeap.GetSize()); }
/// <summary> /// Gets the minimal key of the collection. /// </summary> /// <returns>Minimal key.</returns> public int GetMinKey() { return((int)InternalHeap.GetMinKey()); }