Class OrderedDictionary<TKey, TValue>
- Namespace
- Vintagestory.API.Datastructures
- Assembly
- VintagestoryAPI.dll
Same as your normal C# Dictionary but ensures that the order in which the items are added is remembered. That way you can iterate over the dictionary with the insert order intact or set/get elements by index. Taken from http://www.codeproject.com/Articles/18615/OrderedDictionary-T-A-generic-implementation-of-IO Please be aware that this is not a very efficient implementation, recommed use only for small sets of data.
[ProtoContract]
public class OrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
TKeyTValue
- Inheritance
-
OrderedDictionary<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
- Extension Methods
Constructors
OrderedDictionary()
public OrderedDictionary()
OrderedDictionary(Dictionary<TKey, TValue>)
public OrderedDictionary(Dictionary<TKey, TValue> initialData)
Parameters
initialDataDictionary<TKey, TValue>
OrderedDictionary(IEqualityComparer<TKey>)
public OrderedDictionary(IEqualityComparer<TKey> comparer)
Parameters
comparerIEqualityComparer<TKey>
OrderedDictionary(int)
public OrderedDictionary(int capacity)
Parameters
capacityint
OrderedDictionary(int, IEqualityComparer<TKey>)
public OrderedDictionary(int capacity, IEqualityComparer<TKey> comparer)
Parameters
capacityintcomparerIEqualityComparer<TKey>
OrderedDictionary(OrderedDictionary<TKey, TValue>)
public OrderedDictionary(OrderedDictionary<TKey, TValue> initialData)
Parameters
initialDataOrderedDictionary<TKey, TValue>
Properties
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
InternalDictionary
public Dictionary<TKey, TValue> InternalDictionary { get; }
Property Value
- Dictionary<TKey, TValue>
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[TKey]
Gets or sets the element with the specified key.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the element to get or set.
Property Value
- TValue
The element with the specified key.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keyis not found.- NotSupportedException
The property is set and the IDictionary<TKey, TValue> is read-only.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
Values
Important: these are NOT ordered. The ordered values is obtained by .ValuesOrdered
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
ValuesOrdered
public IEnumerable<TValue> ValuesOrdered { get; }
Property Value
- IEnumerable<TValue>
Methods
Add(TKey, TValue)
public int Add(TKey key, TValue value)
Parameters
keyTKeyvalueTValue
Returns
BeforeSerialization()
[ProtoBeforeSerialization]
protected void BeforeSerialization()
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
ContainsKey(TKey)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the IDictionary<TKey, TValue>.
Returns
- bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
ContainsValue(TValue)
public bool ContainsValue(TValue value)
Parameters
valueTValue
Returns
GetEnumerator()
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
GetKeyAtIndex(int)
public TKey GetKeyAtIndex(int index)
Parameters
indexint
Returns
- TKey
GetValueAtIndex(int)
public TValue GetValueAtIndex(int index)
Parameters
indexint
Returns
- TValue
IndexOfKey(TKey)
Returns -1 if the key was not found
public int IndexOfKey(TKey key)
Parameters
keyTKey
Returns
Insert(int, TKey, TValue)
public void Insert(int index, TKey key, TValue value)
Parameters
indexintkeyTKeyvalueTValue
InsertBefore(TKey, TKey, TValue)
public void InsertBefore(TKey Atkey, TKey key, TValue value)
Parameters
AtkeyTKeykeyTKeyvalueTValue
OnDeserializedMethod(StreamingContext)
[OnDeserialized]
protected void OnDeserializedMethod(StreamingContext context)
Parameters
contextStreamingContext
Remove(TKey)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
- bool
true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original IDictionary<TKey, TValue>.
Exceptions
- ArgumentNullException
keyis null.- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
RemoveAt(int)
public void RemoveAt(int index)
Parameters
indexint
SetAtIndex(int, TValue)
public void SetAtIndex(int index, TValue value)
Parameters
indexintvalueTValue
TryGetValue(TKey)
public TValue TryGetValue(TKey key)
Parameters
keyTKey
Returns
- TValue
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key whose value to get.
valueTValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.