示例#1
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 PriorityQueueBase <TPriority, TValue> MergeQueues(PriorityQueueBase <TPriority, TValue> pq1, PriorityQueueBase <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));
 }
示例#2
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 PriorityQueueBase <TPriority, TValue> MergeQueues(PriorityQueueBase <TPriority, TValue> pq1, PriorityQueueBase <TPriority, TValue> pq2, IComparer <TPriority> comparer)
        {
            if (pq1 == null || pq2 == null || comparer == null)
            {
                throw new ArgumentNullException();
            }
            // merge data
            PriorityQueueBase <TPriority, TValue> result = new PriorityQueueBase <TPriority, TValue>(pq1.Count + pq2.Count, pq1.comparer);

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

            return(result);
        }
示例#3
0
 public PriorityQueueS1(IComparer <ElementType> comparer)
 {
     inner = new PriorityQueueBase <ElementType, ElementType>(0, comparer);
 }