Why is Lookup immutable in C#? - c#

Unlike Dictionary, you cannot construct a Lookup by adding elements one by one. Do you happen to know the reason?
Lookup is just like multimap in C++; why can't we modify it in C#? If we really can't, how can we construct a multimap data structure in C#?

Lookup and ILookup were introduced as part of LINQ, which generally takes a more functional approach than other aspects of the framework. Personally I like the fact that Lookup is (at least publicly) immutable - and I'm looking forward to more immutable collections being available.
If you want to create your own multimap data structure, just maintain a Dictionary<TKey, List<TValue>> or something similar. You might want to look at my Edulinq implementation of Lookup for some sample code.

Here is an implementation I wrote
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class MultiLookup<Key, Value> : ILookup<Key, Value>
{
Dictionary<Key, HashSet<Value>> Contents = new Dictionary<Key, HashSet<Value>>();
public void Add(Key key, Value value)
{
if (!Contains(key))
{
Contents[key]=new HashSet<Value>();
}
Contents[key].Add(value);
}
public void Add(IEnumerable<Tuple<Key, Value>> items)
{
foreach (var item in items)
{
Add(item.Item1, item.Item2);
}
}
public void Remove(Key key, Value value)
{
if (!Contains(key))
{
return;
}
Contents[key].Remove(value);
if (Contents[key].Count==0)
{
Contents.Remove(key);
}
}
public void RemoveKey(Key key)
{
Contents.Remove(key);
}
public IEnumerable<Key> Keys
{
get
{
return Contents.Keys;
}
}
public int Count
{
get
{
return Contents.Count;
}
}
public bool Contains(Key key)
{
return Contents.ContainsKey(key);
}
private class Grouping : IGrouping<Key, Value>
{
public MultiLookup<Key, Value> _source;
public Key _key;
public Key Key
{
get { return _key; }
}
public static HashSet<Value> Empty = new HashSet<Value>();
public IEnumerator<Value> GetEnumerator()
{
if (!_source.Contains(_key))
{
yield break;
}
else
{
foreach (var item in _source[_key])
{
yield return item;
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
public IEnumerator<IGrouping<Key, Value>> GetEnumerator()
{
return (from p in Contents
select new Grouping() { _key = p.Key, _source = this }).GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerable<Value> this[Key key]
{
get { return Contents[key]; }
}
}
and a test case ( probably not exhaustive ) for you
using FluentAssertions;
using System.Linq;
using Xunit;
public class MultiLookupSpec
{
MultiLookup<int, string> Fixture = new MultiLookup<int,string>();
[Fact]
public void NewLookupShouldBeEmpty()
{
Fixture.Count.Should().Be(0);
}
[Fact]
public void AddingANewValueToANonExistentKeyShouldCreateKeyAndAddValue()
{
Fixture.Add(0, "hello");
Fixture.Count.Should().Be(1);
}
[Fact]
public void AddingMultipleValuesToAKeyShouldGenerateMultipleValues()
{
Fixture.Add(0, "hello");
Fixture.Add(0, "cat");
Fixture.Add(0, "dog");
Fixture[0].Should().BeEquivalentTo(new []{"hello", "cat", "dog"});
}
[Fact]
public void RemovingAllElementsOfKeyWillAlsoRemoveKey()
{
Fixture.Add(0, "hello");
Fixture.Add(0, "cat");
Fixture.Add(0, "dog");
Fixture.Remove(0, "dog");
Fixture.Remove(0, "cat");
Fixture.Remove(0, "hello");
Fixture.Contains(0).Should().Be(false);
}
[Fact]
public void EnumerationShouldWork()
{
Fixture.Add(0, "hello");
Fixture.Add(0, "cat");
Fixture.Add(0, "dog");
Fixture.Add(1, "house");
Fixture.Add(2, "pool");
Fixture.Add(2, "office");
Fixture.Select(s => s.Key).Should().Contain(new[] { 0, 1, 2 });
Fixture.SelectMany(s => s).Should().Contain(new[] { "hello", "cat", "dog", "house", "pool", "office" });
}
}

I had this same problem and question. Why is Lookup immutable? I solved it with some extension methods to IDictionary
public static void Add<TKey,TList,TItem>(this IDictionary<TKey,TList> dict,TKey key,TItem item)
where TList : ICollection<TItem>,new()
{
if(!dict.ContainsKey(key))
{
dict.Add(key, new TList());
}
dict[key].Add(item);
}
public static void Remove<TKey, TList, TItem>(this IDictionary<TKey, TList> dict, TKey key)
where TList : IEnumerable<TItem>, new()
{
if (dict.ContainsKey(key))
{
dict.Remove(key);
}
}
public static TList Items<TKey, TList, TItem>(this IDictionary<TKey, TList> dict, TKey key)
where TList : IEnumerable<TItem>, new()
{
if (dict.ContainsKey(key))
{
return dict[key];
}
return default(TList);
}

Related

Generic implementation of OrderDictionary in C# is showing ambiguous method warnings

Since C# has no generic implementation of the OrderedDictionary at the time of asking this question I downloaded one from here. To be very clear I am using this in the Unity game engine with MonoDevelop to code a game.
The implementation seems nicely put together however it gives me an ambiguous method call warning the solution to which I can't seem to figure out. Can somebody please explain me what is going on here and propose a possible solution to get rid of the warnings?
To be specific here are the similar method calls:
IDictionaryEnumerator IOrderedDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
{
return List.GetEnumerator();
}
And here is the error:
[Warning] [CS0278] `TurboLabz.Game.IOrderedDictionary<string,TurboLabz.Game.RoomInfo>' contains ambiguous implementation of `enumerable' pattern.
Method `System.Collections.Specialized.IOrderedDictionary.GetEnumerator()' is ambiguous with method `System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,TurboLabz.Game.RoomInfo>>.GetEnumerator()'
Thanks in advance.
Edit:
Here is the source and its usage in the codebase that I have:
IOrderedDictionary.cs
using System.Collections.Generic;
using System.Collections.Specialized;
namespace TurboLabz.Game
{
public interface IOrderedDictionary<TKey, TValue> : IOrderedDictionary, IDictionary<TKey, TValue>
{
new int Add(TKey key, TValue value);
void Insert(int index, TKey key, TValue value);
new TValue this[int index]
{
get;
set;
}
}
}
OrderedDictionary.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace TurboLabz.Game
{
public class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>
{
private const int DefaultInitialCapacity = 0;
private static readonly string _keyTypeName = typeof(TKey).FullName;
private static readonly string _valueTypeName = typeof(TValue).FullName;
private static readonly bool _valueTypeIsReferenceType = !typeof(ValueType).IsAssignableFrom(typeof(TValue));
private Dictionary<TKey, TValue> _dictionary;
private List<KeyValuePair<TKey, TValue>> _list;
private IEqualityComparer<TKey> _comparer;
private object _syncRoot;
private int _initialCapacity;
public OrderedDictionary()
: this(DefaultInitialCapacity, null)
{
}
public OrderedDictionary(int capacity)
: this(capacity, null)
{
}
public OrderedDictionary(IEqualityComparer<TKey> comparer)
: this(DefaultInitialCapacity, comparer)
{
}
public OrderedDictionary(int capacity, IEqualityComparer<TKey> comparer)
{
if(0 > capacity)
throw new ArgumentOutOfRangeException("capacity", "'capacity' must be non-negative");
_initialCapacity = capacity;
_comparer = comparer;
}
private static TKey ConvertToKeyType(object keyObject)
{
if(null == keyObject)
{
throw new ArgumentNullException("key");
}
else
{
if(keyObject is TKey)
return (TKey)keyObject;
}
throw new ArgumentException("'key' must be of type " + _keyTypeName, "key");
}
private static TValue ConvertToValueType(object value)
{
if(null == value)
{
if(_valueTypeIsReferenceType)
return default(TValue);
else
throw new ArgumentNullException("value");
}
else
{
if(value is TValue)
return (TValue)value;
}
throw new ArgumentException("'value' must be of type " + _valueTypeName, "value");
}
private Dictionary<TKey, TValue> Dictionary
{
get
{
if(null == _dictionary)
{
_dictionary = new Dictionary<TKey, TValue>(_initialCapacity, _comparer);
}
return _dictionary;
}
}
private List<KeyValuePair<TKey, TValue>> List
{
get
{
if(null == _list)
{
_list = new List<KeyValuePair<TKey, TValue>>(_initialCapacity);
}
return _list;
}
}
IDictionaryEnumerator IOrderedDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
{
return List.GetEnumerator();
}
public void Insert(int index, TKey key, TValue value)
{
if(index > Count || index < 0)
throw new ArgumentOutOfRangeException("index");
Dictionary.Add(key, value);
List.Insert(index, new KeyValuePair<TKey, TValue>(key, value));
}
void IOrderedDictionary.Insert(int index, object key, object value)
{
Insert(index, ConvertToKeyType(key), ConvertToValueType(value));
}
public void RemoveAt(int index)
{
if(index >= Count || index < 0)
throw new ArgumentOutOfRangeException("index", "'index' must be non-negative and less than the size of the collection");
TKey key = List[index].Key;
List.RemoveAt(index);
Dictionary.Remove(key);
}
public TValue this[int index]
{
get
{
return List[index].Value;
}
set
{
if(index >= Count || index < 0)
throw new ArgumentOutOfRangeException("index", "'index' must be non-negative and less than the size of the collection");
TKey key = List[index].Key;
List[index] = new KeyValuePair<TKey, TValue>(key, value);
Dictionary[key] = value;
}
}
object IOrderedDictionary.this[int index]
{
get
{
return this[index];
}
set
{
this[index] = ConvertToValueType(value);
}
}
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
Add(key, value);
}
public int Add(TKey key, TValue value)
{
Dictionary.Add(key, value);
List.Add(new KeyValuePair<TKey,TValue>(key, value));
return Count - 1;
}
void IDictionary.Add(object key, object value)
{
Add(ConvertToKeyType(key), ConvertToValueType(value));
}
public void Clear()
{
Dictionary.Clear();
List.Clear();
}
public bool ContainsKey(TKey key)
{
return Dictionary.ContainsKey(key);
}
bool IDictionary.Contains(object key)
{
return ContainsKey(ConvertToKeyType(key));
}
bool IDictionary.IsFixedSize
{
get
{
return false;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
ICollection IDictionary.Keys
{
get
{
return (ICollection)Keys;
}
}
public int IndexOfKey(TKey key)
{
if(null == key)
throw new ArgumentNullException("key");
for(int index = 0; index < List.Count; index++)
{
KeyValuePair<TKey, TValue> entry = List[index];
TKey next = entry.Key;
if(null != _comparer)
{
if(_comparer.Equals(next, key))
{
return index;
}
}
else if(next.Equals(key))
{
return index;
}
}
return -1;
}
public bool Remove(TKey key)
{
if(null == key)
throw new ArgumentNullException("key");
int index = IndexOfKey(key);
if(index >= 0)
{
if(Dictionary.Remove(key))
{
List.RemoveAt(index);
return true;
}
}
return false;
}
void IDictionary.Remove(object key)
{
Remove(ConvertToKeyType(key));
}
ICollection IDictionary.Values
{
get
{
return (ICollection)Values;
}
}
public TValue this[TKey key]
{
get
{
return Dictionary[key];
}
set
{
if(Dictionary.ContainsKey(key))
{
Dictionary[key] = value;
List[IndexOfKey(key)] = new KeyValuePair<TKey, TValue>(key, value);
}
else
{
Add(key, value);
}
}
}
object IDictionary.this[object key]
{
get
{
return this[ConvertToKeyType(key)];
}
set
{
this[ConvertToKeyType(key)] = ConvertToValueType(value);
}
}
void ICollection.CopyTo(Array array, int index)
{
((ICollection)List).CopyTo(array, index);
}
public int Count
{
get
{
return List.Count;
}
}
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
object ICollection.SyncRoot
{
get
{
if(this._syncRoot == null)
{
System.Threading.Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
}
return this._syncRoot;
}
}
public ICollection<TKey> Keys
{
get
{
return Dictionary.Keys;
}
}
public bool TryGetValue(TKey key, out TValue value)
{
return Dictionary.TryGetValue(key, out value);
}
public ICollection<TValue> Values
{
get
{
return Dictionary.Values;
}
}
void ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
bool ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey,TValue>>)Dictionary).Contains(item);
}
void ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey,TValue>>)Dictionary).CopyTo(array, arrayIndex);
}
bool ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
return Remove(item.Key);
}
}
}
This is how the above given OrderedDictionary is being used:
IRoomSettingsModel.cs
namespace TurboLabz.Game
{
public interface IRoomSettingsModel
{
IOrderedDictionary<string, RoomInfo> settings { get; set; }
}
}
RoomSettingsModel.cs
namespace TurboLabz.Game
{
public class RoomSettingsModel : IRoomSettingsModel
{
public IOrderedDictionary<string, RoomInfo> settings { get; set; }
public RoomSettingsModel()
{
settings = new OrderedDictionary<string, RoomInfo>();
}
}
public struct RoomInfo
{
public string id;
public long gameDuration;
public long prize;
}
}
GSService.cs
namespace TurboLabz.Game
{
public class SomeService
{
public IRoomSettingsModel roomSettingsModel = new RoomSettingsModel();
public void ReadModel()
{
foreach (KeyValuePair<string, RoomInfo> room in roomSettingsModel.settings)
{
RoomInfo roomInfo = room.Value;
Debug.Log(roomInfo.id);
}
}
}
}
To keep things confidential I've changed the code a bit here but overall it should deliver the idea. The most important statement in usage above is foreach (KeyValuePair<string, RoomInfo> room in roomSettingsModel.settings) which is the source of the warning. It's in this line that I think the compiler gets confused about which GetEnumerator() method to call.
Firstly, is that really the issue? Secondly, how do I resolve the problem?
I tried to follow what you've done, but it's a spaghetti of nested interfaces.
If you put breakpoints in every GetEnumerator() in OrderedDictionary, you may find it is not calling the enumerator you expect.
The problem, I think, is with trying to implement the non-generic IOrderedDictionary interface along with IDictionary<TKey, TValue>.
If you want generics, why do you need to maintain compatibilty with non-generic IOrderedDictionary?
If you follow (F12) the inheritance trail of IOrderedDictionary, it inherits IDictionary, ICollection, IEnumerable.
Then IDictionary<TKey, TValue> inherits from ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable.
I'm not quite sure what all your requirements are, but I would drop any interfaces you don't need to support. Don't provide code features that you don't need.
This is not completely of your making, but it is the consequence of trying to support multiple interfaces with lots of baggage of their own.
Based on your question, I would only support IDictionary<TKey, TValue> & IList<T>.
And their baggage ;)
For those curious about KeyedCollection, here's an implmentation that does most of what #Mubeen implemented in his code. This is not fully tested, so don't just do a copy->paste if you use this.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
namespace TurboLabz.Game
{
public class GenericComparer<TKey> : IComparer<TKey>
{
public static GenericComparer<TKey> CreateComparer(Func<TKey, TKey, int> comparer)
{
return new GenericComparer<TKey>(comparer);
}
internal GenericComparer(Func<TKey, TKey, int> comparer)
{
Comparer = comparer;
}
private Func<TKey, TKey, int> Comparer { get; set; }
public int Compare(TKey x, TKey y)
{
return Comparer(x, y);
}
}
public class OrderedDictionaryKC<TKey, TValue> : KeyedCollection<TKey,KeyValuePair<TKey, TValue>>
{
public OrderedDictionaryKC()
{ }
public OrderedDictionaryKC(IEnumerable<KeyValuePair<TKey, TValue>> collection)
{
if (collection != null)
{
foreach (KeyValuePair<TKey, TValue> item in collection)
{
base.Add(item);
}
}
}
public OrderedDictionaryKC(IDictionary<TKey, TValue> dictionary) : this((IEnumerable<KeyValuePair<TKey, TValue>>)dictionary)
{ }
public ICollection<TKey> Keys
{
get
{
return base.Dictionary.Keys;
}
}
public ICollection<KeyValuePair<TKey, TValue>> Values
{
get
{
return base.Dictionary.Values;
}
}
public void Add(TKey key, TValue value)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
base.Add(new KeyValuePair<TKey, TValue>(key, value));
}
public bool ContainsKey(TKey key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
return base.Dictionary.ContainsKey(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
KeyValuePair<TKey, TValue> outValue;
var result= base.Dictionary.TryGetValue(key, out outValue);
value = outValue.Value;
return result;
}
protected override TKey GetKeyForItem(KeyValuePair<TKey, TValue> item)
{
return item.Key;
}
}
}
I ended up writing a new implementation which is a pure generic wrapper around System.Collections.Specialized.OrderedDictionary.
Although this is not an answer to the original question, it is warning free and, like #ashley-pillay mentioned in his answer, implements only the necessary interfaces.
I am providing the implementation here in hopes of helping others since it's hard to find a good implementation of a warning free generic OrderedDictionary even after a lot of Googling.
IOrderedDictionary.cs
using System.Collections.Generic;
namespace TurboLabz.Common
{
public interface IOrderedDictionary<TKey, TValue> :
IDictionary<TKey, TValue>,
ICollection<KeyValuePair<TKey, TValue>>,
IEnumerable<KeyValuePair<TKey, TValue>>
{
}
}
OrderedDictionary.cs
//-----------------------------------------------------------------------------
// Initial code provided by Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace TurboLabz.Common
{
// System.Collections.Specialized.OrderedDictionary is NOT generic.
// This class is essentially a generic wrapper for OrderedDictionary.
public class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>
{
private OrderedDictionary _internalDictionary;
public OrderedDictionary()
{
_internalDictionary = new OrderedDictionary();
}
public OrderedDictionary(IDictionary<TKey, TValue> dictionary)
{
if (dictionary != null)
{
_internalDictionary = new OrderedDictionary();
foreach (KeyValuePair<TKey, TValue> pair in dictionary)
{
_internalDictionary.Add(pair.Key, pair.Value);
}
}
}
public int Count
{
get
{
return _internalDictionary.Count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public TValue this[TKey key]
{
get
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (_internalDictionary.Contains(key))
{
return (TValue)_internalDictionary[(object)key];
}
else
{
throw new KeyNotFoundException("Cannot find key " + key);
}
}
set
{
if (key == null)
{
throw new ArgumentNullException("key");
}
_internalDictionary[(object)key] = value;
}
}
public ICollection<TKey> Keys
{
get
{
IList<TKey> keys = new List<TKey>(_internalDictionary.Count);
foreach (TKey key in _internalDictionary.Keys)
{
keys.Add(key);
}
// Keys should be put in a ReadOnlyCollection,
// but since this is an internal class, for performance reasons,
// we choose to avoid creating yet another collection.
return keys;
}
}
public ICollection<TValue> Values
{
get
{
IList<TValue> values = new List<TValue>(_internalDictionary.Count);
foreach (TValue value in _internalDictionary.Values)
{
values.Add(value);
}
// Values should be put in a ReadOnlyCollection,
// but since this is an internal class, for performance reasons,
// we choose to avoid creating yet another collection.
return values;
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
public void Add(TKey key, TValue value)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
_internalDictionary.Add(key, value);
}
public void Clear()
{
_internalDictionary.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
if ((item.Key == null) || !(_internalDictionary.Contains(item.Key)))
{
return false;
}
else
{
return _internalDictionary[(object)item.Key].Equals(item.Value);
}
}
public bool ContainsKey(TKey key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
return _internalDictionary.Contains(key);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException("arrayIndex");
}
if ((array.Rank > 1) ||
(arrayIndex >= array.Length) ||
((array.Length - arrayIndex) < _internalDictionary.Count))
{
throw new Exception("Fx.Exception.Argument('array', SRCore.BadCopyToArray)");
}
int index = arrayIndex;
foreach (DictionaryEntry entry in _internalDictionary)
{
array[index] = new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value);
++index;
}
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
foreach (DictionaryEntry entry in _internalDictionary)
{
yield return new KeyValuePair<TKey, TValue>((TKey)entry.Key, (TValue)entry.Value);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (Contains(item))
{
_internalDictionary.Remove(item.Key);
return true;
}
else
{
return false;
}
}
public bool Remove(TKey key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (_internalDictionary.Contains(key))
{
_internalDictionary.Remove(key);
return true;
}
else
{
return false;
}
}
public bool TryGetValue(TKey key, out TValue value)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
bool keyExists = _internalDictionary.Contains(key);
value = keyExists ? (TValue)_internalDictionary[(object)key] : default(TValue);
return keyExists;
}
}
}

How to implement generic dictionary class?

When I try to run the following code, the foreach statement is throwing the below error at compile time
Cannot convert type 'string' to
'System.Collections.Generic.KeyValuePair>'
namespace myClass
{
public class myDictionary<T>
{
Dictionary<string, List<T>> dictionary = new Dictionary<string, List<T>>();
public void Add(string key, T value)
{
List<T> list;
if (this.dictionary.TryGetValue(key, out list))
{
list.Add(value);
}
else
{
list = new List<T>();
list.Add(value);
this.dictionary[key] = list;
}
}
public IEnumerable<string> Keys
{
get
{
return this.dictionary.Keys;
}
}
public List<T> this[string key]
{
get
{
List<T> list;
if (!this.dictionary.TryGetValue(key, out list))
{
list = new List<T>();
this.dictionary[key] = list;
}
return list;
}
}
public IEnumerator<T> GetEnumerator()
{
return (dictionary as IEnumerable<T>).GetEnumerator();
}
}
class Program
{
static void Main()
{
myDictionary<string> dictionary = new myDictionary<string>();
dictionary.Add("One", "AA");
dictionary.Add("One", "BB");
dictionary.Add("Two", "CC");
dictionary.Add("Two", "DD");
foreach(KeyValuePair<string, List<string>> pair in dictionary)
{
}
}
}
}
Please let me know what is wrong with my implementation. Thanks for your help.
It looks like the problem is:
public IEnumerator<T> GetEnumerator()
{
return (dictionary as IEnumerable<T>).GetEnumerator();
}
However, you'll need to clarify what this should return, since your dictionary is one of lists. Is this meant to be all the values from all the lists in turn? If so, I guess:
public IEnumerator<T> GetEnumerator()
{
return dictionary.Values.SelectMany(x => x).GetEnumerator();
}
If, however, you want to return the pairs, then:
public IEnumerator<KeyValuePair<string, List<T>>> GetEnumerator()
{
return dictionary.GetEnumerator();
}

How to set Dictionary as a property ? Give an example

I'm trying to use a dictionary as a class member. I want to
use a property to get/set the key/value of the dictionary but I'm
confused as how to use a dictionary as a property. Since there are 2
parts, I don't know how to setup the get/sets.
You could try this:
class Example {
private Dictionary<int,string> _map;
public Dictionary<int,string> Map { get { return _map; } }
public Example() { _map = new Dictionary<int,string>(); }
}
Implementation would go along the lines of:
var e = new Example();
e.Map[42] = "The Answer";
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var cl = new cl();
populate(cl.dict);
foreach(var d in cl.dict)
Console.WriteLine(d.Key);
}
private static void populate(Dictionary<int, string> d)
{
for (int i = 0; i < 10 ; i++)
{
if (!d.ContainsKey(i))
{
d.Add(i, i.ToString());
}
}
}
}
public class cl
{
public Dictionary<int, string> dict;
public cl()
{
dict = new Dictionary<int, string>();
}
}
Do you mean this ?
class MyDictionary<TKey, TValue>
{
private readonly Dictionary<TKey, TValue> _dictionary;
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
}
public void Clear()
{
_dictionary.Clear();
}
public bool Remve(TKey key)
{
return _dictionary.Remove(key);
}
.... and other methods...
public MyDictionary(Dictionary<TKey, TValue> dictionary)
{
_dictionary = dictionary;
}
}

Analogue of Python's defaultdict?

Is there a .NET analogue of Python's defaultdict? I find it useful to write short code, eg. counting frequencies:
>>> words = "to be or not to be".split()
>>> print words
['to', 'be', 'or', 'not', 'to', 'be']
>>> from collections import defaultdict
>>> frequencies = defaultdict(int)
>>> for word in words:
... frequencies[word] += 1
...
>>> print frequencies
defaultdict(<type 'int'>, {'not': 1, 'to': 2, 'or': 1, 'be': 2})
So ideally in C# I could write:
var frequencies = new DefaultDictionary<string,int>(() => 0);
foreach(string word in words)
{
frequencies[word] += 1
}
Here's a simple implementation:
public class DefaultDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : new()
{
public new TValue this[TKey key]
{
get
{
TValue val;
if (!TryGetValue(key, out val))
{
val = new TValue();
Add(key, val);
}
return val;
}
set { base[key] = value; }
}
}
And how you would use it:
var dict = new DefaultDictionary<string, int>();
Debug.WriteLine(dict["foo"]); // prints "0"
dict["bar"] = 5;
Debug.WriteLine(dict["bar"]); // prints "5"
Or like this:
var dict = new DefaultDictionary<string, List<int>>();
dict["foo"].Add(1);
dict["foo"].Add(2);
dict["foo"].Add(3);
Something to get you started. I basically just changed the this indexer. Since I don't know the complete functionality of python's defaultdict I cannot improve it further. Your given example will work.
public class DefaultDictionary<TKey, TValue> : IDictionary<TKey,TValue>
{
private readonly Func<TValue> _defaultSelector;
private readonly Dictionary<TKey, TValue> _values = new Dictionary<TKey, TValue>();
public DefaultDictionary()
: this(() => default(TValue))
{
}
public DefaultDictionary(Func<TValue> defaultSelector)
{
_defaultSelector = defaultSelector;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<TKey, TValue> item)
{
((IDictionary<TKey,TValue>)_values).Add(item);
}
public void Clear()
{
_values.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return ((IDictionary<TKey,TValue>)_values).Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((IDictionary<TKey, TValue>)_values).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return ((IDictionary<TKey, TValue>)_values).Remove(item);
}
public int Count { get { return _values.Count; } }
public bool IsReadOnly { get { return ((IDictionary<TKey, TValue>) _values).IsReadOnly; } }
public bool ContainsKey(TKey key)
{
return _values.ContainsKey(key);
}
public void Add(TKey key, TValue value)
{
_values.Add(key, value);
}
public bool Remove(TKey key)
{
return _values.Remove(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
return _values.TryGetValue(key, out value);
}
public TValue this[TKey key]
{
get
{
if (!_values.ContainsKey(key))
{
_values.Add(key, _defaultSelector());
}
return _values[key];
}
set
{
if(!_values.ContainsKey(key))
{
_values.Add(key, _defaultSelector());
}
_values[key] = value;
}
}
public ICollection<TKey> Keys { get { return _values.Keys; } }
public ICollection<TValue> Values { get { return _values.Values; } }
public Dictionary<TKey, TValue> ToDictionary()
{
return new Dictionary<TKey, TValue>(_values);
}
}
I don't think there is an equivalent, but given your example you could do this with LINQ:
var words = new List<string>{ "One", "Two", "Three", "One" };
var frequencies = words.GroupBy (w => w).ToDictionary (w => w.Key, w => w.Count());
The ConcurrentDictionary (in System.Collections.Generic) behaves very similar (although designed for concurrent use)
To retrieve the value:
The GetOrAdd method returns the value for a key, and if it doesn't exist, creates one using a value factory.
To set the value:
The AddOrUpdate method updates a value, or sets it if it doesn't exist yet
Advantages:
Thread safe
Full control over the default value and the update step
Disadvantages:
Slightly more verbose syntax

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.

Categories