-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Concat.cs
267 lines (229 loc) · 10.4 KB
/
Concat.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
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
// 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.Generic;
using System.Diagnostics;
namespace System.Linq
{
public static partial class Enumerable
{
public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
{
if (first == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.first);
}
if (second == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.second);
}
if (IsEmptyArray(first))
{
return second;
}
if (IsEmptyArray(second))
{
return first;
}
return first is ConcatIterator<TSource> firstConcat
? firstConcat.Concat(second)
: new Concat2Iterator<TSource>(first, second);
}
/// <summary>
/// Represents the concatenation of two <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerables.</typeparam>
private sealed partial class Concat2Iterator<TSource> : ConcatIterator<TSource>
{
/// <summary>
/// The first source to concatenate.
/// </summary>
internal readonly IEnumerable<TSource> _first;
/// <summary>
/// The second source to concatenate.
/// </summary>
internal readonly IEnumerable<TSource> _second;
/// <summary>
/// Initializes a new instance of the <see cref="Concat2Iterator{TSource}"/> class.
/// </summary>
/// <param name="first">The first source to concatenate.</param>
/// <param name="second">The second source to concatenate.</param>
internal Concat2Iterator(IEnumerable<TSource> first, IEnumerable<TSource> second)
{
Debug.Assert(first != null);
Debug.Assert(second != null);
_first = first;
_second = second;
}
public override Iterator<TSource> Clone() => new Concat2Iterator<TSource>(_first, _second);
internal override ConcatIterator<TSource> Concat(IEnumerable<TSource> next)
{
bool hasOnlyCollections = next is ICollection<TSource> &&
_first is ICollection<TSource> &&
_second is ICollection<TSource>;
return new ConcatNIterator<TSource>(this, next, 2, hasOnlyCollections);
}
internal override IEnumerable<TSource>? GetEnumerable(int index)
{
Debug.Assert(index >= 0 && index <= 2);
return index switch
{
0 => _first,
1 => _second,
_ => null,
};
}
}
/// <summary>
/// Represents the concatenation of three or more <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerables.</typeparam>
/// <remarks>
/// To handle chains of >= 3 sources, we chain the <see cref="Concat"/> iterators together and allow
/// <see cref="GetEnumerable"/> to fetch enumerables from the previous sources. This means that rather
/// than each <see cref="System.Collections.IEnumerator.MoveNext"/> and <see cref="IEnumerator{T}.Current"/> calls having to traverse all of the previous
/// sources, we only have to traverse all of the previous sources once per chained enumerable. An alternative
/// would be to use an array to store all of the enumerables, but this has a much better memory profile and
/// without much additional run-time cost.
/// </remarks>
private sealed partial class ConcatNIterator<TSource> : ConcatIterator<TSource>
{
/// <summary>
/// The linked list of previous sources.
/// </summary>
private readonly ConcatIterator<TSource> _tail;
/// <summary>
/// The source associated with this iterator.
/// </summary>
private readonly IEnumerable<TSource> _head;
/// <summary>
/// The logical index associated with this iterator.
/// </summary>
private readonly int _headIndex;
/// <summary>
/// <c>true</c> if all sources this iterator concatenates implement <see cref="ICollection{TSource}"/>;
/// otherwise, <c>false</c>.
/// </summary>
/// <remarks>
/// This flag allows us to determine in O(1) time whether we can preallocate for ToArray/ToList,
/// and whether we can get the count of the iterator cheaply.
/// </remarks>
private readonly bool _hasOnlyCollections;
/// <summary>
/// Initializes a new instance of the <see cref="ConcatNIterator{TSource}"/> class.
/// </summary>
/// <param name="tail">The linked list of previous sources.</param>
/// <param name="head">The source associated with this iterator.</param>
/// <param name="headIndex">The logical index associated with this iterator.</param>
/// <param name="hasOnlyCollections">
/// <c>true</c> if all sources this iterator concatenates implement <see cref="ICollection{TSource}"/>;
/// otherwise, <c>false</c>.
/// </param>
internal ConcatNIterator(ConcatIterator<TSource> tail, IEnumerable<TSource> head, int headIndex, bool hasOnlyCollections)
{
Debug.Assert(tail != null);
Debug.Assert(head != null);
Debug.Assert(headIndex >= 2);
_tail = tail;
_head = head;
_headIndex = headIndex;
_hasOnlyCollections = hasOnlyCollections;
}
private ConcatNIterator<TSource>? PreviousN => _tail as ConcatNIterator<TSource>;
public override Iterator<TSource> Clone() => new ConcatNIterator<TSource>(_tail, _head, _headIndex, _hasOnlyCollections);
internal override ConcatIterator<TSource> Concat(IEnumerable<TSource> next)
{
if (_headIndex == int.MaxValue - 2)
{
// In the unlikely case of this many concatenations, if we produced a ConcatNIterator
// with int.MaxValue then state would overflow before it matched its index.
// So we use the naive approach of just having a left and right sequence.
return new Concat2Iterator<TSource>(this, next);
}
bool hasOnlyCollections = _hasOnlyCollections && next is ICollection<TSource>;
return new ConcatNIterator<TSource>(this, next, _headIndex + 1, hasOnlyCollections);
}
internal override IEnumerable<TSource>? GetEnumerable(int index)
{
Debug.Assert(index >= 0);
if (index > _headIndex)
{
return null;
}
ConcatNIterator<TSource>? node, previousN = this;
do
{
node = previousN;
if (index == node._headIndex)
{
return node._head;
}
}
while ((previousN = node.PreviousN) != null);
Debug.Assert(index == 0 || index == 1);
Debug.Assert(node._tail is Concat2Iterator<TSource>);
return node._tail.GetEnumerable(index);
}
}
/// <summary>
/// Represents the concatenation of two or more <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerables.</typeparam>
private abstract partial class ConcatIterator<TSource> : Iterator<TSource>
{
/// <summary>
/// The enumerator of the current source, if <see cref="MoveNext"/> has been called.
/// </summary>
private IEnumerator<TSource>? _enumerator;
public override void Dispose()
{
if (_enumerator != null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
/// <summary>
/// Gets the enumerable at a logical index in this iterator.
/// If the index is equal to the number of enumerables this iterator holds, <c>null</c> is returned.
/// </summary>
/// <param name="index">The logical index.</param>
internal abstract IEnumerable<TSource>? GetEnumerable(int index);
/// <summary>
/// Creates a new iterator that concatenates this iterator with an enumerable.
/// </summary>
/// <param name="next">The next enumerable.</param>
internal abstract ConcatIterator<TSource> Concat(IEnumerable<TSource> next);
public override bool MoveNext()
{
if (_state == 1)
{
_enumerator = GetEnumerable(0)!.GetEnumerator();
_state = 2;
}
if (_state > 1)
{
while (true)
{
Debug.Assert(_enumerator != null);
if (_enumerator.MoveNext())
{
_current = _enumerator.Current;
return true;
}
IEnumerable<TSource>? next = GetEnumerable(_state++ - 1);
if (next != null)
{
_enumerator.Dispose();
_enumerator = next.GetEnumerator();
continue;
}
Dispose();
break;
}
}
return false;
}
}
}
}