Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DensePartialSortBy to use NullKeyDictionary #500

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Source/SuperLinq.Async/DensePartialSort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SuperLinq.Async;
using SuperLinq.Collections;

namespace SuperLinq.Async;

public static partial class AsyncSuperEnumerable
{
Expand Down Expand Up @@ -249,37 +251,37 @@ public static IAsyncEnumerable<TSource> DensePartialSortBy<TSource, TKey>(
static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var top = new SortedSet<TKey>(comparer);
var dic = new Dictionary<(TKey Key, int Index), List<TSource>>(count);
var dic = new NullKeyDictionary<TKey, List<TSource>>(count);

await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var key = keySelector(item);
if (top.TryGetValue(key, out var oKey))
{
dic[(oKey, 1)].Add(item);
dic[oKey].Add(item);
continue;
}

if (top.Count < count)
{
_ = top.Add(key);
dic[(key, 1)] = new() { item, };
dic[key] = new() { item, };
continue;
}

var max = Debug.AssertNotNull(top.Max);
if (comparer.Compare(key, max) > 0)
continue;

_ = dic.Remove((max, 1));
_ = dic.Remove(max);
_ = top.Remove(max);
_ = top.Add(key);
dic[(key, 1)] = new() { item, };
dic[key] = new() { item, };
}

foreach (var entry in top)
{
foreach (var i in dic[(entry, 1)])
foreach (var i in dic[entry])
yield return i;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Source/SuperLinq/Collections/NullKeyDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public NullKeyDictionary(IEqualityComparer<TKey>? comparer)
: base(comparer: ValueTupleEqualityComparer.Create(comparer))
{ }

public NullKeyDictionary(int count)
: base(count, comparer: ValueTupleEqualityComparer.Create<TKey>(default))
{ }

public TValue this[TKey key]
{
get => this[ValueTuple.Create(key)];
Expand Down
16 changes: 9 additions & 7 deletions Source/SuperLinq/DensePartialSort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SuperLinq;
using SuperLinq.Collections;

namespace SuperLinq;

public static partial class SuperEnumerable
{
Expand Down Expand Up @@ -249,37 +251,37 @@ public static IEnumerable<TSource> DensePartialSortBy<TSource, TKey>(
static IEnumerable<TSource> Core(IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
{
var top = new SortedSet<TKey>(comparer);
var dic = new Dictionary<(TKey Key, int Index), List<TSource>>(count);
var dic = new NullKeyDictionary<TKey, List<TSource>>(count);

foreach (var item in source)
{
var key = keySelector(item);
if (top.TryGetValue(key, out var oKey))
{
dic[(oKey, 1)].Add(item);
dic[oKey].Add(item);
continue;
}

if (top.Count < count)
{
_ = top.Add(key);
dic[(key, 1)] = new() { item, };
dic[key] = new() { item, };
continue;
}

var max = Debug.AssertNotNull(top.Max);
if (comparer.Compare(key, max) > 0)
continue;

_ = dic.Remove((max, 1));
_ = dic.Remove(max);
_ = top.Remove(max);
_ = top.Add(key);
dic[(key, 1)] = new() { item, };
dic[key] = new() { item, };
}

foreach (var entry in top)
{
foreach (var i in dic[(entry, 1)])
foreach (var i in dic[entry])
yield return i;
}
}
Expand Down
39 changes: 39 additions & 0 deletions Tests/SuperLinq.Async.Test/DensePartialSortByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,43 @@ public async Task DensePartialSortWithComparer()
await sorted.Select(e => e.Key[0])
.AssertSequenceEqual('A', 'A', 'C', 'C', 'E', 'E');
}

[Fact]
public async Task DensePartialSortByIsStable()
{
await using var list = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
}.AsTestingSequence(maxEnumerations: 5);

var stableSort = new[]
{
(key: 1, text: "9"),
(key: 1, text: "10"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 5, text: "1"),
(key: 5, text: "2"),
};

for (var i = 1; i <= 5; i++)
{
var sorted = list.DensePartialSortBy(i, x => x.key);
await sorted.AssertSequenceEqual(
stableSort.Take(i * 2));
}
}
}
41 changes: 41 additions & 0 deletions Tests/SuperLinq.Async.Test/DensePartialSortTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,45 @@ await xs
.Select(s => s[0])
.AssertSequenceEqual('A', 'A', 'C', 'C', 'E', 'E');
}

[Fact]
public async Task DensePartialSortIsStable()
{
await using var list = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
}.AsTestingSequence(maxEnumerations: 5);

var stableSort = new[]
{
(key: 1, text: "9"),
(key: 1, text: "10"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 5, text: "1"),
(key: 5, text: "2"),
};

var comparer = Comparer<(int key, string text)>.Create((a, b) => a.key.CompareTo(b.key));

for (var i = 1; i <= 5; i++)
{
var sorted = list.DensePartialSort(i, comparer);
await sorted.AssertSequenceEqual(
stableSort.Take(i * 2));
}
}
}
39 changes: 39 additions & 0 deletions Tests/SuperLinq.Test/DensePartialSortByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,43 @@ public void DensePartialSortWithComparer()
.Select(e => e.Key[0])
.AssertSequenceEqual('A', 'A', 'C', 'C', 'E', 'E');
}

[Fact]
public void DensePartialSortByIsStable()
{
using var list = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
}.AsTestingSequence(maxEnumerations: 5);

var stableSort = new[]
{
(key: 1, text: "9"),
(key: 1, text: "10"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 5, text: "1"),
(key: 5, text: "2"),
};

for (var i = 1; i <= 5; i++)
{
var sorted = list.DensePartialSortBy(i, x => x.key);
sorted.AssertSequenceEqual(
stableSort.Take(i * 2));
}
}
}
41 changes: 41 additions & 0 deletions Tests/SuperLinq.Test/DensePartialSortTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,45 @@ public void DensePartialSortWithComparer()
.Select(s => s[0])
.AssertSequenceEqual('A', 'A', 'C', 'C', 'E', 'E');
}

[Fact]
public void DensePartialSortIsStable()
{
using var list = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
}.AsTestingSequence(maxEnumerations: 5);

var stableSort = new[]
{
(key: 1, text: "9"),
(key: 1, text: "10"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 5, text: "1"),
(key: 5, text: "2"),
};

var comparer = Comparer<(int key, string text)>.Create((a, b) => a.key.CompareTo(b.key));

for (var i = 1; i <= 5; i++)
{
var sorted = list.DensePartialSort(i, comparer);
sorted.AssertSequenceEqual(
stableSort.Take(i * 2));
}
}
}