Skip to content

Commit f7a76b9

Browse files
committed
Improve IncrementalValuesProviderExtensions.GroupBy
1 parent 16ce464 commit f7a76b9

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
1010
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
11+
using CommunityToolkit.Mvvm.SourceGenerators.Helpers;
1112
using CommunityToolkit.Mvvm.SourceGenerators.Models;
1213
using Microsoft.CodeAnalysis;
1314
using Microsoft.CodeAnalysis.CSharp;
@@ -58,10 +59,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5859
.Where(static item => item.Info.Value is not null)!;
5960

6061
// Split and group by containing type
61-
IncrementalValuesProvider<(HierarchyInfo Hierarchy, ImmutableArray<PropertyInfo> Properties)> groupedPropertyInfo =
62+
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EquatableArray<PropertyInfo> Properties)> groupedPropertyInfo =
6263
propertyInfo
63-
.GroupBy(EqualityComparer<HierarchyInfo>.Default, static item => item.Value)
64-
.WithComparers(EqualityComparer<HierarchyInfo>.Default, EqualityComparer<PropertyInfo>.Default.ForImmutableArray());
64+
.GroupBy(static item => item.Left, static item => item.Right.Value);
6565

6666
// Generate the requested properties and methods
6767
context.RegisterSourceOutput(groupedPropertyInfo, static (context, item) =>

CommunityToolkit.Mvvm.SourceGenerators/Extensions/IncrementalValuesProviderExtensions.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.Collections.Immutable;
11+
using CommunityToolkit.Mvvm.SourceGenerators.Helpers;
1112
using Microsoft.CodeAnalysis;
1213

1314
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
@@ -22,36 +23,46 @@ internal static class IncrementalValuesProviderExtensions
2223
/// </summary>
2324
/// <typeparam name="TLeft">The type of left items in each tuple.</typeparam>
2425
/// <typeparam name="TRight">The type of right items in each tuple.</typeparam>
26+
/// <typeparam name="TKey">The type of resulting key elements.</typeparam>
2527
/// <typeparam name="TElement">The type of resulting projected elements.</typeparam>
2628
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
27-
/// <param name="comparer">A <typeparamref name="TLeft"/> comparer.</param>
28-
/// <param name="projection">A projection function to convert gathered elements.</param>
29+
/// <param name="keySelector">The key selection <see cref="Func{T, TResult}"/>.</param>
30+
/// <param name="elementSelector">The element selection <see cref="Func{T, TResult}"/>.</param>
2931
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the grouped results.</returns>
30-
public static IncrementalValuesProvider<(TLeft Left, ImmutableArray<TElement> Right)> GroupBy<TLeft, TRight, TElement>(
32+
public static IncrementalValuesProvider<(TKey Key, EquatableArray<TElement> Right)> GroupBy<TLeft, TRight, TKey, TElement>(
3133
this IncrementalValuesProvider<(TLeft Left, TRight Right)> source,
32-
IEqualityComparer<TLeft> comparer,
33-
Func<TRight, TElement> projection)
34+
Func<(TLeft Left, TRight Right), TKey> keySelector,
35+
Func<(TLeft Left, TRight Right), TElement> elementSelector)
36+
where TLeft : IEquatable<TLeft>
37+
where TRight : IEquatable<TRight>
38+
where TKey : IEquatable<TKey>
39+
where TElement : IEquatable<TElement>
3440
{
35-
return source.Collect().SelectMany((item, _) =>
41+
return source.Collect().SelectMany((item, token) =>
3642
{
37-
Dictionary<TLeft, ImmutableArray<TElement>.Builder> map = new(comparer);
43+
Dictionary<TKey, ImmutableArray<TElement>.Builder> map = new();
3844

39-
foreach ((TLeft hierarchy, TRight info) in item)
45+
foreach ((TLeft, TRight) pair in item)
4046
{
41-
if (!map.TryGetValue(hierarchy, out ImmutableArray<TElement>.Builder builder))
47+
TKey key = keySelector(pair);
48+
TElement element = elementSelector(pair);
49+
50+
if (!map.TryGetValue(key, out ImmutableArray<TElement>.Builder builder))
4251
{
4352
builder = ImmutableArray.CreateBuilder<TElement>();
4453

45-
map.Add(hierarchy, builder);
54+
map.Add(key, builder);
4655
}
4756

48-
builder.Add(projection(info));
57+
builder.Add(element);
4958
}
5059

51-
ImmutableArray<(TLeft Hierarchy, ImmutableArray<TElement> Elements)>.Builder result =
52-
ImmutableArray.CreateBuilder<(TLeft, ImmutableArray<TElement>)>();
60+
token.ThrowIfCancellationRequested();
61+
62+
ImmutableArray<(TKey Key, EquatableArray<TElement> Elements)>.Builder result =
63+
ImmutableArray.CreateBuilder<(TKey, EquatableArray<TElement>)>();
5364

54-
foreach (KeyValuePair<TLeft, ImmutableArray<TElement>.Builder> entry in map)
65+
foreach (KeyValuePair<TKey, ImmutableArray<TElement>.Builder> entry in map)
5566
{
5667
result.Add((entry.Key, entry.Value.ToImmutable()));
5768
}

0 commit comments

Comments
 (0)