Skip to content
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

Refactor TypeDictionary #8625

Merged
merged 1 commit into from Jul 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 27 additions & 43 deletions OpenRA.Game/Primitives/TypeDictionary.cs
Expand Up @@ -17,8 +17,8 @@ namespace OpenRA.Primitives
{
public class TypeDictionary : IEnumerable
{
Dictionary<Type, object> dataSingular = new Dictionary<Type, object>();
Dictionary<Type, List<object>> dataMultiple = new Dictionary<Type, List<object>>();
static readonly Func<Type, List<object>> CreateList = type => new List<object>();
readonly Dictionary<Type, List<object>> data = new Dictionary<Type, List<object>>();

public void Add(object val)
{
Expand All @@ -32,63 +32,48 @@ public void Add(object val)

void InnerAdd(Type t, object val)
{
List<object> objs;
object obj;

if (dataMultiple.TryGetValue(t, out objs))
objs.Add(val);
else if (dataSingular.TryGetValue(t, out obj))
{
dataSingular.Remove(t);
dataMultiple.Add(t, new List<object> { obj, val });
}
else
dataSingular.Add(t, val);
data.GetOrAdd(t, CreateList).Add(val);
}

public bool Contains<T>()
{
return dataSingular.ContainsKey(typeof(T)) || dataMultiple.ContainsKey(typeof(T));
return data.ContainsKey(typeof(T));
}

public T Get<T>()
{
if (dataMultiple.ContainsKey(typeof(T)))
throw new InvalidOperationException("TypeDictionary contains multiple instance of type `{0}`".F(typeof(T)));

object ret;
if (!dataSingular.TryGetValue(typeof(T), out ret))
throw new InvalidOperationException("TypeDictionary does not contain instance of type `{0}`".F(typeof(T)));
return (T)ret;
return (T)Get(typeof(T), true);
}

public T GetOrDefault<T>()
{
return (T)GetOrDefault(typeof(T));
var result = Get(typeof(T), false);
if (result == null)
return default(T);
return (T)result;
}

public object GetOrDefault(Type t)
object Get(Type t, bool throwsIfMissing)
{
if (dataMultiple.ContainsKey(t))
throw new InvalidOperationException("TypeDictionary contains multiple instances of type `{0}`".F(t));

object ret;
if (!dataSingular.TryGetValue(t, out ret))
List<object> ret;
if (!data.TryGetValue(t, out ret))
{
if (throwsIfMissing)
throw new InvalidOperationException("TypeDictionary does not contain instance of type `{0}`".F(t));
return null;
return ret;
}

if (ret.Count > 1)
throw new InvalidOperationException("TypeDictionary contains multiple instances of type `{0}`".F(t));
return ret[0];
}

public IEnumerable<T> WithInterface<T>()
{
List<object> objs;
object obj;

if (dataMultiple.TryGetValue(typeof(T), out objs))
if (data.TryGetValue(typeof(T), out objs))
return objs.Cast<T>();
else if (dataSingular.TryGetValue(typeof(T), out obj))
return new T[] { (T)obj };
else
return new T[0];
return new T[0];
}

public void Remove<T>(T val)
Expand All @@ -104,12 +89,11 @@ public void Remove<T>(T val)
void InnerRemove(Type t, object val)
{
List<object> objs;
object obj;

if (dataMultiple.TryGetValue(t, out objs))
objs.Remove(val);
else if (dataSingular.TryGetValue(t, out obj))
dataSingular.Remove(t);
if (!data.TryGetValue(t, out objs))
return;
objs.Remove(val);
if (objs.Count == 0)
data.Remove(t);
}

public IEnumerator GetEnumerator()
Expand Down