Esempio n. 1
0
        /// <summary>
        /// Merges two priority queues and sets specified comparer for resultant priority queue
        /// </summary>
        /// <param name="pq1">first priority queue</param>
        /// <param name="pq2">second priority queue</param>
        /// <param name="comparer">comparer for resultant priority queue</param>
        /// <returns>resultant priority queue</returns>
        public static PriorityQueue <TPriority, TValue> MergeQueues(PriorityQueue <TPriority, TValue> pq1, PriorityQueue <TPriority, TValue> pq2, IComparer <TPriority> comparer)
        {
            if (pq1 == null || pq2 == null || comparer == null)
            {
                throw new ArgumentNullException();
            }
            // merge data
            PriorityQueue <TPriority, TValue> result = new PriorityQueue <TPriority, TValue>(pq1.Count + pq2.Count, pq1._comparer);

            result._baseHeap.AddRange(pq1._baseHeap);
            result._baseHeap.AddRange(pq2._baseHeap);
            // heapify data
            for (int pos = result._baseHeap.Count / 2 - 1; pos >= 0; pos--)
            {
                result.HeapifyFromBeginningToEnd(pos);
            }

            return(result);
        }