Skip to content

Commit

Permalink
Make ArrayPoolExtensions public
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Sep 23, 2021
1 parent d62f946 commit fea305b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ internal DisposableEnumerator(ArraySegment<TSource> source)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
=> ++index < source.Count;
{
if (index < source.Count)
{
index++;
return index < source.Count;
}
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[ExcludeFromCodeCoverage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ internal Enumerator(in ArraySegmentWhereEnumerable<TSource, TPredicate> enumerab
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
var index = this.index;
while (++index <= end)
{
if (predicate.Invoke(source![index]))
{
this.index = index;
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ internal Enumerator(in MemoryWhereEnumerable<TSource, TPredicate> enumerable)
public bool MoveNext()
{
var span = source.Span;
var index = this.index;
while (++index < span.Length)
{
var item = span[index];
if (predicate.Invoke(item))
{
this.index = index;
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ internal Enumerator(in ArraySegmentWhereAtEnumerable<TSource, TPredicate> enumer
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
var index = this.index;
while (++index <= end)
{
if (predicate.Invoke(source![index + offset], index))
{
this.index = index;
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ internal Enumerator(in ArraySegmentWhereSelectEnumerable<TSource, TResult, TPred
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
var index = this.index;
while (++index <= end)
{
if (predicate.Invoke(source![index]))
{
this.index = index;
return true;
}
}
return false;
}
Expand Down
18 changes: 14 additions & 4 deletions NetFabric.Hyperlinq/Lease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class Lease<T>
T[]? rented;
int length;

public Lease(ArrayPool<T> pool, int length, bool clearOnDispose = false)
internal Lease(ArrayPool<T> pool, int length, bool clearOnDispose)
{
Debug.Assert(length >= 0);

Expand All @@ -37,7 +37,7 @@ public Lease(ArrayPool<T> pool, int length, bool clearOnDispose = false)
this.clearOnDispose = clearOnDispose;
rented = length is 0
? Array.Empty<T>()
: this.pool.Rent(length);
: pool.Rent(length);
}

/// <summary>
Expand Down Expand Up @@ -163,7 +163,12 @@ internal Enumerator(T[] source, int length)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
=> ++index < length;
{
if (index >= length)
return false;
index++;
return index < length;
}
}

public struct DisposableEnumerator
Expand Down Expand Up @@ -192,7 +197,12 @@ internal DisposableEnumerator(T[] source, int length)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
=> ++index < length;
{
if (index >= length)
return false;
index++;
return index < length;
}

public void Reset()
=> index = -1;
Expand Down
2 changes: 1 addition & 1 deletion NetFabric.Hyperlinq/NetFabric.Hyperlinq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageId>NetFabric.Hyperlinq</PackageId>
<Title>NetFabric.Hyperlinq</Title>
<Description> High performance LINQ implementation with minimal heap allocations. Supports enumerables, async enumerables, Memory, and Span.</Description>
<Version>3.0.0-beta47</Version>
<Version>3.0.0-beta48</Version>
<PackageIcon>Icon.png</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageTags>netfabric, hyperlinq, linq, enumeration, extensions, performance</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ public TResult Current

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
=> ++index <= end;
{
if (index <= end)
{
index++;
return index <= end;
}
return false;
}

[ExcludeFromCodeCoverage]
[DoesNotReturn]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ public TResult Current

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
=> ++index <= end;

{
if (index <= end)
{
index++;
return index <= end;
}
return false;
}

[ExcludeFromCodeCoverage]
[DoesNotReturn]
public readonly void Reset()
Expand Down
4 changes: 4 additions & 0 deletions NetFabric.Hyperlinq/Set/Distinct/Distinct.ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ internal Enumerator(in ArraySegmentDistinctEnumerable<TSource> enumerable)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
var index = this.index;
while (++index <= end)
{
if (set.Add(source![index]))
{
this.index = index;
return true;
}
}
return false;
}
Expand Down
9 changes: 6 additions & 3 deletions NetFabric.Hyperlinq/Utils/ArrayPoolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace NetFabric.Hyperlinq
{
static class ArrayPoolExtensions
public static class ArrayPoolExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Lease<T> Lease<T>(this ArrayPool<T> pool, int length, bool clearOnDispose)
=> new(pool, length, clearOnDispose);
public static Lease<T> Lease<T>(this ArrayPool<T> pool, int length, bool clearOnDispose = default)
=> length < 0
? Throw.ArgumentOutOfRangeException<Lease<T>>(nameof(length))
// ReSharper disable once HeapView.ObjectAllocation.Evident
: new Lease<T>(pool, length, clearOnDispose);
}
}

0 comments on commit fea305b

Please sign in to comment.