-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Flatten.cs
122 lines (111 loc) · 3.98 KB
/
Flatten.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
using System.Collections;
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Flattens a sequence containing arbitrarily-nested sequences.
/// </summary>
/// <param name="source">
/// The sequence that will be flattened.
/// </param>
/// <returns>
/// A sequence that contains the elements of <paramref name="source"/> and all nested sequences (except
/// strings).
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This method uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<object?> Flatten(this IEnumerable source) =>
Flatten(source, obj => obj is not string);
/// <summary>
/// Flattens a sequence containing arbitrarily-nested sequences. An additional parameter specifies a predicate
/// function used to determine whether a nested <see cref="IEnumerable"/> should be flattened or not.
/// </summary>
/// <param name="source">
/// The sequence that will be flattened.</param>
/// <param name="predicate">
/// A function that receives each element that implements <see cref="IEnumerable"/> and indicates if its
/// elements should be recursively flattened into the resulting sequence.
/// </param>
/// <returns>
/// A sequence that contains the elements of <paramref name="source"/> and all nested sequences for which the
/// predicate function returned <see langword="true"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This method uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<object?> Flatten(this IEnumerable source, Func<IEnumerable, bool> predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
return Flatten(source, obj => obj is IEnumerable inner && predicate(inner) ? inner : null);
}
/// <summary>
/// Flattens a sequence containing arbitrarily-nested sequences. An additional parameter specifies a function
/// that projects an inner sequence via a property of an object.
/// </summary>
/// <param name="source">
/// The sequence that will be flattened.
/// </param>
/// <param name="selector">
/// A function that receives each element of the sequence as an object and projects an inner sequence to be
/// flattened. If the function returns <see langword="null"/> then the object argument is considered a leaf of
/// the flattening process.
/// </param>
/// <returns>
/// A sequence that contains the elements of <paramref name="source"/> and all nested sequences projected via
/// the <paramref name="selector"/> function.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This method uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<object?> Flatten(this IEnumerable source, Func<object?, IEnumerable?> selector)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(selector);
return Core(source, selector);
static IEnumerable<object?> Core(IEnumerable source, Func<object?, IEnumerable?> selector)
{
var e = source.GetEnumerator();
var stack = new Stack<IEnumerator>();
stack.Push(e);
try
{
while (stack.Count != 0)
{
e = stack.Pop();
reloop:
while (e.MoveNext())
{
if (selector(e.Current) is { } inner)
{
stack.Push(e);
e = inner.GetEnumerator();
goto reloop;
}
else
{
yield return e.Current;
}
}
(e as IDisposable)?.Dispose();
e = null;
}
}
finally
{
(e as IDisposable)?.Dispose();
foreach (var se in stack)
(se as IDisposable)?.Dispose();
}
}
}
}