mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
Optimization
This commit is contained in:
@@ -15,6 +15,7 @@ namespace ICD.Common.Utils
|
|||||||
public static class EnumUtils
|
public static class EnumUtils
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<Type, object[]> s_EnumValuesCache;
|
private static readonly Dictionary<Type, object[]> s_EnumValuesCache;
|
||||||
|
private static readonly Dictionary<Type, Dictionary<object, object[]>> s_EnumFlagsCache;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Static constructor.
|
/// Static constructor.
|
||||||
@@ -22,6 +23,7 @@ namespace ICD.Common.Utils
|
|||||||
static EnumUtils()
|
static EnumUtils()
|
||||||
{
|
{
|
||||||
s_EnumValuesCache = new Dictionary<Type, object[]>();
|
s_EnumValuesCache = new Dictionary<Type, object[]>();
|
||||||
|
s_EnumFlagsCache = new Dictionary<Type, Dictionary<object, object[]>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -276,7 +278,15 @@ namespace ICD.Common.Utils
|
|||||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
|
throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
|
||||||
|
|
||||||
return GetValues<T>().Where(e => HasFlag(value, e));
|
Type type = typeof(T);
|
||||||
|
|
||||||
|
if (!s_EnumFlagsCache.ContainsKey(type))
|
||||||
|
s_EnumFlagsCache.Add(type, new Dictionary<object, object[]>());
|
||||||
|
|
||||||
|
if (!s_EnumFlagsCache[type].ContainsKey(value))
|
||||||
|
s_EnumFlagsCache[type][value] = GetValues<T>().Where(e => HasFlag(value, e)).Cast<object>().ToArray();
|
||||||
|
|
||||||
|
return s_EnumFlagsCache[type][value].Cast<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user