mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
perf: Micro-optimization for Unanimous extension method
This commit is contained in:
@@ -400,10 +400,19 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
[Test]
|
[Test]
|
||||||
public void UnanimousTest()
|
public void UnanimousTest()
|
||||||
{
|
{
|
||||||
Assert.IsTrue(new[] {true, true, true}.Unanimous());
|
bool result;
|
||||||
Assert.IsTrue(new[] {false, false, false}.Unanimous());
|
|
||||||
Assert.IsFalse(new[] {false, true, false}.Unanimous());
|
Assert.IsTrue(new[] {true, true, true}.Unanimous(out result));
|
||||||
Assert.IsFalse(new bool[] { }.Unanimous());
|
Assert.IsTrue(result);
|
||||||
|
|
||||||
|
Assert.IsTrue(new[] {false, false, false}.Unanimous(out result));
|
||||||
|
Assert.IsFalse(result);
|
||||||
|
|
||||||
|
Assert.IsFalse(new[] {false, true, false}.Unanimous(out result));
|
||||||
|
Assert.AreEqual(default(bool), result);
|
||||||
|
|
||||||
|
Assert.IsFalse(new bool[] { }.Unanimous(out result));
|
||||||
|
Assert.AreEqual(default(bool), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
@@ -865,8 +865,8 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
T[] array = extends.Distinct().ToArray();
|
T item;
|
||||||
return array.Length == 1 ? array[0] : other;
|
return extends.Unanimous(out item) ? item : other;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -875,14 +875,57 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// Returns true if the sequence is non-empty and all elements are the same.
|
/// Returns true if the sequence is non-empty and all elements are the same.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="result"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static bool Unanimous<T>(this IEnumerable<T> extends)
|
public static bool Unanimous<T>(this IEnumerable<T> extends, out T result)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
return extends.Distinct().Count() == 1;
|
return extends.Unanimous(EqualityComparer<T>.Default, out result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns false if the sequence is empty.
|
||||||
|
/// Returns false if the sequence is non-empty and there are two different elements.
|
||||||
|
/// Returns true if the sequence is non-empty and all elements are the same.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
/// <param name="result"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static bool Unanimous<T>(this IEnumerable<T> extends, IEqualityComparer<T> comparer, out T result)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (comparer == null)
|
||||||
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
|
result = default(T);
|
||||||
|
T output = default(T);
|
||||||
|
bool empty = true;
|
||||||
|
|
||||||
|
foreach (T entry in extends)
|
||||||
|
{
|
||||||
|
if (empty)
|
||||||
|
{
|
||||||
|
empty = false;
|
||||||
|
output = entry;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!comparer.Equals(entry, output))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
result = output;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user