Class DictionaryHeap<TKey, TValue>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic heap data structure with the ability to identify entries by keys.
public class DictionaryHeap<TKey, TValue> : IDictionaryHeap<TKey, TValue>, IReadOnlyDictionaryHeap<TKey, TValue>, IReadOnlyHeap<TValue>, IReadOnlyList<TValue>, IReadOnlyCollection<TValue>, IEnumerable<TValue>, IEnumerable where TKey : notnull
Type Parameters
TKeyKey type.
TValueValue type.
- Inheritance
-
DictionaryHeap<TKey, TValue>
- Implements
-
IDictionaryHeap<TKey, TValue>IReadOnlyDictionaryHeap<TKey, TValue>IReadOnlyHeap<TValue>IReadOnlyList<TValue>IReadOnlyCollection<TValue>IEnumerable<TValue>
- Inherited Members
- Extension Methods
Constructors
DictionaryHeap()
Creates a new empty DictionaryHeap<TKey, TValue> instance with Default key comparer and Default entry comparer.
public DictionaryHeap()
DictionaryHeap(IEnumerable<KeyValuePair<TKey, TValue>>)
Creates a new DictionaryHeap<TKey, TValue> instance with Default key comparer and Default entry comparer.
public DictionaryHeap(IEnumerable<KeyValuePair<TKey, TValue>> collection)
Parameters
collectionIEnumerable<KeyValuePair<TKey, TValue>>Initial collection of entries.
DictionaryHeap(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>, IComparer<TValue>)
Creates a new DictionaryHeap<TKey, TValue> instance.
public DictionaryHeap(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> keyComparer, IComparer<TValue> comparer)
Parameters
collectionIEnumerable<KeyValuePair<TKey, TValue>>Initial collection of entries.
keyComparerIEqualityComparer<TKey>Key equality comparer.
comparerIComparer<TValue>Entry comparer.
DictionaryHeap(IEqualityComparer<TKey>, IComparer<TValue>)
Creates a new empty DictionaryHeap<TKey, TValue> instance.
public DictionaryHeap(IEqualityComparer<TKey> keyComparer, IComparer<TValue> comparer)
Parameters
keyComparerIEqualityComparer<TKey>Key equality comparer.
comparerIComparer<TValue>Entry comparer.
Properties
Comparer
Entry comparer.
public IComparer<TValue> Comparer { get; }
Property Value
- IComparer<TValue>
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 TValue this[int index] { get; }
Parameters
indexintThe zero-based index of the element to get.
Property Value
- TValue
The element at the specified index in the read-only list.
KeyComparer
Key equality comparer.
public IEqualityComparer<TKey> KeyComparer { get; }
Property Value
- IEqualityComparer<TKey>
Methods
Add(TKey, TValue)
Adds a new entry to this heap.
public void Add(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueEntry to add.
Exceptions
- ArgumentException
When
keyalready exists in this heap.
AddOrReplace(TKey, TValue)
Adds or replaces an entry associated with the specified key.
public TValue AddOrReplace(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueEntry to add or replace with.
Returns
- TValue
Removed entry if
keyexisted in this heap, otherwise the providedvalue.
Clear()
Removes all entries from this heap.
public void Clear()
ContainsKey(TKey)
Checks whether or not an entry with the specified key exists in this heap.
[Pure]
public bool ContainsKey(TKey key)
Parameters
keyTKeyKey to check.
Returns
- bool
true when entry with the specified
keyexists, otherwise false.
Extract()
Removes and returns an entry currently at the top of this heap.
public TValue Extract()
Returns
- TValue
Removed entry.
Exceptions
- IndexOutOfRangeException
When this heap is empty.
GetEnumerator()
Creates a new DictionaryHeap<TKey, TValue>.Enumerator instance for this heap.
[Pure]
public DictionaryHeap<TKey, TValue>.Enumerator GetEnumerator()
Returns
- DictionaryHeap<TKey, TValue>.Enumerator
New DictionaryHeap<TKey, TValue>.Enumerator instance.
GetIndex(TKey)
Returns the 0-based position of an entry associated with the specified key.
[Pure]
public int GetIndex(TKey key)
Parameters
keyTKeyKey to get an index for.
Returns
- int
0-based position of an entry associated with the specified
key.
Exceptions
- KeyNotFoundException
When
keydoes not exist in this heap.
GetKey(int)
Returns the key associated with an entry located at the specified index.
[Pure]
public TKey GetKey(int index)
Parameters
indexint0-based position.
Returns
- TKey
Key associated with an entry located at the specified
index.
Exceptions
- IndexOutOfRangeException
When
indexis not in [0, Count) range.
GetValue(TKey)
Returns an entry associated with the specified key.
[Pure]
public TValue GetValue(TKey key)
Parameters
keyTKeyKey to get an entry for.
Returns
- TValue
Entry associated with the specified
key.
Exceptions
- KeyNotFoundException
When
keydoes not exist in this heap.
Peek()
Returns an entry currently at the top of this heap.
[Pure]
public TValue Peek()
Returns
- TValue
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.
Remove(TKey)
Removes an entry associated with the specified key from this heap.
public TValue Remove(TKey key)
Parameters
keyTKeyEntry's key.
Returns
- TValue
Removed entry.
Exceptions
- ArgumentException
When key does not exist in this heap.
Replace(TKey, TValue)
Returns and replaces an entry currently associated with the specified key.
public TValue Replace(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueReplacement entry.
Returns
- TValue
Removed entry.
Exceptions
- KeyNotFoundException
When
keydoes not exist in this heap.
TryAdd(TKey, TValue)
Attempts to add a new entry to this heap.
public bool TryAdd(TKey key, TValue value)
Parameters
keyTKeyEntry's key.
valueTValueEntry to add.
Returns
- bool
true when key did not exist and entry was added, otherwise false.
TryExtract(out TValue)
Attempt to remove and return an entry currently at the top of this heap if it is not empty.
public bool TryExtract(out TValue result)
Parameters
resultTValueout parameter that returns the removed entry.
Returns
- bool
true when this heap was not empty and entry has been removed, otherwise false.
TryGetValue(TKey, out TValue)
Attempts to return an entry associated with the specified key if it exists.
public bool TryGetValue(TKey key, out TValue result)
Parameters
keyTKeyKey to get an entry for.
resultTValueout parameter that returns an entry associated with the specified
key.
Returns
- bool
true when key exists in this heap, otherwise false.
TryPeek(out TValue)
Attempts to return an entry currently at the top of this heap if it is not empty.
public bool TryPeek(out TValue result)
Parameters
resultTValueout 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.
TryRemove(TKey, out TValue)
Attempts to remove an entry associated with the specified key from this heap.
public bool TryRemove(TKey key, out TValue removed)
Parameters
keyTKeyEntry's key.
removedTValueout parameter that returns the removed entry.
Returns
- bool
true when entry was removed, otherwise false.
TryReplace(TKey, TValue, out TValue)
Attempt to return and replace an entry associated with the specified key with a new entry if it exists.
public bool TryReplace(TKey key, TValue value, out TValue replaced)
Parameters
keyTKeyEntry's key.
valueTValueReplacement entry.
replacedTValueout parameter that returns the removed entry.
Returns
- bool
true when
keyexists in this heap, otherwise false.