Repository navigation
Expand file tree
/
Copy pathLookup.cs
More file actions
326 lines (270 loc) · 12 KB
/
Copy pathLookup.cs
File metadata and controls
326 lines (270 loc) · 12 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// 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, comparer: null);
public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (keySelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}
if (IsEmptyArray(source))
{
return EmptyLookup<TKey, TSource>.Instance;
}
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, comparer: 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 is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (keySelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.keySelector);
}
if (elementSelector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.elementSelector);
}
if (IsEmptyArray(source))
{
return EmptyLookup<TKey, TElement>.Instance;
}
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;
internal 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 is not null);
Debug.Assert(keySelector is not null);
Debug.Assert(elementSelector is not null);
var lookup = new CollectionLookup<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 is not null);
Debug.Assert(keySelector is not null);
var lookup = new CollectionLookup<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)
{
var lookup = new CollectionLookup<TKey, TElement>(comparer);
foreach (TElement item in source)
{
TKey key = keySelector(item);
if (key is not null)
{
lookup.GetGrouping(key, create: true)!.Add(item);
}
}
return lookup;
}
private protected 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] => GetGrouping(key, create: false) ?? Enumerable.Empty<TElement>();
public bool Contains(TKey key) => GetGrouping(key, create: false) is not null;
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
{
Grouping<TKey, TElement>? g = _lastGrouping;
if (g is not null)
{
do
{
g = g._next;
Debug.Assert(g is not 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 is not null)
{
Span<TResult> span = Enumerable.SetCountAndGetSpan(list, _count);
int index = 0;
do
{
g = g._next;
Debug.Assert(g is not null);
g.Trim();
span[index] = resultSelector(g._key, g._elements);
++index;
}
while (g != _lastGrouping);
Debug.Assert(index == _count, "All list elements were not initialized.");
}
return list;
}
public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
Grouping<TKey, TElement>? g = _lastGrouping;
if (g is not null)
{
do
{
g = g._next;
Debug.Assert(g is not 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 is 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[(uint)hashCode % _groupings.Length]; g is not 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 is 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;
}
}
internal sealed class CollectionLookup<TKey, TElement> : Lookup<TKey, TElement>, ICollection<IGrouping<TKey, TElement>>, IReadOnlyCollection<IGrouping<TKey, TElement>>
{
internal CollectionLookup(IEqualityComparer<TKey>? comparer) : base(comparer) { }
void ICollection<IGrouping<TKey, TElement>>.CopyTo(IGrouping<TKey, TElement>[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length);
ArgumentOutOfRangeException.ThrowIfLessThan(array.Length - arrayIndex, Count, nameof(arrayIndex));
Grouping<TKey, TElement>? g = _lastGrouping;
if (g is not null)
{
do
{
g = g._next;
Debug.Assert(g is not null);
array[arrayIndex] = g;
++arrayIndex;
}
while (g != _lastGrouping);
}
}
bool ICollection<IGrouping<TKey, TElement>>.Contains(IGrouping<TKey, TElement> item)
{
ArgumentNullException.ThrowIfNull(item);
return GetGrouping(item.Key, create: false) is { } grouping && grouping == item;
}
bool ICollection<IGrouping<TKey, TElement>>.IsReadOnly => true;
void ICollection<IGrouping<TKey, TElement>>.Add(IGrouping<TKey, TElement> item) => throw new NotSupportedException();
void ICollection<IGrouping<TKey, TElement>>.Clear() => throw new NotSupportedException();
bool ICollection<IGrouping<TKey, TElement>>.Remove(IGrouping<TKey, TElement> item) => throw new NotSupportedException();
}
[DebuggerDisplay("Count = 0")]
[DebuggerTypeProxy(typeof(SystemLinq_LookupDebugView<,>))]
internal sealed class EmptyLookup<TKey, TElement> : ILookup<TKey, TElement>, ICollection<IGrouping<TKey, TElement>>, IReadOnlyCollection<IGrouping<TKey, TElement>>
{
public static readonly EmptyLookup<TKey, TElement> Instance = new();
public IEnumerable<TElement> this[TKey key] => [];
public int Count => 0;
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator() => Enumerable.Empty<IGrouping<TKey, TElement>>().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Contains(TKey key) => false;
public bool Contains(IGrouping<TKey, TElement> item) => false;
public void CopyTo(IGrouping<TKey, TElement>[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length);
}
public bool IsReadOnly => true;
public void Add(IGrouping<TKey, TElement> item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Remove(IGrouping<TKey, TElement> item) => throw new NotSupportedException();
}
}