From 3f0ca4dd879bedbff5cd3d6d162207ad54b5fbec Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 2 Feb 2018 10:54:47 -0500 Subject: [PATCH] Optimization in Unanimous methods --- .../Extensions/EnumerableExtensions.cs | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index c8a97de..9965da9 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -597,9 +597,9 @@ namespace ICD.Common.Utils.Extensions } /// - /// Returns other if the list is empty. - /// Returns other if the list is non-empty and there are two different elements. - /// Returns the element of the list if it is non-empty and all elements are the same. + /// Returns other if the sequence is empty. + /// Returns other if the sequence is non-empty and there are two different elements. + /// Returns the element of the sequence if it is non-empty and all elements are the same. /// /// /// @@ -610,14 +610,14 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - T[] array = extends as T[] ?? extends.ToArray(); - return array.Unanimous() ? array.First() : other; + T[] array = extends.Distinct().ToArray(); + return array.Length == 1 ? array[0] : other; } /// - /// Returns false if the list is empty. - /// Returns false if the list is non-empty and there are two different elements. - /// Returns true if the list is non-empty and all elements are the same. + /// 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. /// /// /// @@ -627,13 +627,7 @@ namespace ICD.Common.Utils.Extensions if (extends == null) throw new ArgumentNullException("extends"); - T[] array = extends as T[] ?? extends.ToArray(); - - if (array.Length == 0) - return false; - - T val = array.First(); - return array.All(x => EqualityComparer.Default.Equals(x, val)); + return extends.Distinct().Count() == 1; } ///