CircularSortedListTKey, TValue Class

Represents a dictionary of key/value pairs that are sorted by key based on the associated IComparerT implementation. The dictionary behaves as list as well, as it has a direct indexed access to the elements through Keys and Values properties or by the ElementAt method. CircularSortedListTKey, TValue is fully compatible with SortedListTKey, TValue, but is generally faster than that.

See the online help for a more detailed description.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0
C#
[SerializableAttribute]
public class CircularSortedList<TKey, TValue> : IDictionary<TKey, TValue>, 
	ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, 
	IEnumerable, IList<KeyValuePair<TKey, TValue>>, IDictionary, 
	ICollection, IList, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, 
	IReadOnlyList<KeyValuePair<TKey, TValue>>
Inheritance
Object    CircularSortedListTKey, TValue
Implements
ICollectionKeyValuePairTKey, TValue, IDictionaryTKey, TValue, IEnumerableKeyValuePairTKey, TValue, IListKeyValuePairTKey, TValue, IReadOnlyCollectionKeyValuePairTKey, TValue, IReadOnlyDictionaryTKey, TValue, IReadOnlyListKeyValuePairTKey, TValue, ICollection, IDictionary, IEnumerable, IList

Type Parameters

TKey
Type of the keys stored in the sorted list.
TValue
Type of the values stored in the sorted list.

Remarks

The CircularSortedListTKey, TValue generic class is an array of key/value pairs with O(log n) retrieval, where n is the number of elements in the dictionary.

Similarly to SortedListTKey, TValue, CircularSortedListTKey, TValue supports efficient indexed retrieval of keys and values through the collections returned by the Keys and Values properties. It is not necessary to regenerate the lists when the properties are accessed, because the lists are just wrappers for the internal arrays of keys and values.

CircularSortedListTKey, TValue is implemented as a pair of CircularListT of key/value pairs, sorted by the key. Each element can be retrieved as a KeyValuePairTKey, TValue object.

Key objects must be immutable as long as they are used as keys in the CircularSortedListTKey, TValue. Every key in a CircularSortedListTKey, TValue must be unique. A key cannot be , but a value can be, if the type of values in the list, TValue, is a reference type, or is a NullableT type.

CircularSortedListTKey, TValue requires a comparer implementation to sort and to perform comparisons. If comparer is not defined when CircularSortedListTKey, TValue is instantiated by one of the constructors, the comparer will be chosen automatically. Only when targeting the .NET Framework, if TKey is an enum type, then the comparer will be the EnumComparer<TEnum>.Comparer. Otherwise, the default comparer Comparer<T>.Default will be chosen. The default comparer checks whether the key type TKey implements IComparableT and uses that implementation, if available. If not, Comparer<T>.Default checks whether the key type TKey implements IComparable. If the key type TKey does not implement either interface, you can specify an IComparableT implementation in a constructor overload that accepts a comparer parameter.

The capacity of a CircularSortedListTKey, TValue is the number of elements the CircularSortedListTKey, TValue can hold. As elements are added to a CircularSortedListTKey, TValue, the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling TrimExcess or by setting the Capacity property explicitly. Decreasing the capacity reallocates memory and copies all the elements in the CircularSortedListTKey, TValue.

Examples

CircularSortedListTKey, TValue implements IListT as well, so it can be indexed directly when cast to IListT or though the AsList property:

C#
KeyValuePair<int, string> firstItem = myCircularSortedList.AsList[0];
Setting the IList<T>.Item property or using the IList<T>.Insert method throws a NotSupportedException for CircularSortedListTKey, TValue, as the position of an element cannot be set directly, it always depends on the comparer implementation.

Comparison with SortedList and SortedDictionary classes

CircularSortedListTKey, TValue is similar to the SortedListTKey, TValue and SortedDictionaryTKey, TValue generic classes. These three classes serve a similar purpose, and all have O(log n) retrieval. Where the three classes differ is in memory use and speed of insertion and removal:
Collection typeBehavior
SortedDictionaryTKey, TValue
  • Store model and memory: Elements are stored in a binary search tree where every node is an instance of a class, which references the left and right child nodes and wraps a KeyValuePairTKey, TValue of the element to be stored. (A node is similar to LinkedListNodeT instances in a LinkedListT). This sorted dictionary variant consumes the most memory.
  • Insertion and removal: These operations have generally O(log n) cost at any position, which is the best of any sorted dictionary variants, though due to the slower navigation among nodes, the better general cost starts to outperform the other sorted dictionaries only in case of many elements (hundreds or thousands of elements).
  • Populating from sorted data: Adding a new element is always an O(log n) operation. Though when populating from sorted data, the tree always needed to be re-balanced, so it has worse performance than populating from random data.
  • Enumerating the collection: Considering that navigating among nodes is slower than array access, this sorted dictionary variant has the worst enumeration performance.
SortedListTKey, TValue
  • Store model and memory: Keys and values are stored in separated arrays, which is the most compact storage form among the sorted dictionaries.
  • Insertion and removal: Position of the element is searched with binary search in the array, which in an O(log n) operation. Insertion/removal at the last position has a constant additional cost, so inserting/removing at the end has O(log n) cost, otherwise O(n) cost.
  • Populating from sorted data: Since position of the elements are always checked, adding a new element to the end has always O(log n) cost, though it is faster than in case of a SortedDictionaryTKey, TValue. Though, populating from a reverse ordered data has the worst possible performance, because every already existing element has to be shifted in the underlying arrays.
  • Enumerating the collection: Really fast, it is actually a traversal of arrays.
CircularSortedListTKey, TValue
  • Store model and memory: Keys and values are stored in separated CircularListT instances, which is a wrapper class around an array. This is a minimal overhead compared to the SortedListTKey, TValue class.
  • Insertion and removal: When inserting a new element, first of all it is checked, whether it comes to the last or first position. Due to the underlying CircularListT, inserting at the first/last position are O(1) operations. Removing an element from the last/first position by the Remove method has an O(log n) cost, because the item is found by binary search. However, removing the first or last element by the RemoveAt method is an O(1) operation. When an element is inserted/removed at any other position, it has generally O(n) cost, though the CircularSortedListTKey, TValue is designed so, that in worst case no more than half of the elements will be moved.
  • Populating from sorted data: Inserting element to the end or to the first position is O(1) cost, so it is faster than any other sorted dictionary types, even if populating from reverse ordered data.
  • Enumerating the collection: Really fast, it is actually a traversal of arrays.

Constructors

CircularSortedListTKey, TValue Creates a new instance of CircularSortedListTKey, TValue with empty capacity and a default comparer.
CircularSortedListTKey, TValue(IComparerTKey) Creates a new instance of CircularSortedListTKey, TValue with empty capacity, that uses the specified comparer.
CircularSortedListTKey, TValue(IDictionaryTKey, TValue, IComparerTKey) Creates a new instance of CircularSortedListTKey, TValue, that initializes its elements from the provided dictionary, and uses the specified comparer.
CircularSortedListTKey, TValue(Int32, IComparerTKey) Creates a new instance of CircularSortedListTKey, TValue, that is empty, and has the specified initial capacity, and uses the specified comparer.

Properties

AsList Gets the CircularSortedListTKey, TValue cast to an IListT.
Capacity Gets or sets the actual size of the internal storage of held elements.
Comparer Gets the IComparerT that is used in the CircularSortedListTKey, TValue.
Count Gets the number of key/value pairs contained in the CircularSortedListTKey, TValue.
Item Gets or sets the value associated with the specified key.
Keys Gets an indexable list containing the keys in the CircularSortedListTKey, TValue, in sorted order.
Values Gets an indexable list containing the values in the CircularSortedListTKey, TValue, in the order of the sorted keys.

Methods

Add Adds an element with the provided key and value to the CircularSortedListTKey, TValue.
Clear Removes all items from the CircularSortedListTKey, TValue.
ContainsKey Determines whether the CircularSortedListTKey, TValue contains an element with the specified key.
ContainsValue Determines whether the CircularSortedListTKey, TValue contains a specific value.
ElementAt Gets a KeyValuePairTKey, TValue of the TKey and TValue elements at the specified index.
EnsureCapacity Ensures that the Capacity of the CircularSortedListTKey, TValue is at least the specified capacity.
GetEnumerator Returns an enumerator that iterates through the collection in the order of the sorted keys.
GetKeyAtIndex Gets the key of an element at the specified index.
GetValueAtIndex Gets the value of an element at the specified index.
IndexOfKey Searches for the specified key and returns the zero-based index within the entire CircularSortedListTKey, TValue.
IndexOfValue Searches for the specified value and returns the zero-based index of the first occurrence within the entire CircularSortedListTKey, TValue.
Remove Removes the element with the specified key from the CircularSortedListTKey, TValue.
RemoveAt Removes the element at the specified index of the CircularSortedListTKey, TValue.
Reset Removes all items from the CircularSortedListTKey, TValue and resets the Capacity to 0.
SetValueAtIndex Sets the value of an element at the specified index.
TrimExcess Sets the capacity to the actual number of elements in the CircularSortedListTKey, TValue, if that number is less than 90 percent of current capacity.
TryGetValue Gets the value associated with the specified key.

Extension Methods

AddRangeKeyValuePairTKey, TValue Adds a collection to the target ICollectionT.
(Defined by CollectionExtensions)
AsThreadSafeKeyValuePairTKey, TValue Returns a LockingCollectionT, which provides a thread-safe wrapper for the specified collection. This only means that if the members are accessed through the returned LockingCollectionT, then the inner state of the wrapped collection remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingCollectionT class for details and some examples.
(Defined by CollectionExtensions)
AsThreadSafeKeyValuePairTKey, TValue Returns a LockingListT, which provides a thread-safe wrapper for the specified list. This only means that if the members are accessed through the returned LockingListT, then the inner state of the wrapped list remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingListT class for details and some examples.
(Defined by ListExtensions)
Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
ForEachKeyValuePairTKey, TValue Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions)
GetRandomElementKeyValuePairTKey, TValue Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions)
GetRandomElementKeyValuePairTKey, TValue Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
IndexOf Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IndexOf Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IndexOfKeyValuePairTKey, TValue Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IndexOfKeyValuePairTKey, TValue Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
InsertRangeKeyValuePairTKey, TValue Inserts a collection into the target IListT.
(Defined by ListExtensions)
IsNullOrEmpty Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
IsNullOrEmptyKeyValuePairTKey, TValue Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
JoinKeyValuePairTKey, TValue Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
JoinKeyValuePairTKey, TValue Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
RemoveRangeKeyValuePairTKey, TValue Removes count amount of items from the specified collection at the specified index.
(Defined by ListExtensions)
ReplaceRangeKeyValuePairTKey, TValue Removes count amount of items from the target IListT at the specified index, and inserts the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by ListExtensions)
ShuffleKeyValuePairTKey, TValue Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairTKey, TValue Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairTKey, TValue Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairTKey, TValue Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions)
ToCircularListKeyValuePairTKey, TValue Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryKeyValuePairTKey, TValue Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified keySelector delegate and a comparer.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryKeyValuePairTKey, TValue, TValue Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified key and value selector delegates and a comparer.
(Defined by EnumerableExtensions)
TryAdd Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddKeyValuePairTKey, TValue Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddRange Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryAddRangeKeyValuePairTKey, TValue Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryClear Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryClearKeyValuePairTKey, TValue Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryGetCount Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetCountKeyValuePairTKey, TValue Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetElementAt Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryGetElementAtKeyValuePairTKey, TValue Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryInsert Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertKeyValuePairTKey, TValue Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertRange Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryInsertRangeKeyValuePairTKey, TValue Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryRemove Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveKeyValuePairTKey, TValue Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveAt Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveAtKeyValuePairTKey, TValue Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveRange Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryRemoveRangeKeyValuePairTKey, TValue Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryReplaceRange Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions)
TryReplaceRangeKeyValuePairTKey, TValue Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions)
TrySetElementAt Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)
TrySetElementAtKeyValuePairTKey, TValue Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)

See Also