#region CPL License /* Nuclex Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Generic; using System.Collections; namespace Nuclex.Support.Collections { /// Queue that dequeues items in order of their priority /// /// This variant of the priority queue uses an external priority value. If the /// priority data type implements the IComparable interface, the user does not /// even /// public class PairPriorityQueue : ICollection, IEnumerable> { #region class PairComparer /// Compares two priority queue entries based on their priority private class PairComparer : IComparer> { /// Initializes a new entry comparer /// Comparer used to compare entry priorities public PairComparer(IComparer priorityComparer) { this.priorityComparer = priorityComparer; } /// Compares the left entry to the right entry /// Entry on the left side /// Entry on the right side /// The relationship of the two entries public int Compare( PriorityItemPair left, PriorityItemPair right ) { return this.priorityComparer.Compare(left.Priority, right.Priority); } /// Comparer used to compare the priorities of the entries private IComparer priorityComparer; } #endregion // class EntryComparer /// Initializes a new non-intrusive priority queue public PairPriorityQueue() : this(Comparer.Default) { } /// Initializes a new non-intrusive priority queue /// Comparer used to compare the item priorities public PairPriorityQueue(IComparer priorityComparer) { this.internalQueue = new PriorityQueue>( new PairComparer(priorityComparer) ); } /// Returns the topmost item in the queue without dequeueing it /// The topmost item in the queue public PriorityItemPair Peek() { return this.internalQueue.Peek(); } /// Takes the item with the highest priority off from the queue /// The item with the highest priority in the list public PriorityItemPair Dequeue() { return this.internalQueue.Dequeue(); } /// Puts an item into the priority queue /// Priority of the item to be queued /// Item to be queued public void Enqueue(TPriority priority, TItem item) { this.internalQueue.Enqueue( new PriorityItemPair(priority, item) ); } /// Removes all items from the priority queue public void Clear() { this.internalQueue.Clear(); } /// Total number of items in the priority queue public int Count { get { return this.internalQueue.Count; } } /// Copies the contents of the priority queue into an array /// Array to copy the priority queue into /// Starting index for the destination array public void CopyTo(Array array, int index) { this.internalQueue.CopyTo(array, index); } /// /// Obtains an object that can be used to synchronize accesses to the priority queue /// from different threads /// public object SyncRoot { get { return this.internalQueue.SyncRoot; } } /// Whether operations performed on this priority queue are thread safe public bool IsSynchronized { get { return this.internalQueue.IsSynchronized; } } /// Returns a typesafe enumerator for the priority queue /// A new enumerator for the priority queue public IEnumerator> GetEnumerator() { return this.internalQueue.GetEnumerator(); } /// Returns an enumerator for the priority queue /// A new enumerator for the priority queue IEnumerator IEnumerable.GetEnumerator() { return this.internalQueue.GetEnumerator(); } /// Intrusive priority queue being wrapped by this class private PriorityQueue> internalQueue; } } // namespace Nuclex.Support.Collections