Skip to content

Commit

Permalink
Support .NET Standard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Jul 15, 2024
1 parent a459891 commit 02b0073
Show file tree
Hide file tree
Showing 39 changed files with 946 additions and 79 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="Scriban" Version="5.10.0" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="ThisAssembly.Resources" Version="1.4.3" />
<PackageVersion Include="xunit" Version="2.8.1" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Source/SuperLinq.Async/GetShortestPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public partial class AsyncSuperEnumerable
var queue = new UpdatablePriorityQueue<TState, (TState? parent, TCost? cost)>(
16,
priorityComparer: Comparer<(TState? parent, TCost? cost)>.Create(
(x, y) => costComparer.Compare(x.cost, y.cost)),
(x, y) => costComparer.Compare(x.cost!, y.cost!)),
stateComparer);

var current = start;
Expand Down Expand Up @@ -1154,7 +1154,7 @@ public partial class AsyncSuperEnumerable
cancellationToken.ThrowIfCancellationRequested();

if (!totalCost.TryGetValue(current, out var oldCost)
|| costComparer.Compare(costs.traversed, oldCost) < 0)
|| costComparer.Compare(costs.traversed!, oldCost!) < 0)
{
totalCost[current] = costs.traversed;
if (await predicate(current).ConfigureAwait(false))
Expand Down Expand Up @@ -1503,7 +1503,7 @@ public partial class AsyncSuperEnumerable
cancellationToken.ThrowIfCancellationRequested();

if (!totalCost.TryGetValue(current, out var oldCost)
|| costComparer.Compare(from.traversed, oldCost.traversed) < 0)
|| costComparer.Compare(from.traversed!, oldCost.traversed!) < 0)
{
totalCost[current] = (from.parent, from.traversed);
if (await predicate(current).ConfigureAwait(false))
Expand Down
2 changes: 2 additions & 0 deletions Source/SuperLinq.Async/IAsyncBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface IAsyncBuffer<out T> : IAsyncEnumerable<T>, IAsyncDisposable
/// </summary>
int Count { get; }

#if !NETSTANDARD
/// <summary>
/// Configures how awaits on the tasks returned from an async disposable are performed.
/// </summary>
Expand All @@ -31,4 +32,5 @@ public interface IAsyncBuffer<out T> : IAsyncEnumerable<T>, IAsyncDisposable
/// </returns>
public ConfiguredAsyncDisposable ConfigureAwait(bool continueOnCapturedContext) =>
((IAsyncDisposable)this).ConfigureAwait(continueOnCapturedContext);
#endif
}
6 changes: 5 additions & 1 deletion Source/SuperLinq.Async/Join.MergeJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,10 @@ file sealed class ComparerEqualityComparer<TKey>(
IComparer<TKey> comparer
) : IEqualityComparer<TKey>
{
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;
#if NETCOREAPP
public int GetHashCode([DisallowNull] TKey obj) => ThrowHelper.ThrowNotSupportedException<int>();
#else
public int GetHashCode(TKey obj) => ThrowHelper.ThrowNotSupportedException<int>();
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Source/SuperLinq.Async/SuperLinq.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
<PackageReference Include="MinVer" PrivateAssets="All" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Memory" PrivateAssets="All" Condition="$(TargetFramework) == 'netstandard2.0'" />
<ProjectReference Include="..\SuperLinq\SuperLinq.csproj" />
</ItemGroup>

Expand Down
9 changes: 9 additions & 0 deletions Source/SuperLinq/Collections/UpdatablePriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

#if !NETSTANDARD
using System.Runtime.CompilerServices;
#endif

namespace SuperLinq.Collections;

Expand Down Expand Up @@ -650,12 +653,16 @@ public void EnqueueRangeMinimum(IEnumerable<TElement> elements, TPriority priori
/// </summary>
public void Clear()
{
#if !NETSTANDARD
if (RuntimeHelpers.IsReferenceOrContainsReferences<(TElement, TPriority)>())
{
#endif
// Clear the elements so that the gc can reclaim the references
Array.Clear(_nodes, 0, Count);
_elementIndex.Clear();
#if !NETSTANDARD
}
#endif

Count = 0;
_version++;
Expand Down Expand Up @@ -748,7 +755,9 @@ private void RemoveRootNode()
MoveDownCustomComparer(lastNode, 0);
}

#if !NETSTANDARD
if (RuntimeHelpers.IsReferenceOrContainsReferences<(TElement, TPriority)>())
#endif
_nodes[lastNodeIndex] = default;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/SuperLinq/EndsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
comparer ??= EqualityComparer<T>.Default;

var snd = second.ToList();
return first.TakeLast(snd.Count)
return first.Take(^snd.Count..)
.SequenceEqual(snd, comparer);
}
}
47 changes: 47 additions & 0 deletions Source/SuperLinq/Future.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETCOREAPP

using System.Diagnostics.CodeAnalysis;

namespace SuperLinq;

internal static class Future
{
public static bool TryDequeue<T>(this Queue<T> queue, out T result)
{
if (queue.Count == 0)
{
result = default!;
return false;
}

result = queue.Dequeue();
return !EqualityComparer<T>.Default.Equals(result, default!);
}

public static bool TryGetValue<T>(this SortedSet<T> set, T equalValue, [MaybeNullWhen(false)] out T actualValue)
{
var index = set.IndexOf(equalValue);
if (index == -1)
{
actualValue = default;
return false;
}

actualValue = set.ElementAt(index);
return true;
}

public static HashSet<TSource> ToHashSet<TSource>(
this IEnumerable<TSource> source,
IEqualityComparer<TSource>? comparer)
{
if (source == null)
ArgumentNullException.ThrowIfNull(source);
return new HashSet<TSource>(source, comparer);
}
}

#endif
2 changes: 1 addition & 1 deletion Source/SuperLinq/GetShortestPath.A-Star.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public partial class SuperEnumerable
do
{
if (!totalCost.TryGetValue(current, out var oldCost)
|| costComparer.Compare(from.traversed, oldCost.traversed) < 0)
|| costComparer.Compare(from.traversed!, oldCost.traversed!) < 0)
{
totalCost[current] = (from.parent, from.traversed);
if (predicate(current))
Expand Down
2 changes: 1 addition & 1 deletion Source/SuperLinq/GetShortestPathCost.A-Star.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public partial class SuperEnumerable
do
{
if (!totalCost.TryGetValue(current, out var oldCost)
|| costComparer.Compare(costs.traversed, oldCost) < 0)
|| costComparer.Compare(costs.traversed!, oldCost!) < 0)
{
totalCost[current] = costs.traversed;
if (predicate(current))
Expand Down
2 changes: 1 addition & 1 deletion Source/SuperLinq/GetShortestPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public partial class SuperEnumerable
var queue = new UpdatablePriorityQueue<TState, (TState? parent, TCost? cost)>(
16,
priorityComparer: Comparer<(TState? parent, TCost? cost)>.Create(
(x, y) => costComparer.Compare(x.cost, y.cost)),
(x, y) => costComparer.Compare(x.cost!, y.cost!)),
stateComparer);

var current = start;
Expand Down
6 changes: 5 additions & 1 deletion Source/SuperLinq/Join.MergeJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ file sealed class ComparerEqualityComparer<TKey>(
IComparer<TKey> comparer
) : IEqualityComparer<TKey>
{
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;
#if NETCOREAPP
public int GetHashCode([DisallowNull] TKey obj) => ThrowHelper.ThrowNotSupportedException<int>();
#else
public int GetHashCode(TKey obj) => ThrowHelper.ThrowNotSupportedException<int>();
#endif
}
4 changes: 4 additions & 0 deletions Source/SuperLinq/KeyValuePairEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public bool Equals(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y) =
&& _valueComparer.Equals(x.Value, y.Value);

public int GetHashCode(KeyValuePair<TKey, TValue> obj) =>
#if NETCOREAPP
HashCode.Combine(
_keyComparer.GetHashCode(obj.Key!),
_valueComparer.GetHashCode(obj.Value!));
#else
_keyComparer.GetHashCode(obj.Key) * 397 ^ _valueComparer.GetHashCode(obj.Value);
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Loading

0 comments on commit 02b0073

Please sign in to comment.