Beispiel #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);
        }
Beispiel #2
0
 /// <summary>
 /// Merges two priority queues
 /// </summary>
 /// <param name="pq1">first priority queue</param>
 /// <param name="pq2">second priority queue</param>
 /// <returns>resultant priority queue</returns>
 /// <remarks>
 /// source priority queues must have equal comparers,
 /// otherwise <see cref="InvalidOperationException"/> will be thrown
 /// </remarks>
 public static PriorityQueue <TPriority, TValue> MergeQueues(PriorityQueue <TPriority, TValue> pq1, PriorityQueue <TPriority, TValue> pq2)
 {
     if (pq1 == null || pq2 == null)
     {
         throw new ArgumentNullException();
     }
     if (pq1._comparer != pq2._comparer)
     {
         throw new InvalidOperationException("Priority queues to be merged must have equal comparers");
     }
     return(MergeQueues(pq1, pq2, pq1._comparer));
 }