8
8
using System ;
9
9
using System . Collections . Generic ;
10
10
using System . Collections . Immutable ;
11
+ using CommunityToolkit . Mvvm . SourceGenerators . Helpers ;
11
12
using Microsoft . CodeAnalysis ;
12
13
13
14
namespace CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
@@ -22,36 +23,46 @@ internal static class IncrementalValuesProviderExtensions
22
23
/// </summary>
23
24
/// <typeparam name="TLeft">The type of left items in each tuple.</typeparam>
24
25
/// <typeparam name="TRight">The type of right items in each tuple.</typeparam>
26
+ /// <typeparam name="TKey">The type of resulting key elements.</typeparam>
25
27
/// <typeparam name="TElement">The type of resulting projected elements.</typeparam>
26
28
/// <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>
29
31
/// <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 > (
31
33
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 >
34
40
{
35
- return source . Collect ( ) . SelectMany ( ( item , _ ) =>
41
+ return source . Collect ( ) . SelectMany ( ( item , token ) =>
36
42
{
37
- Dictionary < TLeft , ImmutableArray < TElement > . Builder > map = new ( comparer ) ;
43
+ Dictionary < TKey , ImmutableArray < TElement > . Builder > map = new ( ) ;
38
44
39
- foreach ( ( TLeft hierarchy , TRight info ) in item )
45
+ foreach ( ( TLeft , TRight ) pair in item )
40
46
{
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 ) )
42
51
{
43
52
builder = ImmutableArray . CreateBuilder < TElement > ( ) ;
44
53
45
- map . Add ( hierarchy , builder ) ;
54
+ map . Add ( key , builder ) ;
46
55
}
47
56
48
- builder . Add ( projection ( info ) ) ;
57
+ builder . Add ( element ) ;
49
58
}
50
59
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 > ) > ( ) ;
53
64
54
- foreach ( KeyValuePair < TLeft , ImmutableArray < TElement > . Builder > entry in map )
65
+ foreach ( KeyValuePair < TKey , ImmutableArray < TElement > . Builder > entry in map )
55
66
{
56
67
result . Add ( ( entry . Key , entry . Value . ToImmutable ( ) ) ) ;
57
68
}
0 commit comments