Skip to content

Commit

Permalink
Use foreach for full iteration of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Aug 18, 2020
1 parent 07a6f53 commit b1fd6fb
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 91 deletions.
12 changes: 6 additions & 6 deletions NetFabric.Hyperlinq/Aggregation/Count/Count.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ static unsafe int Count<TSource>(this in ArraySegment<TSource> source, Predicate
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
var result = predicate(array![index]);
var result = predicate(item);
counter += *(int*)&result;
}
}
Expand All @@ -46,11 +45,12 @@ static unsafe int Count<TSource>(this in ArraySegment<TSource> source, Predicate
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
var result = predicate(array![index], index);
var result = predicate(item, index);
counter += *(int*)&result;
index++;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ public static partial class ArrayExtensions
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
{
var item = array![index];
foreach (var item in source.Array)
dictionary.Add(keySelector(item), item);
}
}
else
{
Expand All @@ -48,12 +44,8 @@ public static partial class ArrayExtensions
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
{
var item = array![index];
foreach (var item in source.Array)
dictionary.Add(keySelector(item), elementSelector(item)!);
}
}
else
{
Expand Down
15 changes: 6 additions & 9 deletions NetFabric.Hyperlinq/Element/ElementAt/ElementAt.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ static Option<TSource> ElementAt<TSource>(this in ArraySegment<TSource> source,
{
if (source.IsWhole())
{
var array = source.Array;
for (var sourceIndex = 0; sourceIndex < array.Length; sourceIndex++)
foreach (var item in source.Array)
{
var item = array![sourceIndex];
if (predicate(item) && index-- == 0)
return Option.Some(item);
}
Expand All @@ -47,12 +45,13 @@ static Option<TSource> ElementAt<TSource>(this in ArraySegment<TSource> source,
{
if (source.IsWhole())
{
var array = source.Array;
for (var sourceIndex = 0; sourceIndex < array.Length; sourceIndex++)
var sourceIndex = 0;
foreach (var item in source.Array)
{
var item = array![sourceIndex];
if (predicate(item, sourceIndex) && index-- == 0)
return Option.Some(item);

sourceIndex++;
}
}
else
Expand Down Expand Up @@ -106,10 +105,8 @@ static Option<TSource> ElementAt<TSource>(this in ArraySegment<TSource> source,
{
if (source.IsWhole())
{
var array = source.Array;
for (var sourceIndex = 0; sourceIndex < array.Length; sourceIndex++)
foreach (var item in source.Array)
{
var item = array![sourceIndex];
if (predicate(item) && index-- == 0)
return Option.Some(selector(item));
}
Expand Down
15 changes: 6 additions & 9 deletions NetFabric.Hyperlinq/Element/First/First.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ static Option<TSource> First<TSource>(this in ArraySegment<TSource> source, Pred
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item))
return Option.Some(item);
}
Expand All @@ -47,12 +45,13 @@ static Option<TSource> First<TSource>(this in ArraySegment<TSource> source, Pred
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item, index))
return Option.Some(item);

index++;
}
}
else
Expand Down Expand Up @@ -106,10 +105,8 @@ static Option<TSource> First<TSource>(this in ArraySegment<TSource> source, Pred
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item))
return Option.Some(selector(item));
}
Expand Down
8 changes: 5 additions & 3 deletions NetFabric.Hyperlinq/Projection/Select/Select.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ int IList<TResult>.IndexOf(TResult item)
{
if (Utils.IsValueType<TResult>())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var sourceItem in source.Array)
{
if (EqualityComparer<TResult>.Default.Equals(selector(array![index])!, item))
if (EqualityComparer<TResult>.Default.Equals(selector(sourceItem)!, item))
return index;

index++;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ int IList<TResult>.IndexOf(TResult item)
{
if (Utils.IsValueType<TResult>())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var sourceItem in source.Array)
{
if (EqualityComparer<TResult>.Default.Equals(selector(array![index], index)!, item))
if (EqualityComparer<TResult>.Default.Equals(selector(sourceItem, index)!, item))
return index;
}
}
Expand Down
13 changes: 7 additions & 6 deletions NetFabric.Hyperlinq/Quantifier/All/All.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public static bool All<TSource>(this in ArraySegment<TSource> source, Predicate<
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (!predicate(array![index]))
if (!predicate(item))
return false;
}
}
Expand Down Expand Up @@ -47,11 +46,13 @@ public static bool All<TSource>(this in ArraySegment<TSource> source, PredicateA
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
if (!predicate(array![index], index))
if (!predicate(item, index))
return false;

index++;
}
}
else
Expand Down
13 changes: 7 additions & 6 deletions NetFabric.Hyperlinq/Quantifier/Any/Any.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public static bool Any<TSource>(this in ArraySegment<TSource> source, Predicate<
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (predicate(array![index]))
if (predicate(item))
return true;
}
}
Expand Down Expand Up @@ -51,11 +50,13 @@ public static bool Any<TSource>(this in ArraySegment<TSource> source, PredicateA
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
if (predicate(array![index], index))
if (predicate(item, index))
return true;

index++;
}
}
else
Expand Down
41 changes: 20 additions & 21 deletions NetFabric.Hyperlinq/Quantifier/Contains/Contains.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public static partial class ArrayExtensions
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (EqualityComparer<TSource>.Default.Equals(array![index], value!))
if (EqualityComparer<TSource>.Default.Equals(item, value!))
return true;
}
}
Expand Down Expand Up @@ -51,10 +50,9 @@ static bool DefaultContains(in ArraySegment<TSource> source, [AllowNull] TSource
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (EqualityComparer<TSource>.Default.Equals(array![index], value!))
if (EqualityComparer<TSource>.Default.Equals(item, value!))
return true;
}
}
Expand All @@ -78,10 +76,9 @@ static bool ComparerContains(in ArraySegment<TSource> source, [AllowNull] TSourc
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (comparer.Equals(array![index], value!))
if (comparer.Equals(item, value!))
return true;
}
}
Expand Down Expand Up @@ -117,10 +114,9 @@ static bool ValueContains(in ArraySegment<TSource> source, [AllowNull] TResult v
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (EqualityComparer<TResult>.Default.Equals(selector(array![index])!, value!))
if (EqualityComparer<TResult>.Default.Equals(selector(item)!, value!))
return true;
}
}
Expand All @@ -146,10 +142,9 @@ static bool ReferenceContains(in ArraySegment<TSource> source, [AllowNull] TResu
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
if (defaultComparer.Equals(selector(array![index])!, value!))
if (defaultComparer.Equals(selector(item)!, value!))
return true;
}
}
Expand Down Expand Up @@ -185,11 +180,13 @@ static bool ValueContains(in ArraySegment<TSource> source, [AllowNull] TResult v
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
if (EqualityComparer<TResult>.Default.Equals(selector(array![index], index)!, value!))
if (EqualityComparer<TResult>.Default.Equals(selector(item, index)!, value!))
return true;

index++;
}
}
else
Expand Down Expand Up @@ -227,11 +224,13 @@ static bool ReferenceContains(in ArraySegment<TSource> source, [AllowNull] TResu
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
if (defaultComparer.Equals(selector(array![index], index)!, value!))
if (defaultComparer.Equals(selector(item, index)!, value!))
return true;

index++;
}
}
else
Expand Down
5 changes: 2 additions & 3 deletions NetFabric.Hyperlinq/Set/Distinct/Distinct.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ public void Dispose()
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
_ = set.Add(array![index]);
foreach (var item in source.Array)
_ = set.Add(item);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ static LargeArrayBuilder<TSource> ToArrayBuilder<TSource>(in ArraySegment<TSourc
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item))
builder.Add(item);
}
Expand Down Expand Up @@ -49,12 +47,13 @@ static LargeArrayBuilder<TSource> ToArrayBuilder<TSource>(in ArraySegment<TSourc
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
var index = 0;
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item, index))
builder.Add(item);

index++;
}
}
else
Expand Down Expand Up @@ -95,10 +94,8 @@ static LargeArrayBuilder<TSource> ToArrayBuilder<TSource>(in ArraySegment<TSourc
{
if (source.IsWhole())
{
var array = source.Array;
for (var index = 0; index < array.Length; index++)
foreach (var item in source.Array)
{
var item = array![index];
if (predicate(item))
builder.Add(selector(item));
}
Expand Down
Loading

0 comments on commit b1fd6fb

Please sign in to comment.