Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions SharedCode.Core/Linq/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ public static TCollection AddRangeIfRangeNotNull<T, TCollection>(this TCollectio
_ = @this ?? throw new ArgumentNullException(nameof(@this));
_ = predicate ?? throw new ArgumentNullException(nameof(predicate));

foreach (var item in @this.Where(item => predicate(item)))
foreach (var item in @this)
{
return item;
if (predicate(item))
{
return item;
}
}

return default;
Expand All @@ -92,9 +95,12 @@ public static Collection<T> FindAll<T>(this ICollection<T> @this, Predicate<T> p
_ = predicate ?? throw new ArgumentNullException(nameof(predicate));

var all = new Collection<T>();
foreach (var item in @this.Where(item => predicate(item)))
foreach (var item in @this)
{
all.Add(item);
if (predicate(item))
{
all.Add(item);
}
}

return all;
Expand Down Expand Up @@ -175,7 +181,7 @@ public static int FindIndex<T>(
throw new ArgumentOutOfRangeException(nameof(@this));
}

if (predicate(@this.ElementAt(i)))
if (predicate(@this is IList<T> list ? list[i] : @this.ElementAt(i)))
{
return i;
}
Expand All @@ -199,9 +205,10 @@ public static int FindIndex<T>(

for (var i = @this.Count - 1; i >= 0; i--)
{
if (predicate(@this.ElementAt(i)))
var item = @this is IList<T> list ? list[i] : @this.ElementAt(i);
if (predicate(item))
{
return @this.ElementAt(i);
return item;
}
}

Expand Down Expand Up @@ -285,7 +292,7 @@ public static int FindLastIndex<T>(
throw new ArgumentOutOfRangeException(nameof(@this));
}

if (predicate(@this.ElementAt(i)))
if (predicate(@this is IList<T> list ? list[i] : @this.ElementAt(i)))
{
return i;
}
Expand Down Expand Up @@ -360,12 +367,27 @@ public static int RemoveAll<T>(this ICollection<T> @this, Predicate<T> match)
var count = 0;
for (var i = 0; i < @this.Count; i++)
{
if (!match(@this.ElementAt(i)))
if (@this is IList<T> list)
{
Comment thread
wforney marked this conversation as resolved.
continue;
var item = list[i];
if (!match(item))
{
continue;
}

list.RemoveAt(i);
}
else
{
var item = @this.ElementAt(i);
if (!match(item))
{
continue;
}

_ = @this.Remove(item);
}

_ = @this.Remove(@this.ElementAt(i));
count++;
i--;
}
Expand Down
44 changes: 40 additions & 4 deletions SharedCode.Core/Linq/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,43 @@ public static class EnumerableExtensions
/// <param name="defaultValue">The default value.</param>
/// <param name="aggregateFunction">The aggregate function.</param>
/// <returns>The result.</returns>
public static T? Aggregate<T>(this IEnumerable<T> @this, T? defaultValue, Func<T?, T?, T?> aggregateFunction) =>
@this?.Any() ?? false ? System.Linq.Enumerable.Aggregate(@this, (a, b) => aggregateFunction(a, b)!) : defaultValue;
public static T? Aggregate<T>(this IEnumerable<T> @this, T? defaultValue, Func<T?, T?, T?> aggregateFunction)
{
_ = aggregateFunction ?? throw new ArgumentNullException(nameof(aggregateFunction));

if (@this is null)
{
return defaultValue;
}

if (@this is ICollection<T> collection)
{
return collection.Count == 0
? defaultValue
: System.Linq.Enumerable.Aggregate(@this, (a, b) => aggregateFunction(a, b)!);
}

if (@this is IReadOnlyCollection<T> readOnlyCollection)
{
return readOnlyCollection.Count == 0
? defaultValue
: System.Linq.Enumerable.Aggregate(@this, (a, b) => aggregateFunction(a, b)!);
}

using var enumerator = @this.GetEnumerator();
if (!enumerator.MoveNext())
{
return defaultValue;
}

T? result = enumerator.Current;
while (enumerator.MoveNext())
{
result = aggregateFunction(result, enumerator.Current);
}

return result;
}

/// <summary>
/// Starts execution of IQueryable on a ThreadPool thread and returns immediately with a
Expand Down Expand Up @@ -236,7 +271,8 @@ public static int IndexOf<TSource>(this IEnumerable<TSource> @this, TSource valu
/// <returns>
/// <c>true</c> if the source enumerable is not null and contains items; otherwise, <c>false</c>.
/// </returns>
public static bool IsNotNullOrEmpty<T>(this IEnumerable<T> @this) => @this?.Any() == true;
public static bool IsNotNullOrEmpty<T>(this IEnumerable<T> @this) =>
@this is not null && (@this.TryGetNonEnumeratedCount(out var count) ? count > 0 : @this.Any());

/// <summary>
/// Determines whether the source enumerable is null or contains no items.
Expand All @@ -260,7 +296,7 @@ public static int IndexOf<TSource>(this IEnumerable<TSource> @this, TSource valu
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> @this, string sortExpression)
{
sortExpression += string.Empty;
var parts = sortExpression.Split(' ');
var parts = sortExpression.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var descending = false;

if (parts.Length == 0 || string.IsNullOrEmpty(parts[0]))
Expand Down
13 changes: 9 additions & 4 deletions SharedCode.Core/Reflection/DeepCloneGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,15 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] obj, DeepCloneState state)

var rank = obj.Rank;

var lengths = Enumerable.Range(0, rank).Select(obj.GetLength).ToArray();

var lowerBounds = Enumerable.Range(0, rank).Select(obj.GetLowerBound).ToArray();
var idxes = Enumerable.Range(0, rank).Select(obj.GetLowerBound).ToArray();
var lengths = new int[rank];
var lowerBounds = new int[rank];
var idxes = new int[rank];
for (var i = 0; i < rank; i++)
{
lengths[i] = obj.GetLength(i);
lowerBounds[i] = obj.GetLowerBound(i);
idxes[i] = lowerBounds[i];
}

var elementType = obj.GetType().GetElementType();
var outArray = Array.CreateInstance(elementType!, lengths, lowerBounds);
Expand Down
5 changes: 3 additions & 2 deletions SharedCode.DependencyInjection/TypeSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ public ICatalogSelector FromAssemblyDependencies(Assembly assembly)
}
#endif

var assemblies = new List<Assembly> { assembly };
var referencedAssemblies = assembly.GetReferencedAssemblies();
var assemblies = new List<Assembly>(capacity: referencedAssemblies.Length + 1) { assembly };

try
{
foreach (var dependencyName in assembly.GetReferencedAssemblies())
foreach (var dependencyName in referencedAssemblies)
{
try
{
Expand Down
Loading