예제 #1
0
        /// <summary>
        /// Union two heaps together, returns a new min-heap of both heaps' elements,
        /// ... and then destroys the original ones.
        /// </summary>
        public BinaryMinHeap <T> Union(ref BinaryMinHeap <T> firstMinHeap, ref BinaryMinHeap <T> secondMinHeap)
        {
            if (firstMinHeap == null || secondMinHeap == null)
            {
                throw new ArgumentNullException("Null heaps are not allowed.");
            }

            // Create a new heap with reserved size.
            int size    = firstMinHeap.Count + secondMinHeap.Count;
            var newHeap = new BinaryMinHeap <T>(size, Comparer <T> .Default);

            // Insert into the new heap.
            while (firstMinHeap.IsEmpty == false)
            {
                newHeap.Add(firstMinHeap.ExtractMin());
            }

            while (secondMinHeap.IsEmpty == false)
            {
                newHeap.Add(secondMinHeap.ExtractMin());
            }

            // Destroy the two heaps.
            firstMinHeap = secondMinHeap = null;

            return(newHeap);
        }
예제 #2
0
        public MinPriorityQueue(uint capacity, Comparer <PriorityQueueNode <TKey, TPriority> > priorityComparer)
        {
            // Make sure the TPriority is elegible for a priority
            if (!_validPriorityType())
            {
                throw new NotSupportedException("The priority type is not supported.");
            }

            // Initialize comparer
            if (priorityComparer == null)
            {
                _priorityComparer = Comparer <PriorityQueueNode <TKey, TPriority> > .Default;
            }
            else
            {
                _priorityComparer = priorityComparer;
            }

            // Initialize.
            _keys = new Dictionary <TKey, long>();
            _heap = new BinaryMinHeap <PriorityQueueNode <TKey, TPriority> >((int)capacity, this._priorityComparer);
        }