Class BinaryHeapListWrapper<T>
A wrapper around a
Inheritance
Inherited Members
Namespace: MoreStructures.PriorityQueues.BinaryHeap
Assembly: MoreStructures.dll
Syntax
public sealed class BinaryHeapListWrapper<T>
Type Parameters
Name | Description |
---|---|
T | The type of items in the wrapped list. |
Remarks
Heap items are either stored:
- from index 0 to HeapCount - 1, i.e. at the beginning of the list,
- or from index ListCount - HeapCount to ListCount - 1, i.e. at the end.
That leaves a buffer of ListCount - HeapCount items, either at the end or at the beginning of the list.
The functionalities of this wrapper can be used to support HeapSort or a priority queue based on a Max Binary Heap.
In the first case the buffer area is used (to store already sorted items). In the second case it is not.
Constructors
| Improve this Doc View SourceBinaryHeapListWrapper(IList<T>, IComparer<T>, Int32, Boolean, Int32)
Built around the provided items
.
Declaration
public BinaryHeapListWrapper(IList<T> items, IComparer<T> comparer, int count, bool storeHeapAtTheEnd = false, int indexDelta = 0)
Parameters
Type | Name | Description |
---|---|---|
IList<T> | items | The |
IComparer<T> | comparer | The comparer to be used to establish a order relationship between items. |
System.Int32 | count | The size of the subset of |
System.Boolean | storeHeapAtTheEnd | Whether heap items should be stored at the beginning or at the end of the |
System.Int32 | indexDelta | The delta, positive or negative, with which heap items will be stored in the |
BinaryHeapListWrapper(BinaryHeapListWrapper<T>)
Built from the provided BinaryHeapListWrapper<T> source
.
Declaration
public BinaryHeapListWrapper(BinaryHeapListWrapper<T> source)
Parameters
Type | Name | Description |
---|---|---|
BinaryHeapListWrapper<T> | source |
Properties
| Improve this Doc View SourceHeapCount
The number of items in the heap only, buffer area of the underlying list excluded.
Declaration
public int HeapCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Remarks
May be smaller than the current
Item[Int32]
Retrieves the index
-th item in the underlying list (heap or buffer).
Declaration
public T this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | A non-negative System.Int32. It can be non-smaller than HeapCount, if an element of the
buffer is being accessed, but it necessarily has to be smaller than the |
Property Value
Type | Description |
---|---|
T | The item, an instance of type |
ListCount
The number of items in the underlying list, heap and buffer are included.
Declaration
public int ListCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Remarks
It's always non-smaller than HeapCount, since all the heap items are contained in the underlying list.
RaiseItemPopping
Invoked just before an item is removed from MoreStructures.PriorityQueues.BinaryHeap.BinaryHeapListWrapper`1.Items (at the beginning of the heap), and before "sifting down" procedure is performed.
Declaration
public Action<int, int> RaiseItemPopping { get; set; }
Property Value
Type | Description |
---|---|
Action<System.Int32, System.Int32> |
Remarks
The actual index of the item pushed may be different than 0, if the heap is stored in reverse, in which case
it is equal to ListCount - 1.
Same applies to the index of the item pushed out of the heap (but still in the list, in the buffer area).
RaiseItemPushed
Invoked just after an item has been pushed into MoreStructures.PriorityQueues.BinaryHeap.BinaryHeapListWrapper`1.Items (at the end of the heap), and before the "sifting up" procedure is performed.
Declaration
public Action<int> RaiseItemPushed { get; set; }
Property Value
Type | Description |
---|---|
Action<System.Int32> |
Remarks
The actual index of the item pushed will be different than ListCount - 1, if ListCount is different than HeapCount, or if the heap is stored in reverse.
RaiseItemsSwapped
Invoked just after two items have been swapped of position in MoreStructures.PriorityQueues.BinaryHeap.BinaryHeapListWrapper`1.Items.
Declaration
public Action<int, int> RaiseItemsSwapped { get; set; }
Property Value
Type | Description |
---|---|
Action<System.Int32, System.Int32> |
Methods
| Improve this Doc View SourceClear()
Clears the underlying list (heap and buffer), wiping all its items out, if the list is not readonly.
If it is readonly (e.g. an array), it just resets the HeapCount.
Declaration
public void Clear()
GetEnumerator()
Returns an enumerator of T
instances, going through all items of the underlying list,
not just the first HeapCount items which form the heap, but also the buffer area at the end of the
underlying list.
Declaration
public IEnumerator<T> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> | An |
Peek()
Peeks the item with max priority from the root of the heap, if any.
Declaration
public T Peek()
Returns
Type | Description |
---|---|
T |
PeekKth(Int32)
Retrieves the item of the heap with priority k
, without extracting any of the items in the
heap.
Declaration
public (T result, bool valid) PeekKth(int k)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | k | The non-negative priority rank: 0 means highest priority, 1 second highest, etc. |
Returns
Type | Description |
---|---|
System.ValueTuple<T, System.Boolean> | The item with k-th highest priority if any, with true as valid flag.
|
Pop()
Pops the item with max priority from the root of the heap, if any, moving the last leaf to the now vacant root and sifting it down until the heap property is restored.
Declaration
public T Pop()
Returns
Type | Description |
---|---|
T | The popped item. |
PopAll()
Pops all items of the heap in sequence, from the one with highest priority to the one with lowest priority.
Declaration
public IEnumerable<T> PopAll()
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence of |
Push(T, Boolean)
Pushes the provided item
into the heap, sifting it up until the max heap property is
restored.
Declaration
public void Push(T item, bool siftUp = true)
Parameters
Type | Name | Description |
---|---|---|
T | item | The item to be added to the heap. |
System.Boolean | siftUp | Whether the sift up procedure should be executed or not. By default it is set to true.
|
RestoreHeapProperty()
Restores the max heap property, ensuring that each node of the heap is at least as big as its children, if any.
Declaration
public void RestoreHeapProperty()