Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
danielearwicker committed May 4, 2024
1 parent 43c8d70 commit 8792a9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 76 deletions.
48 changes: 10 additions & 38 deletions Source/SuperLinq.Async/SortedMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,7 @@ static async IAsyncEnumerable<TSource> Impl(
foreach (var sequence in sequences)
{
var e = sequence.GetAsyncEnumerator(cancellationToken);
if (await e.MoveNextAsync())
{
enumerators.Add(e);
}
else
{
await e.DisposeAsync();
}
if (await e.MoveNextAsync()) enumerators.Add(e); else await e.DisposeAsync();
}
#if NET6_0_OR_GREATER
var queue = new PriorityQueue<IAsyncEnumerator<TSource>, TKey>(
Expand All @@ -319,18 +312,11 @@ static async IAsyncEnumerable<TSource> Impl(
// Fast drain of final enumerator
if (queue.Count == 0)
{
while (await e.MoveNextAsync())
{
yield return e.Current;
}

while (await e.MoveNextAsync()) yield return e.Current;
break;
}

if (await e.MoveNextAsync())
{
queue.Enqueue(e, keySelector(e.Current));
}
if (await e.MoveNextAsync()) queue.Enqueue(e, keySelector(e.Current));
}

#else
Expand All @@ -353,18 +339,10 @@ static async IAsyncEnumerable<TSource> Impl(
}

var index = Array.BinarySearch(arr, 1, count - 1, e, sourceComparer);
if (index < 0)
{
index = ~index;
}
if (index < 0) index = ~index;

index--;

if (index > 0)
{
Array.Copy(arr, 1, arr, 0, index);
}

if (index > 0) Array.Copy(arr, 1, arr, 0, index);
arr[index] = e;
}

Expand All @@ -373,31 +351,25 @@ static async IAsyncEnumerable<TSource> Impl(
var e = arr[0];
yield return e.Current;

while (await e.MoveNextAsync())
{
yield return e.Current;
}
while (await e.MoveNextAsync()) yield return e.Current;
}
#endif
}
finally
{
foreach (var e in enumerators)
{
await e.DisposeAsync();
}
foreach (var e in enumerators) await e.DisposeAsync();
}
}
}

#if !NET6_0_OR_GREATER
internal sealed record class SourceComparer<TItem, TKey>(
IComparer<TKey> KeyComparer,
Func<TItem, TKey> KeySelector
IComparer<TKey> keyComparer,
Func<TItem, TKey> keySelector
) : IComparer<IAsyncEnumerator<TItem>>
{
public int Compare(IAsyncEnumerator<TItem>? x, IAsyncEnumerator<TItem>? y)
=> KeyComparer.Compare(KeySelector(x!.Current), KeySelector(y!.Current));
=> keyComparer.Compare(keySelector(x!.Current), keySelector(y!.Current));
}
#endif
}
48 changes: 10 additions & 38 deletions Source/SuperLinq/SortedMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,7 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu
foreach (var sequence in sequences)
{
var e = sequence.GetEnumerator();
if (e.MoveNext())
{
enumerators.Add(e);
}
else
{
e.Dispose();
}
if (e.MoveNext()) enumerators.Add(e); else e.Dispose();
}

#if NET6_0_OR_GREATER
Expand All @@ -394,18 +387,11 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu
// Fast drain of final enumerator
if (queue.Count == 0)
{
while (e.MoveNext())
{
yield return e.Current;
}

while (e.MoveNext()) yield return e.Current;
break;
}

if (e.MoveNext())
{
queue.Enqueue(e, keySelector(e.Current));
}
if (e.MoveNext()) queue.Enqueue(e, keySelector(e.Current));
}

#else
Expand All @@ -428,18 +414,10 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu
}

var index = Array.BinarySearch(arr, 1, count - 1, e, sourceComparer);
if (index < 0)
{
index = ~index;
}
if (index < 0) index = ~index;

index--;

if (index > 0)
{
Array.Copy(arr, 1, arr, 0, index);
}

if (index > 0) Array.Copy(arr, 1, arr, 0, index);
arr[index] = e;
}

Expand All @@ -448,31 +426,25 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu
var e = arr[0];
yield return e.Current;

while (e.MoveNext())
{
yield return e.Current;
}
while (e.MoveNext()) yield return e.Current;
}
#endif
}
finally
{
foreach (var e in enumerators)
{
e.Dispose();
}
foreach (var e in enumerators) e.Dispose();
}
}
}

#if !NET6_0_OR_GREATER
internal sealed record class SourceComparer<TItem, TKey>(
IComparer<TKey> KeyComparer,
Func<TItem, TKey> KeySelector
IComparer<TKey> keyComparer,

Check failure on line 442 in Source/SuperLinq/SortedMerge.cs

View workflow job for this annotation

GitHub Actions / build

Naming rule violation: These words must begin with upper case characters: keyComparer (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide1006)

Check failure on line 442 in Source/SuperLinq/SortedMerge.cs

View workflow job for this annotation

GitHub Actions / build

Naming rule violation: These words must begin with upper case characters: keyComparer (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide1006)
Func<TItem, TKey> keySelector

Check failure on line 443 in Source/SuperLinq/SortedMerge.cs

View workflow job for this annotation

GitHub Actions / build

Naming rule violation: These words must begin with upper case characters: keySelector (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide1006)

Check failure on line 443 in Source/SuperLinq/SortedMerge.cs

View workflow job for this annotation

GitHub Actions / build

Naming rule violation: These words must begin with upper case characters: keySelector (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide1006)
) : IComparer<IEnumerator<TItem>>
{
public int Compare(IEnumerator<TItem>? x, IEnumerator<TItem>? y)
=> KeyComparer.Compare(KeySelector(x!.Current), KeySelector(y!.Current));
=> keyComparer.Compare(keySelector(x!.Current), keySelector(y!.Current));
}
#endif
}

0 comments on commit 8792a9c

Please sign in to comment.