1
0
mirror of https://github.com/chylex/Code-Statistics.git synced 2024-11-25 06:42:45 +01:00
Code-Statistics/CodeStatisticsCore/Collections/BiDictionary.cs

88 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
namespace CodeStatisticsCore.Collections{
public class BiDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>{
private readonly Dictionary<TKey, TValue> kv;
private readonly Dictionary<TValue, TKey> vk;
public ICollection<TKey> Keys { get { return kv.Keys; } }
public ICollection<TValue> Values { get { return vk.Keys; } }
public int Count { get { return kv.Count; } }
public IEnumerable<KeyValuePair<TValue, TKey>> Reverse { get { return vk; } }
public BiDictionary(){
kv = new Dictionary<TKey, TValue>();
vk = new Dictionary<TValue, TKey>();
}
public BiDictionary(int capacity){
kv = new Dictionary<TKey, TValue>(capacity);
vk = new Dictionary<TValue, TKey>(capacity);
}
public void Add(TKey key, TValue value){
kv.Add(key, value);
vk.Add(value, key);
}
public bool ContainsKey(TKey key){
return kv.ContainsKey(key);
}
public bool ContainsValue(TValue value){
return vk.ContainsKey(value);
}
public void Clear(){
kv.Clear();
vk.Clear();
}
public bool RemoveKey(TKey key){
TValue value;
if (kv.TryGetValue(key, out value) && kv.Remove(key)){
vk.Remove(value);
return true;
}
else return false;
}
public bool RemoveValue(TValue value){
TKey key;
if (vk.TryGetValue(value, out key) && vk.Remove(value)){
kv.Remove(key);
return true;
}
else return false;
}
public TValue GetValue(TKey key){
return kv[key];
}
public bool TryGetValue(TKey key, out TValue value){
return kv.TryGetValue(key, out value);
}
public TKey GetKey(TValue value){
return vk[value];
}
public bool TryGetKey(TValue value, out TKey key){
return vk.TryGetValue(value, out key);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(){
return kv.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator(){
return ((IEnumerable)kv).GetEnumerator();
}
}
}