Class PriorityQueue<T>
A PriorityQueue<T> holds elements on a priority heap, which orders the elements according to their natural order or according to the comparator specified at construction time. If the queue uses natural ordering, only elements that are comparable are permitted to be inserted into the queue.
The least element of the specified ordering is stored at the head of the queue and the greatest element is stored at the tail of the queue.
A PriorityQueue<T> is not synchronized.
Inheritance
Implements
Inherited Members
Namespace: Lucene.Net.Support
Assembly: Lucene.Net.dll
Syntax
public class PriorityQueue<T> : ICollection<T>, IEnumerable<T>, IEnumerable where T : class
Type Parameters
| Name | Description |
|---|---|
| T | Type of elements |
Constructors
| Improve this Doc View SourcePriorityQueue()
Constructs a priority queue with an initial capacity of 11 and natural ordering.
Declaration
public PriorityQueue()
PriorityQueue(PriorityQueue<T>)
Creates a PriorityQueue<T> containing the elements in the specified priority queue. This priority queue will be ordered according to the same ordering as the given priority queue.
Declaration
public PriorityQueue(PriorityQueue<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| PriorityQueue<T> | collection | the priority queue whose elements are to be placed into this priority queue |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if elements of |
PriorityQueue(TreeSet<T>)
Creates a PriorityQueue<T> containing the elements in the specified TreeSet<T>. This priority queue will be ordered according to the same ordering as the given TreeSet<T>.
The constructed priority queue has the initial capacity of 110% of the size of the sorted set. The priority queue will have the same comparator as the sorted set.
Declaration
public PriorityQueue(TreeSet<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| TreeSet<T> | collection | the sorted set whose elements are to be placed into this priority queue |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if elements of |
| System.ArgumentNullException | if the specified collection or any of its elements are null |
PriorityQueue(ICollection<T>)
Creates a PriorityQueue<T> containing the elements in the specified collection. If the specified collection is an instance of a System.Collections.Generic.SortedSet<T>, TreeSet<T>, or is another PriorityQueue<T>, this priority queue will be ordered according to the same ordering. Otherwise, this priority queue will be ordered according to the System.IComparable<T> natural ordering of its elements.
Declaration
public PriorityQueue(ICollection<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.ICollection<T> | collection | collection to be inserted into priority queue |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if elements of the specified collection cannot be compared to one another according to the priority queue's ordering |
PriorityQueue(IComparer<T>)
Constructs a priority queue with the specified capacity and comparer.
Declaration
public PriorityQueue(IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IComparer<T> | comparer | The System.Collections.Generic.IComparer<T> that will be used to order this
priority queue. If |
PriorityQueue(SortedSet<T>)
Creates a PriorityQueue<T> containing the elements in the specified System.Collections.Generic.SortedSet<T>. This priority queue will be ordered according to the same ordering as the given System.Collections.Generic.SortedSet<T>.
The constructed priority queue has the initial capacity of 110% of the size of the sorted set. The priority queue will have the same comparator as the sorted set.
Declaration
public PriorityQueue(SortedSet<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.SortedSet<T> | collection | the sorted set whose elements are to be placed into this priority queue |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if elements of |
| System.ArgumentNullException | if the specified collection or any of its elements are null |
PriorityQueue(Int32)
Constructs a priority queue with the specified capacity and natural ordering.
Declaration
public PriorityQueue(int initialCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | initialCapacity | initial capacity |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | if the |
PriorityQueue(Int32, IComparer<T>)
Creates a PriorityQueue<T> with the specified initial capacity that orders its elements according to the specified comparer.
Declaration
public PriorityQueue(int initialCapacity, IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | initialCapacity | the initial capacity for this priority queue |
| System.Collections.Generic.IComparer<T> | comparer | The System.Collections.Generic.IComparer<T> that will be used to order this
priority queue. If |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | if the |
Properties
| Improve this Doc View SourceComparer
Gets the System.Collections.Generic.IComparer<T> used to order the elements in this queue, or Lucene.Net.Util.ArrayUtil.NaturalComparer`1 if this queue is sorted according to the System.IComparable<T> natural ordering of its elements.
Declaration
public virtual IComparer<T> Comparer { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.IComparer<T> |
Count
Gets the size of the priority queue. If the size of the queue is greater than the System.Int32.MaxValue, then it returns System.Int32.MaxValue.
Declaration
public virtual int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
IsReadOnly
Gets a value indicating whether the collection is read-only.
Declaration
public virtual bool IsReadOnly { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Remarks
For priority queue this property returns false.
Methods
| Improve this Doc View SourceAdd(T)
Adds the specified object to the priority queue.
Declaration
public virtual void Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | the object to be added. |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering |
| System.ArgumentNullException | if |
AddAll(ICollection<T>)
Declaration
public virtual bool AddAll(ICollection<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.ICollection<T> | collection |
Returns
| Type | Description |
|---|---|
| System.Boolean |
Clear()
Removes all of the elements from this priority queue.
Declaration
public virtual void Clear()
Contains(T)
Returns true if this queue contains the specified element.
More formally, returns true if and only if this queue contains
at least one element item such that o.Equals(item).
Declaration
public virtual bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The object to locate in the priority queue |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
CopyTo(T[], Int32)
Copies the elements of the priority queue to an Array, starting at a particular Array index.
Declaration
public virtual void CopyTo(T[] array, int arrayIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The one-dimensional Array that is the destination of the elements copied from the priority queue. The Array must have zero-based indexing. |
| System.Int32 | arrayIndex | The zero-based index in array at which copying begins. |
Remarks
It is not guaranteed that items will be copied in the sorted order.
Element()
Retrieves, but does not remove, the head of this queue. This method differs from Peek() only in that it throws an exception if this queue is empty.
This implementation returns the result of Peek() unless the queue is empty.
Declaration
public virtual T Element()
Returns
| Type | Description |
|---|---|
| T | the head of this queue |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | if this queue is empty |
GetEnumerator()
Gets the enumerator of the priority queue, which will not return elements in any specified ordering.
Declaration
public virtual IEnumerator<T> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<T> | The enumerator of the priority queue. |
Remarks
Returned enumerator does not iterate elements in sorted order.
Offer(T)
Inserts the specified element into this priority queue.
Declaration
public virtual bool Offer(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | the element to add to the priority queue. |
Returns
| Type | Description |
|---|---|
| System.Boolean | always |
Exceptions
| Type | Condition |
|---|---|
| System.InvalidCastException | if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering |
| System.ArgumentNullException | if the specified element is null |
Peek()
Gets but does not remove the head of the queue.
Declaration
public virtual T Peek()
Returns
| Type | Description |
|---|---|
| T | the head of the queue or null if the queue is empty. |
Poll()
Gets and removes the head of the queue.
Declaration
public virtual T Poll()
Returns
| Type | Description |
|---|---|
| T | the head of the queue or null if the queue is empty. |
Remove()
Retrieves and removes the head of this queue. This method differs from Poll() only in that it throws an exception if this queue is empty.
This implementation returns the result of Poll() unless the queue is empty.
Declaration
public virtual T Remove()
Returns
| Type | Description |
|---|---|
| T | the head of this queue |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | if this queue is empty |
Remove(T)
Removes the specified object from the priority queue.
Declaration
public virtual bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | the object to be removed. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Explicit Interface Implementations
| Improve this Doc View SourceIEnumerable.GetEnumerator()
Returns an enumerator that iterates through the collection.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.IEnumerator | Enumerator |
Remarks
Returned enumerator does not iterate elements in sorted order.