2007-09-05 18:20:10 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2014-07-19 09:13:36 +00:00
|
|
|
Copyright (C) 2002-2014 Nuclex Development Labs
|
2007-09-05 18:20:10 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/// <summary>Queue that dequeues items in order of their priority</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// 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
|
|
|
|
/// </remarks>
|
2012-03-08 11:05:20 +00:00
|
|
|
public class PairPriorityQueue<TPriority, TItem> :
|
|
|
|
ICollection, IEnumerable<PriorityItemPair<TPriority, TItem>> {
|
|
|
|
|
2007-09-05 18:20:10 +00:00
|
|
|
#region class PairComparer
|
|
|
|
|
|
|
|
/// <summary>Compares two priority queue entries based on their priority</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
private class PairComparer : IComparer<PriorityItemPair<TPriority, TItem>> {
|
2007-09-05 18:20:10 +00:00
|
|
|
|
|
|
|
/// <summary>Initializes a new entry comparer</summary>
|
|
|
|
/// <param name="priorityComparer">Comparer used to compare entry priorities</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
public PairComparer(IComparer<TPriority> priorityComparer) {
|
2007-09-05 18:20:10 +00:00
|
|
|
this.priorityComparer = priorityComparer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Compares the left entry to the right entry</summary>
|
|
|
|
/// <param name="left">Entry on the left side</param>
|
|
|
|
/// <param name="right">Entry on the right side</param>
|
|
|
|
/// <returns>The relationship of the two entries</returns>
|
|
|
|
public int Compare(
|
2012-02-29 16:27:43 +00:00
|
|
|
PriorityItemPair<TPriority, TItem> left,
|
|
|
|
PriorityItemPair<TPriority, TItem> right
|
2007-09-05 18:20:10 +00:00
|
|
|
) {
|
|
|
|
return this.priorityComparer.Compare(left.Priority, right.Priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Comparer used to compare the priorities of the entries</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
private IComparer<TPriority> priorityComparer;
|
2007-09-05 18:20:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion // class EntryComparer
|
|
|
|
|
|
|
|
/// <summary>Initializes a new non-intrusive priority queue</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
public PairPriorityQueue() : this(Comparer<TPriority>.Default) { }
|
2007-09-05 18:20:10 +00:00
|
|
|
|
|
|
|
/// <summary>Initializes a new non-intrusive priority queue</summary>
|
|
|
|
/// <param name="priorityComparer">Comparer used to compare the item priorities</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
public PairPriorityQueue(IComparer<TPriority> priorityComparer) {
|
|
|
|
this.internalQueue = new PriorityQueue<PriorityItemPair<TPriority, TItem>>(
|
2007-09-05 18:20:10 +00:00
|
|
|
new PairComparer(priorityComparer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-07-15 20:36:16 +00:00
|
|
|
/// <summary>Returns the topmost item in the queue without dequeueing it</summary>
|
|
|
|
/// <returns>The topmost item in the queue</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
public PriorityItemPair<TPriority, TItem> Peek() {
|
2008-07-15 20:36:16 +00:00
|
|
|
return this.internalQueue.Peek();
|
|
|
|
}
|
|
|
|
|
2007-09-05 18:20:10 +00:00
|
|
|
/// <summary>Takes the item with the highest priority off from the queue</summary>
|
|
|
|
/// <returns>The item with the highest priority in the list</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
public PriorityItemPair<TPriority, TItem> Dequeue() {
|
2007-09-05 18:20:10 +00:00
|
|
|
return this.internalQueue.Dequeue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Puts an item into the priority queue</summary>
|
|
|
|
/// <param name="priority">Priority of the item to be queued</param>
|
|
|
|
/// <param name="item">Item to be queued</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
public void Enqueue(TPriority priority, TItem item) {
|
2007-09-05 18:20:10 +00:00
|
|
|
this.internalQueue.Enqueue(
|
2012-02-29 16:27:43 +00:00
|
|
|
new PriorityItemPair<TPriority, TItem>(priority, item)
|
2007-09-05 18:20:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Removes all items from the priority queue</summary>
|
|
|
|
public void Clear() {
|
|
|
|
this.internalQueue.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Total number of items in the priority queue</summary>
|
|
|
|
public int Count {
|
|
|
|
get { return this.internalQueue.Count; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Copies the contents of the priority queue into an array</summary>
|
|
|
|
/// <param name="array">Array to copy the priority queue into</param>
|
|
|
|
/// <param name="index">Starting index for the destination array</param>
|
|
|
|
public void CopyTo(Array array, int index) {
|
|
|
|
this.internalQueue.CopyTo(array, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Obtains an object that can be used to synchronize accesses to the priority queue
|
|
|
|
/// from different threads
|
|
|
|
/// </summary>
|
|
|
|
public object SyncRoot {
|
|
|
|
get { return this.internalQueue.SyncRoot; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Whether operations performed on this priority queue are thread safe</summary>
|
|
|
|
public bool IsSynchronized {
|
|
|
|
get { return this.internalQueue.IsSynchronized; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Returns a typesafe enumerator for the priority queue</summary>
|
|
|
|
/// <returns>A new enumerator for the priority queue</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
public IEnumerator<PriorityItemPair<TPriority, TItem>> GetEnumerator() {
|
2007-09-05 18:20:10 +00:00
|
|
|
return this.internalQueue.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Returns an enumerator for the priority queue</summary>
|
|
|
|
/// <returns>A new enumerator for the priority queue</returns>
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
|
|
return this.internalQueue.GetEnumerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Intrusive priority queue being wrapped by this class</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
private PriorityQueue<PriorityItemPair<TPriority, TItem>> internalQueue;
|
2007-09-05 18:20:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|