mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
perf: Small enumerable optimizations
This commit is contained in:
@@ -335,9 +335,9 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ToDictionaryIntTest()
|
public void ToIndexedDictionaryTest()
|
||||||
{
|
{
|
||||||
Dictionary<int, int> values = new[] {1, 2, 3}.ToDictionary();
|
Dictionary<int, int> values = new[] {1, 2, 3}.ToIndexedDictionary();
|
||||||
|
|
||||||
Assert.AreEqual(3, values.Count);
|
Assert.AreEqual(3, values.Count);
|
||||||
Assert.AreEqual(1, values[0]);
|
Assert.AreEqual(1, values[0]);
|
||||||
@@ -346,9 +346,9 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ToDictionaryUIntTest()
|
public void ToIndexedDictionaryUIntTest()
|
||||||
{
|
{
|
||||||
Dictionary<uint, int> values = new[] {1, 2, 3}.ToDictionaryUInt();
|
Dictionary<uint, int> values = new[] {1, 2, 3}.ToIndexedDictionaryUInt();
|
||||||
|
|
||||||
Assert.AreEqual(3, values.Count);
|
Assert.AreEqual(3, values.Count);
|
||||||
Assert.AreEqual(1, values[0]);
|
Assert.AreEqual(1, values[0]);
|
||||||
|
|||||||
@@ -79,6 +79,15 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
|
|
||||||
item = default(T);
|
item = default(T);
|
||||||
|
|
||||||
|
IList<T> list = extends as IList<T>;
|
||||||
|
if (list != null)
|
||||||
|
{
|
||||||
|
if (list.Count <= 0)
|
||||||
|
return false;
|
||||||
|
item = list[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
using (IEnumerator<T> iterator = extends.GetEnumerator())
|
using (IEnumerator<T> iterator = extends.GetEnumerator())
|
||||||
{
|
{
|
||||||
while (iterator.MoveNext())
|
while (iterator.MoveNext())
|
||||||
@@ -126,6 +135,16 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
throw new ArgumentNullException("predicate");
|
throw new ArgumentNullException("predicate");
|
||||||
|
|
||||||
item = default(T);
|
item = default(T);
|
||||||
|
|
||||||
|
IList<T> list = extends as IList<T>;
|
||||||
|
if (list != null)
|
||||||
|
{
|
||||||
|
if (list.Count <= 0)
|
||||||
|
return false;
|
||||||
|
item = list[list.Count - 1];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool output = false;
|
bool output = false;
|
||||||
|
|
||||||
using (IEnumerator<T> iterator = extends.GetEnumerator())
|
using (IEnumerator<T> iterator = extends.GetEnumerator())
|
||||||
@@ -156,17 +175,32 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (index < 0)
|
||||||
|
throw new ArgumentOutOfRangeException("index");
|
||||||
|
|
||||||
item = default(T);
|
item = default(T);
|
||||||
|
|
||||||
try
|
IList<T> list = extends as IList<T>;
|
||||||
|
if (list != null)
|
||||||
{
|
{
|
||||||
item = extends.ElementAt(index);
|
if (index >= list.Count)
|
||||||
|
return false;
|
||||||
|
item = list[index];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (ArgumentOutOfRangeException)
|
|
||||||
|
int current = 0;
|
||||||
|
foreach (T value in extends)
|
||||||
{
|
{
|
||||||
return false;
|
if (current == index)
|
||||||
|
{
|
||||||
|
item = value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
current++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -395,7 +429,11 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
extends.ForEach(item => { });
|
// ReSharper disable UnusedVariable
|
||||||
|
foreach (T item in extends)
|
||||||
|
// ReSharper restore UnusedVariable
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -628,7 +666,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="comparer"></param>
|
/// <param name="comparer"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -646,7 +684,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="comparer"></param>
|
/// <param name="comparer"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IOrderedEnumerable<T> OrderByDescending<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -704,8 +742,22 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
ICollection<T> collection = extends as ICollection<T> ?? extends.ToArray();
|
if (array == null)
|
||||||
collection.CopyTo(array, index);
|
throw new ArgumentNullException("array");
|
||||||
|
|
||||||
|
ICollection<T> collection = extends as ICollection<T>;
|
||||||
|
if (collection != null)
|
||||||
|
{
|
||||||
|
collection.CopyTo(array, index);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int current = 0;
|
||||||
|
foreach (T item in extends)
|
||||||
|
{
|
||||||
|
array[index + current] = item;
|
||||||
|
current++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -719,7 +771,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
return new IcdHashSet<T>(extends);
|
return extends.ToIcdHashSet(EqualityComparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -790,7 +842,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static Dictionary<int, T> ToDictionary<T>(this IEnumerable<T> extends)
|
public static Dictionary<int, T> ToIndexedDictionary<T>(this IEnumerable<T> extends)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -807,7 +859,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static Dictionary<uint, T> ToDictionaryUInt<T>(this IEnumerable<T> extends)
|
public static Dictionary<uint, T> ToIndexedDictionaryUInt<T>(this IEnumerable<T> extends)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|||||||
Reference in New Issue
Block a user