-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathLookup.cs
More file actions
253 lines (211 loc) · 8.46 KB
/
Lookup.cs
File metadata and controls
253 lines (211 loc) · 8.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Linq
{
public static partial class Enumerable
{
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
ToLookup(source, keySelector, null);
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (keySelector == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}
return Lookup<TKey, TSource>.Create(source, keySelector, comparer);
}
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) =>
ToLookup(source, keySelector, elementSelector, null);
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (keySelector == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}
if (elementSelector == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector);
}
return Lookup<TKey, TElement>.Create(source, keySelector, elementSelector, comparer);
}
}
public interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>
{
int Count { get; }
IEnumerable<TElement> this[TKey key] { get; }
bool Contains(TKey key);
}
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(SystemLinq_LookupDebugView<,>))]
public partial class Lookup<TKey, TElement> : ILookup<TKey, TElement>
{
private readonly IEqualityComparer<TKey> _comparer;
private Grouping<TKey, TElement>[] _groupings;
private Grouping<TKey, TElement>? _lastGrouping;
private int _count;
internal static Lookup<TKey, TElement> Create<TSource>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer)
{
Debug.Assert(source != null);
Debug.Assert(keySelector != null);
Debug.Assert(elementSelector != null);
Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
foreach (TSource item in source)
{
lookup.GetGrouping(keySelector(item), create: true)!.Add(elementSelector(item));
}
return lookup;
}
internal static Lookup<TKey, TElement> Create(IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
Debug.Assert(source != null);
Debug.Assert(keySelector != null);
Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
foreach (TElement item in source)
{
lookup.GetGrouping(keySelector(item), create: true)!.Add(item);
}
return lookup;
}
internal static Lookup<TKey, TElement> CreateForJoin(IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
foreach (TElement item in source)
{
TKey key = keySelector(item);
if (key != null)
{
lookup.GetGrouping(key, create: true)!.Add(item);
}
}
return lookup;
}
private Lookup(IEqualityComparer<TKey>? comparer)
{
_comparer = comparer ?? EqualityComparer<TKey>.Default;
_groupings = new Grouping<TKey, TElement>[7];
}
public int Count => _count;
public IEnumerable<TElement> this[TKey key]
{
get
{
Grouping<TKey, TElement>? grouping = GetGrouping(key, create: false);
return grouping ?? Enumerable.Empty<TElement>();
}
}
public bool Contains(TKey key) => GetGrouping(key, create: false) != null;
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
{
Grouping<TKey, TElement>? g = _lastGrouping;
if (g != null)
{
do
{
g = g._next;
Debug.Assert(g != null);
yield return g;
}
while (g != _lastGrouping);
}
}
internal List<TResult> ToList<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
List<TResult> list = new List<TResult>(_count);
Grouping<TKey, TElement>? g = _lastGrouping;
if (g != null)
{
do
{
g = g._next;
Debug.Assert(g != null);
g.Trim();
list.Add(resultSelector(g._key, g._elements));
}
while (g != _lastGrouping);
}
return list;
}
public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
Grouping<TKey, TElement>? g = _lastGrouping;
if (g != null)
{
do
{
g = g._next;
Debug.Assert(g != null);
g.Trim();
yield return resultSelector(g._key, g._elements);
}
while (g != _lastGrouping);
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private int InternalGetHashCode(TKey key)
{
// Handle comparer implementations that throw when passed null
return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
}
internal Grouping<TKey, TElement>? GetGrouping(TKey key, bool create)
{
int hashCode = InternalGetHashCode(key);
for (Grouping<TKey, TElement>? g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
{
if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
{
return g;
}
}
if (create)
{
if (_count == _groupings.Length)
{
Resize();
}
int index = hashCode % _groupings.Length;
Grouping<TKey, TElement> g = new Grouping<TKey, TElement>(key, hashCode);
g._hashNext = _groupings[index];
_groupings[index] = g;
if (_lastGrouping == null)
{
g._next = g;
}
else
{
g._next = _lastGrouping._next;
_lastGrouping._next = g;
}
_lastGrouping = g;
_count++;
return g;
}
return null;
}
private void Resize()
{
int newSize = checked((_count * 2) + 1);
Grouping<TKey, TElement>[] newGroupings = new Grouping<TKey, TElement>[newSize];
Grouping<TKey, TElement> g = _lastGrouping!;
do
{
g = g._next!;
int index = g._hashCode % newSize;
g._hashNext = newGroupings[index];
newGroupings[index] = g;
}
while (g != _lastGrouping);
_groupings = newGroupings;
}
}
}