Dictionary with Tuple as Key GetHashCode is not called - c#

Description
Trying to create a Dictionary with Tuple as the key.
However the GetHashCode and Equals functions are not being called, hence duplicate keys will be added to the dictionary.
This is the Keyclass that I want to use as my Dictionary's key:
class Key : IEqualityComparer<Tuple<int, int>>
{
private Tuple<int, int> _tuple;
public Key(int a, int b)
{
_tuple = new Tuple<int, int>(a, b);
}
public bool Equals(Tuple<int, int> x, Tuple<int, int> y)
{
return (x.Item1 == y.Item1 && x.Item2 == y.Item2);
}
public int GetHashCode(Tuple<int, int> obj)
{
return obj.Item1.GetHashCode() ^ obj.Item2.GetHashCode();
}
}
Driver code:
public static void Main() {
var map = new Dictionary<Key, int>();
map.Add(new Key(1, 2), 3);
map.Add(new Key(1, 2), 4); // <==== Should not add!
}
Questions
How to fix this?
What is the easiest implementation for Dictionary<Tuple<int, int>, int> to work properly?

Another approach is to use ValueTuple as a key, which will be compared by it's values by default.
public static void Main()
{
var map = new Dictionary<(int, int), int>();
map.Add((1, 2), 3);
map.Add((1, 2), 4); // Throw an exception
}
If you like to have own class to represent a key you can simply create subclass of Tuple<int, int> and get required behaviour "for free"
public class Key : Tuple<int, int>
{
public Key(int item1, int item2) : base(item1, item2)
{
}
}

If you want to use own class Key:
public class Key
{
public Key(int item1, int item2)
{
Tuple = new Tuple<int, int>(item1, item2);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is Key other)
{
return Tuple.Equals(other.Tuple);
}
return false;
}
public override int GetHashCode()
{
return Tuple.GetHashCode();
}
public Tuple<int, int> Tuple { get; private set; }
}
public void Do()
{
var map = new Dictionary<Key, int>();
map.Add(new Key(1, 2), 3);
map.Add(new Key(1, 2), 4); // will throw System.ArgumentException
}
Another way is just using Tuple class:
public void Do()
{
var map = new Dictionary<Tuple<int, int>, int>();
map.Add(new Tuple<int, int>(1, 2), 3);
map.Add(new Tuple<int, int>(1, 2), 4); // will throw System.ArgumentException
}

The problem is that, when adding an item to the dictionary, the default Equals and GetHashCode methods are being called, which use a reference comparison to determine equality.
If you want to override this behavior, then you need to use the override keyword, and override the method:
class Key : IEquatable<Key>
{
private readonly Tuple<int, int> tuple;
public Key(int a, int b)
{
tuple = new Tuple<int, int>(a, b);
}
public bool Equals(Key other)
{
return other != null &&
tuple.Item1 == other.tuple.Item1 &&
tuple.Item2 == other.tuple.Item2;
}
public override bool Equals(object obj)
{
return Equals(obj as Key);
}
public override int GetHashCode()
{
return tuple.Item1.GetHashCode() ^ tuple.Item2.GetHashCode();
}
}

You could try following solution.
public class Key : IEquatable<Key>
{
private Tuple<int, int> _tuple;
public Key(int a, int b)
{
_tuple = new Tuple<int, int>(a, b);
}
public bool Equals(Key other)
{
return (this.GetHashCode() == other.GetHashCode());
}
public override int GetHashCode()
{
return _tuple.GetHashCode();
}
}

Just posting a simplified version of #Dmitri's answer as a reference here.
The simplest way (without installing extra packages) and not implementing any interfaces, is to just override the Equals and GetHashCode methods as follows:
public class Key
{
private readonly Tuple<int, int> _tuple;
public Key(int item1, int item2)
{
_tuple = new Tuple<int, int>(item1, item2);
}
public override bool Equals(object obj)
{
var other = obj as Key;
return _tuple.Item1 == other?._tuple.Item1 && _tuple.Item2 == other?._tuple.Item2;
}
public override int GetHashCode()
{
return _tuple.Item1.GetHashCode() ^ _tuple.Item2.GetHashCode();
}
}

Related

KeyValueComparer based on key

I have 2 key value comparers - in both I am using key to compare, which is string.
Can I use generic for the value - so instead of have 2 classes I can have only 1?
So instead of the classes below I wish I could have only 1 class
class KvpComparer : IEqualityComparer < KeyValuePair < string, T>>
{
...
}
class KvpComparer : IEqualityComparer<KeyValuePair<string, Tuple<string, string>>>
{
public bool Equals(KeyValuePair<string, Tuple<string, string>> x, KeyValuePair<string, Tuple<string, string>> y)
{
return x.Key.Equals(y.Key);
}
public int GetHashCode(KeyValuePair<string, Tuple<string, string>> obj)
{
return obj.Key.GetHashCode();
}
}
class KvpComparer2 : IEqualityComparer<KeyValuePair<string, string>>
{
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
return x.Key.Equals(y.Key);
}
public int GetHashCode(KeyValuePair<string, string> obj)
{
return obj.Key.GetHashCode();
}
}
As the comparers differ only in the value type of the KeyValuePair, you could use a type parameter for the value type to generalize the two comparers into a single generic type as follows.
class KvpComparer<T> : IEqualityComparer<KeyValuePair<string,T>> where T : class
{
public bool Equals(KeyValuePair<string,T> x, KeyValuePair<string,T> y)
{
return x.Key.Equals(y.Key);
}
public int GetHashCode(KeyValuePair<string, T> obj)
{
return obj.Key.GetHashCode();
}
}

ContainsKey in dictionary of hashset<myClass>

I have a dictionary:
Dictionary<HashSet<myClass>, List<MyObj>> myDict = ...
And I have:
HashSet<myClass> myHashSet = ...
I want to check if the dictionary (myDict) contains myHashSet.
I tried to override two methods:
1) equal
2) GetHashCode
public class myClass
{
public string id;
public int number;
public override bool Equals(object obj)
{
myClass other = obj as myClass;
bool ret = false;
if (other != null)
{
ret = (this.number == other.number) && (this.id == other.id);
}
return ret;
}
public override int GetHashCode()
{
return this.number ^ this.id.GetHashCode();
}
};
Unfortunately, a key that is found in dictionary, returns false for the code:
myDict.ContainsKey(myHashSet)
Any help appreciated!
Just because you overrode myClass's Equals( and GetHashCode() does not mean that that you overrode HashSet<myClass>'s Equals( and GetHashCode(), that is what is being used when you do the dictionary lookup.
If you want it to work you need to pass a IEqualityComparer<HashSet<myClass>> in to the constructor of the dictionary so it will use that comparer when doing the dictionary lookup.
public class myClassSetComperer : IEqualityComparer<HashSet<myClass>>
{
public bool Equals(HashSet<myClass> x, HashSet<myClass> y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(null, x)) return false;
if (ReferenceEquals(null, y)) return false;
return x.SetEquals(y);
}
public int GetHashCode(HashSet<myClass> obj)
{
unchecked
{
int x = 0;
foreach (var myClass in obj)
{
x = (x*397) ^ myClass?.GetHashCode() ?? 0;
}
return x;
}
}
}
//elsewhere
Dictionary<HashSet<myClass>, List<MyObj>> myDict = new Dictionary<HashSet<myClass>, List<MyObj>>(new myClassSetComperer());
VERY IMPORTANT NOTE: Dictionary keys (and hash sets) break horribly if you do anything that causes Equals( or GetHashCode() to change once put in as the lookup key. If you modify the HashSet<myClass> or one of the myClass objects after you put it in the dictionary you will break the dictionary and potentially the HashSet. See this very good blog post by Eric Lippert on "Guidelines and rules for GetHashCode"
overriding the GetHasCode and Equal is when comparing instances of myClass.
Here's a sample of using ContainsKey, which is checking by objects reference.
Dictionary<HashSet<string>, List<string>> hashSetDictionary = new Dictionary<HashSet<string>, List<string>>();
var myHashSet = new HashSet<string>();
hashSetDictionary.Add(myHashSet, null);
Console.WriteLine(hashSetDictionary.ContainsKey(myHashSet));
Here's an update to your code
public class myClass
{
public myClass(string text, int num)
{
this.Text = text;
this.Num = num;
}
public string Text { get; set; }
public int Num { get; set; }
}
public class MyObj { }
public class AlwaysTrueHashSet<T> : HashSet<T>
{
public override bool Equals(object obj)
{
return this.GetHashCode() == obj.GetHashCode();
}
public override int GetHashCode()
{
return "Counting hashcode".GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<HashSet<myClass>, List<MyObj>> myDict = new Dictionary<HashSet<myClass>,
List<MyObj>>();
var myHashSet1 = new AlwaysTrueHashSet<myClass>();
myHashSet1.Add(new myClass("123", 5));
myDict.Add(myHashSet1, null);
var myHashSet2 = new AlwaysTrueHashSet<myClass>();
myHashSet2.Add(new myClass("123", 5));
/*
* when containsKey is invoked, it's checking if the reference of myHashSet2 is the same as myHashSet1.
* That's the default behavior.
*
* extend HashSet, and override the gethashcode and equal methods
*/
if (myDict.ContainsKey(myHashSet2))
{
Console.WriteLine("in");
int i = 3; // it doesn't get this line }
}
}
}

Get the reference of a collection given a type

I have several dictionaries:
Dictionary<int, Type1> Type1Dictionary { get; set; }
Dictionary<int, Type2> Type2Dictionary { get; set; }
Dictionary<int, Type3> Type3Dictionary { get; set; }
Dictionary<int, Type4> Type4Dictionary { get; set; }
Where Typei (i = 1..4) are derived from the same base class (BaseType). I want a method that returns the reference of a dictionary given a type. Later, I will perform some operations like Add or Remove on that dictionary:
Type1 example = new Type1();
var dic = GetDictionary(example);
dic.Add(example.ID, example);
Notes: I don't want to set my dictionaries as Dictionary<int, BaseType>
I could write something like this but that wouldn't return a reference to the dictionary:
Dictionary<int, BaseType> GetDictionary(BaseType myObject)
{
var dic = new Dictionary<int, BaseType>();
if(myObject is Type1)
{
//ideally I would return my Type1Dictionary here but I can't due type incompatibility
foreach(var x in Type1Dictionary)
{
dic.Add(x.Key, x.Value);
}
return dic;
}
if(myObject is Type2) { /*...*/ }
if(myObject is Type3) { /*...*/ }
if(myObject is Type4) { /*...*/ }
}
EDIT:
What I really want is to avoid the following structure:
AddObject(BaseType x)
{
Type1 x1 = x as Type1;
if(x1 != null) { Type1Dictionary.Add(x1.ID, x1); }
Type2 x2 = x as Type2;
if(x2 != null) { Type2Dictionary.Add(x2.ID, x2); }
Type3 x3 = x as Type3;
if(x3 != null) { Type3Dictionary.Add(x3.ID, x3); }
Type4 x4 = x as Type4;
if(x4 != null) { Type4Dictionary.Add(x4.ID, x4); }
}
RemoveObject(BaseType x)
{
Type1 x1 = x as Type1;
if(x1 != null) { Type1Dictionary.Remove(x1.ID); }
Type2 x2 = x as Type2;
if(x2 != null) { Type2Dictionary.Remove(x2.ID); }
Type3 x3 = x as Type3;
if(x3 != null) { Type3Dictionary.Remove(x3.ID); }
Type4 x4 = x as Type4;
if(x4 != null) { Type4Dictionary.Remove(x4.ID); }
}
But instead:
AddObject(BaseType x)
{
var dic = GetDictionary(x);
dic.Add(x.ID, x);
}
RemoveObject(BaseType x)
{
var dic = GetDictionary(x);
dic.Remove(x.ID);
}
This can be polished in the terms of thead safety, etc. But you should be able to get the basic idea:
public interface IEntity
{
int ID { get; }
}
public class Superset<T> where T : IEntity
{
public Dictionary<Type, Dictionary<int, T>> m_Map =
new Dictionary<Type, Dictionary<int, T>>();
private Dictionary<int, T> GetDictionary(Type t)
{
Dictionary<int, T> result = null;
if (!m_Map.TryGetValue(t, out result))
{
result = new Dictionary<int, T>();
m_Map.Add(t, result);
}
return result;
}
public void Add<K>(K item) where K : T
{
GetDictionary(typeof(K)).Add(item.ID, item);
}
public bool Remove<K>(K item) where K : T
{
return GetDictionary(typeof(K)).Remove(item.ID);
}
}
The DLR can play these tricks: dynamic dispatch. It resolves the required method at runtime. This allows you to have many dictionaries of strong types, but a more generalised handling mechanism. I do this for handling events that come from a common event base.
class Program
{
private static Dictionary<int, Foo> _foos = new Dictionary<int, Foo>();
private static Dictionary<int, Baz> _bazs = new Dictionary<int, Baz>();
static void Main(string[] args)
{
Bar foo = new Foo();
Bar baz = new Baz();
Add(foo); // Resolves at runtime to Add(Foo f)
Add(baz); // Resolves at runtime to Add(Baz b)
}
public static void Add(Bar b)
{
Add((dynamic)b);
}
private static void Add(Foo f)
{
_foos.Add(1, f);
}
private static void Add(Baz b)
{
_bazs.Add(1, b);
}
}
class Foo : Bar
{
}
class Baz : Bar
{
}
class Bar
{
}
Moving stuff into the DLR comes with its own pitfalls (namely what were compile-time problems are now runtime problems), so this needs reviewing. It is just one approach. Another approach is an implementation of the visitor pattern. Another approach is what you've currently got.
Don't take this answer as the way to do it.

Two-way / bidirectional Dictionary in C#?

I want to store words in a dictionary in following way:
I can get word code by word: dict["SomeWord"] -> 123 and get word by word code: dict[123] -> "SomeWord"
Is it real? Of course one way to do it is two dictionaries: Dictionary<string,int> and Dictionary<int,string> but is there another way?
I wrote a quick couple of classes that lets you do what you want. You'd probably need to extend it with more features, but it is a good starting point.
The use of the code looks like this:
var map = new Map<int, string>();
map.Add(42, "Hello");
Console.WriteLine(map.Forward[42]);
// Outputs "Hello"
Console.WriteLine(map.Reverse["Hello"]);
//Outputs 42
Here's the definition:
public class Map<T1, T2>
{
private Dictionary<T1, T2> _forward = new Dictionary<T1, T2>();
private Dictionary<T2, T1> _reverse = new Dictionary<T2, T1>();
public Map()
{
this.Forward = new Indexer<T1, T2>(_forward);
this.Reverse = new Indexer<T2, T1>(_reverse);
}
public class Indexer<T3, T4>
{
private Dictionary<T3, T4> _dictionary;
public Indexer(Dictionary<T3, T4> dictionary)
{
_dictionary = dictionary;
}
public T4 this[T3 index]
{
get { return _dictionary[index]; }
set { _dictionary[index] = value; }
}
}
public void Add(T1 t1, T2 t2)
{
_forward.Add(t1, t2);
_reverse.Add(t2, t1);
}
public Indexer<T1, T2> Forward { get; private set; }
public Indexer<T2, T1> Reverse { get; private set; }
}
Regrettably, you need two dictionaries, one for each direction. However, you can easily get the inverse dictionary using LINQ:
Dictionary<T1, T2> dict = new Dictionary<T1, T2>();
Dictionary<T2, T1> dictInverse = dict.ToDictionary((i) => i.Value, (i) => i.Key);
Expanded on Enigmativity code by adding initializes and Contains method.
public class Map<T1, T2> : IEnumerable<KeyValuePair<T1, T2>>
{
private readonly Dictionary<T1, T2> _forward = new Dictionary<T1, T2>();
private readonly Dictionary<T2, T1> _reverse = new Dictionary<T2, T1>();
public Map()
{
Forward = new Indexer<T1, T2>(_forward);
Reverse = new Indexer<T2, T1>(_reverse);
}
public Indexer<T1, T2> Forward { get; private set; }
public Indexer<T2, T1> Reverse { get; private set; }
public void Add(T1 t1, T2 t2)
{
_forward.Add(t1, t2);
_reverse.Add(t2, t1);
}
public void Remove(T1 t1)
{
T2 revKey = Forward[t1];
_forward.Remove(t1);
_reverse.Remove(revKey);
}
public void Remove(T2 t2)
{
T1 forwardKey = Reverse[t2];
_reverse.Remove(t2);
_forward.Remove(forwardKey);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<T1, T2>> GetEnumerator()
{
return _forward.GetEnumerator();
}
public class Indexer<T3, T4>
{
private readonly Dictionary<T3, T4> _dictionary;
public Indexer(Dictionary<T3, T4> dictionary)
{
_dictionary = dictionary;
}
public T4 this[T3 index]
{
get { return _dictionary[index]; }
set { _dictionary[index] = value; }
}
public bool Contains(T3 key)
{
return _dictionary.ContainsKey(key);
}
}
}
Here is a use case, check valid parentheses
public static class ValidParenthesisExt
{
private static readonly Map<char, char>
_parenthesis = new Map<char, char>
{
{'(', ')'},
{'{', '}'},
{'[', ']'}
};
public static bool IsValidParenthesis(this string input)
{
var stack = new Stack<char>();
foreach (var c in input)
{
if (_parenthesis.Forward.Contains(c))
stack.Push(c);
else
{
if (stack.Count == 0) return false;
if (_parenthesis.Reverse[c] != stack.Pop())
return false;
}
}
return stack.Count == 0;
}
}
You could use two dictionaries, as others have said, but note also that if both TKey and TValue are the of same type (and their runtime value domains are known to be disjoint) then you can just use the same dictionary by creating two entries for each key/value pairing:
dict["SomeWord"]= "123" and dict["123"]="SomeWord"
This way a single dictionary can be used for either type of lookup.
What the heck, I'll throw my version into the mix:
public class BijectiveDictionary<TKey, TValue>
{
private EqualityComparer<TKey> _keyComparer;
private Dictionary<TKey, ISet<TValue>> _forwardLookup;
private EqualityComparer<TValue> _valueComparer;
private Dictionary<TValue, ISet<TKey>> _reverseLookup;
public BijectiveDictionary()
: this(EqualityComparer<TKey>.Default, EqualityComparer<TValue>.Default)
{
}
public BijectiveDictionary(EqualityComparer<TKey> keyComparer, EqualityComparer<TValue> valueComparer)
: this(0, EqualityComparer<TKey>.Default, EqualityComparer<TValue>.Default)
{
}
public BijectiveDictionary(int capacity, EqualityComparer<TKey> keyComparer, EqualityComparer<TValue> valueComparer)
{
_keyComparer = keyComparer;
_forwardLookup = new Dictionary<TKey, ISet<TValue>>(capacity, keyComparer);
_valueComparer = valueComparer;
_reverseLookup = new Dictionary<TValue, ISet<TKey>>(capacity, valueComparer);
}
public void Add(TKey key, TValue value)
{
AddForward(key, value);
AddReverse(key, value);
}
public void AddForward(TKey key, TValue value)
{
ISet<TValue> values;
if (!_forwardLookup.TryGetValue(key, out values))
{
values = new HashSet<TValue>(_valueComparer);
_forwardLookup.Add(key, values);
}
values.Add(value);
}
public void AddReverse(TKey key, TValue value)
{
ISet<TKey> keys;
if (!_reverseLookup.TryGetValue(value, out keys))
{
keys = new HashSet<TKey>(_keyComparer);
_reverseLookup.Add(value, keys);
}
keys.Add(key);
}
public bool TryGetReverse(TValue value, out ISet<TKey> keys)
{
return _reverseLookup.TryGetValue(value, out keys);
}
public ISet<TKey> GetReverse(TValue value)
{
ISet<TKey> keys;
TryGetReverse(value, out keys);
return keys;
}
public bool ContainsForward(TKey key)
{
return _forwardLookup.ContainsKey(key);
}
public bool TryGetForward(TKey key, out ISet<TValue> values)
{
return _forwardLookup.TryGetValue(key, out values);
}
public ISet<TValue> GetForward(TKey key)
{
ISet<TValue> values;
TryGetForward(key, out values);
return values;
}
public bool ContainsReverse(TValue value)
{
return _reverseLookup.ContainsKey(value);
}
public void Clear()
{
_forwardLookup.Clear();
_reverseLookup.Clear();
}
}
Add some data to it:
var lookup = new BijectiveDictionary<int, int>();
lookup.Add(1, 2);
lookup.Add(1, 3);
lookup.Add(1, 4);
lookup.Add(1, 5);
lookup.Add(6, 2);
lookup.Add(6, 8);
lookup.Add(6, 9);
lookup.Add(6, 10);
And then do the lookup:
lookup[2] --> 1, 6
lookup[3] --> 1
lookup[8] --> 6
You can use this extension method, although it uses enumeration, and thus may not be as performant for large data sets. If you are worried about efficiency, then you need two dictionaries. If you want to wrap the two dictionaries into one class, see the accepted answer for this question: Bidirectional 1 to 1 Dictionary in C#
public static class IDictionaryExtensions
{
public static TKey FindKeyByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
foreach (KeyValuePair<TKey, TValue> pair in dictionary)
if (value.Equals(pair.Value)) return pair.Key;
throw new Exception("the value is not found in the dictionary");
}
}
I made an expanded version of Enigmativity's answer available as a nuget package
https://www.nuget.org/packages/BidirectionalMap/
It is open sourced here
A modified version of Xavier John's answer, with an additional constructor to take forward and reverse Comparers. This would support case-insensitive keys, for example. Further constructors could be added, if needed, to pass further arguments to the forward and reverse Dictionary constructors.
public class Map<T1, T2> : IEnumerable<KeyValuePair<T1, T2>>
{
private readonly Dictionary<T1, T2> _forward;
private readonly Dictionary<T2, T1> _reverse;
/// <summary>
/// Constructor that uses the default comparers for the keys in each direction.
/// </summary>
public Map()
: this(null, null)
{
}
/// <summary>
/// Constructor that defines the comparers to use when comparing keys in each direction.
/// </summary>
/// <param name="t1Comparer">Comparer for the keys of type T1.</param>
/// <param name="t2Comparer">Comparer for the keys of type T2.</param>
/// <remarks>Pass null to use the default comparer.</remarks>
public Map(IEqualityComparer<T1> t1Comparer, IEqualityComparer<T2> t2Comparer)
{
_forward = new Dictionary<T1, T2>(t1Comparer);
_reverse = new Dictionary<T2, T1>(t2Comparer);
Forward = new Indexer<T1, T2>(_forward);
Reverse = new Indexer<T2, T1>(_reverse);
}
// Remainder is the same as Xavier John's answer:
// https://stackoverflow.com/a/41907561/216440
...
}
Usage example, with a case-insensitive key:
Map<int, string> categories =
new Map<int, string>(null, StringComparer.CurrentCultureIgnoreCase)
{
{ 1, "Bedroom Furniture" },
{ 2, "Dining Furniture" },
{ 3, "Outdoor Furniture" },
{ 4, "Kitchen Appliances" }
};
int categoryId = 3;
Console.WriteLine("Description for category ID {0}: '{1}'",
categoryId, categories.Forward[categoryId]);
string categoryDescription = "DINING FURNITURE";
Console.WriteLine("Category ID for description '{0}': {1}",
categoryDescription, categories.Reverse[categoryDescription]);
categoryDescription = "outdoor furniture";
Console.WriteLine("Category ID for description '{0}': {1}",
categoryDescription, categories.Reverse[categoryDescription]);
// Results:
/*
Description for category ID 3: 'Outdoor Furniture'
Category ID for description 'DINING FURNITURE': 2
Category ID for description 'outdoor furniture': 3
*/
Here's my code. Everything is O(1) except for the seeded constructors.
using System.Collections.Generic;
using System.Linq;
public class TwoWayDictionary<T1, T2>
{
Dictionary<T1, T2> _Forwards = new Dictionary<T1, T2>();
Dictionary<T2, T1> _Backwards = new Dictionary<T2, T1>();
public IReadOnlyDictionary<T1, T2> Forwards => _Forwards;
public IReadOnlyDictionary<T2, T1> Backwards => _Backwards;
public IEnumerable<T1> Set1 => Forwards.Keys;
public IEnumerable<T2> Set2 => Backwards.Keys;
public TwoWayDictionary()
{
_Forwards = new Dictionary<T1, T2>();
_Backwards = new Dictionary<T2, T1>();
}
public TwoWayDictionary(int capacity)
{
_Forwards = new Dictionary<T1, T2>(capacity);
_Backwards = new Dictionary<T2, T1>(capacity);
}
public TwoWayDictionary(Dictionary<T1, T2> initial)
{
_Forwards = initial;
_Backwards = initial.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
public TwoWayDictionary(Dictionary<T2, T1> initial)
{
_Backwards = initial;
_Forwards = initial.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
public T1 this[T2 index]
{
get => _Backwards[index];
set
{
if (_Backwards.TryGetValue(index, out var removeThis))
_Forwards.Remove(removeThis);
_Backwards[index] = value;
_Forwards[value] = index;
}
}
public T2 this[T1 index]
{
get => _Forwards[index];
set
{
if (_Forwards.TryGetValue(index, out var removeThis))
_Backwards.Remove(removeThis);
_Forwards[index] = value;
_Backwards[value] = index;
}
}
public int Count => _Forwards.Count;
public bool Contains(T1 item) => _Forwards.ContainsKey(item);
public bool Contains(T2 item) => _Backwards.ContainsKey(item);
public bool Remove(T1 item)
{
if (!this.Contains(item))
return false;
var t2 = _Forwards[item];
_Backwards.Remove(t2);
_Forwards.Remove(item);
return true;
}
public bool Remove(T2 item)
{
if (!this.Contains(item))
return false;
var t1 = _Backwards[item];
_Forwards.Remove(t1);
_Backwards.Remove(item);
return true;
}
public void Clear()
{
_Forwards.Clear();
_Backwards.Clear();
}
}
Bictionary
Here is a commingling of what I liked in each answer. It implements IEnumerable so it can use collection initializer, as you can see in the example.
Usage Constraint:
You are using different datatypes. (i.e., T1≠T2)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Bictionary<string, int> bictionary =
new Bictionary<string,int>() {
{ "a",1 },
{ "b",2 },
{ "c",3 }
};
// test forward lookup
Console.WriteLine(bictionary["b"]);
// test forward lookup error
//Console.WriteLine(bictionary["d"]);
// test reverse lookup
Console.WriteLine(bictionary[3]);
// test reverse lookup error (throws same error as forward lookup does)
Console.WriteLine(bictionary[4]);
}
}
public class Bictionary<T1, T2> : Dictionary<T1, T2>
{
public T1 this[T2 index]
{
get
{
if(!this.Any(x => x.Value.Equals(index)))
throw new System.Collections.Generic.KeyNotFoundException();
return this.First(x => x.Value.Equals(index)).Key;
}
}
}
Fiddle:
https://dotnetfiddle.net/mTNEuw
This is an old issue but I wanted to add a two extension methods in case anyone finds it useful. The second is not as useful but it provides a starting point if one to one dictionaries need to be supported.
public static Dictionary<VALUE,KEY> Inverse<KEY,VALUE>(this Dictionary<KEY,VALUE> dictionary)
{
if (dictionary==null || dictionary.Count == 0) { return null; }
var result = new Dictionary<VALUE, KEY>(dictionary.Count);
foreach(KeyValuePair<KEY,VALUE> entry in dictionary)
{
result.Add(entry.Value, entry.Key);
}
return result;
}
public static Dictionary<VALUE, KEY> SafeInverse<KEY, VALUE>(this Dictionary<KEY, VALUE> dictionary)
{
if (dictionary == null || dictionary.Count == 0) { return null; }
var result = new Dictionary<VALUE, KEY>(dictionary.Count);
foreach (KeyValuePair<KEY, VALUE> entry in dictionary)
{
if (result.ContainsKey(entry.Value)) { continue; }
result.Add(entry.Value, entry.Key);
}
return result;
}
Here's an alternative solution to those that were suggested. Removed the inner class and insured the coherence when adding/removing items
using System.Collections;
using System.Collections.Generic;
public class Map<E, F> : IEnumerable<KeyValuePair<E, F>>
{
private readonly Dictionary<E, F> _left = new Dictionary<E, F>();
public IReadOnlyDictionary<E, F> left => this._left;
private readonly Dictionary<F, E> _right = new Dictionary<F, E>();
public IReadOnlyDictionary<F, E> right => this._right;
public void RemoveLeft(E e)
{
if (!this.left.ContainsKey(e)) return;
this._right.Remove(this.left[e]);
this._left.Remove(e);
}
public void RemoveRight(F f)
{
if (!this.right.ContainsKey(f)) return;
this._left.Remove(this.right[f]);
this._right.Remove(f);
}
public int Count()
{
return this.left.Count;
}
public void Set(E left, F right)
{
if (this.left.ContainsKey(left))
{
this.RemoveLeft(left);
}
if (this.right.ContainsKey(right))
{
this.RemoveRight(right);
}
this._left.Add(left, right);
this._right.Add(right, left);
}
public IEnumerator<KeyValuePair<E, F>> GetEnumerator()
{
return this.left.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.left.GetEnumerator();
}
}
The following encapsulating class utilizes linq (IEnumerable Extensions) over 1 dictionary instance.
public class TwoWayDictionary<TKey, TValue>
{
readonly IDictionary<TKey, TValue> dict;
readonly Func<TKey, TValue> GetValueWhereKey;
readonly Func<TValue, TKey> GetKeyWhereValue;
readonly bool _mustValueBeUnique = true;
public TwoWayDictionary()
{
this.dict = new Dictionary<TKey, TValue>();
this.GetValueWhereKey = (strValue) => dict.Where(kvp => Object.Equals(kvp.Key, strValue)).Select(kvp => kvp.Value).FirstOrDefault();
this.GetKeyWhereValue = (intValue) => dict.Where(kvp => Object.Equals(kvp.Value, intValue)).Select(kvp => kvp.Key).FirstOrDefault();
}
public TwoWayDictionary(KeyValuePair<TKey, TValue>[] kvps)
: this()
{
this.AddRange(kvps);
}
public void AddRange(KeyValuePair<TKey, TValue>[] kvps)
{
kvps.ToList().ForEach( kvp => {
if (!_mustValueBeUnique || !this.dict.Any(item => Object.Equals(item.Value, kvp.Value)))
{
dict.Add(kvp.Key, kvp.Value);
} else {
throw new InvalidOperationException("Value must be unique");
}
});
}
public TValue this[TKey key]
{
get { return GetValueWhereKey(key); }
}
public TKey this[TValue value]
{
get { return GetKeyWhereValue(value); }
}
}
class Program
{
static void Main(string[] args)
{
var dict = new TwoWayDictionary<string, int>(new KeyValuePair<string, int>[] {
new KeyValuePair<string, int>(".jpeg",100),
new KeyValuePair<string, int>(".jpg",101),
new KeyValuePair<string, int>(".txt",102),
new KeyValuePair<string, int>(".zip",103)
});
var r1 = dict[100];
var r2 = dict[".jpg"];
}
}
This uses an indexer for the reverse lookup.
The reverse lookup is O(n) but it also does not use two dictionaries
public sealed class DictionaryDoubleKeyed : Dictionary<UInt32, string>
{ // used UInt32 as the key as it has a perfect hash
// if most of the lookup is by word then swap
public void Add(UInt32 ID, string Word)
{
if (this.ContainsValue(Word)) throw new ArgumentException();
base.Add(ID, Word);
}
public UInt32 this[string Word]
{ // this will be O(n)
get
{
return this.FirstOrDefault(x => x.Value == Word).Key;
}
}
}
There is a BijectionDictionary type available in this open source repo:
https://github.com/ColmBhandal/CsharpExtras.
It isn't qualitatively much different to the other answers given. It uses two dictionaries, like most of those answers.
What is novel, I believe, about this dictionary vs. the other answers so far, is that rather than behaving like a two way dictionary, it just behaves like a one-way, familiar dictionary and then dynamically allows you to flip the dictionary using the Reverse property. The flipped object reference is shallow, so it will still be able to modify the same core object as the original reference. So you can have two references to the same object, except one of them is flipped.
Another thing that is probably unique about this dictionary is that there are some tests written for it in the test project under that repo. It's been used by us in practice and has been pretty stable so far.

Are there any implementations of multiset for .Net?

I'm looking for a .Net implementation of a multiset. Can anyone recommend a good one?
(A multiset, or bag, is a set that can have duplicate values, and on which you can do set operations: intersection, difference, etc. A shopping cart for instance could be thought of as a multiset because you can have multiple occurrences of the same product.)
I do not know about one, however you could use a Dictionary for that, in which the value is the quantity of the item. And when the item is added for the second time, you vould increase the value for it in the dictionary.
An other possibility would be to simply use a List of items, in which you could put duplicates. This might be a better approach for a shopping cart.
Anything calling itself a C# implementation of a multiset should not be based on a Dictionary internally. Dictionaries are hash tables, unordered collections. C++'s sets, multisets, maps, and multimaps are ordered. Internally each is represented as some flavor of a self-balancing binary search tree.
In C# we should then use a SortedDictionary as the basis of our implementation as according to Microsoft's own documentation a SortedDictionary "is a binary search tree with O(log n) retrieval". A basic multiset can be implemented as follows:
public class SortedMultiSet<T> : IEnumerable<T>
{
private SortedDictionary<T, int> _dict;
public SortedMultiSet()
{
_dict = new SortedDictionary<T, int>();
}
public SortedMultiSet(IEnumerable<T> items) : this()
{
Add(items);
}
public bool Contains(T item)
{
return _dict.ContainsKey(item);
}
public void Add(T item)
{
if (_dict.ContainsKey(item))
_dict[item]++;
else
_dict[item] = 1;
}
public void Add(IEnumerable<T> items)
{
foreach (var item in items)
Add(item);
}
public void Remove(T item)
{
if (!_dict.ContainsKey(item))
throw new ArgumentException();
if (--_dict[item] == 0)
_dict.Remove(item);
}
// Return the last value in the multiset
public T Peek()
{
if (!_dict.Any())
throw new NullReferenceException();
return _dict.Last().Key;
}
// Return the last value in the multiset and remove it.
public T Pop()
{
T item = Peek();
Remove(item);
return item;
}
public IEnumerator<T> GetEnumerator()
{
foreach(var kvp in _dict)
for(int i = 0; i < kvp.Value; i++)
yield return kvp.Key;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
Another option is to just wrap SortedSet, but instead of storing your type T in it, you store the value tuple (T value, int counter) where counter goes up by 1 with each new instance of value that is inserted. Essentially you're forcing the values to be distinct. You can efficiently use GetViewBetween() to find the largest value of counter for a particular value, then increment it to get the counter for a newly-added value. And unlike the count dictionary solution, you can use GetViewBetween() to replicate the functionality equal_range, lower_bound, and upper_bound gives in C++. Here is some code showing what I mean:
public class SortedMultiSet<T> : IEnumerable<T>
{
public void Add(T value)
{
var view = set.GetViewBetween((value, 0), (value, int.MaxValue));
int nextCounter = view.Count > 0 ? view.Max.counter + 1 : 0;
set.Add((value, nextCounter));
}
public bool RemoveOne(T value)
{
var view = set.GetViewBetween((value, 0), (value, int.MaxValue));
if (view.Count == 0) return false;
set.Remove(view.Max);
return true;
}
public bool RemoveAll(T value)
{
var view = set.GetViewBetween((value, 0), (value, int.MaxValue));
bool result = view.Count > 0;
view.Clear();
return result;
}
public SortedMultiSet<T> GetViewBetween(T min, T max)
{
var result = new SortedMultiSet<T>();
result.set = set.GetViewBetween((min, 0), (max, int.MaxValue));
return result;
}
public IEnumerator<T> GetEnumerator() =>
set.Select(x => x.value).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
set.Select(x => x.value).GetEnumerator();
private SortedSet<(T value, int counter)> set =
new SortedSet<(T value, int counter)>();
}
Now you can write something like this:
var multiset = new SortedMultiSet<int>();
foreach (int i in new int[] { 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8 })
{
multiset.Add(i);
}
foreach (int i in multiset.GetViewBetween(2, 7))
{
Console.Write(i + " "); // Output: 2 2 3 4 5 5 6 7 7
}
In the past, there were some issues where GetViewBetween() ran in time O(output size), rather than time O(log n), but I think those have been resolved. At the time it would count up nodes to cache the count, it now uses hierarchical counts to perform Count operations efficiently. See this StackOverflow post and this library code.
public class Multiset<T>: ICollection<T>
{
private readonly Dictionary<T, int> data;
public Multiset()
{
data = new Dictionary<T, int>();
}
private Multiset(Dictionary<T, int> data)
{
this.data = data;
}
public void Add(T item)
{
int count = 0;
data.TryGetValue(item, out count);
count++;
data[item] = count;
}
public void Clear()
{
data.Clear();
}
public Multiset<T> Except(Multiset<T> another)
{
Multiset<T> copy = new Multiset<T>(new Dictionary<T, int>(data));
foreach (KeyValuePair<T, int> kvp in another.data)
{
int count;
if (copy.data.TryGetValue(kvp.Key, out count))
{
if (count > kvp.Value)
{
copy.data[kvp.Key] = count - kvp.Value;
}
else
{
copy.data.Remove(kvp.Key);
}
}
}
return copy;
}
public Multiset<T> Intersection(Multiset<T> another)
{
Dictionary<T, int> newData = new Dictionary<T, int>();
foreach (T t in data.Keys.Intersect(another.data.Keys))
{
newData[t] = Math.Min(data[t], another.data[t]);
}
return new Multiset<T>(newData);
}
public bool Contains(T item)
{
return data.ContainsKey(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
foreach (KeyValuePair<T, int> kvp in data)
{
for (int i = 0; i < kvp.Value; i++)
{
array[arrayIndex] = kvp.Key;
arrayIndex++;
}
}
}
public IEnumerable<T> Mode()
{
if (!data.Any())
{
return Enumerable.Empty<T>();
}
int modalFrequency = data.Values.Max();
return data.Where(kvp => kvp.Value == modalFrequency).Select(kvp => kvp.Key);
}
public int Count
{
get
{
return data.Values.Sum();
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool Remove(T item)
{
int count;
if (!data.TryGetValue(item, out count))
{
return false;
}
count--;
if (count == 0)
{
data.Remove(item);
}
else
{
data[item] = count;
}
return true;
}
public IEnumerator<T> GetEnumerator()
{
return new MultisetEnumerator<T>(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new MultisetEnumerator<T>(this);
}
private class MultisetEnumerator<T> : IEnumerator<T>
{
public MultisetEnumerator(Multiset<T> multiset)
{
this.multiset = multiset;
baseEnumerator = multiset.data.GetEnumerator();
index = 0;
}
private readonly Multiset<T> multiset;
private readonly IEnumerator<KeyValuePair<T, int>> baseEnumerator;
private int index;
public T Current
{
get
{
return baseEnumerator.Current.Key;
}
}
public void Dispose()
{
baseEnumerator.Dispose();
}
object System.Collections.IEnumerator.Current
{
get
{
return baseEnumerator.Current.Key;
}
}
public bool MoveNext()
{
KeyValuePair<T, int> kvp = baseEnumerator.Current;
if (index < (kvp.Value - 1))
{
index++;
return true;
}
else
{
bool result = baseEnumerator.MoveNext();
index = 0;
return result;
}
}
public void Reset()
{
baseEnumerator.Reset();
}
}
}
You can use this implementation of a sorted multiset: SortedMultiSet.cs

Categories