diff --git a/FishNet/Plugins/FishySteamworks/Core/BidirectionalDictionary.cs b/FishNet/Plugins/FishySteamworks/Core/BidirectionalDictionary.cs index 0d5b161..1ce930d 100644 --- a/FishNet/Plugins/FishySteamworks/Core/BidirectionalDictionary.cs +++ b/FishNet/Plugins/FishySteamworks/Core/BidirectionalDictionary.cs @@ -5,8 +5,8 @@ namespace FishySteamworks { public class BidirectionalDictionary : IEnumerable { - private Dictionary t1ToT2Dict = new Dictionary(); - private Dictionary t2ToT1Dict = new Dictionary(); + private readonly Dictionary t1ToT2Dict = new Dictionary(); + private readonly Dictionary t2ToT1Dict = new Dictionary(); public IEnumerable FirstTypes => t1ToT2Dict.Keys; public IEnumerable SecondTypes => t2ToT1Dict.Keys; @@ -20,10 +20,7 @@ public class BidirectionalDictionary : IEnumerable public void Add(T1 key, T2 value) { - if (t1ToT2Dict.ContainsKey(key)) - { - Remove(key); - } + Remove(key); t1ToT2Dict[key] = value; t2ToT1Dict[value] = key; @@ -31,15 +28,18 @@ public void Add(T1 key, T2 value) public void Add(T2 key, T1 value) { - if (t2ToT1Dict.ContainsKey(key)) - { - Remove(key); - } + Remove(key); t2ToT1Dict[key] = value; t1ToT2Dict[value] = key; } + public void Clear() + { + t1ToT2Dict.Clear(); + t2ToT1Dict.Clear(); + } + public T2 Get(T1 key) => t1ToT2Dict[key]; public T1 Get(T2 key) => t2ToT1Dict[key]; @@ -54,20 +54,16 @@ public void Add(T2 key, T1 value) public void Remove(T1 key) { - if (Contains(key)) + if (t1ToT2Dict.Remove(key, out T2 val)) { - T2 val = t1ToT2Dict[key]; - t1ToT2Dict.Remove(key); t2ToT1Dict.Remove(val); } } public void Remove(T2 key) { - if (Contains(key)) + if (t2ToT1Dict.Remove(key, out T1 val)) { - T1 val = t2ToT1Dict[key]; t1ToT2Dict.Remove(val); - t2ToT1Dict.Remove(key); } } @@ -89,4 +85,4 @@ public T2 this[T1 key] } } } -} \ No newline at end of file +}