From b6fd7ba089899182fa7cfeabe2696f69bc827fe5 Mon Sep 17 00:00:00 2001
From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com>
Date: Fri, 2 Feb 2024 17:50:07 +0000
Subject: [PATCH 1/5] Add primary constructors
---
.editorconfig | 7 +-
Directory.Build.props | 2 +-
Source/SuperLinq.Async/EnumeratorList.cs | 31 +++---
Source/SuperLinq.Async/GroupAdjacent.cs | 15 ++-
Source/SuperLinq.Async/Join.MergeJoin.cs | 13 +--
Source/SuperLinq.Async/Memoize.cs | 11 +-
Source/SuperLinq.Async/Publish.cs | 11 +-
Source/SuperLinq.Async/Share.cs | 11 +-
Source/SuperLinq/AssertCount.cs | 48 ++++-----
Source/SuperLinq/Batch.cs | 38 +++----
Source/SuperLinq/CountDown.cs | 62 +++++------
Source/SuperLinq/Exclude.cs | 64 +++++------
Source/SuperLinq/FallbackIfEmpty.cs | 26 ++---
Source/SuperLinq/FillBackward.cs | 33 +++---
Source/SuperLinq/FillForward.cs | 33 +++---
Source/SuperLinq/GroupAdjacent.cs | 15 ++-
Source/SuperLinq/Index.cs | 40 +++----
Source/SuperLinq/Insert.cs | 102 ++++++++----------
Source/SuperLinq/Interleave.cs | 15 +--
Source/SuperLinq/Join.MergeJoin.cs | 13 +--
.../SuperLinq/KeyValuePairEqualityComparer.cs | 17 ++-
Source/SuperLinq/Lag.cs | 36 +++----
Source/SuperLinq/Lead.cs | 42 +++-----
Source/SuperLinq/Memoize.cs | 36 +++----
Source/SuperLinq/Pad.cs | 65 +++++------
Source/SuperLinq/PadStart.cs | 72 +++++--------
Source/SuperLinq/Publish.cs | 13 +--
Source/SuperLinq/Replace.cs | 45 ++++----
Source/SuperLinq/Return.cs | 16 ++-
Source/SuperLinq/ReverseComparer.cs | 11 +-
Source/SuperLinq/ScanRight.cs | 28 ++---
Source/SuperLinq/Sequence.cs | 25 ++---
Source/SuperLinq/Share.cs | 11 +-
Source/SuperLinq/TagFirstLast.cs | 32 +++---
Source/SuperLinq/ValueTupleComparer.cs | 17 ++-
.../SuperLinq/ValueTupleEqualityComparer.cs | 29 ++---
Source/SuperLinq/Window.cs | 40 +++----
Source/SuperLinq/WindowLeft.cs | 52 ++++-----
Source/SuperLinq/WindowRight.cs | 40 +++----
Source/SuperLinq/ZipMap.cs | 24 ++---
Tests/SuperLinq.Async.Test/TestingSequence.cs | 5 +-
Tests/SuperLinq.Async.Test/TraverseTest.cs | 15 ++-
Tests/SuperLinq.Test/EqualityComparer.cs | 15 ++-
Tests/SuperLinq.Test/FlattenTest.cs | 18 ++--
Tests/SuperLinq.Test/MemoizeTest.cs | 22 ++--
Tests/SuperLinq.Test/ReadOnlyCollection.cs | 8 +-
Tests/SuperLinq.Test/TestingSequence.cs | 5 +-
Tests/SuperLinq.Test/ToDataTableTest.cs | 20 +---
Tests/SuperLinq.Test/TraverseTest.cs | 15 ++-
Tests/SuperLinq.Test/TrySingleTest.cs | 11 +-
50 files changed, 538 insertions(+), 837 deletions(-)
diff --git a/.editorconfig b/.editorconfig
index 34d8b9f50..12e46e3f0 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -209,7 +209,6 @@ dotnet_analyzer_diagnostic.category-Style.severity = warning
dotnet_diagnostic.IDE0011.severity = silent # IDE0011: Add braces
dotnet_diagnostic.IDE0046.severity = silent # IDE0046: Convert to conditional expression
-dotnet_diagnostic.IDE0290.severity = none # IDE0290: Use primary constructor
dotnet_diagnostic.IDE0028.severity = none # IDE0028: Simplify collection initialization
dotnet_diagnostic.IDE0305.severity = none # IDE0305: Simplify collection initialization
@@ -241,3 +240,9 @@ dotnet_diagnostic.RS0026.severity = none # RS0026: Do not add multiple public
# Bug in compiler
dotnet_diagnostic.CA1861.severity = none # CA1861: Avoid constant arrays as arguments
+
+# Primary Constructors
+dotnet_diagnostic.CS9107.severity = error # CS9107: Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well. This warning indicates that your code may be allocated two copies of a primary constructor parameter. Because the parameter is passed to the base class, the base class likely uses it. Because the derived class accesses it, it may have a second copy of the same parameter. That extra storage may not be intended
+dotnet_diagnostic.CS9113.severity = error # CS9113: Parameter is not being used
+dotnet_diagnostic.CS9124.severity = error # CS9124: Use primary constructor
+dotnet_diagnostic.CS9179.severity = error # CS9179: Use primary constructor
diff --git a/Directory.Build.props b/Directory.Build.props
index 96155806c..e78f31022 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -8,7 +8,7 @@
enable
false
-
+
latest-all
true
diff --git a/Source/SuperLinq.Async/EnumeratorList.cs b/Source/SuperLinq.Async/EnumeratorList.cs
index de982d69e..f8015406f 100644
--- a/Source/SuperLinq.Async/EnumeratorList.cs
+++ b/Source/SuperLinq.Async/EnumeratorList.cs
@@ -1,9 +1,9 @@
namespace SuperLinq.Async;
-internal sealed class EnumeratorList : IAsyncDisposable
+internal sealed class EnumeratorList(
+ List.Enumerator> iter
+) : IAsyncDisposable
{
- private readonly List.Enumerator> _iter;
-
internal static async ValueTask> Create(IEnumerable> sources, CancellationToken cancellationToken)
{
var list = new List.Enumerator>();
@@ -24,24 +24,19 @@ internal static async ValueTask> Create(IEnumerable.Enumerator> iter)
- {
- _iter = iter;
- }
-
- public int Count => _iter.Count;
+ public int Count => iter.Count;
- public bool Any() => _iter.Count != 0;
+ public bool Any() => iter.Count != 0;
public async ValueTask MoveNext(int i)
{
- while (i < _iter.Count)
+ while (i < iter.Count)
{
- var e = _iter[i];
+ var e = iter[i];
if (await e.MoveNextAsync())
return true;
- _iter.RemoveAt(i);
+ iter.RemoveAt(i);
await e.DisposeAsync();
}
@@ -50,13 +45,13 @@ public async ValueTask MoveNext(int i)
public async ValueTask MoveNextOnce(int i)
{
- if (i < _iter.Count)
+ if (i < iter.Count)
{
- var e = _iter[i];
+ var e = iter[i];
if (await e.MoveNextAsync())
return true;
- _iter.RemoveAt(i);
+ iter.RemoveAt(i);
await e.DisposeAsync();
}
@@ -64,11 +59,11 @@ public async ValueTask MoveNextOnce(int i)
}
public T Current(int i) =>
- _iter[i].Current;
+ iter[i].Current;
public async ValueTask DisposeAsync()
{
- foreach (var e in _iter)
+ foreach (var e in iter)
await e.DisposeAsync();
}
}
diff --git a/Source/SuperLinq.Async/GroupAdjacent.cs b/Source/SuperLinq.Async/GroupAdjacent.cs
index 648eacd4a..02fbe41f4 100644
--- a/Source/SuperLinq.Async/GroupAdjacent.cs
+++ b/Source/SuperLinq.Async/GroupAdjacent.cs
@@ -297,17 +297,14 @@ private static Grouping CreateGroupAdjacentGrouping : IGrouping
+ private sealed class Grouping(
+ TKey key,
+ IEnumerable members
+ ) : IGrouping
{
- private readonly IEnumerable _members;
+ private readonly IEnumerable _members = members;
- public Grouping(TKey key, IEnumerable members)
- {
- Key = key;
- _members = members;
- }
-
- public TKey Key { get; }
+ public TKey Key { get; } = key;
public IEnumerator GetEnumerator() => _members.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
diff --git a/Source/SuperLinq.Async/Join.MergeJoin.cs b/Source/SuperLinq.Async/Join.MergeJoin.cs
index c15dc4f8d..d639fcc01 100644
--- a/Source/SuperLinq.Async/Join.MergeJoin.cs
+++ b/Source/SuperLinq.Async/Join.MergeJoin.cs
@@ -717,15 +717,10 @@ private static async IAsyncEnumerable JoinMerge : IEqualityComparer
+file sealed class ComparerEqualityComparer(
+ IComparer comparer
+) : IEqualityComparer
{
- private readonly IComparer _comparer;
-
- public ComparerEqualityComparer(IComparer comparer)
- {
- _comparer = comparer;
- }
-
- public bool Equals([AllowNull] TKey x, [AllowNull] TKey y) => _comparer.Compare(x, y) == 0;
+ public bool Equals([AllowNull] TKey x, [AllowNull] TKey y) => comparer.Compare(x, y) == 0;
public int GetHashCode([DisallowNull] TKey obj) => ThrowHelper.ThrowNotSupportedException();
}
diff --git a/Source/SuperLinq.Async/Memoize.cs b/Source/SuperLinq.Async/Memoize.cs
index c9c63108c..01c7018e2 100644
--- a/Source/SuperLinq.Async/Memoize.cs
+++ b/Source/SuperLinq.Async/Memoize.cs
@@ -32,11 +32,13 @@ public static IAsyncBuffer Memoize(this IAsyncEnumerable(source);
}
- private sealed class EnumerableMemoizedBuffer : IAsyncBuffer
+ private sealed class EnumerableMemoizedBuffer(
+ IAsyncEnumerable source
+ ) : IAsyncBuffer
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);
- private IAsyncEnumerable? _source;
+ private IAsyncEnumerable? _source = source;
private IAsyncEnumerator? _enumerator;
private List _buffer = new();
@@ -47,11 +49,6 @@ private sealed class EnumerableMemoizedBuffer : IAsyncBuffer
private bool _disposed;
- public EnumerableMemoizedBuffer(IAsyncEnumerable source)
- {
- _source = source;
- }
-
public int Count => _buffer.Count;
public async ValueTask Reset(CancellationToken cancellationToken = default)
diff --git a/Source/SuperLinq.Async/Publish.cs b/Source/SuperLinq.Async/Publish.cs
index f3302edfb..561022f09 100644
--- a/Source/SuperLinq.Async/Publish.cs
+++ b/Source/SuperLinq.Async/Publish.cs
@@ -22,11 +22,13 @@ public static IAsyncBuffer Publish(this IAsyncEnumerable(source);
}
- private sealed class PublishBuffer : IAsyncBuffer
+ private sealed class PublishBuffer(
+ IAsyncEnumerable source
+ ) : IAsyncBuffer
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);
- private IAsyncEnumerable? _source;
+ private IAsyncEnumerable? _source = source;
private IAsyncEnumerator? _enumerator;
private List>? _buffers;
@@ -38,11 +40,6 @@ private sealed class PublishBuffer : IAsyncBuffer
private bool _disposed;
- public PublishBuffer(IAsyncEnumerable source)
- {
- _source = source;
- }
-
public int Count => _buffers?.Count > 0 ? _buffers.Max(x => x.Count) : 0;
public async ValueTask Reset(CancellationToken cancellationToken = default)
diff --git a/Source/SuperLinq.Async/Share.cs b/Source/SuperLinq.Async/Share.cs
index b7a404331..33495099f 100644
--- a/Source/SuperLinq.Async/Share.cs
+++ b/Source/SuperLinq.Async/Share.cs
@@ -19,11 +19,13 @@ public static IAsyncBuffer Share(this IAsyncEnumerable(source);
}
- private sealed class SharedBuffer : IAsyncBuffer
+ private sealed class SharedBuffer(
+ IAsyncEnumerable source
+ ) : IAsyncBuffer
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);
- private IAsyncEnumerable? _source;
+ private IAsyncEnumerable? _source = source;
private IAsyncEnumerator? _enumerator;
private bool _initialized;
@@ -33,11 +35,6 @@ private sealed class SharedBuffer : IAsyncBuffer
private bool _disposed;
- public SharedBuffer(IAsyncEnumerable source)
- {
- _source = source;
- }
-
public int Count => 0;
public async ValueTask Reset(CancellationToken cancellationToken = default)
diff --git a/Source/SuperLinq/AssertCount.cs b/Source/SuperLinq/AssertCount.cs
index d00b17026..db262b9d6 100644
--- a/Source/SuperLinq/AssertCount.cs
+++ b/Source/SuperLinq/AssertCount.cs
@@ -57,31 +57,25 @@ static IEnumerable Core(IEnumerable source, int count)
}
}
- private sealed class AssertCountCollectionIterator : CollectionIterator
+ private sealed class AssertCountCollectionIterator(
+ IEnumerable source,
+ int count
+ ) : CollectionIterator
{
- private readonly IEnumerable _source;
- private readonly int _count;
-
- public AssertCountCollectionIterator(IEnumerable source, int count)
- {
- _source = source;
- _count = count;
- }
-
public override int Count
{
get
{
- ArgumentOutOfRangeException.ThrowIfNotEqual(_source.GetCollectionCount(), _count, "source.Count()");
- return _count;
+ ArgumentOutOfRangeException.ThrowIfNotEqual(source.GetCollectionCount(), count, "source.Count()");
+ return count;
}
}
protected override IEnumerable GetEnumerable()
{
- ArgumentOutOfRangeException.ThrowIfNotEqual(_source.GetCollectionCount(), _count, "source.Count()");
+ ArgumentOutOfRangeException.ThrowIfNotEqual(source.GetCollectionCount(), count, "source.Count()");
- foreach (var item in _source)
+ foreach (var item in source)
yield return item;
}
@@ -91,27 +85,21 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
- _ = _source.CopyTo(array, arrayIndex);
+ _ = source.CopyTo(array, arrayIndex);
}
}
- private sealed class AssertCountListIterator : ListIterator
+ private sealed class AssertCountListIterator(
+ IList source,
+ int count
+ ) : ListIterator
{
- private readonly IList _source;
- private readonly int _count;
-
- public AssertCountListIterator(IList source, int count)
- {
- _source = source;
- _count = count;
- }
-
public override int Count
{
get
{
- ArgumentOutOfRangeException.ThrowIfNotEqual(_source.Count, _count, "source.Count()");
- return _count;
+ ArgumentOutOfRangeException.ThrowIfNotEqual(source.Count, count, "source.Count()");
+ return count;
}
}
@@ -120,7 +108,7 @@ protected override IEnumerable GetEnumerable()
var cnt = (uint)Count;
for (var i = 0; i < cnt; i++)
{
- yield return _source[i];
+ yield return source[i];
}
}
@@ -130,7 +118,7 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
- _source.CopyTo(array, arrayIndex);
+ source.CopyTo(array, arrayIndex);
}
protected override T ElementAt(int index)
@@ -138,7 +126,7 @@ protected override T ElementAt(int index)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
- return _source[index];
+ return source[index];
}
}
}
diff --git a/Source/SuperLinq/Batch.cs b/Source/SuperLinq/Batch.cs
index 818a82e03..15be4e540 100644
--- a/Source/SuperLinq/Batch.cs
+++ b/Source/SuperLinq/Batch.cs
@@ -86,28 +86,22 @@ static IEnumerable> Core(IEnumerable source, int size)
}
}
- private sealed class BatchIterator : ListIterator>
+ private sealed class BatchIterator(
+ IList source,
+ int size
+ ) : ListIterator>
{
- private readonly IList _source;
- private readonly int _size;
-
- public BatchIterator(IList source, int size)
- {
- _source = source;
- _size = size;
- }
-
- public override int Count => _source.Count == 0 ? 0 : ((_source.Count - 1) / _size) + 1;
+ public override int Count => source.Count == 0 ? 0 : ((source.Count - 1) / size) + 1;
protected override IEnumerable> GetEnumerable()
{
var sourceIndex = 0;
- var count = (uint)_source.Count;
- while (sourceIndex + _size - 1 < count)
+ var count = (uint)source.Count;
+ while (sourceIndex + size - 1 < count)
{
- var window = new T[_size];
- for (var i = 0; i < _size && sourceIndex < count; sourceIndex++, i++)
- window[i] = _source[sourceIndex];
+ var window = new T[size];
+ for (var i = 0; i < size && sourceIndex < count; sourceIndex++, i++)
+ window[i] = source[sourceIndex];
yield return window;
}
@@ -116,7 +110,7 @@ protected override IEnumerable> GetEnumerable()
{
var window = new T[count - sourceIndex];
for (var j = 0; sourceIndex < count; sourceIndex++, j++)
- window[j] = _source[sourceIndex];
+ window[j] = source[sourceIndex];
yield return window;
}
@@ -127,11 +121,11 @@ protected override IList ElementAt(int index)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
- var start = index * _size;
- var max = (uint)Math.Min(_source.Count, start + _size);
- var arr = new T[Math.Min(_size, max - start)];
- for (int i = 0, j = start; i < _size && j < max; i++, j++)
- arr[i] = _source[j];
+ var start = index * size;
+ var max = (uint)Math.Min(source.Count, start + size);
+ var arr = new T[Math.Min(size, max - start)];
+ for (int i = 0, j = start; i < size && j < max; i++, j++)
+ arr[i] = source[j];
return arr;
}
diff --git a/Source/SuperLinq/CountDown.cs b/Source/SuperLinq/CountDown.cs
index 4d62e319a..b01b3c2df 100644
--- a/Source/SuperLinq/CountDown.cs
+++ b/Source/SuperLinq/CountDown.cs
@@ -19,7 +19,7 @@ public static partial class SuperEnumerable
///
/// A sequence of tuples containing the element and it's count from the end of the sequence, or .
- ///
+ ///
///
/// is .
///
@@ -110,60 +110,46 @@ static IEnumerable Core(IEnumerable source, int count, Func : CollectionIterator
+ private sealed class CountDownCollectionIterator(
+ IEnumerable source,
+ int count,
+ Func resultSelector
+ ) : CollectionIterator
{
- private readonly IEnumerable _source;
- private readonly int _count;
- private readonly Func _resultSelector;
-
- public CountDownCollectionIterator(IEnumerable source, int count, Func resultSelector)
- {
- _source = source;
- _count = count;
- _resultSelector = resultSelector;
- }
-
- public override int Count => _source.GetCollectionCount();
+ public override int Count => source.GetCollectionCount();
protected override IEnumerable GetEnumerable()
{
var i = Count;
- foreach (var item in _source)
- yield return _resultSelector(item, i-- <= _count ? i : null);
+ foreach (var item in source)
+ yield return resultSelector(item, i-- <= count ? i : null);
}
}
- private sealed class CountDownListIterator : ListIterator
+ private sealed class CountDownListIterator(
+ IList source,
+ int count,
+ Func resultSelector
+ ) : ListIterator
{
- private readonly IList _source;
- private readonly int _count;
- private readonly Func _resultSelector;
-
- public CountDownListIterator(IList source, int count, Func resultSelector)
- {
- _source = source;
- _count = count;
- _resultSelector = resultSelector;
- }
-
- public override int Count => _source.Count;
+ public override int Count => source.Count;
protected override IEnumerable GetEnumerable()
{
- var cnt = (uint)_source.Count - _count;
+ var cnt = (uint)source.Count - count;
var i = 0;
for (; i < cnt; i++)
{
- yield return _resultSelector(
- _source[i],
+ yield return resultSelector(
+ source[i],
null);
}
- cnt = (uint)_source.Count;
+ cnt = (uint)source.Count;
for (; i < cnt; i++)
{
- yield return _resultSelector(
- _source[i],
+ yield return resultSelector(
+ source[i],
(int)cnt - i - 1);
}
}
@@ -173,9 +159,9 @@ protected override TResult ElementAt(int index)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
- return _resultSelector(
- _source[index],
- _source.Count - index < _count ? _source.Count - index - 1 : null);
+ return resultSelector(
+ source[index],
+ source.Count - index < count ? source.Count - index - 1 : null);
}
}
}
diff --git a/Source/SuperLinq/Exclude.cs b/Source/SuperLinq/Exclude.cs
index ec1549f0f..065f6506a 100644
--- a/Source/SuperLinq/Exclude.cs
+++ b/Source/SuperLinq/Exclude.cs
@@ -46,54 +46,40 @@ public static IEnumerable Exclude(this IEnumerable sequence, int startI
};
}
- private sealed class ExcludeCollectionIterator : CollectionIterator
+ private sealed class ExcludeCollectionIterator(
+ ICollection source,
+ int startIndex,
+ int count
+ ) : CollectionIterator
{
- private readonly ICollection _source;
- private readonly int _startIndex;
- private readonly int _count;
-
- public ExcludeCollectionIterator(ICollection source, int startIndex, int count)
- {
- _source = source;
- _startIndex = startIndex;
- _count = count;
- }
-
public override int Count =>
- _source.Count < _startIndex ? _source.Count :
- _source.Count < _startIndex + _count ? _startIndex :
- _source.Count - _count;
+ source.Count < startIndex ? source.Count :
+ source.Count < startIndex + count ? startIndex :
+ source.Count - count;
protected override IEnumerable GetEnumerable() =>
- ExcludeCore(_source, _startIndex, _count);
+ ExcludeCore(source, startIndex, count);
}
- private sealed class ExcludeListIterator : ListIterator
+ private sealed class ExcludeListIterator(
+ IList source,
+ int startIndex,
+ int count
+ ) : ListIterator
{
- private readonly IList _source;
- private readonly int _startIndex;
- private readonly int _count;
-
- public ExcludeListIterator(IList source, int startIndex, int count)
- {
- _source = source;
- _startIndex = startIndex;
- _count = count;
- }
-
public override int Count =>
- _source.Count < _startIndex ? _source.Count :
- _source.Count < _startIndex + _count ? _startIndex :
- _source.Count - _count;
+ source.Count < startIndex ? source.Count :
+ source.Count < startIndex + count ? startIndex :
+ source.Count - count;
protected override IEnumerable GetEnumerable()
{
- var cnt = (uint)_source.Count;
- for (var i = 0; i < cnt && i < _startIndex; i++)
- yield return _source[i];
+ var cnt = (uint)source.Count;
+ for (var i = 0; i < cnt && i < startIndex; i++)
+ yield return source[i];
- for (var i = _startIndex + _count; i < cnt; i++)
- yield return _source[i];
+ for (var i = startIndex + count; i < cnt; i++)
+ yield return source[i];
}
protected override T ElementAt(int index)
@@ -101,9 +87,9 @@ protected override T ElementAt(int index)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
- return index < _startIndex
- ? _source[index]
- : _source[index + _count];
+ return index < startIndex
+ ? source[index]
+ : source[index + count];
}
}
diff --git a/Source/SuperLinq/FallbackIfEmpty.cs b/Source/SuperLinq/FallbackIfEmpty.cs
index 7dcc2ea10..36406933d 100644
--- a/Source/SuperLinq/FallbackIfEmpty.cs
+++ b/Source/SuperLinq/FallbackIfEmpty.cs
@@ -92,27 +92,21 @@ static IEnumerable Core(IEnumerable source, IEnumerable fallback)
}
}
- private sealed class FallbackIfEmptyCollectionIterator : CollectionIterator
+ private sealed class FallbackIfEmptyCollectionIterator(
+ IEnumerable source,
+ IEnumerable fallback
+ ) : CollectionIterator
{
- private readonly IEnumerable _source;
- private readonly IEnumerable _fallback;
-
- public FallbackIfEmptyCollectionIterator(IEnumerable source, IEnumerable fallback)
- {
- _source = source;
- _fallback = fallback;
- }
-
public override int Count =>
- _source.GetCollectionCount() == 0
- ? _fallback.Count()
- : _source.GetCollectionCount();
+ source.GetCollectionCount() == 0
+ ? fallback.Count()
+ : source.GetCollectionCount();
protected override IEnumerable GetEnumerable()
{
- return _source.GetCollectionCount() == 0
- ? _fallback
- : _source;
+ return source.GetCollectionCount() == 0
+ ? fallback
+ : source;
}
}
}
diff --git a/Source/SuperLinq/FillBackward.cs b/Source/SuperLinq/FillBackward.cs
index 9c44ef9bc..3cd6bf313 100644
--- a/Source/SuperLinq/FillBackward.cs
+++ b/Source/SuperLinq/FillBackward.cs
@@ -138,24 +138,17 @@ private static IEnumerable FillBackwardCore(IEnumerable source, Func : CollectionIterator
+ private sealed class FillBackwardCollection(
+ ICollection source,
+ Func predicate,
+ Func? fillSelector
+ ) : CollectionIterator
{
- private readonly ICollection _source;
- private readonly Func _predicate;
- private readonly Func? _fillSelector;
-
- public FillBackwardCollection(ICollection source, Func predicate, Func? fillSelector)
- {
- _source = source;
- _predicate = predicate;
- _fillSelector = fillSelector;
- }
-
- public override int Count => _source.Count;
+ public override int Count => source.Count;
[ExcludeFromCodeCoverage]
protected override IEnumerable GetEnumerable() =>
- FillBackwardCore(_source, _predicate, _fillSelector);
+ FillBackwardCore(source, predicate, fillSelector);
public override void CopyTo(T[] array, int arrayIndex)
{
@@ -163,10 +156,10 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, Count);
- _source.CopyTo(array, arrayIndex);
+ source.CopyTo(array, arrayIndex);
- var i = arrayIndex + _source.Count - 1;
- for (; i >= arrayIndex && _predicate(array[i]); i--)
+ var i = arrayIndex + source.Count - 1;
+ for (; i >= arrayIndex && predicate(array[i]); i--)
;
if (i < arrayIndex)
@@ -175,10 +168,10 @@ public override void CopyTo(T[] array, int arrayIndex)
var last = array[i--];
for (; i >= arrayIndex; i--)
{
- if (_predicate(array[i]))
+ if (predicate(array[i]))
{
- array[i] = _fillSelector != null
- ? _fillSelector(array[i], last)
+ array[i] = fillSelector != null
+ ? fillSelector(array[i], last)
: last;
}
else
diff --git a/Source/SuperLinq/FillForward.cs b/Source/SuperLinq/FillForward.cs
index ee7c9e040..562506616 100644
--- a/Source/SuperLinq/FillForward.cs
+++ b/Source/SuperLinq/FillForward.cs
@@ -127,24 +127,17 @@ private static IEnumerable FillForwardCore(IEnumerable source, Func : CollectionIterator
+ private sealed class FillForwardCollection(
+ ICollection source,
+ Func predicate,
+ Func? fillSelector
+ ) : CollectionIterator
{
- private readonly ICollection _source;
- private readonly Func _predicate;
- private readonly Func? _fillSelector;
-
- public FillForwardCollection(ICollection source, Func predicate, Func? fillSelector)
- {
- _source = source;
- _predicate = predicate;
- _fillSelector = fillSelector;
- }
-
- public override int Count => _source.Count;
+ public override int Count => source.Count;
[ExcludeFromCodeCoverage]
protected override IEnumerable GetEnumerable() =>
- FillForwardCore(_source, _predicate, _fillSelector);
+ FillForwardCore(source, predicate, fillSelector);
public override void CopyTo(T[] array, int arrayIndex)
{
@@ -152,11 +145,11 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, Count);
- _source.CopyTo(array, arrayIndex);
+ source.CopyTo(array, arrayIndex);
var i = arrayIndex;
- var max = arrayIndex + _source.Count;
- for (; i < max && _predicate(array[i]); i++)
+ var max = arrayIndex + source.Count;
+ for (; i < max && predicate(array[i]); i++)
;
if (i >= max)
@@ -165,10 +158,10 @@ public override void CopyTo(T[] array, int arrayIndex)
var last = array[i++];
for (; i < max; i++)
{
- if (_predicate(array[i]))
+ if (predicate(array[i]))
{
- array[i] = _fillSelector != null
- ? _fillSelector(array[i], last)
+ array[i] = fillSelector != null
+ ? fillSelector(array[i], last)
: last;
}
else
diff --git a/Source/SuperLinq/GroupAdjacent.cs b/Source/SuperLinq/GroupAdjacent.cs
index fa3e165f3..0921c969e 100644
--- a/Source/SuperLinq/GroupAdjacent.cs
+++ b/Source/SuperLinq/GroupAdjacent.cs
@@ -328,17 +328,14 @@ private static Grouping CreateGroupAdjacentGrouping : IGrouping
+ private sealed class Grouping(
+ TKey key,
+ IEnumerable members
+ ) : IGrouping
{
- private readonly IEnumerable _members;
+ private readonly IEnumerable _members = members;
- public Grouping(TKey key, IEnumerable members)
- {
- Key = key;
- _members = members;
- }
-
- public TKey Key { get; }
+ public TKey Key { get; } = key;
public IEnumerator GetEnumerator() => _members.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
diff --git a/Source/SuperLinq/Index.cs b/Source/SuperLinq/Index.cs
index 7cd9591be..283330ff0 100644
--- a/Source/SuperLinq/Index.cs
+++ b/Source/SuperLinq/Index.cs
@@ -67,46 +67,34 @@ public static partial class SuperEnumerable
}
}
- private sealed class IndexCollectionIterator : CollectionIterator<(int index, T item)>
+ private sealed class IndexCollectionIterator(
+ IEnumerable source,
+ int startIndex
+ ) : CollectionIterator<(int index, T item)>
{
- private readonly IEnumerable _source;
- private readonly int _startIndex;
-
- public IndexCollectionIterator(IEnumerable source, int startIndex)
- {
- _source = source;
- _startIndex = startIndex;
- }
-
- public override int Count => _source.GetCollectionCount();
+ public override int Count => source.GetCollectionCount();
protected override IEnumerable<(int index, T item)> GetEnumerable()
{
- var index = _startIndex;
- foreach (var item in _source)
+ var index = startIndex;
+ foreach (var item in source)
yield return (index++, item);
}
}
- private sealed class IndexListIterator : ListIterator<(int index, T item)>
+ private sealed class IndexListIterator(
+ IList source,
+ int startIndex
+ ) : ListIterator<(int index, T item)>
{
- private readonly IList _source;
- private readonly int _startIndex;
-
- public IndexListIterator(IList source, int startIndex)
- {
- _source = source;
- _startIndex = startIndex;
- }
-
- public override int Count => _source.Count;
+ public override int Count => source.Count;
protected override IEnumerable<(int index, T item)> GetEnumerable()
{
var cnt = (uint)Count;
for (var i = 0; i < cnt; i++)
{
- yield return (_startIndex + i, _source[i]);
+ yield return (startIndex + i, source[i]);
}
}
@@ -115,7 +103,7 @@ protected override (int index, T item) ElementAt(int index)
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
- return (_startIndex + index, _source[index]);
+ return (startIndex + index, source[index]);
}
}
}
diff --git a/Source/SuperLinq/Insert.cs b/Source/SuperLinq/Insert.cs
index 26d3e09cc..ea50d2dd2 100644
--- a/Source/SuperLinq/Insert.cs
+++ b/Source/SuperLinq/Insert.cs
@@ -146,40 +146,33 @@ private static IEnumerable InsertCore(IEnumerable first, IEnumerable
yield return iter.Current;
}
- private sealed class InsertCollectionIterator : CollectionIterator
+ private sealed class InsertCollectionIterator(
+ IEnumerable first,
+ IEnumerable second,
+ Index index
+ ) : CollectionIterator
{
- private readonly IEnumerable _first;
- private readonly IEnumerable _second;
- private readonly Index _index;
-
- public InsertCollectionIterator(IEnumerable first, IEnumerable second, Index index)
- {
- _first = first;
- _second = second;
- _index = index;
- }
-
public override int Count
{
get
{
- var fCount = _first.GetCollectionCount();
- var idx = _index.GetOffset(fCount);
+ var fCount = first.GetCollectionCount();
+ var idx = index.GetOffset(fCount);
ArgumentOutOfRangeException.ThrowIfNegative(idx);
ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, fCount);
- return fCount + _second.GetCollectionCount();
+ return fCount + second.GetCollectionCount();
}
}
protected override IEnumerable GetEnumerable()
{
- var fCount = _first.GetCollectionCount();
- var idx = _index.GetOffset(fCount);
+ var fCount = first.GetCollectionCount();
+ var idx = index.GetOffset(fCount);
ArgumentOutOfRangeException.ThrowIfNegative(idx);
ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, fCount);
- return InsertCore(_first, _second, idx);
+ return InsertCore(first, second, idx);
}
public override void CopyTo(T[] array, int arrayIndex)
@@ -188,58 +181,51 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
- _ = _first.CopyTo(array, arrayIndex);
+ _ = first.CopyTo(array, arrayIndex);
var span = array.AsSpan()[arrayIndex..];
- var cnt = _first.GetCollectionCount();
- var idx = _index.GetOffset(cnt);
- span[idx..cnt].CopyTo(span[(idx + _second.GetCollectionCount())..]);
+ var cnt = first.GetCollectionCount();
+ var idx = index.GetOffset(cnt);
+ span[idx..cnt].CopyTo(span[(idx + second.GetCollectionCount())..]);
- _ = _second.CopyTo(array, arrayIndex + idx);
+ _ = second.CopyTo(array, arrayIndex + idx);
}
}
- private sealed class InsertListIterator : ListIterator
+ private sealed class InsertListIterator(
+ IList first,
+ IList second,
+ Index index
+ ) : ListIterator
{
- private readonly IList _first;
- private readonly IList _second;
- private readonly Index _index;
-
- public InsertListIterator(IList first, IList second, Index index)
- {
- _first = first;
- _second = second;
- _index = index;
- }
-
public override int Count
{
get
{
- var idx = _index.GetOffset(_first.Count);
+ var idx = index.GetOffset(first.Count);
ArgumentOutOfRangeException.ThrowIfNegative(idx);
- ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, _first.Count);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, first.Count);
- return _first.Count + _second.Count;
+ return first.Count + second.Count;
}
}
protected override IEnumerable GetEnumerable()
{
- var idx = _index.GetOffset(_first.Count);
+ var idx = index.GetOffset(first.Count);
ArgumentOutOfRangeException.ThrowIfNegative(idx);
- ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, _first.Count);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, first.Count);
for (var i = 0; i < (uint)idx; i++)
- yield return _first[i];
+ yield return first[i];
- var cnt = (uint)_second.Count;
+ var cnt = (uint)second.Count;
for (var j = 0; j < cnt; j++)
- yield return _second[j];
+ yield return second[j];
- cnt = (uint)_first.Count;
+ cnt = (uint)first.Count;
for (var i = idx; i < cnt; i++)
- yield return _first[i];
+ yield return first[i];
}
public override void CopyTo(T[] array, int arrayIndex)
@@ -248,28 +234,28 @@ public override void CopyTo(T[] array, int arrayIndex)
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
- _first.CopyTo(array, arrayIndex);
+ first.CopyTo(array, arrayIndex);
var span = array.AsSpan()[arrayIndex..];
- var cnt = _first.Count;
- var idx = _index.GetOffset(cnt);
- span[idx..cnt].CopyTo(span[(idx + _second.Count)..]);
+ var cnt = first.Count;
+ var idx = index.GetOffset(cnt);
+ span[idx..cnt].CopyTo(span[(idx + second.Count)..]);
- _second.CopyTo(array, arrayIndex + idx);
+ second.CopyTo(array, arrayIndex + idx);
}
- protected override T ElementAt(int index)
+ protected override T ElementAt(int index1)
{
- ArgumentOutOfRangeException.ThrowIfNegative(index);
- ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
+ ArgumentOutOfRangeException.ThrowIfNegative(index1);
+ ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index1, Count);
- var idx = _index.GetOffset(_first.Count);
+ var idx = index.GetOffset(first.Count);
ArgumentOutOfRangeException.ThrowIfNegative(idx);
- ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, _first.Count);
+ ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, first.Count);
- return index < idx ? _first[index] :
- index < idx + _second.Count ? _second[index - idx] :
- _first[index - _second.Count];
+ return index1 < idx ? first[index1] :
+ index1 < idx + second.Count ? second[index1 - idx] :
+ first[index1 - second.Count];
}
}
}
diff --git a/Source/SuperLinq/Interleave.cs b/Source/SuperLinq/Interleave.cs
index 43101c5d9..b60d9d472 100644
--- a/Source/SuperLinq/Interleave.cs
+++ b/Source/SuperLinq/Interleave.cs
@@ -87,19 +87,14 @@ private static IEnumerable InterleaveCore(IEnumerable> sour
}
}
- private sealed class InterleaveIterator : CollectionIterator
+ private sealed class InterleaveIterator(
+ IEnumerable> sources
+ ) : CollectionIterator
{
- private readonly IEnumerable> _sources;
-
- public InterleaveIterator(IEnumerable> sources)
- {
- _sources = sources;
- }
-
- public override int Count => _sources.Sum(static s => s.Count);
+ public override int Count => sources.Sum(static s => s.Count);
[ExcludeFromCodeCoverage]
protected override IEnumerable GetEnumerable() =>
- InterleaveCore(_sources);
+ InterleaveCore(sources);
}
}
diff --git a/Source/SuperLinq/Join.MergeJoin.cs b/Source/SuperLinq/Join.MergeJoin.cs
index 12c450baf..93fcc25df 100644
--- a/Source/SuperLinq/Join.MergeJoin.cs
+++ b/Source/SuperLinq/Join.MergeJoin.cs
@@ -720,15 +720,10 @@ private static IEnumerable