Skip to content

Commit

Permalink
Add primary constructors (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Head0nF1re authored Feb 2, 2024
1 parent 8fcdd11 commit 7691a3f
Show file tree
Hide file tree
Showing 50 changed files with 507 additions and 798 deletions.
7 changes: 6 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ImplicitUsings>enable</ImplicitUsings>

<CheckEolTargetFramework>false</CheckEolTargetFramework>

<AnalysisLevel>latest-all</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>

Expand Down
31 changes: 13 additions & 18 deletions Source/SuperLinq.Async/EnumeratorList.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace SuperLinq.Async;

internal sealed class EnumeratorList<T> : IAsyncDisposable
internal sealed class EnumeratorList<T>(
List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator> iter
) : IAsyncDisposable
{
private readonly List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator> _iter;

internal static async ValueTask<EnumeratorList<T>> Create(IEnumerable<IAsyncEnumerable<T>> sources, CancellationToken cancellationToken)
{
var list = new List<ConfiguredCancelableAsyncEnumerable<T>.Enumerator>();
Expand All @@ -24,24 +24,19 @@ internal static async ValueTask<EnumeratorList<T>> Create(IEnumerable<IAsyncEnum
}
}

public EnumeratorList(List<ConfiguredCancelableAsyncEnumerable<T>.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<bool> 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();
}

Expand All @@ -50,25 +45,25 @@ public async ValueTask<bool> MoveNext(int i)

public async ValueTask<bool> 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();
}

return false;
}

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();
}
}
15 changes: 6 additions & 9 deletions Source/SuperLinq.Async/GroupAdjacent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,14 @@ private static Grouping<TKey, TElement> CreateGroupAdjacentGrouping<TKey, TEleme
new(key, members.AsReadOnly());

[Serializable]
private sealed class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
private sealed class Grouping<TKey, TElement>(
TKey key,
IEnumerable<TElement> members
) : IGrouping<TKey, TElement>
{
private readonly IEnumerable<TElement> _members;
private readonly IEnumerable<TElement> _members = members;

public Grouping(TKey key, IEnumerable<TElement> members)
{
Key = key;
_members = members;
}

public TKey Key { get; }
public TKey Key { get; } = key;

public IEnumerator<TElement> GetEnumerator() => _members.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
13 changes: 4 additions & 9 deletions Source/SuperLinq.Async/Join.MergeJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,10 @@ private static async IAsyncEnumerable<TResult> JoinMerge<TLeft, TRight, TKey, TR
}
}

file sealed class ComparerEqualityComparer<TKey> : IEqualityComparer<TKey>
file sealed class ComparerEqualityComparer<TKey>(
IComparer<TKey> comparer
) : IEqualityComparer<TKey>
{
private readonly IComparer<TKey> _comparer;

public ComparerEqualityComparer(IComparer<TKey> 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<int>();
}
11 changes: 4 additions & 7 deletions Source/SuperLinq.Async/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public static IAsyncBuffer<TSource> Memoize<TSource>(this IAsyncEnumerable<TSour
return new EnumerableMemoizedBuffer<TSource>(source);
}

private sealed class EnumerableMemoizedBuffer<T> : IAsyncBuffer<T>
private sealed class EnumerableMemoizedBuffer<T>(
IAsyncEnumerable<T> source
) : IAsyncBuffer<T>
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);

private IAsyncEnumerable<T>? _source;
private IAsyncEnumerable<T>? _source = source;

private IAsyncEnumerator<T>? _enumerator;
private List<T> _buffer = new();
Expand All @@ -47,11 +49,6 @@ private sealed class EnumerableMemoizedBuffer<T> : IAsyncBuffer<T>

private bool _disposed;

public EnumerableMemoizedBuffer(IAsyncEnumerable<T> source)
{
_source = source;
}

public int Count => _buffer.Count;

public async ValueTask Reset(CancellationToken cancellationToken = default)
Expand Down
11 changes: 4 additions & 7 deletions Source/SuperLinq.Async/Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public static IAsyncBuffer<TSource> Publish<TSource>(this IAsyncEnumerable<TSour
return new PublishBuffer<TSource>(source);
}

private sealed class PublishBuffer<T> : IAsyncBuffer<T>
private sealed class PublishBuffer<T>(
IAsyncEnumerable<T> source
) : IAsyncBuffer<T>
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);

private IAsyncEnumerable<T>? _source;
private IAsyncEnumerable<T>? _source = source;

private IAsyncEnumerator<T>? _enumerator;
private List<Queue<T>>? _buffers;
Expand All @@ -38,11 +40,6 @@ private sealed class PublishBuffer<T> : IAsyncBuffer<T>

private bool _disposed;

public PublishBuffer(IAsyncEnumerable<T> source)
{
_source = source;
}

public int Count => _buffers?.Count > 0 ? _buffers.Max(x => x.Count) : 0;

public async ValueTask Reset(CancellationToken cancellationToken = default)
Expand Down
11 changes: 4 additions & 7 deletions Source/SuperLinq.Async/Share.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public static IAsyncBuffer<TSource> Share<TSource>(this IAsyncEnumerable<TSource
return new SharedBuffer<TSource>(source);
}

private sealed class SharedBuffer<T> : IAsyncBuffer<T>
private sealed class SharedBuffer<T>(
IAsyncEnumerable<T> source
) : IAsyncBuffer<T>
{
private readonly SemaphoreSlim _lock = new(initialCount: 1);

private IAsyncEnumerable<T>? _source;
private IAsyncEnumerable<T>? _source = source;

private IAsyncEnumerator<T>? _enumerator;
private bool _initialized;
Expand All @@ -33,11 +35,6 @@ private sealed class SharedBuffer<T> : IAsyncBuffer<T>

private bool _disposed;

public SharedBuffer(IAsyncEnumerable<T> source)
{
_source = source;
}

public int Count => 0;

public async ValueTask Reset(CancellationToken cancellationToken = default)
Expand Down
48 changes: 18 additions & 30 deletions Source/SuperLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,25 @@ static IEnumerable<TSource> Core(IEnumerable<TSource> source, int count)
}
}

private sealed class AssertCountCollectionIterator<T> : CollectionIterator<T>
private sealed class AssertCountCollectionIterator<T>(
IEnumerable<T> source,
int count
) : CollectionIterator<T>
{
private readonly IEnumerable<T> _source;
private readonly int _count;

public AssertCountCollectionIterator(IEnumerable<T> 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<T> 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;
}

Expand All @@ -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<T> : ListIterator<T>
private sealed class AssertCountListIterator<T>(
IList<T> source,
int count
) : ListIterator<T>
{
private readonly IList<T> _source;
private readonly int _count;

public AssertCountListIterator(IList<T> 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;
}
}

Expand All @@ -120,7 +108,7 @@ protected override IEnumerable<T> GetEnumerable()
var cnt = (uint)Count;
for (var i = 0; i < cnt; i++)
{
yield return _source[i];
yield return source[i];
}
}

Expand All @@ -130,15 +118,15 @@ 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)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);

return _source[index];
return source[index];
}
}
}
38 changes: 16 additions & 22 deletions Source/SuperLinq/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,22 @@ static IEnumerable<IList<TSource>> Core(IEnumerable<TSource> source, int size)
}
}

private sealed class BatchIterator<T> : ListIterator<IList<T>>
private sealed class BatchIterator<T>(
IList<T> source,
int size
) : ListIterator<IList<T>>
{
private readonly IList<T> _source;
private readonly int _size;

public BatchIterator(IList<T> 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<IList<T>> 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;
}
Expand All @@ -116,7 +110,7 @@ protected override IEnumerable<IList<T>> 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;
}
Expand All @@ -127,11 +121,11 @@ protected override IList<T> 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;
}
Expand Down
Loading

0 comments on commit 7691a3f

Please sign in to comment.