-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rewriter.SelfAndDescendants.cs
120 lines (110 loc) · 3.96 KB
/
Rewriter.SelfAndDescendants.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sawmill
{
public static partial class Rewriter
{
/// <summary>
/// Yields all of the nodes in the tree represented by <paramref name="value"/>, starting at the top.
/// <seealso cref="SelfAndDescendantsLazy"/>
/// <seealso cref="DescendantsAndSelf"/>
/// <seealso cref="DescendantsAndSelfLazy"/>
/// </summary>
/// <example>
/// <code>
/// Expr expr = new Add(
/// new Add(
/// new Lit(1),
/// new Lit(2)
/// ),
/// new Lit(3)
/// );
/// Expr[] expected = new[]
/// {
/// expr,
/// new Add(new Lit(1), new Lit(2)),
/// new Lit(1),
/// new Lit(2),
/// new Lit(3),
/// };
/// Assert.Equal(expected, rewriter.SelfAndDescendants(expr));
/// </code>
/// </example>
/// <typeparam name="T">The rewritable tree type</typeparam>
/// <param name="rewriter">The rewriter</param>
/// <param name="value">The value to traverse</param>
/// <returns>An enumerable containing all of the nodes in the tree represented by <paramref name="value"/>, starting at the top.</returns>
public static IEnumerable<T> SelfAndDescendants<T>(this IRewriter<T> rewriter, T value)
{
if (rewriter == null)
{
throw new ArgumentNullException(nameof(rewriter));
}
var results = new List<T>();
void Go(T x)
{
results.Add(x);
foreach (var child in rewriter.GetChildren(x))
{
Go(child);
}
}
Go(value);
return results;
}
/// <summary>
/// Lazily yields all of the nodes in the tree represented by <paramref name="value"/>, starting at the top.
/// <para>
/// <see cref="SelfAndDescendants"/> will usually be faster than this method for small trees,
/// but the lazy version can be more efficient when you only need to query part of the tree.
/// </para>
/// <seealso cref="SelfAndDescendants"/>
/// <seealso cref="DescendantsAndSelf"/>
/// <seealso cref="DescendantsAndSelfLazy"/>
/// </summary>
/// <example>
/// <code>
/// Expr expr = new Add(
/// new Add(
/// new Lit(1),
/// new Lit(2)
/// ),
/// new Lit(3)
/// );
/// Expr[] expected = new[]
/// {
/// expr,
/// new Add(new Lit(1), new Lit(2)),
/// new Lit(1),
/// new Lit(2),
/// new Lit(3),
/// };
/// Assert.Equal(expected, rewriter.SelfAndDescendantsLazy(expr));
/// </code>
/// </example>
/// <typeparam name="T">The rewritable tree type</typeparam>
/// <param name="rewriter">The rewriter</param>
/// <param name="value">The value to traverse</param>
/// <returns>An enumerable containing all of the nodes in the tree represented by <paramref name="value"/>, starting at the top.</returns>
public static IEnumerable<T> SelfAndDescendantsLazy<T>(this IRewriter<T> rewriter, T value)
{
if (rewriter == null)
{
throw new ArgumentNullException(nameof(rewriter));
}
IEnumerable<T> Iterator(T x)
{
yield return x;
foreach (var child in rewriter.GetChildren(x))
{
foreach (var descendant in Iterator(child))
{
yield return descendant;
}
}
}
return Iterator(value);
}
}
}