diff --git a/ICD.Common.Utils/AttributeUtils.cs b/ICD.Common.Utils/AttributeUtils.cs index 637b8cc..ac671ef 100644 --- a/ICD.Common.Utils/AttributeUtils.cs +++ b/ICD.Common.Utils/AttributeUtils.cs @@ -232,8 +232,10 @@ namespace ICD.Common.Utils CacheType(type); - return s_TypeToAttributesCache.ContainsKey(type) - ? s_TypeToAttributesCache[type].ToArray() + IcdHashSet attributes; + + return s_TypeToAttributesCache.TryGetValue(type, out attributes) + ? attributes.ToArray(attributes.Count) : Enumerable.Empty(); } diff --git a/ICD.Common.Utils/Collections/IcdHashSet.cs b/ICD.Common.Utils/Collections/IcdHashSet.cs index 17fbe65..277a3b9 100644 --- a/ICD.Common.Utils/Collections/IcdHashSet.cs +++ b/ICD.Common.Utils/Collections/IcdHashSet.cs @@ -246,15 +246,7 @@ namespace ICD.Common.Utils.Collections throw new ArgumentNullException("items"); foreach (T item in items) - { - // ReSharper disable CompareNonConstrainedGenericWithNull - if (item == null) - // ReSharper restore CompareNonConstrainedGenericWithNull - throw new InvalidOperationException("item"); - - if (!m_Dict.ContainsKey(item)) - m_Dict[item] = null; - } + Add(item); } /// diff --git a/ICD.Common.Utils/Collections/PriorityQueue.cs b/ICD.Common.Utils/Collections/PriorityQueue.cs index bd7787d..c62ecd0 100644 --- a/ICD.Common.Utils/Collections/PriorityQueue.cs +++ b/ICD.Common.Utils/Collections/PriorityQueue.cs @@ -73,10 +73,14 @@ namespace ICD.Common.Utils.Collections [PublicAPI] public void Enqueue(T item, int priority) { - if (!m_PriorityToQueue.ContainsKey(priority)) - m_PriorityToQueue.Add(priority, new List()); + List queue; + if (!m_PriorityToQueue.TryGetValue(priority, out queue)) + { + queue = new List(); + m_PriorityToQueue[priority] = queue; + } - m_PriorityToQueue[priority].Add(item); + queue.Add(item); m_Count++; } @@ -89,10 +93,14 @@ namespace ICD.Common.Utils.Collections { const int priority = int.MinValue; - if (!m_PriorityToQueue.ContainsKey(priority)) - m_PriorityToQueue.Add(priority, new List()); + List queue; + if (!m_PriorityToQueue.TryGetValue(priority, out queue)) + { + queue = new List(); + m_PriorityToQueue[priority] = queue; + } - m_PriorityToQueue[priority].Insert(0, item); + queue.Insert(0, item); m_Count++; } diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index e327472..b80857a 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -185,18 +185,18 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - if (!s_TypeAllTypes.ContainsKey(extends)) + Type[] types; + if (!s_TypeAllTypes.TryGetValue(extends, out types)) { - Type[] types = - extends.GetBaseTypes() - .Concat(extends.GetInterfaces()) - .Prepend(extends) - .ToArray(); + types = extends.GetBaseTypes() + .Concat(extends.GetInterfaces()) + .Prepend(extends) + .ToArray(); - s_TypeAllTypes.Add(extends, types); + s_TypeAllTypes[extends] = types; } - return s_TypeAllTypes[extends]; + return types; } /// @@ -209,13 +209,14 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - if (!s_TypeBaseTypes.ContainsKey(extends)) + Type[] types; + if (!s_TypeBaseTypes.TryGetValue(extends, out types)) { - Type[] types = GetBaseTypesIterator(extends).ToArray(); - s_TypeBaseTypes.Add(extends, types); + types = GetBaseTypesIterator(extends).ToArray(); + s_TypeBaseTypes[extends] = types; } - return s_TypeBaseTypes[extends]; + return types; } private static IEnumerable GetBaseTypesIterator(Type type) @@ -244,7 +245,8 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - if (!s_TypeImmediateInterfaces.ContainsKey(extends)) + Type[] immediateInterfaces; + if (!s_TypeImmediateInterfaces.TryGetValue(extends, out immediateInterfaces)) { IEnumerable allInterfaces = extends.GetInterfaces(); @@ -254,12 +256,12 @@ namespace ICD.Common.Utils.Extensions .SelectMany(t => t.GetImmediateInterfaces()) .Distinct(); - Type[] immediateInterfaces = allInterfaces.Except(childInterfaces).ToArray(); + immediateInterfaces = allInterfaces.Except(childInterfaces).ToArray(); - s_TypeImmediateInterfaces.Add(extends, immediateInterfaces); + s_TypeImmediateInterfaces[extends] = immediateInterfaces; } - return s_TypeImmediateInterfaces[extends]; + return immediateInterfaces; } /// @@ -272,17 +274,17 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - if (!s_TypeMinimalInterfaces.ContainsKey(extends)) + Type[] minimalInterfaces; + if (!s_TypeMinimalInterfaces.TryGetValue(extends, out minimalInterfaces)) { Type[] allInterfaces = extends.GetInterfaces(); - Type[] minimalInterfaces = - allInterfaces.Except(allInterfaces.SelectMany(t => t.GetInterfaces())) - .ToArray(); + minimalInterfaces = allInterfaces.Except(allInterfaces.SelectMany(t => t.GetInterfaces())) + .ToArray(); - s_TypeMinimalInterfaces.Add(extends, minimalInterfaces); + s_TypeMinimalInterfaces[extends] = minimalInterfaces; } - return s_TypeMinimalInterfaces[extends]; + return minimalInterfaces; } /// diff --git a/ICD.Common.Utils/Services/ServiceProvider.cs b/ICD.Common.Utils/Services/ServiceProvider.cs index 08b82e3..45c9202 100644 --- a/ICD.Common.Utils/Services/ServiceProvider.cs +++ b/ICD.Common.Utils/Services/ServiceProvider.cs @@ -345,10 +345,11 @@ namespace ICD.Common.Utils.Services { m_ServicesSection.Enter(); - if (!m_Services.ContainsKey(tService)) + object stored; + if (!m_Services.TryGetValue(tService, out stored)) return false; - return m_Services[tService] == service && m_Services.Remove(tService); + return service == stored && m_Services.Remove(tService); } finally {