public class TwoSideDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> mKeyToValue = new Dictionary<TKey, TValue>();
private readonly IDictionary<TValue, TKey> mValueToKey = new Dictionary<TValue, TKey>();
public void Map(TKey key, TValue value)
{
if (mKeyToValue.ContainsKey(key))
{
throw new ArgumentException("Key already mapped in dictionary", "key");
}
if (mValueToKey.ContainsKey(value))
{
throw new ArgumentException("Value already mapped in dictionary", "value");
}
mKeyToValue[key] = value;
mValueToKey[value] = key;
}
public TValue GetByKey(TKey key)
{
return mKeyToValue[key];
}
public TKey GetByValue(TValue key)
{
return mValueToKey[key];
}
public bool ContainsKey(TKey key)
{
return mKeyToValue.ContainsKey(key);
}
public bool ContainsValue(TValue value)
{
return mValueToKey.ContainsKey(value);
}
public TValue this[TKey key]
{
get
{
return GetByKey(key);
}
set
{
Map(key, value);
}
}
public TKey this[TValue key]
{
get
{
return GetByValue(key);
}
set
{
Map(value, key);
}
}
public IEnumerable<KeyValuePair<TKey, TValue>> KeyToValue
{
get
{
return mKeyToValue;
}
}
public IEnumerable<KeyValuePair<TKey, TValue>> ValueToKey
{
get
{
return mKeyToValue;
}
}
}