private TItem[] ToArray() { var cloneHeap = new MaxDHeap <MaxPriorityQueueNode>(2, _maxHeap.Items, _comparer); var result = new TItem[cloneHeap.Count]; for (int i = 0; i < result.Length; i++) { result[i] = cloneHeap.RemoveRoot().Data; } return(result); }
/// <summary> /// Initialises a new instance of <see cref="SynchronisedMaxPriorityQueue{TItem, TPriority}"/> with specified /// queue size and specified priority comparer. /// </summary> /// <param name="priorityComparer">The <see cref="IComparer{T}"/> to use when comparing 2 priorities.</param> /// <param name="initialQueueSize">The initial size of the queue.</param> /// <exception cref="ArgumentNullException"><paramref name="priorityComparer"/> was null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="initialQueueSize"/> was less than 1.</exception> public SynchronisedMaxPriorityQueue(IComparer <TPriority> priorityComparer, int initialQueueSize) { if (priorityComparer == null) { throw new ArgumentNullException(nameof(priorityComparer), $"Unable to initialise Maximum Priority-Queue. {nameof(priorityComparer)} cannot be null."); } if (initialQueueSize <= 0) { throw new ArgumentOutOfRangeException(nameof(initialQueueSize), $"Unable to initialise Maximum Priority-Queue. {nameof(initialQueueSize)} must be greater than zero."); } _comparer = new SynchronisedMaxPriorityQueueNodeComparer(priorityComparer); _maxHeap = new MaxDHeap <SynchronisedMaxPriorityQueueNode>(2, initialQueueSize, _comparer); }