perf: Simpler HasAnyFlags check, some tidying

This commit is contained in:
Chris Cameron
2018-07-25 14:02:14 -04:00
parent 1193c8e3bb
commit b8225b7842
3 changed files with 17 additions and 6 deletions

View File

@@ -25,7 +25,7 @@ namespace ICD.Common.Utils.Collections
get get
{ {
return m_OrderedKeys.Select(k => m_Dictionary[k]) return m_OrderedKeys.Select(k => m_Dictionary[k])
.ToArray(Count); .ToArray(m_OrderedKeys.Count);
} }
} }

View File

@@ -102,7 +102,7 @@ namespace ICD.Common.Utils
#if SIMPLSHARP #if SIMPLSHARP
return Convert.ChangeType(value, ToEnum(value).GetTypeCode(), CultureInfo.InvariantCulture); return Convert.ChangeType(value, ToEnum(value).GetTypeCode(), CultureInfo.InvariantCulture);
#else #else
return Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); return Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
#endif #endif
} }
@@ -419,7 +419,7 @@ 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 (int)(object)value != (int)(object)GetNoneValue<T>() && !HasMultipleFlags(value); return HasAnyFlags(value) && !HasMultipleFlags(value);
} }
/// <summary> /// <summary>
@@ -444,7 +444,7 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static bool HasMultipleFlags(int value) public static bool HasMultipleFlags(int value)
{ {
return ((value & (value - 1)) != 0); return (value & (value - 1)) != 0;
} }
/// <summary> /// <summary>
@@ -455,7 +455,18 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static bool HasAnyFlags<T>(T value) public static bool HasAnyFlags<T>(T value)
{ {
return GetFlagsExceptNone(value).Any(); return HasAnyFlags((int)(object)value);
}
/// <summary>
/// Returns true if the enum has any flags set.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[PublicAPI]
public static bool HasAnyFlags(int value)
{
return value > 0;
} }
/// <summary> /// <summary>

View File

@@ -44,7 +44,7 @@ namespace ICD.Common.Utils.Extensions
// Not as good as the .NET 4 version of this function, but should be good enough // Not as good as the .NET 4 version of this function, but should be good enough
if (extends.GetType() != value.GetType()) if (extends.GetType() != value.GetType())
{ {
string message = string.Format("Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.", string message = string.Format("Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.",
value.GetType(), extends.GetType()); value.GetType(), extends.GetType());
throw new ArgumentException(message); throw new ArgumentException(message);
} }