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 JoinMerge( } } -file sealed class ComparerEqualityComparer : 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/KeyValuePairEqualityComparer.cs b/Source/SuperLinq/KeyValuePairEqualityComparer.cs index 4df83b5d2..ca0ce5b9e 100644 --- a/Source/SuperLinq/KeyValuePairEqualityComparer.cs +++ b/Source/SuperLinq/KeyValuePairEqualityComparer.cs @@ -35,18 +35,13 @@ public static IEqualityComparer> Create keyComparer, valueComparer); - private sealed class ItemEqualityComparer : IEqualityComparer> + private sealed class ItemEqualityComparer( + IEqualityComparer? keyComparer, + IEqualityComparer? valueComparer + ) : IEqualityComparer> { - private readonly IEqualityComparer _keyComparer; - private readonly IEqualityComparer _valueComparer; - - public ItemEqualityComparer( - IEqualityComparer? keyComparer, - IEqualityComparer? valueComparer) - { - _keyComparer = keyComparer ?? EqualityComparer.Default; - _valueComparer = valueComparer ?? EqualityComparer.Default; - } + private readonly IEqualityComparer _keyComparer = keyComparer ?? EqualityComparer.Default; + private readonly IEqualityComparer _valueComparer = valueComparer ?? EqualityComparer.Default; public bool Equals(KeyValuePair x, KeyValuePair y) => _keyComparer.Equals(x.Key, y.Key) diff --git a/Source/SuperLinq/Lag.cs b/Source/SuperLinq/Lag.cs index 38d85eebc..e9c1faab0 100644 --- a/Source/SuperLinq/Lag.cs +++ b/Source/SuperLinq/Lag.cs @@ -140,30 +140,22 @@ static IEnumerable Core(IEnumerable source, int offset, TSourc } } - private sealed class LagIterator : ListIterator + private sealed class LagIterator( + IList source, + int offset, + TSource defaultLagValue, + Func resultSelector + ) : ListIterator { - private readonly IList _source; - private readonly int _offset; - private readonly TSource _defaultLagValue; - private readonly Func _resultSelector; - - public LagIterator(IList source, int offset, TSource defaultLagValue, Func resultSelector) - { - _source = source; - _offset = offset; - _defaultLagValue = defaultLagValue; - _resultSelector = resultSelector; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable GetEnumerable() { - var cnt = (uint)_source.Count; + var cnt = (uint)source.Count; for (var i = 0; i < cnt; i++) - yield return _resultSelector( - _source[i], - i < _offset ? _defaultLagValue : _source[i - _offset]); + yield return resultSelector( + source[i], + i < offset ? defaultLagValue : source[i - offset]); } protected override TResult ElementAt(int index) @@ -171,9 +163,9 @@ protected override TResult ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return _resultSelector( - _source[index], - index < _offset ? _defaultLagValue : _source[index - _offset]); + return resultSelector( + source[index], + index < offset ? defaultLagValue : source[index - offset]); } } } diff --git a/Source/SuperLinq/Lead.cs b/Source/SuperLinq/Lead.cs index be9a18586..f2845abb5 100644 --- a/Source/SuperLinq/Lead.cs +++ b/Source/SuperLinq/Lead.cs @@ -145,31 +145,23 @@ static IEnumerable Core(IEnumerable source, int offset, TSourc } } - private sealed class LeadIterator : ListIterator + private sealed class LeadIterator( + IList source, + int offset, + TSource defaultLeadValue, + Func resultSelector + ) : ListIterator { - private readonly IList _source; - private readonly int _offset; - private readonly TSource _defaultLeadValue; - private readonly Func _resultSelector; - - public LeadIterator(IList source, int offset, TSource defaultLeadValue, Func resultSelector) - { - _source = source; - _offset = offset; - _defaultLeadValue = defaultLeadValue; - _resultSelector = resultSelector; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable GetEnumerable() { - var cnt = (uint)_source.Count; - var maxOffset = Math.Max(_source.Count - _offset, 0); + var cnt = (uint)source.Count; + var maxOffset = Math.Max(source.Count - offset, 0); for (var i = 0; i < cnt; i++) - yield return _resultSelector( - _source[i], - i < maxOffset ? _source[i + _offset] : _defaultLeadValue); + yield return resultSelector( + source[i], + i < maxOffset ? source[i + offset] : defaultLeadValue); } protected override TResult ElementAt(int index) @@ -177,11 +169,11 @@ protected override TResult ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return _resultSelector( - _source[index], - index < Math.Max(_source.Count - _offset, 0) - ? _source[index + _offset] - : _defaultLeadValue); + return resultSelector( + source[index], + index < Math.Max(source.Count - offset, 0) + ? source[index + offset] + : defaultLeadValue); } } } diff --git a/Source/SuperLinq/Memoize.cs b/Source/SuperLinq/Memoize.cs index 77485df6a..384f9c200 100644 --- a/Source/SuperLinq/Memoize.cs +++ b/Source/SuperLinq/Memoize.cs @@ -56,11 +56,13 @@ public static IBuffer Memoize(this IEnumerable source }; } - private sealed class EnumerableMemoizedBuffer : IBuffer + private sealed class EnumerableMemoizedBuffer( + IEnumerable source + ) : IBuffer { private readonly object _lock = new(); - private IEnumerable? _source; + private IEnumerable? _source = source; private IEnumerator? _enumerator; private List _buffer = new(); @@ -71,11 +73,6 @@ private sealed class EnumerableMemoizedBuffer : IBuffer private bool _disposed; - public EnumerableMemoizedBuffer(IEnumerable source) - { - _source = source; - } - public int Count => _buffer.Count; public void Reset() @@ -215,7 +212,9 @@ public void Dispose() } } - private sealed class CollectionMemoizedBuffer : IBuffer + private sealed class CollectionMemoizedBuffer( + ICollection source + ) : IBuffer { private enum State { @@ -228,15 +227,9 @@ private enum State private sealed record CmbHelper(State State, T[]? Buffer = null, ExceptionDispatchInfo? Exception = null); - private ICollection? _source; + private ICollection? _source = source; - private volatile CmbHelper _state; - - public CollectionMemoizedBuffer(ICollection source) - { - _source = source; - _state = new(State.Uninitialized, null); - } + private volatile CmbHelper _state = new(State.Uninitialized, null); public int Count { @@ -381,14 +374,11 @@ public void Dispose() } } - private sealed class CollectionProxyBuffer : IBuffer + private sealed class CollectionProxyBuffer( + ICollection source + ) : IBuffer { - public CollectionProxyBuffer(ICollection source) - { - _source = source; - } - - private ICollection? _source; + private ICollection? _source = source; private ICollection Source { get diff --git a/Source/SuperLinq/Pad.cs b/Source/SuperLinq/Pad.cs index ab0038d6d..e80d3b669 100644 --- a/Source/SuperLinq/Pad.cs +++ b/Source/SuperLinq/Pad.cs @@ -127,25 +127,16 @@ private static IEnumerable PadCore( } } - private sealed class PadCollectionIterator : CollectionIterator + private sealed class PadCollectionIterator( + IEnumerable source, + int width, + Func paddingSelector + ) : CollectionIterator { - private readonly IEnumerable _source; - private readonly int _width; - private readonly Func _paddingSelector; - - public PadCollectionIterator( - IEnumerable source, int width, - Func paddingSelector) - { - _source = source; - _width = width; - _paddingSelector = paddingSelector; - } - - public override int Count => Math.Max(_source.GetCollectionCount(), _width); + public override int Count => Math.Max(source.GetCollectionCount(), width); protected override IEnumerable GetEnumerable() => - PadCore(_source, _width, _paddingSelector); + PadCore(source, width, paddingSelector); public override void CopyTo(T[] array, int arrayIndex) { @@ -153,37 +144,29 @@ public override void CopyTo(T[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - var cnt = _source.CopyTo(array, arrayIndex); + var cnt = source.CopyTo(array, arrayIndex); - for (var i = cnt; i < _width; i++) - array[arrayIndex + i] = _paddingSelector(i); + for (var i = cnt; i < width; i++) + array[arrayIndex + i] = paddingSelector(i); } } - private sealed class PadListIterator : ListIterator + private sealed class PadListIterator( + IList source, + int width, Func paddingSelector + ) : ListIterator { - private readonly IList _source; - private readonly int _width; - private readonly Func _paddingSelector; - - public PadListIterator(IList source, int width, Func paddingSelector) - { - _source = source; - _width = width; - _paddingSelector = paddingSelector; - } - - public override int Count => Math.Max(_source.Count, _width); + public override int Count => Math.Max(source.Count, width); protected override IEnumerable GetEnumerable() { - var src = _source; + var src = source; var cnt = (uint)src.Count; for (var i = 0; i < cnt; i++) yield return src[i]; - for (var i = (int)cnt; i < _width; i++) - yield return _paddingSelector(i); + for (var i = (int)cnt; i < width; i++) + yield return paddingSelector(i); } public override void CopyTo(T[] array, int arrayIndex) @@ -192,10 +175,10 @@ 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); - for (var i = _source.Count; i < _width; i++) - array[arrayIndex + i] = _paddingSelector(i); + for (var i = source.Count; i < width; i++) + array[arrayIndex + i] = paddingSelector(i); } protected override T ElementAt(int index) @@ -203,9 +186,9 @@ protected override T ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return index < _source.Count - ? _source[index] - : _paddingSelector(index); + return index < source.Count + ? source[index] + : paddingSelector(index); } } } diff --git a/Source/SuperLinq/PadStart.cs b/Source/SuperLinq/PadStart.cs index 55c903b13..8792d8041 100644 --- a/Source/SuperLinq/PadStart.cs +++ b/Source/SuperLinq/PadStart.cs @@ -144,29 +144,20 @@ static IEnumerable Core( } } - private sealed class PadStartCollectionIterator : CollectionIterator + private sealed class PadStartCollectionIterator( + IEnumerable source, + int width, + Func paddingSelector + ) : CollectionIterator { - private readonly IEnumerable _source; - private readonly int _width; - private readonly Func _paddingSelector; - - public PadStartCollectionIterator( - IEnumerable source, int width, - Func paddingSelector) - { - _source = source; - _width = width; - _paddingSelector = paddingSelector; - } - - public override int Count => Math.Max(_source.GetCollectionCount(), _width); + public override int Count => Math.Max(source.GetCollectionCount(), width); protected override IEnumerable GetEnumerable() { - var cnt = _width - _source.GetCollectionCount(); + var cnt = width - source.GetCollectionCount(); for (var i = 0; i < cnt; i++) - yield return _paddingSelector(i); - foreach (var item in _source) + yield return paddingSelector(i); + foreach (var item in source) yield return item; } @@ -176,36 +167,29 @@ public override void CopyTo(T[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - var offset = Math.Max(_width - _source.GetCollectionCount(), 0); + var offset = Math.Max(width - source.GetCollectionCount(), 0); for (var i = 0; i < offset; i++) - array[arrayIndex + i] = _paddingSelector(i); + array[arrayIndex + i] = paddingSelector(i); - _ = _source.CopyTo(array, arrayIndex + offset); + _ = source.CopyTo(array, arrayIndex + offset); } } - private sealed class PadStartListIterator : ListIterator + private sealed class PadStartListIterator( + IList source, + int width, + Func paddingSelector + ) : ListIterator { - private readonly IList _source; - private readonly int _width; - private readonly Func _paddingSelector; - - public PadStartListIterator(IList source, int width, Func paddingSelector) - { - _source = source; - _width = width; - _paddingSelector = paddingSelector; - } - - public override int Count => Math.Max(_source.Count, _width); + public override int Count => Math.Max(source.Count, width); protected override IEnumerable GetEnumerable() { - var cnt = (uint)_source.Count; - for (var i = 0; i < _width - cnt; i++) - yield return _paddingSelector(i); + var cnt = (uint)source.Count; + for (var i = 0; i < width - cnt; i++) + yield return paddingSelector(i); for (var i = 0; i < cnt; i++) - yield return _source[i]; + yield return source[i]; } public override void CopyTo(T[] array, int arrayIndex) @@ -214,11 +198,11 @@ public override void CopyTo(T[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - var offset = Math.Max(_width - _source.Count, 0); + var offset = Math.Max(width - source.Count, 0); for (var i = 0; i < offset; i++) - array[arrayIndex + i] = _paddingSelector(i); + array[arrayIndex + i] = paddingSelector(i); - _source.CopyTo(array, arrayIndex + offset); + source.CopyTo(array, arrayIndex + offset); } protected override T ElementAt(int index) @@ -226,10 +210,10 @@ protected override T ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - var offset = Math.Max(_width - _source.Count, 0); + var offset = Math.Max(width - source.Count, 0); return index < offset - ? _paddingSelector(index) - : _source[index - offset]; + ? paddingSelector(index) + : source[index - offset]; } } } diff --git a/Source/SuperLinq/Publish.cs b/Source/SuperLinq/Publish.cs index ea274df50..d119011c3 100644 --- a/Source/SuperLinq/Publish.cs +++ b/Source/SuperLinq/Publish.cs @@ -27,7 +27,7 @@ public static partial class SuperEnumerable /// A separate buffer will be maintained for each created from the returned . This buffer will be maintained until the enumerator is disposed, and will contain /// all elements returned by from the time that the is - /// created. + /// created. /// /// /// This operator uses deferred execution and streams its result. @@ -40,11 +40,13 @@ public static IBuffer Publish(this IEnumerable source return new PublishBuffer(source); } - private sealed class PublishBuffer : IBuffer + private sealed class PublishBuffer( + IEnumerable source + ) : IBuffer { private readonly object _lock = new(); - private IEnumerable? _source; + private IEnumerable? _source = source; private IEnumerator? _enumerator; private List>? _buffers; @@ -56,11 +58,6 @@ private sealed class PublishBuffer : IBuffer private bool _disposed; - public PublishBuffer(IEnumerable source) - { - _source = source; - } - public int Count => _buffers?.Count > 0 ? _buffers.Max(x => x.Count) : 0; public void Reset() diff --git a/Source/SuperLinq/Replace.cs b/Source/SuperLinq/Replace.cs index ab5f891ac..41e840c99 100644 --- a/Source/SuperLinq/Replace.cs +++ b/Source/SuperLinq/Replace.cs @@ -129,28 +129,21 @@ static IEnumerable Core(IEnumerable source, TSource value, Ind } } - private sealed class ReplaceIterator : ListIterator + private sealed class ReplaceIterator( + IList source, + TSource value, + Index index + ) : ListIterator { - private readonly IList _source; - private readonly TSource _value; - private readonly Index _index; - - public ReplaceIterator(IList source, TSource value, Index index) - { - _source = source; - _value = value; - _index = index; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable GetEnumerable() { - var cnt = (uint)_source.Count; - var idx = _index.GetOffset(_source.Count); + var cnt = (uint)source.Count; + var idx = index.GetOffset(source.Count); for (var i = 0; i < cnt; i++) - yield return i == idx ? _value : _source[i]; + yield return i == idx ? value : source[i]; } public override void CopyTo(TSource[] array, int arrayIndex) @@ -159,21 +152,21 @@ public override void CopyTo(TSource[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - _source.CopyTo(array, arrayIndex); + source.CopyTo(array, arrayIndex); - var idx = _index.GetOffset(_source.Count); - if (idx >= 0 && idx < _source.Count) - array[arrayIndex + idx] = _value; + var idx = index.GetOffset(source.Count); + if (idx >= 0 && idx < source.Count) + array[arrayIndex + idx] = value; } - protected override TSource ElementAt(int index) + protected override TSource ElementAt(int index1) { - ArgumentOutOfRangeException.ThrowIfNegative(index); - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); + ArgumentOutOfRangeException.ThrowIfNegative(index1); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index1, Count); - return index == _index.GetOffset(_source.Count) - ? _value - : _source[index]; + return index1 == index.GetOffset(source.Count) + ? value + : source[index1]; } } } diff --git a/Source/SuperLinq/Return.cs b/Source/SuperLinq/Return.cs index 9664237b1..d58699867 100644 --- a/Source/SuperLinq/Return.cs +++ b/Source/SuperLinq/Return.cs @@ -19,28 +19,26 @@ public partial class SuperEnumerable public static IEnumerable Return(T item) => new SingleElementList(item); - private sealed class SingleElementList : IList, IReadOnlyList + private sealed class SingleElementList( + T item + ) : IList, IReadOnlyList { - private readonly T _item; - - public SingleElementList(T item) => _item = item; - public int Count => 1; public bool IsReadOnly => true; public T this[int index] { - get => index == 0 ? _item : ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index)); + get => index == 0 ? item : ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index)); set => throw ReadOnlyException(); } - public IEnumerator GetEnumerator() { yield return _item; } + public IEnumerator GetEnumerator() { yield return item; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public int IndexOf(T item) => Contains(item) ? 0 : -1; - public bool Contains(T item) => EqualityComparer.Default.Equals(_item, item); + public bool Contains(T item1) => EqualityComparer.Default.Equals(item, item1); - public void CopyTo(T[] array, int arrayIndex) => array[arrayIndex] = _item; + public void CopyTo(T[] array, int arrayIndex) => array[arrayIndex] = item; // Following methods are unsupported as this is a read-only list. diff --git a/Source/SuperLinq/ReverseComparer.cs b/Source/SuperLinq/ReverseComparer.cs index 9f58ca5e0..2c6c7bb96 100644 --- a/Source/SuperLinq/ReverseComparer.cs +++ b/Source/SuperLinq/ReverseComparer.cs @@ -1,12 +1,9 @@ namespace SuperLinq; -internal sealed class ReverseComparer : IComparer +internal sealed class ReverseComparer( + IComparer underlying +) : IComparer { - private readonly IComparer _underlying; - - public ReverseComparer(IComparer underlying) => - _underlying = underlying; - public int Compare(T? x, T? y) => - -_underlying.Compare(x, y); + -underlying.Compare(x, y); } diff --git a/Source/SuperLinq/ScanRight.cs b/Source/SuperLinq/ScanRight.cs index 44f86d9d1..d6d32db92 100644 --- a/Source/SuperLinq/ScanRight.cs +++ b/Source/SuperLinq/ScanRight.cs @@ -28,7 +28,7 @@ public static partial class SuperEnumerable /// /// /// This method is implemented by using deferred execution. However, will be consumed - /// in it's entirety immediately when first element of the returned sequence is consumed. + /// in it's entirety immediately when first element of the returned sequence is consumed. /// /// public static IEnumerable ScanRight(this IEnumerable source, Func func) @@ -63,22 +63,16 @@ private static IEnumerable ScanRightCore(IEnumerable yield return item; } - private sealed class ScanRightIterator : CollectionIterator + private sealed class ScanRightIterator( + ICollection source, + Func func + ) : CollectionIterator { - private readonly ICollection _source; - private readonly Func _func; - - public ScanRightIterator(ICollection source, Func func) - { - _source = source; - _func = func; - } - - public override int Count => _source.Count; + public override int Count => source.Count; [ExcludeFromCodeCoverage] protected override IEnumerable GetEnumerable() => - ScanRightCore(_source, _func); + ScanRightCore(source, func); public override void CopyTo(T[] array, int arrayIndex) { @@ -86,9 +80,9 @@ public override void CopyTo(T[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - var (sList, b, cnt) = _source is IList s + var (sList, b, cnt) = source is IList s ? (s, 0, s.Count) - : (array, arrayIndex, SuperEnumerable.CopyTo(_source, array, arrayIndex)); + : (array, arrayIndex, SuperEnumerable.CopyTo(source, array, arrayIndex)); var i = cnt - 1; var state = sList[b + i]; @@ -96,7 +90,7 @@ public override void CopyTo(T[] array, int arrayIndex) for (i--; i >= 0; i--) { - state = _func(sList[b + i], state); + state = func(sList[b + i], state); array[arrayIndex + i] = state; } } @@ -132,7 +126,7 @@ public override void CopyTo(T[] array, int arrayIndex) /// /// /// This method is implemented by using deferred execution. However, will be consumed - /// in it's entirety immediately when first element of the returned sequence is consumed. + /// in it's entirety immediately when first element of the returned sequence is consumed. /// /// public static IEnumerable ScanRight(this IEnumerable source, TAccumulate seed, Func func) diff --git a/Source/SuperLinq/Sequence.cs b/Source/SuperLinq/Sequence.cs index d063b360b..e21bf083c 100644 --- a/Source/SuperLinq/Sequence.cs +++ b/Source/SuperLinq/Sequence.cs @@ -96,28 +96,21 @@ public static IEnumerable Sequence(int start, int stop, int step) return new SequenceIterator(start, step, (((long)stop - start) / step) + 1); } - private sealed class SequenceIterator : ListIterator + private sealed class SequenceIterator( + int start, + int step, + long count + ) : ListIterator { - private readonly int _start; - private readonly int _step; - private readonly long _count; - - public SequenceIterator(int start, int step, long count) - { - _start = start; - _step = step; - _count = count; - } - - public override int Count => _count <= int.MaxValue ? (int)_count : int.MaxValue; + public override int Count => count <= int.MaxValue ? (int)count : int.MaxValue; protected override IEnumerable GetEnumerable() { - var value = _start; + var value = start; for (var i = 0; i < Count; i++) { yield return value; - value += _step; + value += step; } } @@ -126,7 +119,7 @@ protected override int ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return _start + (_step * index); + return start + (step * index); } } } diff --git a/Source/SuperLinq/Share.cs b/Source/SuperLinq/Share.cs index 0376f2b19..694791ce8 100644 --- a/Source/SuperLinq/Share.cs +++ b/Source/SuperLinq/Share.cs @@ -33,11 +33,13 @@ public static IBuffer Share(this IEnumerable source) return new SharedBuffer(source); } - private sealed class SharedBuffer : IBuffer + private sealed class SharedBuffer( + IEnumerable source + ) : IBuffer { private readonly object _lock = new(); - private IEnumerable? _source; + private IEnumerable? _source = source; private IEnumerator? _enumerator; private bool _initialized; @@ -47,11 +49,6 @@ private sealed class SharedBuffer : IBuffer private bool _disposed; - public SharedBuffer(IEnumerable source) - { - _source = source; - } - public int Count => 0; public void Reset() diff --git a/Source/SuperLinq/TagFirstLast.cs b/Source/SuperLinq/TagFirstLast.cs index f83311033..89f161ab9 100644 --- a/Source/SuperLinq/TagFirstLast.cs +++ b/Source/SuperLinq/TagFirstLast.cs @@ -86,36 +86,30 @@ static IEnumerable Core(IEnumerable source, Func : ListIterator + private class TagFirstLastIterator( + IList source, + Func resultSelector + ) : ListIterator { - private readonly IList _source; - private readonly Func _resultSelector; - - public TagFirstLastIterator(IList source, Func resultSelector) - { - _source = source; - _resultSelector = resultSelector; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable GetEnumerable() { - if (_source.Count <= 1) + if (source.Count <= 1) { - if (_source.Count == 1) - yield return _resultSelector(_source[0], true, true); + if (source.Count == 1) + yield return resultSelector(source[0], true, true); yield break; } - yield return _resultSelector(_source[0], true, false); + yield return resultSelector(source[0], true, false); - var cnt = (uint)_source.Count - 1; + var cnt = (uint)source.Count - 1; for (var i = 1; i < cnt; i++) - yield return _resultSelector(_source[i], false, false); + yield return resultSelector(source[i], false, false); - yield return _resultSelector(_source[^1], false, true); + yield return resultSelector(source[^1], false, true); } protected override TResult ElementAt(int index) @@ -123,7 +117,7 @@ protected override TResult ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return _resultSelector(_source[index], index == 0, index == _source.Count - 1); + return resultSelector(source[index], index == 0, index == source.Count - 1); } } } diff --git a/Source/SuperLinq/ValueTupleComparer.cs b/Source/SuperLinq/ValueTupleComparer.cs index 53e5b9dab..f0dfe10b9 100644 --- a/Source/SuperLinq/ValueTupleComparer.cs +++ b/Source/SuperLinq/ValueTupleComparer.cs @@ -37,18 +37,13 @@ internal static class ValueTupleComparer comparer1, comparer2); - private sealed class Comparer : IComparer<(T1, T2)> + private sealed class Comparer( + IComparer? comparer1, + IComparer? comparer2 + ) : IComparer<(T1, T2)> { - private readonly IComparer _comparer1; - private readonly IComparer _comparer2; - - public Comparer( - IComparer? comparer1, - IComparer? comparer2) - { - _comparer1 = comparer1 ?? Comparer.Default; - _comparer2 = comparer2 ?? Comparer.Default; - } + private readonly IComparer _comparer1 = comparer1 ?? Comparer.Default; + private readonly IComparer _comparer2 = comparer2 ?? Comparer.Default; public int Compare([AllowNull] (T1, T2) x, [AllowNull] (T1, T2) y) { diff --git a/Source/SuperLinq/ValueTupleEqualityComparer.cs b/Source/SuperLinq/ValueTupleEqualityComparer.cs index acd6e6359..7b2b65bc6 100644 --- a/Source/SuperLinq/ValueTupleEqualityComparer.cs +++ b/Source/SuperLinq/ValueTupleEqualityComparer.cs @@ -27,15 +27,11 @@ comparer1 is null ? EqualityComparer>.Default : new ItemEqualityComparer(comparer1); - private sealed class ItemEqualityComparer : IEqualityComparer> + private sealed class ItemEqualityComparer( + IEqualityComparer? comparer1 + ) : IEqualityComparer> { - private readonly IEqualityComparer _comparer1; - - public ItemEqualityComparer( - IEqualityComparer? comparer1) - { - _comparer1 = comparer1 ?? EqualityComparer.Default; - } + private readonly IEqualityComparer _comparer1 = comparer1 ?? EqualityComparer.Default; public bool Equals(ValueTuple x, ValueTuple y) @@ -74,18 +70,13 @@ public int GetHashCode(ValueTuple obj) => comparer1, comparer2); - private sealed class ItemEqualityComparer : IEqualityComparer<(T1, T2)> + private sealed class ItemEqualityComparer( + IEqualityComparer? comparer1, + IEqualityComparer? comparer2 + ) : IEqualityComparer<(T1, T2)> { - private readonly IEqualityComparer _comparer1; - private readonly IEqualityComparer _comparer2; - - public ItemEqualityComparer( - IEqualityComparer? comparer1, - IEqualityComparer? comparer2) - { - _comparer1 = comparer1 ?? EqualityComparer.Default; - _comparer2 = comparer2 ?? EqualityComparer.Default; - } + private readonly IEqualityComparer _comparer1 = comparer1 ?? EqualityComparer.Default; + private readonly IEqualityComparer _comparer2 = comparer2 ?? EqualityComparer.Default; public bool Equals((T1, T2) x, (T1, T2) y) diff --git a/Source/SuperLinq/Window.cs b/Source/SuperLinq/Window.cs index 33de977af..02d9d6c5b 100644 --- a/Source/SuperLinq/Window.cs +++ b/Source/SuperLinq/Window.cs @@ -67,35 +67,29 @@ static IEnumerable> Core(IEnumerable source, int size) } } - private sealed class WindowIterator : ListIterator> + private sealed class WindowIterator( + IList source, + int size + ) : ListIterator> { - private readonly IList _source; - private readonly int _size; - - public WindowIterator(IList source, int size) - { - _source = source; - _size = size; - } - - public override int Count => Math.Max(_source.Count - _size + 1, 0); + public override int Count => Math.Max(source.Count - size + 1, 0); protected override IEnumerable> GetEnumerable() { - if (Count < _size) + if (Count < size) yield break; - var window = new T[_size]; + var window = new T[size]; - for (var i = 0; i < _size; i++) - window[i] = _source[i]; + for (var i = 0; i < size; i++) + window[i] = source[i]; - var count = (uint)_source.Count; - for (var i = _size; i < count; i++) + var count = (uint)source.Count; + for (var i = size; i < count; i++) { - var newWindow = new T[_size]; + var newWindow = new T[size]; window.AsSpan()[1..].CopyTo(newWindow); - newWindow[^1] = _source[i]; + newWindow[^1] = source[i]; yield return window; window = newWindow; @@ -109,10 +103,10 @@ protected override IList ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - var arr = new T[_size]; - var max = (uint)(index + _size); - for (int i = 0, j = index; i < _size && j < max; i++, j++) - arr[i] = _source[j]; + var arr = new T[size]; + var max = (uint)(index + size); + for (int i = 0, j = index; i < size && j < max; i++, j++) + arr[i] = source[j]; return arr; } diff --git a/Source/SuperLinq/WindowLeft.cs b/Source/SuperLinq/WindowLeft.cs index 354e966db..3327bb1f1 100644 --- a/Source/SuperLinq/WindowLeft.cs +++ b/Source/SuperLinq/WindowLeft.cs @@ -85,40 +85,34 @@ static IEnumerable> Core(IEnumerable source, int size) } } - private sealed class WindowLeftIterator : ListIterator> + private sealed class WindowLeftIterator( + IList source, + int size + ) : ListIterator> { - private readonly IList _source; - private readonly int _size; - - public WindowLeftIterator(IList source, int size) - { - _source = source; - _size = size; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable> GetEnumerable() { T[] window; - if (_source.Count == 0) + if (source.Count == 0) { yield break; } - else if (_source.Count > _size) + else if (source.Count > size) { - window = new T[_size]; + window = new T[size]; - for (var i = 0; i < _size; i++) - window[i] = _source[i]; + for (var i = 0; i < size; i++) + window[i] = source[i]; - var count = (uint)_source.Count; - for (var i = _size; i < count; i++) + var count = (uint)source.Count; + for (var i = size; i < count; i++) { - var newWindow = new T[_size]; + var newWindow = new T[size]; window.AsSpan()[1..].CopyTo(newWindow); - newWindow[^1] = _source[i]; + newWindow[^1] = source[i]; yield return window; window = newWindow; @@ -126,7 +120,7 @@ protected override IEnumerable> GetEnumerable() } else { - window = _source.ToArray(); + window = source.ToArray(); } while (window.Length > 1) @@ -146,21 +140,21 @@ protected override IList ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - if (index < _source.Count - _size) + if (index < source.Count - size) { - var arr = new T[_size]; - var max = (uint)(index + _size); - for (int i = 0, j = index; i < _size && j < max; i++, j++) - arr[i] = _source[j]; + var arr = new T[size]; + var max = (uint)(index + size); + for (int i = 0, j = index; i < size && j < max; i++, j++) + arr[i] = source[j]; return arr; } else { - var arr = new T[_source.Count - index]; - var max = (uint)_source.Count; + var arr = new T[source.Count - index]; + var max = (uint)source.Count; for (int i = 0, j = index; i < arr.Length && j < max; i++, j++) - arr[i] = _source[j]; + arr[i] = source[j]; return arr; } } diff --git a/Source/SuperLinq/WindowRight.cs b/Source/SuperLinq/WindowRight.cs index bc7e8cf73..bbd80aff0 100644 --- a/Source/SuperLinq/WindowRight.cs +++ b/Source/SuperLinq/WindowRight.cs @@ -80,44 +80,38 @@ static IEnumerable> Core(IEnumerable source, int size) } } - private sealed class WindowRightIterator : ListIterator> + private sealed class WindowRightIterator( + IList source, + int size + ) : ListIterator> { - private readonly IList _source; - private readonly int _size; - - public WindowRightIterator(IList source, int size) - { - _source = source; - _size = size; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable> GetEnumerable() { - if (_source.Count == 0) + if (source.Count == 0) { yield break; } - var window = new T[1] { _source[0], }; - var max = (uint)Math.Min(_source.Count, _size); + var window = new T[1] { source[0], }; + var max = (uint)Math.Min(source.Count, size); for (var i = 1; i < max; i++) { var newWindow = new T[i + 1]; window.AsSpan()[..].CopyTo(newWindow); - newWindow[^1] = _source[i]; + newWindow[^1] = source[i]; yield return window; window = newWindow; } - max = (uint)_source.Count; + max = (uint)source.Count; for (var i = window.Length; i < max; i++) { - var newWindow = new T[_size]; + var newWindow = new T[size]; window.AsSpan()[1..].CopyTo(newWindow); - newWindow[^1] = _source[i]; + newWindow[^1] = source[i]; yield return window; window = newWindow; @@ -131,21 +125,21 @@ protected override IList ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - if (index < _size) + if (index < size) { var arr = new T[index]; var max = (uint)index; for (var i = 0; i < max; i++) - arr[i] = _source[i]; + arr[i] = source[i]; return arr; } else { - var arr = new T[_size]; + var arr = new T[size]; var max = (uint)index + 1; - for (int i = 0, j = index - _size + 1; i < arr.Length && j < max; i++, j++) - arr[i] = _source[j]; + for (int i = 0, j = index - size + 1; i < arr.Length && j < max; i++, j++) + arr[i] = source[j]; return arr; } } diff --git a/Source/SuperLinq/ZipMap.cs b/Source/SuperLinq/ZipMap.cs index 2b7184f38..ef4d1cbed 100644 --- a/Source/SuperLinq/ZipMap.cs +++ b/Source/SuperLinq/ZipMap.cs @@ -47,27 +47,21 @@ public static partial class SuperEnumerable } } - private sealed class ZipMapIterator : ListIterator<(TSource, TResult)> + private sealed class ZipMapIterator( + IList source, + Func selector + ) : ListIterator<(TSource, TResult)> { - private readonly IList _source; - private readonly Func _selector; - - public ZipMapIterator(IList source, Func selector) - { - _source = source; - _selector = selector; - } - - public override int Count => _source.Count; + public override int Count => source.Count; protected override IEnumerable<(TSource, TResult)> GetEnumerable() { - var src = _source; + var src = source; var cnt = (uint)src.Count; for (var i = 0; i < cnt; i++) { var el = src[i]; - yield return (el, _selector(el)); + yield return (el, selector(el)); } } @@ -76,8 +70,8 @@ protected override (TSource, TResult) ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - var el = _source[index]; - return (el, _selector(el)); + var el = source[index]; + return (el, selector(el)); } } } diff --git a/Tests/SuperLinq.Async.Test/TestingSequence.cs b/Tests/SuperLinq.Async.Test/TestingSequence.cs index d0fc1c017..240d5525e 100644 --- a/Tests/SuperLinq.Async.Test/TestingSequence.cs +++ b/Tests/SuperLinq.Async.Test/TestingSequence.cs @@ -56,10 +56,7 @@ public enum Options } } -public class TestingSequenceException : Exception -{ - public TestingSequenceException(string message) : base(message) { } -} +public class TestingSequenceException(string message) : Exception(message); /// /// Sequence that asserts whether its iterator has been disposed diff --git a/Tests/SuperLinq.Async.Test/TraverseTest.cs b/Tests/SuperLinq.Async.Test/TraverseTest.cs index 6c775ebe2..f10226a8b 100644 --- a/Tests/SuperLinq.Async.Test/TraverseTest.cs +++ b/Tests/SuperLinq.Async.Test/TraverseTest.cs @@ -33,16 +33,13 @@ public async Task TraverseBreadthFirstPreservesChildrenOrder() await res.AssertSequenceEqual(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } - private class Tree + private class Tree( + T value, + IEnumerable> children + ) { - public T Value { get; } - public IEnumerable> Children { get; } - - public Tree(T value, IEnumerable> children) - { - Value = value; - Children = children; - } + public T Value { get; } = value; + public IEnumerable> Children { get; } = children; } private static class Tree diff --git a/Tests/SuperLinq.Test/EqualityComparer.cs b/Tests/SuperLinq.Test/EqualityComparer.cs index 4e4251762..9104bb0e7 100644 --- a/Tests/SuperLinq.Test/EqualityComparer.cs +++ b/Tests/SuperLinq.Test/EqualityComparer.cs @@ -8,20 +8,17 @@ public static IEqualityComparer Create(Func comparer) => public static IEqualityComparer Create(Func comparer, Func hasher) => new DelegatingComparer(comparer, hasher); - private sealed class DelegatingComparer : IEqualityComparer + private sealed class DelegatingComparer( + Func comparer, + Func hasher + ) : IEqualityComparer { - private readonly Func _comparer; - private readonly Func _hasher; + private readonly Func _comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); + private readonly Func _hasher = hasher ?? throw new ArgumentNullException(nameof(hasher)); public DelegatingComparer(Func comparer) : this(comparer, x => x == null ? 0 : x.GetHashCode()) { } - public DelegatingComparer(Func comparer, Func hasher) - { - _comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); - _hasher = hasher ?? throw new ArgumentNullException(nameof(hasher)); - } - public bool Equals(T? x, T? y) => _comparer(x!, y!); public int GetHashCode(T obj) => _hasher(obj); } diff --git a/Tests/SuperLinq.Test/FlattenTest.cs b/Tests/SuperLinq.Test/FlattenTest.cs index 980a2977c..de8c56313 100644 --- a/Tests/SuperLinq.Test/FlattenTest.cs +++ b/Tests/SuperLinq.Test/FlattenTest.cs @@ -343,18 +343,16 @@ private class Attribute public int[] Values { get; init; } = []; } - private class Tree + private class Tree( + Tree? left, + T value, + Tree? right + ) { - public readonly T Value; - public readonly Tree? Left; - public readonly Tree? Right; + public readonly T Value = value; + public readonly Tree? Left = left; + public readonly Tree? Right = right; public Tree(T value) : this(null, value, null) { } - public Tree(Tree? left, T value, Tree? right) - { - Left = left; - Value = value; - Right = right; - } } } diff --git a/Tests/SuperLinq.Test/MemoizeTest.cs b/Tests/SuperLinq.Test/MemoizeTest.cs index d3ee0b0b2..b66da7532 100644 --- a/Tests/SuperLinq.Test/MemoizeTest.cs +++ b/Tests/SuperLinq.Test/MemoizeTest.cs @@ -443,20 +443,12 @@ public void MemoizeProxyReturnsCollectionIteratorDirectly() Assert.Equal(42, memo.Count); } - private class ProxyCollection : ICollection + private class ProxyCollection( + Func> enumeratorFunc, + Func countFunc + ) : ICollection { - private readonly Func> _enumeratorFunc; - private readonly Func _countFunc; - - public ProxyCollection( - Func> enumeratorFunc, - Func countFunc) - { - _enumeratorFunc = enumeratorFunc; - _countFunc = countFunc; - } - - public int Count => _countFunc(); + public int Count => countFunc(); public void Add(int item) => throw new TestException(); public void Clear() => throw new TestException(); @@ -466,8 +458,8 @@ public ProxyCollection( public virtual void CopyTo(int[] array, int arrayIndex) => throw new TestException(); - public IEnumerator GetEnumerator() => _enumeratorFunc(); - IEnumerator IEnumerable.GetEnumerator() => _enumeratorFunc(); + public IEnumerator GetEnumerator() => enumeratorFunc(); + IEnumerator IEnumerable.GetEnumerator() => enumeratorFunc(); } #if false diff --git a/Tests/SuperLinq.Test/ReadOnlyCollection.cs b/Tests/SuperLinq.Test/ReadOnlyCollection.cs index 00e6198b5..453eec86e 100644 --- a/Tests/SuperLinq.Test/ReadOnlyCollection.cs +++ b/Tests/SuperLinq.Test/ReadOnlyCollection.cs @@ -7,12 +7,12 @@ internal static class ReadOnlyCollection public static IReadOnlyCollection From(params T[] items) => new ListCollection(items); - private sealed class ListCollection : IReadOnlyCollection + private sealed class ListCollection( + TList list + ) : IReadOnlyCollection where TList : IList { - private readonly TList _list; - - public ListCollection(TList list) => _list = list; + private readonly TList _list = list; public IEnumerator GetEnumerator() => _list.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); diff --git a/Tests/SuperLinq.Test/TestingSequence.cs b/Tests/SuperLinq.Test/TestingSequence.cs index 14fbb663d..8eb311c0a 100644 --- a/Tests/SuperLinq.Test/TestingSequence.cs +++ b/Tests/SuperLinq.Test/TestingSequence.cs @@ -48,10 +48,7 @@ public enum Options } } -public class TestingSequenceException : Exception -{ - public TestingSequenceException(string message) : base(message) { } -} +public class TestingSequenceException(string message) : Exception(message); /// /// Sequence that asserts whether its iterator has been disposed diff --git a/Tests/SuperLinq.Test/ToDataTableTest.cs b/Tests/SuperLinq.Test/ToDataTableTest.cs index a0cfaf543..fca6a2ab0 100644 --- a/Tests/SuperLinq.Test/ToDataTableTest.cs +++ b/Tests/SuperLinq.Test/ToDataTableTest.cs @@ -5,13 +5,13 @@ namespace Test; public class ToDataTableTest { - private class TestObject + private class TestObject(int key) { - public int _keyField; - public Guid? _aNullableGuidField; + public int _keyField = key; + public Guid? _aNullableGuidField = Guid.NewGuid(); - public string AString { get; } - public decimal? ANullableDecimal { get; } + public string AString { get; } = "ABCDEFGHIKKLMNOPQRSTUVWXYSZ"; + public decimal? ANullableDecimal { get; } = key / 3; public object Unreadable { set => throw new NotImplementedException(); } public object this[int index] @@ -19,16 +19,6 @@ public object this[int index] get => new(); set { } } - - - public TestObject(int key) - { - _keyField = key; - _aNullableGuidField = Guid.NewGuid(); - - ANullableDecimal = key / 3; - AString = "ABCDEFGHIKKLMNOPQRSTUVWXYSZ"; - } } private readonly IReadOnlyCollection _testObjects; diff --git a/Tests/SuperLinq.Test/TraverseTest.cs b/Tests/SuperLinq.Test/TraverseTest.cs index 69ff62497..5d2ae200e 100644 --- a/Tests/SuperLinq.Test/TraverseTest.cs +++ b/Tests/SuperLinq.Test/TraverseTest.cs @@ -33,16 +33,13 @@ public void TraverseBreadthFirstPreservesChildrenOrder() res.AssertSequenceEqual(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } - private class Tree + private class Tree( + T value, + IEnumerable> children + ) { - public T Value { get; } - public IEnumerable> Children { get; } - - public Tree(T value, IEnumerable> children) - { - Value = value; - Children = children; - } + public T Value { get; } = value; + public IEnumerable> Children { get; } = children; } private static class Tree diff --git a/Tests/SuperLinq.Test/TrySingleTest.cs b/Tests/SuperLinq.Test/TrySingleTest.cs index 8adcac976..a94693ff6 100644 --- a/Tests/SuperLinq.Test/TrySingleTest.cs +++ b/Tests/SuperLinq.Test/TrySingleTest.cs @@ -50,18 +50,15 @@ public void TrySingleWithSingletonCollection() Assert.Equal(10, value); } - private sealed class BreakingSingleElementCollection : ICollection + private sealed class BreakingSingleElementCollection( + T element + ) : ICollection { - private readonly T _element; - - public BreakingSingleElementCollection(T element) => - _element = element; - public int Count { get; } = 1; public IEnumerator GetEnumerator() { - yield return _element; + yield return element; throw new InvalidOperationException($"{nameof(SuperEnumerable.TrySingle)} should not have attempted to consume a second element."); } From 9263433f74a1277b0f06c4c615a080760ff24805 Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:07:24 +0000 Subject: [PATCH 2/5] Fix .editorconfig error messages --- .editorconfig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.editorconfig b/.editorconfig index 12e46e3f0..c785362a8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -242,7 +242,7 @@ dotnet_diagnostic.RS0026.severity = none # RS0026: Do not add multiple public 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 +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 +dotnet_diagnostic.CS9113.severity = error # CS9113: Your class never references the primary constructor +dotnet_diagnostic.CS9124.severity = error # CS9124: Parameter is captured into the state of the enclosing type and its value is also used to initialize a field, property, or event +dotnet_diagnostic.CS9179.severity = error # CS9179: Primary constructor parameter is shadowed by a member from base From 47cdb0936071596a6b6477e0b0fe36bddfd817de Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:00:20 +0000 Subject: [PATCH 3/5] Fix naming conflict --- Source/SuperLinq/Insert.cs | 22 ++++++++++++---------- Source/SuperLinq/Replace.cs | 16 +++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Source/SuperLinq/Insert.cs b/Source/SuperLinq/Insert.cs index ea50d2dd2..b09f0df7d 100644 --- a/Source/SuperLinq/Insert.cs +++ b/Source/SuperLinq/Insert.cs @@ -198,11 +198,13 @@ private sealed class InsertListIterator( Index index ) : ListIterator { + private readonly Index _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); @@ -212,7 +214,7 @@ public override int 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); @@ -238,24 +240,24 @@ public override void CopyTo(T[] array, int arrayIndex) var span = array.AsSpan()[arrayIndex..]; var cnt = first.Count; - var idx = index.GetOffset(cnt); + var idx = _index.GetOffset(cnt); span[idx..cnt].CopyTo(span[(idx + second.Count)..]); second.CopyTo(array, arrayIndex + idx); } - protected override T ElementAt(int index1) + protected override T ElementAt(int index) { - ArgumentOutOfRangeException.ThrowIfNegative(index1); - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index1, Count); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - var idx = index.GetOffset(first.Count); + var idx = _index.GetOffset(first.Count); ArgumentOutOfRangeException.ThrowIfNegative(idx); ArgumentOutOfRangeException.ThrowIfGreaterThan(idx, first.Count); - return index1 < idx ? first[index1] : - index1 < idx + second.Count ? second[index1 - idx] : - first[index1 - second.Count]; + return index < idx ? first[index] : + index < idx + second.Count ? second[index - idx] : + first[index - second.Count]; } } } diff --git a/Source/SuperLinq/Replace.cs b/Source/SuperLinq/Replace.cs index 41e840c99..95b069df7 100644 --- a/Source/SuperLinq/Replace.cs +++ b/Source/SuperLinq/Replace.cs @@ -135,12 +135,14 @@ private sealed class ReplaceIterator( Index index ) : ListIterator { + private readonly Index _index = index; + public override int Count => source.Count; protected override IEnumerable GetEnumerable() { var cnt = (uint)source.Count; - var idx = index.GetOffset(source.Count); + var idx = _index.GetOffset(source.Count); for (var i = 0; i < cnt; i++) yield return i == idx ? value : source[i]; @@ -154,19 +156,19 @@ public override void CopyTo(TSource[] array, int arrayIndex) source.CopyTo(array, arrayIndex); - var idx = index.GetOffset(source.Count); + var idx = _index.GetOffset(source.Count); if (idx >= 0 && idx < source.Count) array[arrayIndex + idx] = value; } - protected override TSource ElementAt(int index1) + protected override TSource ElementAt(int index) { - ArgumentOutOfRangeException.ThrowIfNegative(index1); - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index1, Count); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return index1 == index.GetOffset(source.Count) + return index == _index.GetOffset(source.Count) ? value - : source[index1]; + : source[index]; } } } From 16768edb69614f00d8a1a9879d6f56e6d5378a9f Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:15:16 +0000 Subject: [PATCH 4/5] Add all fields when a field has a naming conflict --- Source/SuperLinq/Insert.cs | 40 +++++++++++++++++++------------------ Source/SuperLinq/Replace.cs | 28 +++++++++++++++----------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Source/SuperLinq/Insert.cs b/Source/SuperLinq/Insert.cs index b09f0df7d..a3bf7a223 100644 --- a/Source/SuperLinq/Insert.cs +++ b/Source/SuperLinq/Insert.cs @@ -198,36 +198,38 @@ private sealed class InsertListIterator( Index index ) : ListIterator { + private readonly IList _first = first; + private readonly IList _second = second; private readonly Index _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) @@ -236,14 +238,14 @@ 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 cnt = _first.Count; var idx = _index.GetOffset(cnt); - span[idx..cnt].CopyTo(span[(idx + second.Count)..]); + span[idx..cnt].CopyTo(span[(idx + _second.Count)..]); - second.CopyTo(array, arrayIndex + idx); + _second.CopyTo(array, arrayIndex + idx); } protected override T ElementAt(int index) @@ -251,13 +253,13 @@ protected override T ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, 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 index < idx ? _first[index] : + index < idx + _second.Count ? _second[index - idx] : + _first[index - _second.Count]; } } } diff --git a/Source/SuperLinq/Replace.cs b/Source/SuperLinq/Replace.cs index 95b069df7..49a631179 100644 --- a/Source/SuperLinq/Replace.cs +++ b/Source/SuperLinq/Replace.cs @@ -1,4 +1,6 @@ -namespace SuperLinq; +using System.Diagnostics.CodeAnalysis; + +namespace SuperLinq; public static partial class SuperEnumerable { @@ -135,17 +137,19 @@ private sealed class ReplaceIterator( Index index ) : ListIterator { + private readonly IList _source = source; + private readonly TSource _value = value; private readonly Index _index = index; - public override int Count => source.Count; + public override int Count => _source.Count; protected override IEnumerable GetEnumerable() { - var cnt = (uint)source.Count; - var idx = _index.GetOffset(source.Count); + var cnt = (uint)_source.Count; + var idx = _index.GetOffset(_source.Count); for (var i = 0; i < cnt; i++) - yield return i == idx ? value : source[i]; + yield return i == idx ? _value : _source[i]; } public override void CopyTo(TSource[] array, int arrayIndex) @@ -154,11 +158,11 @@ public override void CopyTo(TSource[] array, int arrayIndex) ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count); - source.CopyTo(array, arrayIndex); + _source.CopyTo(array, arrayIndex); - var idx = _index.GetOffset(source.Count); - if (idx >= 0 && idx < source.Count) - array[arrayIndex + idx] = value; + var idx = _index.GetOffset(_source.Count); + if (idx >= 0 && idx < _source.Count) + array[arrayIndex + idx] = _value; } protected override TSource ElementAt(int index) @@ -166,9 +170,9 @@ protected override TSource ElementAt(int index) ArgumentOutOfRangeException.ThrowIfNegative(index); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); - return index == _index.GetOffset(source.Count) - ? value - : source[index]; + return index == _index.GetOffset(_source.Count) + ? _value + : _source[index]; } } } From 2607cc4ec9efcdb825c8f5df069d4474becf54b6 Mon Sep 17 00:00:00 2001 From: Head0nF1re <77078775+Head0nF1re@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:24:35 +0000 Subject: [PATCH 5/5] Remove unnecessary using directive --- Source/SuperLinq/Replace.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Source/SuperLinq/Replace.cs b/Source/SuperLinq/Replace.cs index 49a631179..f7da40bd1 100644 --- a/Source/SuperLinq/Replace.cs +++ b/Source/SuperLinq/Replace.cs @@ -1,6 +1,4 @@ -using System.Diagnostics.CodeAnalysis; - -namespace SuperLinq; +namespace SuperLinq; public static partial class SuperEnumerable {