#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
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