Enumerable extensions optimizations

This commit is contained in:
Chris Cameron
2018-02-02 11:25:06 -05:00
parent 3f0ca4dd87
commit b53482cd64

View File

@@ -156,19 +156,26 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
item = default(T);
if (index < 0) if (index < 0)
return false; throw new ArgumentException("Index must be greater or equal to 0", "index");
T[] itemArray = extends as T[] ?? extends.ToArray(); item = default(T);
if (index >= itemArray.Length) int eachIndex = 0;
return false;
item = itemArray[index]; foreach (T each in extends)
{
if (eachIndex == index)
{
item = each;
return true; return true;
} }
eachIndex++;
}
return false;
}
/// <summary> /// <summary>
/// Compares the two sequences for identical values. /// Compares the two sequences for identical values.
/// </summary> /// </summary>
@@ -396,7 +403,9 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
return extends.PrependMany(new[] {item}); yield return item;
foreach (T next in extends)
yield return next;
} }
#endif #endif
@@ -435,7 +444,9 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
return extends.AppendMany(new[] {item}); foreach (T first in extends)
yield return first;
yield return item;
} }
#endif #endif
@@ -487,7 +498,26 @@ namespace ICD.Common.Utils.Extensions
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
return extends.Except(new[] {item}); return extends.Except(item, EqualityComparer<T>.Default);
}
/// <summary>
/// Returns every item in the sequence except the given item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static IEnumerable<T> Except<T>(this IEnumerable<T> extends, T item, IEqualityComparer<T> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (comparer == null)
throw new ArgumentNullException("comparer");
return extends.Where(i => !comparer.Equals(item, i));
} }
/// <summary> /// <summary>