Class OrderedDict<TKey, TValue>
- Namespace
- OptunaSharp.Collections
- Assembly
- OptunaSharp.dll
Insertion-order-preserving dictionary helpers shared by all target frameworks.
public sealed class OrderedDict<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>, IDictionary, IList<KeyValuePair<TKey, TValue>>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyList<KeyValuePair<TKey, TValue>>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IList, ICollection, IEnumerable where TKey : notnull
Type Parameters
TKeyThe non-null key type compared by the dictionary comparer.
TValueThe type of values stored with each key.
- Inheritance
-
OrderedDict<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>IReadOnlyDictionary<TKey, TValue>IList<KeyValuePair<TKey, TValue>>ICollection<KeyValuePair<TKey, TValue>>IReadOnlyList<KeyValuePair<TKey, TValue>>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
Remarks
Operations on the collection have algorithmic complexities that are similar to that of the List<T> class, except with lookups by key similar in complexity to that of Dictionary<TKey, TValue>.
Constructors
OrderedDict()
Initializes a new instance of the OrderedDict<TKey, TValue> class that is empty, has the default initial capacity, and uses the default equality comparer for the key type.
public OrderedDict()
OrderedDict(IDictionary<TKey, TValue>)
Initializes a new instance of the OrderedDict<TKey, TValue> class that contains elements copied from the specified IDictionary<TKey, TValue> and uses the default equality comparer for the key type.
public OrderedDict(IDictionary<TKey, TValue> dictionary)
Parameters
dictionaryIDictionary<TKey, TValue>The IDictionary<TKey, TValue> whose elements are copied to the new OrderedDict<TKey, TValue>. The initial order of the elements in the new collection is the order the elements are enumerated from the supplied dictionary.
Exceptions
- ArgumentNullException
dictionaryis null.
OrderedDict(IDictionary<TKey, TValue>, IEqualityComparer<TKey>?)
Initializes a new instance of the OrderedDict<TKey, TValue> class that contains elements copied from the specified IDictionary<TKey, TValue> and uses the specified IEqualityComparer<T>.
public OrderedDict(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer)
Parameters
dictionaryIDictionary<TKey, TValue>The IDictionary<TKey, TValue> whose elements are copied to the new OrderedDict<TKey, TValue>. The initial order of the elements in the new collection is the order the elements are enumerated from the supplied dictionary.
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default EqualityComparer<T> for the type of the key.
Exceptions
- ArgumentNullException
dictionaryis null.
OrderedDict(IEnumerable<KeyValuePair<TKey, TValue>>)
Initializes a new instance of the OrderedDict<TKey, TValue> class that contains elements copied from the specified IEnumerable<T> and uses the default equality comparer for the key type.
public OrderedDict(IEnumerable<KeyValuePair<TKey, TValue>> collection)
Parameters
collectionIEnumerable<KeyValuePair<TKey, TValue>>The IEnumerable<T> whose elements are copied to the new OrderedDict<TKey, TValue>. The initial order of the elements in the new collection is the order the elements are enumerated from the supplied collection.
Exceptions
- ArgumentNullException
collectionis null.
OrderedDict(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>?)
Initializes a new instance of the OrderedDict<TKey, TValue> class that contains elements copied from the specified IEnumerable<T> and uses the specified IEqualityComparer<T>.
public OrderedDict(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer)
Parameters
collectionIEnumerable<KeyValuePair<TKey, TValue>>The IEnumerable<T> whose elements are copied to the new OrderedDict<TKey, TValue>. The initial order of the elements in the new collection is the order the elements are enumerated from the supplied collection.
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default EqualityComparer<T> for the type of the key.
Exceptions
- ArgumentNullException
collectionis null.
OrderedDict(IEqualityComparer<TKey>?)
Initializes a new instance of the OrderedDict<TKey, TValue> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<T>.
public OrderedDict(IEqualityComparer<TKey>? comparer)
Parameters
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default EqualityComparer<T> for the type of the key.
OrderedDict(int)
Initializes a new instance of the OrderedDict<TKey, TValue> class that is empty, has the specified initial capacity, and uses the default equality comparer for the key type.
public OrderedDict(int capacity)
Parameters
capacityintThe initial number of elements that the OrderedDict<TKey, TValue> can contain.
Exceptions
- ArgumentOutOfRangeException
capacity is less than 0.
OrderedDict(int, IEqualityComparer<TKey>?)
Initializes a new instance of the OrderedDict<TKey, TValue> class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer<T>.
public OrderedDict(int capacity, IEqualityComparer<TKey>? comparer)
Parameters
capacityintThe initial number of elements that the OrderedDict<TKey, TValue> can contain.
comparerIEqualityComparer<TKey>The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default EqualityComparer<T> for the type of the key.
Exceptions
- ArgumentOutOfRangeException
capacityis less than 0.
Properties
Capacity
Gets the total number of key/value pairs the internal data structure can hold without resizing.
public int Capacity { get; }
Property Value
Comparer
Gets the IEqualityComparer<T> that is used to determine equality of keys for the dictionary.
public IEqualityComparer<TKey> Comparer { get; }
Property Value
- IEqualityComparer<TKey>
Count
Gets the number of key/value pairs contained in the OrderedDict<TKey, TValue>.
public int Count { get; }
Property Value
this[TKey]
Gets or sets the value associated with the specified key.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the value to get or set.
Property Value
- TValue
The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.
Remarks
Setting the value of an existing key does not impact its order in the collection.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keydoes not exist in the collection.
Keys
Gets a collection containing the keys in the OrderedDict<TKey, TValue>.
public OrderedDict<TKey, TValue>.KeyCollection Keys { get; }
Property Value
- OrderedDict<TKey, TValue>.KeyCollection
Values
Gets a collection containing the values in the OrderedDict<TKey, TValue>.
public OrderedDict<TKey, TValue>.ValueCollection Values { get; }
Property Value
- OrderedDict<TKey, TValue>.ValueCollection
Methods
Add(TKey, TValue)
Adds the specified key and value to the dictionary.
public void Add(TKey key, TValue value)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value of the element to add. The value can be null for reference types.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the OrderedDict<TKey, TValue>.
Clear()
Removes all keys and values from the OrderedDict<TKey, TValue>.
public void Clear()
ContainsKey(TKey)
Determines whether the OrderedDict<TKey, TValue> contains the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the OrderedDict<TKey, TValue>.
Returns
- bool
true if the OrderedDict<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
ContainsValue(TValue)
Determines whether the OrderedDict<TKey, TValue> contains a specific value.
public bool ContainsValue(TValue value)
Parameters
valueTValueThe value to locate in the OrderedDict<TKey, TValue>. The value can be null for reference types.
Returns
- bool
true if the OrderedDict<TKey, TValue> contains an element with the specified value; otherwise, false.
EnsureCapacity(int)
Ensures that the dictionary can hold up to capacity entries without resizing.
public int EnsureCapacity(int capacity)
Parameters
capacityintThe desired minimum capacity of the dictionary. The actual capacity provided may be larger.
Returns
- int
The new capacity of the dictionary.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
GetAt(int)
Gets the key/value pair at the specified index.
public KeyValuePair<TKey, TValue> GetAt(int index)
Parameters
indexintThe zero-based index of the pair to get.
Returns
- KeyValuePair<TKey, TValue>
The element at the specified index.
Exceptions
- ArgumentOutOfRangeException
indexis less than 0 or greater than or equal to Count.
GetEnumerator()
Returns an enumerator that iterates through the OrderedDict<TKey, TValue>.
public OrderedDict<TKey, TValue>.Enumerator GetEnumerator()
Returns
- OrderedDict<TKey, TValue>.Enumerator
A OrderedDict<TKey, TValue>.Enumerator structure for the OrderedDict<TKey, TValue>.
GetValueOrDefault(TKey, TValue?)
Python dict.get(key, default).
public TValue? GetValueOrDefault(TKey key, TValue? defaultValue = default)
Parameters
keyTKeyThe non-null lookup key, compared with the dictionary's configured comparer.
defaultValueTValueThe value returned when the key has no entry.
Returns
- TValue
The stored value when the key exists, otherwise the supplied fallback.
IndexOf(TKey)
Determines the index of a specific key in the OrderedDict<TKey, TValue>.
public int IndexOf(TKey key)
Parameters
keyTKeyThe key to locate.
Returns
- int
The index of
keyif found; otherwise, -1.
Exceptions
- ArgumentNullException
keyis null.
Insert(int, TKey, TValue)
Inserts an item into the collection at the specified index.
public void Insert(int index, TKey key, TValue value)
Parameters
indexintThe zero-based index at which item should be inserted.
keyTKeyThe key to insert.
valueTValueThe value to insert.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the OrderedDict<TKey, TValue>.
- ArgumentOutOfRangeException
indexis less than 0 or greater than Count.
Remove(TKey)
Removes the value with the specified key from the OrderedDict<TKey, TValue>.
public bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
Exceptions
- ArgumentNullException
keyis null.
Remove(TKey, out TValue)
Removes the value with the specified key from the OrderedDict<TKey, TValue> and copies the element to the value parameter.
public bool Remove(TKey key, out TValue value)
Parameters
keyTKeyThe key of the element to remove.
valueTValueThe removed element.
Returns
Exceptions
- ArgumentNullException
keyis null.
RemoveAt(int)
Removes the key/value pair at the specified index.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
indexis less than 0 or greater than or equal to Count.
SetAt(int, TKey, TValue)
Sets the key/value pair at the specified index.
public void SetAt(int index, TKey key, TValue value)
Parameters
indexintThe zero-based index at which to set the key/value pair.
keyTKeyThe key to store at the specified index.
valueTValueThe value to store at the specified index.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists at an index different to
index.- ArgumentOutOfRangeException
indexis less than 0 or greater than or equal to Count.
SetAt(int, TValue)
Sets the value for the key at the specified index.
public void SetAt(int index, TValue value)
Parameters
indexintThe zero-based index at which to set the value.
valueTValueThe value to store at the specified index.
Exceptions
- ArgumentOutOfRangeException
indexis less than 0 or greater than or equal to Count.
ShallowClone()
Shallow copy preserving insertion order (Python copy.copy(dict)).
public OrderedDict<TKey, TValue> ShallowClone()
Returns
- OrderedDict<TKey, TValue>
A new dictionary preserving insertion order and sharing the original key and value objects.
TrimExcess()
Sets the capacity of this dictionary to what it would be if it had been originally initialized with all its entries.
public void TrimExcess()
TrimExcess(int)
Sets the capacity of this dictionary to hold up a specified number of entries without resizing.
public void TrimExcess(int capacity)
Parameters
capacityintThe desired capacity to which to shrink the dictionary.
Exceptions
- ArgumentOutOfRangeException
capacityis less than Count.
TryAdd(TKey, TValue)
Adds the specified key and value to the dictionary if the key doesn't already exist.
public bool TryAdd(TKey key, TValue value)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value of the element to add. The value can be null for reference types.
Returns
- bool
true if the key didn't exist and the key and value were added to the dictionary; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
TryAdd(TKey, TValue, out int)
Adds the specified key and value to the dictionary if the key doesn't already exist.
public bool TryAdd(TKey key, TValue value, out int index)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value of the element to add. The value can be null for reference types.
indexintThe index of the added or existing
key. This is always a valid index into the dictionary.
Returns
- bool
true if the key didn't exist and the key and value were added to the dictionary; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key of the value to get.
valueTValueWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.
Returns
- bool
true if the OrderedDict<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
TryGetValue(TKey, out TValue, out int)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value, out int index)
Parameters
keyTKeyThe key of the value to get.
valueTValueWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter.
indexintThe index of
keyif found; otherwise, -1.
Returns
- bool
true if the OrderedDict<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.