-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathAppendPrepend.cs
More file actions
247 lines (212 loc) · 9.09 KB
/
Copy pathAppendPrepend.cs
File metadata and controls
247 lines (212 loc) · 9.09 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
// 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> Append<TSource>(this IEnumerable<TSource> source, TSource element)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return source is AppendPrependIterator<TSource> appendable
? appendable.Append(element)
: new AppendPrepend1Iterator<TSource>(source, element, appending: true);
}
public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source, TSource element)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return source is AppendPrependIterator<TSource> appendable
? appendable.Prepend(element)
: new AppendPrepend1Iterator<TSource>(source, element, appending: false);
}
/// <summary>
/// Represents the insertion of one or more items before or after an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
private abstract partial class AppendPrependIterator<TSource> : Iterator<TSource>
{
protected readonly IEnumerable<TSource> _source;
protected IEnumerator<TSource>? _enumerator;
protected AppendPrependIterator(IEnumerable<TSource> source)
{
Debug.Assert(source != null);
_source = source;
}
protected void GetSourceEnumerator()
{
Debug.Assert(_enumerator == null);
_enumerator = _source.GetEnumerator();
}
public abstract AppendPrependIterator<TSource> Append(TSource item);
public abstract AppendPrependIterator<TSource> Prepend(TSource item);
protected bool LoadFromEnumerator()
{
Debug.Assert(_enumerator != null);
if (_enumerator.MoveNext())
{
_current = _enumerator.Current;
return true;
}
Dispose();
return false;
}
public override void Dispose()
{
if (_enumerator != null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
}
/// <summary>
/// Represents the insertion of an item before or after an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
private sealed partial class AppendPrepend1Iterator<TSource> : AppendPrependIterator<TSource>
{
private readonly TSource _item;
private readonly bool _appending;
public AppendPrepend1Iterator(IEnumerable<TSource> source, TSource item, bool appending)
: base(source)
{
_item = item;
_appending = appending;
}
public override Iterator<TSource> Clone() => new AppendPrepend1Iterator<TSource>(_source, _item, _appending);
public override bool MoveNext()
{
switch (_state)
{
case 1:
_state = 2;
if (!_appending)
{
_current = _item;
return true;
}
goto case 2;
case 2:
GetSourceEnumerator();
_state = 3;
goto case 3;
case 3:
if (LoadFromEnumerator())
{
return true;
}
if (_appending)
{
_current = _item;
return true;
}
break;
}
Dispose();
return false;
}
public override AppendPrependIterator<TSource> Append(TSource item)
{
if (_appending)
{
return new AppendPrependN<TSource>(_source, null, new SingleLinkedNode<TSource>(_item).Add(item), prependCount: 0, appendCount: 2);
}
else
{
return new AppendPrependN<TSource>(_source, new SingleLinkedNode<TSource>(_item), new SingleLinkedNode<TSource>(item), prependCount: 1, appendCount: 1);
}
}
public override AppendPrependIterator<TSource> Prepend(TSource item)
{
if (_appending)
{
return new AppendPrependN<TSource>(_source, new SingleLinkedNode<TSource>(item), new SingleLinkedNode<TSource>(_item), prependCount: 1, appendCount: 1);
}
else
{
return new AppendPrependN<TSource>(_source, new SingleLinkedNode<TSource>(_item).Add(item), null, prependCount: 2, appendCount: 0);
}
}
}
/// <summary>
/// Represents the insertion of multiple items before or after an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
private sealed partial class AppendPrependN<TSource> : AppendPrependIterator<TSource>
{
private readonly SingleLinkedNode<TSource>? _prepended;
private readonly SingleLinkedNode<TSource>? _appended;
private readonly int _prependCount;
private readonly int _appendCount;
private SingleLinkedNode<TSource>? _node;
public AppendPrependN(IEnumerable<TSource> source, SingleLinkedNode<TSource>? prepended, SingleLinkedNode<TSource>? appended, int prependCount, int appendCount)
: base(source)
{
Debug.Assert(prepended != null || appended != null);
Debug.Assert(prependCount > 0 || appendCount > 0);
Debug.Assert(prependCount + appendCount >= 2);
Debug.Assert((prepended?.GetCount() ?? 0) == prependCount);
Debug.Assert((appended?.GetCount() ?? 0) == appendCount);
_prepended = prepended;
_appended = appended;
_prependCount = prependCount;
_appendCount = appendCount;
}
public override Iterator<TSource> Clone() => new AppendPrependN<TSource>(_source, _prepended, _appended, _prependCount, _appendCount);
public override bool MoveNext()
{
switch (_state)
{
case 1:
_node = _prepended;
_state = 2;
goto case 2;
case 2:
if (_node != null)
{
_current = _node.Item;
_node = _node.Linked;
return true;
}
GetSourceEnumerator();
_state = 3;
goto case 3;
case 3:
if (LoadFromEnumerator())
{
return true;
}
if (_appended == null)
{
return false;
}
_enumerator = ((IEnumerable<TSource>)_appended.ToArray(_appendCount)).GetEnumerator();
_state = 4;
goto case 4;
case 4:
return LoadFromEnumerator();
}
Dispose();
return false;
}
public override AppendPrependIterator<TSource> Append(TSource item)
{
var appended = _appended != null ? _appended.Add(item) : new SingleLinkedNode<TSource>(item);
return new AppendPrependN<TSource>(_source, _prepended, appended, _prependCount, _appendCount + 1);
}
public override AppendPrependIterator<TSource> Prepend(TSource item)
{
var prepended = _prepended != null ? _prepended.Add(item) : new SingleLinkedNode<TSource>(item);
return new AppendPrependN<TSource>(_source, prepended, _appended, _prependCount + 1, _appendCount);
}
}
}
}