Class SwiftDictionary<TKey, TValue>
- Namespace
- SwiftCollections
- Assembly
- SwiftCollections.dll
A high-performance, memory-efficient dictionary providing lightning-fast O(1) operations for addition, retrieval, and removal, optimized to outperform standard dictionaries.
[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftDictionary<TKey, TValue> : IStateBacked<SwiftDictionaryState<TKey, TValue>>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, IMemoryPackable<SwiftDictionary<TKey, TValue>>, IMemoryPackFormatterRegister where TKey : notnull
Type Parameters
TKeySpecifies the type of keys in the dictionary.
TValueSpecifies the type of values in the dictionary.
- Inheritance
-
SwiftDictionary<TKey, TValue>
- Implements
-
IStateBacked<SwiftDictionaryState<TKey, TValue>>IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>IMemoryPackable<SwiftDictionary<TKey, TValue>>IMemoryPackFormatterRegister
- Derived
- Inherited Members
- Extension Methods
Remarks
The comparer is not serialized. After deserialization the dictionary reverts to the same default comparer selection used by a new instance. String keys use SwiftCollections' deterministic default comparer. Object keys use a SwiftCollections comparer that hashes strings deterministically, while other object-key determinism still depends on the underlying key type's GetHashCode() implementation. Other key types use Default.
If a custom comparer is required it can be reapplied using SetComparer(IEqualityComparer<TKey>).
Constructors
SwiftDictionary()
Initialize a new instance of SwiftDictionary<TKey, TValue> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftDictionary()
SwiftDictionary(SwiftDictionaryState<TKey, TValue>)
Initializes a new instance of the SwiftDictionary<TKey, TValue> class with the specified SwiftDictionaryState<TKey, TValue>.
[MemoryPackConstructor]
public SwiftDictionary(SwiftDictionaryState<TKey, TValue> state)
Parameters
stateSwiftDictionaryState<TKey, TValue>The state containing the internal array, count, offset, and version for initialization.
SwiftDictionary(IDictionary<TKey, TValue>, IEqualityComparer<TKey>?)
Initialize a new instance of SwiftDictionary<TKey, TValue> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer = null)
Parameters
dictionaryIDictionary<TKey, TValue>comparerIEqualityComparer<TKey>
SwiftDictionary(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>?)
Initialize a new instance of SwiftDictionary<TKey, TValue> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer = null)
Parameters
collectionIEnumerable<KeyValuePair<TKey, TValue>>comparerIEqualityComparer<TKey>
SwiftDictionary(int, IEqualityComparer<TKey>?)
Initialize a new instance of SwiftDictionary<TKey, TValue> with customizable capacity and comparer for optimal performance tailored to your needs.
public SwiftDictionary(int capacity, IEqualityComparer<TKey>? comparer = null)
Parameters
capacityintcomparerIEqualityComparer<TKey>
Fields
DefaultCapacity
The default initial capacity of the dictionary.
public const int DefaultCapacity = 8
Field Value
_comparer
The comparer used to determine equality of keys and to generate hash codes.
protected IEqualityComparer<TKey> _comparer
Field Value
- IEqualityComparer<TKey>
_entries
The array containing the entries of the dictionary.
protected SwiftDictionary<TKey, TValue>.Entry[] _entries
Field Value
- Entry[]
_version
A version counter used to track modifications to the dictionary. Incremented on mutations to detect changes during enumeration and ensure enumerator validity.
protected uint _version
Field Value
Properties
Capacity
Gets the total number of elements that the collection can hold without resizing.
[JsonIgnore]
[MemoryPackIgnore]
public int Capacity { get; }
Property Value
Comparer
Gets the equality comparer used to determine equality of keys in the collection.
[JsonIgnore]
[MemoryPackIgnore]
public IEqualityComparer<TKey> Comparer { get; }
Property Value
- IEqualityComparer<TKey>
Count
Gets the number of elements contained in the dictionary.
[JsonIgnore]
[MemoryPackIgnore]
public int Count { get; }
Property Value
this[TKey]
Gets or sets the value associated with the specified key.
[JsonIgnore]
[MemoryPackIgnore]
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key whose value to get or set.
Property Value
- TValue
Remarks
Getting a value with a key that does not exist will throw an exception. Setting a value for a key that does not exist will add a new entry with the specified key and value.
Keys
[JsonIgnore]
[MemoryPackIgnore]
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
State
Gets or sets the current state of the dictionary, including all key-value pairs.
[JsonInclude]
[MemoryPackInclude]
public SwiftDictionaryState<TKey, TValue> State { get; }
Property Value
- SwiftDictionaryState<TKey, TValue>
Remarks
The state can be used to serialize or restore the contents of the dictionary. Setting this property replaces the entire contents of the dictionary with the provided state. The setter is intended for internal use and is not accessible to external callers.
SyncRoot
[JsonIgnore]
[MemoryPackIgnore]
public object SyncRoot { get; }
Property Value
Values
[JsonIgnore]
[MemoryPackIgnore]
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
Methods
Add(KeyValuePair<TKey, TValue>)
public void Add(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>
Add(object, object?)
public void Add(object key, object? value)
Parameters
Add(TKey, TValue)
Attempts to add the specified key and value to the dictionary.
public virtual bool Add(TKey key, TValue value)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value of the element to add.
Returns
- bool
true if the key/value pair was added to the dictionary successfully; false if the key already exists.
CheckLoadThreshold()
Ensures that the dictionary is resized when the current load factor exceeds the predefined threshold.
protected void CheckLoadThreshold()
Clear()
public virtual void Clear()
Contains(KeyValuePair<TKey, TValue>)
public bool Contains(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>
Returns
ContainsKey(TKey)
Determines whether the dictionary contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the dictionary.
Returns
- bool
true if the dictionary contains an element with the specified key; otherwise, false.
CopyTo(Array, int)
public void CopyTo(Array array, int arrayIndex)
Parameters
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the collection to the specified array, starting at the given array index.
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<TKey, TValue>[]The one-dimensional array of key/value pairs that is the destination of the elements copied from the collection. The array must have zero-based indexing.
arrayIndexintThe zero-based index in the destination array at which copying begins.
Exceptions
- ArgumentOutOfRangeException
Thrown if arrayIndex is less than 0 or greater than the length of array.
- ArgumentException
Thrown if the number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array.
EnsureCapacity(int)
Ensures that the dictionary can hold up to the specified number of entries, if not it resizes.
public void EnsureCapacity(int capacity)
Parameters
capacityintThe minimum capacity to ensure.
Exceptions
- ArgumentOutOfRangeException
The capacity is less than zero.
FindEntry(TKey)
Finds the arrayIndex of the entry with the specified key.
protected int FindEntry(TKey key)
Parameters
keyTKeyThe key to locate in the dictionary.
Returns
- int
The arrayIndex of the entry if found; otherwise, -1.
Exceptions
- ArgumentNullException
The key is null.
GetEnumerator()
public SwiftDictionary<TKey, TValue>.SwiftDictionaryEnumerator GetEnumerator()
Returns
- SwiftDictionary<TKey, TValue>.SwiftDictionaryEnumerator
Remove(KeyValuePair<TKey, TValue>)
public bool Remove(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>
Returns
Remove(TKey)
public virtual bool Remove(TKey key)
Parameters
keyTKey
Returns
SetComparer(IEqualityComparer<TKey>)
Sets a new comparer for the dictionary and rehashes the entries.
public void SetComparer(IEqualityComparer<TKey> comparer)
Parameters
comparerIEqualityComparer<TKey>The new comparer to use.
TrimExcess()
Sets the capacity of a SwiftDictionary<TKey, TValue> to the actual number of elements it contains, rounded up to a nearby next power of 2 value.
public void TrimExcess()
TryGetValue(TKey, out TValue)
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyvalueTValue