From b58220d3c74ba0ef6bc5f34317b66135c9d2520e Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 30 Oct 2018 16:13:48 -0400 Subject: [PATCH] perf: Small enumerable optimizations --- .../Extensions/EnumerableExtensionsTest.cs | 8 +- .../Extensions/EnumerableExtensions.cs | 76 ++++++++++++++++--- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs index 17180f0..2de5281 100644 --- a/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/EnumerableExtensionsTest.cs @@ -335,9 +335,9 @@ namespace ICD.Common.Utils.Tests.Extensions } [Test] - public void ToDictionaryIntTest() + public void ToIndexedDictionaryTest() { - Dictionary values = new[] {1, 2, 3}.ToDictionary(); + Dictionary values = new[] {1, 2, 3}.ToIndexedDictionary(); Assert.AreEqual(3, values.Count); Assert.AreEqual(1, values[0]); @@ -346,9 +346,9 @@ namespace ICD.Common.Utils.Tests.Extensions } [Test] - public void ToDictionaryUIntTest() + public void ToIndexedDictionaryUIntTest() { - Dictionary values = new[] {1, 2, 3}.ToDictionaryUInt(); + Dictionary values = new[] {1, 2, 3}.ToIndexedDictionaryUInt(); Assert.AreEqual(3, values.Count); Assert.AreEqual(1, values[0]); diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index edac918..cd9dd79 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -79,6 +79,15 @@ namespace ICD.Common.Utils.Extensions item = default(T); + IList list = extends as IList; + if (list != null) + { + if (list.Count <= 0) + return false; + item = list[0]; + return true; + } + using (IEnumerator iterator = extends.GetEnumerator()) { while (iterator.MoveNext()) @@ -126,6 +135,16 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentNullException("predicate"); item = default(T); + + IList list = extends as IList; + if (list != null) + { + if (list.Count <= 0) + return false; + item = list[list.Count - 1]; + return true; + } + bool output = false; using (IEnumerator iterator = extends.GetEnumerator()) @@ -156,17 +175,32 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); + if (index < 0) + throw new ArgumentOutOfRangeException("index"); + item = default(T); - try + IList list = extends as IList; + if (list != null) { - item = extends.ElementAt(index); + if (index >= list.Count) + return false; + item = list[index]; return true; } - catch (ArgumentOutOfRangeException) + + int current = 0; + foreach (T value in extends) { - return false; + if (current == index) + { + item = value; + return true; + } + current++; } + + return false; } /// @@ -395,7 +429,11 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - extends.ForEach(item => { }); +// ReSharper disable UnusedVariable + foreach (T item in extends) +// ReSharper restore UnusedVariable + { + } } /// @@ -628,7 +666,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IOrderedEnumerable OrderBy(this IEnumerable extends, IComparer comparer) + public static IOrderedEnumerable Order(this IEnumerable extends, IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -646,7 +684,7 @@ namespace ICD.Common.Utils.Extensions /// /// /// - public static IOrderedEnumerable OrderByDescending(this IEnumerable extends, IComparer comparer) + public static IOrderedEnumerable OrderDescending(this IEnumerable extends, IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -704,8 +742,22 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - ICollection collection = extends as ICollection ?? extends.ToArray(); - collection.CopyTo(array, index); + if (array == null) + throw new ArgumentNullException("array"); + + ICollection collection = extends as ICollection; + if (collection != null) + { + collection.CopyTo(array, index); + return; + } + + int current = 0; + foreach (T item in extends) + { + array[index + current] = item; + current++; + } } /// @@ -719,7 +771,7 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - return new IcdHashSet(extends); + return extends.ToIcdHashSet(EqualityComparer.Default); } /// @@ -790,7 +842,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Dictionary ToDictionary(this IEnumerable extends) + public static Dictionary ToIndexedDictionary(this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends"); @@ -807,7 +859,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static Dictionary ToDictionaryUInt(this IEnumerable extends) + public static Dictionary ToIndexedDictionaryUInt(this IEnumerable extends) { if (extends == null) throw new ArgumentNullException("extends");