Last active
April 1, 2024 20:18
-
-
Save Nucs/44687fd878926b13e7c7a0a250030a9c to your computer and use it in GitHub Desktop.
A technique to define a dictionary with any kind of keys combination, supporting items with N keys and M keys. All works as long as they implement Equals and GetHashcode, or using IEquitable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.CompilerServices; | |
| using System.Runtime.Serialization; | |
| var specificCombinationDictionary = new MultikeyDictionary<(int,int), string>(); | |
| specificCombinationDictionary[(1, 2)] = "3"; | |
| var anyKeyDictionary = new MultikeyDictionary<ITuple, string>(); | |
| anyKeyDictionary[(1, 2)] = "3"; | |
| anyKeyDictionary[("str", DateTime.UtcNow.Date)] = "undefined err"; | |
| anyKeyDictionary[(1, 3, 5, "what? a string?!", ("sub?", "Tuple??"))] = "undefined err"; | |
| public class MultikeyDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TKey : ITuple { | |
| public MultikeyDictionary() { } | |
| public MultikeyDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary) { } | |
| public MultikeyDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer) : base(dictionary, comparer) { } | |
| public MultikeyDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) : base(collection) { } | |
| public MultikeyDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer) : base(collection, comparer) { } | |
| public MultikeyDictionary(IEqualityComparer<TKey>? comparer) : base(comparer) { } | |
| public MultikeyDictionary(int capacity) : base(capacity) { } | |
| public MultikeyDictionary(int capacity, IEqualityComparer<TKey>? comparer) : base(capacity, comparer) { } | |
| protected MultikeyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment