From b8225b78422191404e7b7cc67a83d92e12ed1584 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 25 Jul 2018 14:02:14 -0400 Subject: [PATCH] perf: Simpler HasAnyFlags check, some tidying --- .../Collections/IcdOrderedDictionary.cs | 2 +- ICD.Common.Utils/EnumUtils.cs | 19 +++++++++++++++---- ICD.Common.Utils/Extensions/EnumExtensions.cs | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs b/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs index a688522..2a2c577 100644 --- a/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs +++ b/ICD.Common.Utils/Collections/IcdOrderedDictionary.cs @@ -25,7 +25,7 @@ namespace ICD.Common.Utils.Collections get { return m_OrderedKeys.Select(k => m_Dictionary[k]) - .ToArray(Count); + .ToArray(m_OrderedKeys.Count); } } diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index 1014bd5..eb8cde5 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -102,7 +102,7 @@ namespace ICD.Common.Utils #if SIMPLSHARP return Convert.ChangeType(value, ToEnum(value).GetTypeCode(), CultureInfo.InvariantCulture); #else - return Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); + return Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType())); #endif } @@ -419,7 +419,7 @@ namespace ICD.Common.Utils // ReSharper disable once CompareNonConstrainedGenericWithNull throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); - return (int)(object)value != (int)(object)GetNoneValue() && !HasMultipleFlags(value); + return HasAnyFlags(value) && !HasMultipleFlags(value); } /// @@ -444,7 +444,7 @@ namespace ICD.Common.Utils [PublicAPI] public static bool HasMultipleFlags(int value) { - return ((value & (value - 1)) != 0); + return (value & (value - 1)) != 0; } /// @@ -455,7 +455,18 @@ namespace ICD.Common.Utils [PublicAPI] public static bool HasAnyFlags(T value) { - return GetFlagsExceptNone(value).Any(); + return HasAnyFlags((int)(object)value); + } + + /// + /// Returns true if the enum has any flags set. + /// + /// + /// + [PublicAPI] + public static bool HasAnyFlags(int value) + { + return value > 0; } /// diff --git a/ICD.Common.Utils/Extensions/EnumExtensions.cs b/ICD.Common.Utils/Extensions/EnumExtensions.cs index 9c21d33..f3bceb2 100644 --- a/ICD.Common.Utils/Extensions/EnumExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumExtensions.cs @@ -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 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()); throw new ArgumentException(message); }