Skip to content

Collection fixes #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions FishNet/Plugins/FishySteamworks/Core/BidirectionalDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace FishySteamworks
{
public class BidirectionalDictionary<T1, T2> : IEnumerable
{
private Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
private Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();
private readonly Dictionary<T1, T2> t1ToT2Dict = new Dictionary<T1, T2>();
private readonly Dictionary<T2, T1> t2ToT1Dict = new Dictionary<T2, T1>();

public IEnumerable<T1> FirstTypes => t1ToT2Dict.Keys;
public IEnumerable<T2> SecondTypes => t2ToT1Dict.Keys;
Expand All @@ -20,26 +20,26 @@ public class BidirectionalDictionary<T1, T2> : IEnumerable

public void Add(T1 key, T2 value)
{
if (t1ToT2Dict.ContainsKey(key))
{
Remove(key);
}
Remove(key);

t1ToT2Dict[key] = value;
t2ToT1Dict[value] = key;
}

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];
Expand All @@ -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);
}
}

Expand All @@ -89,4 +85,4 @@ public T2 this[T1 key]
}
}
}
}
}