-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ToLookup.cs
134 lines (130 loc) · 4.17 KB
/
ToLookup.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Creates an <see cref="ILookup{TKey,TValue}" /> from a sequence of <see cref="KeyValuePair{TKey,TValue}" />
/// elements.
/// </summary>
/// <typeparam name="TKey">
/// The type of the key.
/// </typeparam>
/// <typeparam name="TValue">
/// The type of the value.
/// </typeparam>
/// <param name="source">
/// The source sequence of key-value pairs.
/// </param>
/// <returns>
/// An <see cref="ILookup{TKey,TValue}"/> containing the values mapped to their keys.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>
/// </exception>
/// <remarks>
/// <para>
/// This method executes immediately.
/// </para>
/// </remarks>
public static ILookup<TKey, TValue> ToLookup<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
return source.ToLookup(comparer: null);
}
/// <summary>
/// Creates an <see cref="ILookup{TKey,TValue}" /> from a sequence of <see cref="KeyValuePair{TKey,TValue}" />
/// elements. An additional parameter specifies a comparer for keys.
/// </summary>
/// <typeparam name="TKey">
/// The type of the key.
/// </typeparam>
/// <typeparam name="TValue">
/// The type of the value.
/// </typeparam>
/// <param name="source">
/// The source sequence of key-value pairs.
/// </param>
/// <param name="comparer">
/// The comparer for keys.
/// </param>
/// <returns>
/// An <see cref="ILookup{TKey,TValue}"/> containing the values mapped to their keys.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>
/// </exception>
/// <remarks>
/// <para>
/// This method executes immediately.
/// </para>
/// </remarks>
public static ILookup<TKey, TValue> ToLookup<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source,
IEqualityComparer<TKey>? comparer)
{
ArgumentNullException.ThrowIfNull(source);
return source.ToLookup(e => e.Key, e => e.Value, comparer);
}
/// <summary>
/// Creates an <see cref="ILookup{TKey,TValue}" /> /> from a sequence of tuples of 2 where the first item is the
/// key and the second the value.
/// </summary>
/// <typeparam name="TKey">
/// The type of the key.
/// </typeparam>
/// <typeparam name="TValue">
/// The type of the value.
/// </typeparam>
/// <param name="source">
/// The source sequence of tuples of 2.
/// </param>
/// <returns>
/// An <see cref="ILookup{TKey,TValue}"/> containing the values mapped to their keys.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>
/// </exception>
/// <remarks>
/// <para>
/// This method executes immediately.
/// </para>
/// </remarks>
public static ILookup<TKey, TValue> ToLookup<TKey, TValue>(
this IEnumerable<(TKey Key, TValue Value)> source)
{
return source.ToLookup(comparer: null);
}
/// <summary>
/// Creates an <see cref="ILookup{TKey,TValue}" /> from a sequence of tuples of 2 where the first item is the
/// key and the second the value. An additional parameter specifies a comparer for keys.
/// </summary>
/// <typeparam name="TKey">
/// The type of the key.
/// </typeparam>
/// <typeparam name="TValue">
/// The type of the value.
/// </typeparam>
/// <param name="source">
/// The source sequence of tuples of 2.
/// </param>
/// <param name="comparer">
/// The comparer for keys.
/// </param>
/// <returns>
/// An <see cref="ILookup{TKey,TValue}"/> containing the values mapped to their keys.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>
/// </exception>
/// <remarks>
/// <para>
/// This method executes immediately.
/// </para>
/// </remarks>
public static ILookup<TKey, TValue> ToLookup<TKey, TValue>(
this IEnumerable<(TKey Key, TValue Value)> source,
IEqualityComparer<TKey>? comparer)
{
ArgumentNullException.ThrowIfNull(source);
return source.ToLookup(e => e.Key, e => e.Value, comparer);
}
}