-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
IndexBy.cs
88 lines (86 loc) · 3.1 KB
/
IndexBy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Applies a key-generating function to each element of a sequence and returns a sequence that contains the
/// elements of the original sequence as well its key and index inside the group of its key.
/// </summary>
/// <typeparam name="TSource">
/// Type of the source sequence elements.
/// </typeparam>
/// <typeparam name="TKey">
/// Type of the projected key.
/// </typeparam>
/// <param name="source">
/// Source sequence.
/// </param>
/// <param name="keySelector">
/// Function that projects the key given an element in the source sequence.
/// </param>
/// <returns>
/// A sequence of elements paired with their index within the key-group. The index is the key and the element is
/// the value of the pair.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="keySelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This operator uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<(int index, TSource item)>
IndexBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
return source.IndexBy(keySelector, comparer: null);
}
/// <summary>
/// Applies a key-generating function to each element of a sequence and returns a sequence that contains the
/// elements of the original sequence as well its key and index inside the group of its key. An additional
/// parameter specifies a comparer to use for testing the equivalence of keys.
/// </summary>
/// <typeparam name="TSource">
/// Type of the source sequence elements.
/// </typeparam>
/// <typeparam name="TKey">
/// Type of the projected key.
/// </typeparam>
/// <param name="source">
/// Source sequence.
/// </param>
/// <param name="keySelector">
/// Function that projects the key given an element in the source sequence.
/// </param>
/// <param name="comparer">
/// The equality comparer to use to determine whether or not keys are equal. If <see langword="null"/>, the
/// default equality comparer for <typeparamref name="TSource"/> is used.
/// </param>
/// <returns>
/// A sequence of elements paired with their index within the key-group. The index is the key and the element is
/// the value of the pair.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="keySelector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This operator uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<(int index, TSource item)>
IndexBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey>? comparer)
{
return source
.ScanBy(
keySelector,
static k => (Index: -1, Item: default(TSource)!),
static (s, k, e) => (s.Index + 1, e),
comparer)
.Select(e => e.state);
}
}