RankedDictionaryTKey, TValue Class |
Namespace: Kaos.Collections
[SerializableAttribute] public class RankedDictionary<TKey, TValue> : Btree<TKey>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, ISerializable, IDeserializationCallback
The RankedDictionaryTKey, TValue type exposes the following members.
Name | Description | |
---|---|---|
RankedDictionaryTKey, TValue | Initializes a new dictionary instance using the default key comparer. | |
RankedDictionaryTKey, TValue(IComparerTKey) | Initializes a new dictionary instance using the supplied key comparer. | |
RankedDictionaryTKey, TValue(IDictionaryTKey, TValue) | Initializes a new dictionary instance that contains key/value pairs copied from the supplied dictionary and sorted by the default comparer. | |
RankedDictionaryTKey, TValue(IDictionaryTKey, TValue, IComparerTKey) | Initializes a new dictionary instance that contains key/value pairs copied from the supplied dictionary and sorted by the supplied comparer. | |
RankedDictionaryTKey, TValue(SerializationInfo, StreamingContext) | Initializes a new dictionary instance that contains serialized data. |
Name | Description | |
---|---|---|
Capacity | Gets or sets the order of the underlying B+ tree structure. (Inherited from BtreeT.) | |
Comparer | Returns a wrapper of the method used to order elements in the dictionary. | |
Count | Gets the number of elements in the dictionary. | |
Item | Gets or sets the value associated with the supplied key. | |
Keys | Gets a RankedDictionaryTKey, TValueKeyCollection containing the keys of the dictionary. | |
MaxKey | Gets the maximum key in the dictionary per the comparer. | |
MinKey | Gets the minimum key in the dictionary per the comparer. | |
Values | Gets a RankedDictionaryTKey, TValueValueCollection containing the values of the dictionary. |
Name | Description | |
---|---|---|
Add | Adds an element with the supplied key and value. | |
Clear | Removes all elements from the dictionary. | |
ContainsKey | Determines whether the dictionary contains the supplied key. | |
ContainsValue | Determines whether the dictionary contains the supplied value. | |
CopyTo | Copies the dictionary to a compatible array, starting at the supplied position. | |
ElementAt | Gets the key/value pair at the supplied index. | |
ElementAtOrDefault | Gets the key/value pair at the supplied index or the default if the index is out of range. | |
ElementsBetween | Returns an enumerator that iterates over a range with the supplied bounds. | |
ElementsBetweenIndexes | Returns an enumerator that iterates over a range with the supplied index bounds. | |
ElementsFrom | Returns an enumerator that iterates over a range with the supplied lower bound. | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
First | Gets the element with the minimum key in the dictionary per the comparer. | |
GetEnumerator | Gets an enumerator that iterates thru the dictionary. | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetObjectData | Returns the data needed to serialize the dictionary. | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
IndexOfKey | Gets the index of the element with the supplied key. | |
IndexOfValue | Gets the index of the first element with the supplied value. | |
Last | Gets the element with the maximum key in the dictionary per the comparer. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
OnDeserialization | Implements the deserialization callback and raises the deserialization event when completed. | |
Remove | Removes an element with the supplied key from the dictionary. | |
RemoveAll |
Removes all elements with keys in the supplied collection from the dictionary.
| |
RemoveAt | Removes an element at the supplied index from the dictionary. | |
RemoveRange | Removes an index range of elements from the dictionary. | |
RemoveWhere | Removes all elements from the dictionary that match the condition defined by the supplied key-parameterized predicate. | |
RemoveWhereElement | Removes all elements from the dictionary that match the condition defined by the supplied key/value-parameterized predicate. | |
Reverse | Returns an enumerator that iterates thru the dictionary in reverse order. | |
Skip | Bypasses a supplied number of elements and yields the remaining elements. | |
SkipWhile(FuncKeyValuePairTKey, TValue, Boolean) |
Bypasses elements as long as a supplied condition is true and yields the remaining elements.
| |
SkipWhile(FuncKeyValuePairTKey, TValue, Int32, Boolean) |
Bypasses elements as long as a supplied index-based condition is true and yields the remaining elements.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
TryGetGreaterThan | Gets the element with the least key greater than the supplied key. | |
TryGetGreaterThanOrEqual | Gets the element with the least key greater than or equal to the supplied key. | |
TryGetLessThan | Gets the element with the greatest key less than the supplied key. | |
TryGetLessThanOrEqual | Gets the element with the greatest key less than or equal to the supplied key. | |
TryGetValue | Gets the value associated with the supplied key. | |
TryGetValueAndIndex | Gets the value and index associated with the supplied key. |
Name | Description | |
---|---|---|
ICollectionKeyValuePairTKey, TValueAdd | Adds an element with the supplied key/value pair. | |
IDictionaryAdd | Adds the supplied key and value to the dictionary. | |
ICollectionKeyValuePairTKey, TValueContains | Determines if the collection contains the supplied key/value pair. | |
IDictionaryContains | Determines whether the dictionary contains an element with the supplied key. | |
ICollectionCopyTo | Copies the elements of the collection to an array, starting at the supplied array index. | |
IEnumerableKeyValuePairTKey, TValueGetEnumerator | Gets an enumerator that iterates thru the dictionary. | |
IDictionaryGetEnumerator | Gets an enumerator that iterates thru the collection. | |
IEnumerableGetEnumerator | Gets an enumerator that iterates thru the collection. | |
ISerializableGetObjectData | Returns the data needed to serialize the dictionary. | |
IDictionaryIsFixedSize | Indicating that the dictionary is not fixed size. | |
ICollectionKeyValuePairTKey, TValueIsReadOnly | Indicates that this collection may be modified. | |
IDictionaryIsReadOnly | Indicates that the dictionary may be modified. | |
ICollectionIsSynchronized | Indicates that the collection is not thread safe. | |
IDictionaryItem | Gets or sets the value associated with the supplied key. | |
IDictionaryTKey, TValueKeys | Gets only the collection of keys from this key/value pair collection. | |
IReadOnlyDictionaryTKey, TValueKeys | Gets a RankedDictionaryTKey, TValueKeyCollection containing the keys in the dictionary. | |
IDictionaryKeys | Gets an ICollection containing the keys of the dictionary. | |
IDeserializationCallbackOnDeserialization | Implements the deserialization callback and raises the deserialization event when completed. | |
ICollectionKeyValuePairTKey, TValueRemove | Removes an element with the supplied key and value from the collection. | |
IDictionaryRemove | Removes an element with the supplied key from the dictionary. | |
ICollectionSyncRoot | Gets an object that can be used to synchronize access to the collection. | |
IDictionaryTKey, TValueValues | Gets only the collection of values from this key/value pair collection. | |
IReadOnlyDictionaryTKey, TValueValues | Gets a RankedDictionaryTKey, TValueValueCollection containing the values in the dictionary. | |
IDictionaryValues | Gets an ICollection containing the values of the dictionary. |
This class emulates and extends SortedDictionaryTKey, TValuewhile significantly improving performance on large collections. Keys must be immutable as long as they are used as keys in the RankedDictionaryTKey, TValueclass. Every key must be distinct and cannot be null, but a value can be for a reference type.
Optimized instance methods with the signatures of LINQ methods have been implemented:
This class also borrows from SortedListTKey, TValuefor indexing functionality:
These properties and methods are inspired by SortedSetT:
Optimized range enumerators are included:
First is an example of some common operations of this class.
using System; using System.Collections.Generic; using Kaos.Collections; namespace ExampleApp { class RdExample01 { static void Main() { // Instantiate with four ISO 3166-1 country codes: var cc = new RankedDictionary<string,string> { { "TO", "Tonga" }, { "DD", "German Democratic Republic" }, { "CH", "Switzerland" }, { "RU", "Burundi" } }; // The Add method throws an exception if the new key is // already in the dictionary. try { cc.Add ("DD", "East Germany"); } catch (ArgumentException) { Console.WriteLine ("An element with Key = 'DD' already exists."); } // The Item property is another name for the indexer, // so you can omit its name when accessing elements. Console.WriteLine ($"For key = 'CH', value = {cc["CH"]}."); // The indexer can be used to change the value associated with a key. cc["RU"] = "Russian Federation"; // The indexer can be used to get a value for a key. Console.WriteLine ($"For key = 'RU', value = {cc["RU"]}."); // If a key does not exist, setting the indexer for that key // adds a new key/value pair. cc["SS"] = "South Sudan"; // The indexer throws an exception if the supplied key is // not in the dictionary. try { Console.WriteLine ($"For key = 'ZZ', value = {cc["ZZ"]}."); } catch (KeyNotFoundException) { Console.WriteLine ("Key = 'ZZ' is not found."); } // When a program often has to try keys that are usually not in the // dictionary, TryGetValue can be a more efficient way to get values. if (cc.TryGetValue ("ZZ", out string value)) Console.WriteLine ($"For key = 'ZZ', value = {value}."); else Console.WriteLine ("Key = 'ZZ' is not found."); // ContainsKey can be used to test keys before inserting them. if (! cc.ContainsKey ("GG")) { cc.Add ("GG", "Guernsey"); Console.WriteLine ($"Value added for key = 'GG': {cc["GG"]}"); } // When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair instances. Console.WriteLine(); foreach (KeyValuePair<string,string> pair in cc) Console.WriteLine ($"Key = {pair.Key}, Value = {pair.Value}"); // To get the values alone, use the Values property. RankedDictionary<string,string>.ValueCollection vals = cc.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); foreach(string val in vals) Console.WriteLine ($"Value = {val}"); // To get the keys alone, use the Keys property. RankedDictionary<string,string>.KeyCollection keys = cc.Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary keys. Console.WriteLine(); foreach (string key in keys) Console.WriteLine ($"Key = {key}"); // Use the Remove method to remove a key/value pair. Console.WriteLine ("\nRemoving 'DD'."); cc.Remove ("DD"); Console.WriteLine ("\nChecking if 'DD' exists:"); if (! cc.ContainsKey ("DD")) Console.WriteLine (" Key 'DD' not found."); } /* Output: An element with Key = 'DD' already exists. For key = 'CH', value = Switzerland. For key = 'RU', value = Russian Federation. Key = 'ZZ' is not found. Key = 'ZZ' is not found. Value added for key = 'GG': Guernsey Key = CH, Value = Switzerland Key = DD, Value = German Democratic Republic Key = GG, Value = Guernsey Key = RU, Value = Russian Federation Key = SS, Value = South Sudan Key = TO, Value = Tonga Value = Switzerland Value = German Democratic Republic Value = Guernsey Value = Russian Federation Value = South Sudan Value = Tonga Key = CH Key = DD Key = GG Key = RU Key = SS Key = TO Removing 'DD'. Checking if 'DD' exists: Key 'DD' not found. */ } }
Next is an example showing binary serialization round tripped.
Note: Serialization is not supported in .NET Standard 1.0.
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using Kaos.Collections; namespace ExampleApp { [Serializable] public class PersonComparer : Comparer<Person> { public override int Compare (Person x, Person y) { return x==null? (y==null? 0 : -1) : (y==null? 1 : String.Compare (x.ToString(), y.ToString())); } } [Serializable] public class Person : ISerializable { public string First { get; private set; } public string Last { get; private set; } public Person (string first, string last) { this.First = first; this.Last = last; } protected Person (SerializationInfo info, StreamingContext context) { this.First = (string) info.GetValue ("First", typeof (String)); this.Last = (string) info.GetValue ("Last", typeof (String)); } public virtual void GetObjectData (SerializationInfo info, StreamingContext context) { info.AddValue ("First", First, typeof (String)); info.AddValue ("Last", Last, typeof (String)); } public override string ToString() => Last + ", " + First; } class RdExample05 { static void Main() { IFormatter formatter = new BinaryFormatter(); var set1 = new RankedDictionary<Person,string> (new PersonComparer()); set1.Add (new Person ("Hugh", "Mann"), "B+"); set1.Add (new Person ("Hammond", "Egger"), "C-"); SerializePersons ("Persons.bin", set1, formatter); Console.WriteLine ($"Wrote {set1.Count} key/value pairs."); Console.WriteLine (); RankedDictionary<Person,string> set2 = DeserializePersons ("Persons.bin", formatter); Console.WriteLine ("Read back:"); foreach (var kv in set2) Console.WriteLine (kv); } public static void SerializePersons (string fn, RankedDictionary<Person,string> set, IFormatter formatter) { using (var fs = new FileStream (fn, FileMode.Create)) { formatter.Serialize (fs, set); } } static RankedDictionary<Person,string> DeserializePersons (string fn, IFormatter formatter) { using (var fs = new FileStream (fn, FileMode.Open)) { return (RankedDictionary<Person,string>) formatter.Deserialize (fs); } } /* Output: Wrote 2 key/value pairs. Read back: [Egger, Hammond, C-] [Mann, Hugh, B+] */ } }