Class Heap<T>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic heap data structure.
public class Heap<T> : IHeap<T>, IReadOnlyHeap<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
T
Value type.
- Inheritance
-
Heap<T>
- Implements
-
IHeap<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Constructors
Heap()
public Heap()
Heap(IComparer<T>)
Creates a new empty Heap<T> instance.
public Heap(IComparer<T> comparer)
Parameters
comparer
IComparer<T>Comparer to use.
Heap(IEnumerable<T>)
public Heap(IEnumerable<T> collection)
Parameters
collection
IEnumerable<T>Initial collection of entries.
Heap(IEnumerable<T>, IComparer<T>)
Creates a new Heap<T> instance.
public Heap(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
collection
IEnumerable<T>Initial collection of entries.
comparer
IComparer<T>Comparer to use.
Properties
Comparer
Entry comparer.
public IComparer<T> Comparer { get; }
Property Value
- IComparer<T>
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
this[int]
Gets the element at the specified index in the read-only list.
public T this[int index] { get; }
Parameters
index
intThe zero-based index of the element to get.
Property Value
- T
The element at the specified index in the read-only list.
Methods
Add(T)
Adds a new entry to this heap.
public void Add(T item)
Parameters
item
TEntry to add.
Clear()
Removes all entries from this heap.
public void Clear()
Extract()
Removes and returns an entry currently at the top of this heap.
public T Extract()
Returns
- T
Removed entry.
Exceptions
- IndexOutOfRangeException
When this heap is empty.
GetEnumerator()
Creates a new Heap<T>.Enumerator instance for this heap.
[Pure]
public Heap<T>.Enumerator GetEnumerator()
Returns
- Heap<T>.Enumerator
New Heap<T>.Enumerator instance.
Peek()
Returns an entry currently at the top of this heap.
[Pure]
public T Peek()
Returns
- T
Entry currently at the top of this heap.
Exceptions
- IndexOutOfRangeException
When this heap is empty.
Pop()
Removes an entry currently at the top of this heap.
public void Pop()
Exceptions
- IndexOutOfRangeException
When this heap is empty.
Replace(T)
Returns and replaces an entry currently at the top of the heap with a new entry.
public T Replace(T item)
Parameters
item
TReplacement entry.
Returns
- T
Removed entry.
Exceptions
- IndexOutOfRangeException
When this heap is empty.
TryExtract(out T)
Attempt to remove and return an entry currently at the top of this heap if it is not empty.
public bool TryExtract(out T result)
Parameters
result
Tout parameter that returns the removed entry.
Returns
- bool
true when this heap was not empty and entry has been removed, otherwise false.
TryPeek(out T)
Attempts to return an entry currently at the top of this heap if it is not empty.
public bool TryPeek(out T result)
Parameters
result
Tout parameter that returns an entry currently at the top of the heap.
Returns
- bool
true when this heap is not empty, otherwise false.
TryPop()
Attempts to remove an entry currently at the top of the heap if it is not empty.
public bool TryPop()
Returns
- bool
true when this heap was not empty and entry has been removed, otherwise false.
TryReplace(T, out T)
Attempt to return and replace an entry currently at the top of this heap with a new entry if it is not empty.
public bool TryReplace(T item, out T replaced)
Parameters
item
TReplacement entry.
replaced
Tout parameter that returns the removed entry.
Returns
- bool
true when this heap is not empty and entry has been replaced, otherwise false.