99using System . Buffers ;
1010using System . Collections ;
1111using System . Collections . Generic ;
12- using System . Threading ;
1312
1413namespace AngouriMath
1514{
1615 partial record Entity
1716 {
17+ /// <summary>Whether any node, this one included, satisfies <paramref name="predicate"/>,
18+ /// asked in the order of <see cref="Nodes"/> and stopping at the first; what
19+ /// <c>Nodes.Any(predicate)</c> answers, without allocating.</summary>
20+ internal bool AnyNode ( Func < Entity , bool > predicate )
21+ {
22+ var walk = new NodeStack ( this ) ;
23+ try
24+ {
25+ while ( walk . MoveNext ( ) )
26+ if ( predicate ( walk . Current ) )
27+ return true ;
28+ return false ;
29+ }
30+ finally { walk . Release ( ) ; }
31+ }
32+
33+ /// <summary>What <c>Nodes.All(predicate)</c> answers, without allocating.</summary>
34+ internal bool AllNodes ( Func < Entity , bool > predicate )
35+ {
36+ var walk = new NodeStack ( this ) ;
37+ try
38+ {
39+ while ( walk . MoveNext ( ) )
40+ if ( ! predicate ( walk . Current ) )
41+ return false ;
42+ return true ;
43+ }
44+ finally { walk . Release ( ) ; }
45+ }
46+
47+ /// <summary>What <c>Nodes.Contains(node)</c> answers, without allocating.</summary>
48+ private bool WalkFinds ( Entity node )
49+ {
50+ var walk = new NodeStack ( this ) ;
51+ try
52+ {
53+ while ( walk . MoveNext ( ) )
54+ if ( walk . Current == node )
55+ return true ;
56+ return false ;
57+ }
58+ finally { walk . Release ( ) ; }
59+ }
60+
61+ /// <summary>How many nodes, this one included, are equal to <paramref name="node"/>.</summary>
62+ internal int CountNode ( Entity node )
63+ {
64+ var walk = new NodeStack ( this ) ;
65+ try
66+ {
67+ var count = 0 ;
68+ while ( walk . MoveNext ( ) )
69+ if ( walk . Current == node )
70+ count ++ ;
71+ return count ;
72+ }
73+ finally { walk . Release ( ) ; }
74+ }
75+
1876 /// <summary>
1977 /// The pre-order walk behind <see cref="Nodes"/>: the node, then each child's walk in
2078 /// order, kept on an explicit stack rented from <see cref="ArrayPool{T}.Shared"/>.
2179 /// </summary>
2280 /// <remarks>
2381 /// <para>
24- /// It costs one object per enumeration and nothing per node , and keeps nothing on the
25- /// tree. A chain of LINQ iterators costs an iterator per node per enumeration, and caching
26- /// it on every node keeps that chain alive as long as the node; caching the walk's result
27- /// instead is quadratic in memory, since every node would hold its whole subtree (31 MB on
28- /// a sum of 2000 terms).
82+ /// It allocates nothing, per node or per walk , and keeps nothing on the tree. A chain of
83+ /// LINQ iterators costs an iterator per node per enumeration, and caching it on every
84+ /// node keeps that chain alive as long as the node; caching the walk's result instead is
85+ /// quadratic in memory, since every node would hold its whole subtree (31 MB on a sum of
86+ /// 2000 terms).
2987 /// <a href="https://github.com/asc-community/AngouriMath/issues/534">#534</a>
3088 /// </para>
3189 /// <para>
32- /// Every enumeration has its own stack, so a predicate that walks another tree while this
33- /// one is being walked, on this thread or another, shares nothing with it. A walk that is
34- /// abandoned without being disposed only fails to give its array back to the pool.
90+ /// Every walk has its own stack, so a predicate that walks another tree while this one
91+ /// is being walked, on this thread or another, shares nothing with it. A walk that is
92+ /// abandoned without <see cref="Release"/> only fails to give its array back to the
93+ /// pool. It is a mutable struct: copying one mid-walk would share the array, so it is
94+ /// only ever held in a local or in <see cref="NodeWalk"/>.
3595 /// </para>
3696 /// </remarks>
37- private sealed class NodeWalk : IEnumerable < Entity > , IEnumerator < Entity >
97+ private struct NodeStack
3898 {
39- private const int NotStarted = 0 , Walking = 1 , Finished = 2 ;
40-
41- private readonly Entity root ;
42- private readonly int ownerThread ;
43- private bool handedOut ;
44- private int state ;
99+ private Entity ? next ;
45100 private Entity ? current ;
46101 private Entity [ ] ? stack ;
47102 private int count ;
48103
49- internal NodeWalk ( Entity root )
104+ internal NodeStack ( Entity root )
50105 {
51- this . root = root ;
52- ownerThread = Environment . CurrentManagedThreadId ;
106+ next = root ;
107+ current = null ;
108+ stack = null ;
109+ count = 0 ;
53110 }
54111
55- // The first enumeration on the thread that asked for Nodes is this object itself, as
56- // LINQ's own iterators do, so the common Nodes.Any(...) costs one object, not two.
57- public IEnumerator < Entity > GetEnumerator ( )
112+ internal readonly Entity Current => current ! ;
113+
114+ internal bool MoveNext ( )
58115 {
59- if ( ! handedOut && ownerThread == Environment . CurrentManagedThreadId )
116+ if ( next is not null )
60117 {
61- handedOut = true ;
62- return this ;
118+ current = next ;
119+ next = null ;
120+ return true ;
63121 }
64- return new NodeWalk ( root ) { handedOut = true } ;
65- }
66-
67- IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
68-
69- public Entity Current => current ! ;
70-
71- object IEnumerator . Current => Current ;
72-
73- public bool MoveNext ( )
74- {
75- switch ( state )
122+ if ( current is null )
123+ return false ;
124+ // The children of the node just yielded are pushed only now, so a walk stopped
125+ // at a node never asks for its children.
126+ var children = current . DirectChildren ;
127+ if ( children . Count > 0 )
76128 {
77- case NotStarted :
78- current = root ;
79- state = Walking ;
80- return true ;
81- case Walking :
82- // The children of the node just yielded are pushed only now, so a walk
83- // stopped at a node never asks for its children.
84- var children = current ! . DirectChildren ;
85- if ( children . Count > 0 )
86- {
87- Reserve ( children . Count ) ;
88- for ( var i = children . Count - 1 ; i >= 0 ; i -- )
89- stack ! [ count ++ ] = children [ i ] ;
90- }
91- if ( count == 0 )
92- {
93- Dispose ( ) ;
94- return false ;
95- }
96- current = stack ! [ -- count ] ;
97- stack [ count ] = null ! ;
98- return true ;
99- default :
100- return false ;
129+ Reserve ( children . Count ) ;
130+ for ( var i = children . Count - 1 ; i >= 0 ; i -- )
131+ stack ! [ count ++ ] = children [ i ] ;
101132 }
133+ if ( count == 0 )
134+ {
135+ Release ( ) ;
136+ return false ;
137+ }
138+ current = stack ! [ -- count ] ;
139+ stack [ count ] = null ! ;
140+ return true ;
102141 }
103142
104143 private void Reserve ( int more )
@@ -115,7 +154,7 @@ private void Reserve(int more)
115154 }
116155 }
117156
118- public void Dispose ( )
157+ internal void Release ( )
119158 {
120159 if ( stack is not null )
121160 {
@@ -126,9 +165,50 @@ public void Dispose()
126165 stack = null ;
127166 }
128167 count = 0 ;
168+ next = null ;
129169 current = null ;
130- state = Finished ;
131170 }
171+ }
172+
173+ /// <summary>
174+ /// <see cref="NodeStack"/> as the <see cref="IEnumerable{T}"/> that <see cref="Nodes"/>
175+ /// publishes, at one object per enumeration.
176+ /// </summary>
177+ private sealed class NodeWalk : IEnumerable < Entity > , IEnumerator < Entity >
178+ {
179+ private readonly Entity root ;
180+ private readonly int ownerThread ;
181+ private bool handedOut ;
182+ private NodeStack walk ;
183+
184+ internal NodeWalk ( Entity root )
185+ {
186+ this . root = root ;
187+ ownerThread = Environment . CurrentManagedThreadId ;
188+ walk = new NodeStack ( root ) ;
189+ }
190+
191+ // The first enumeration on the thread that asked for Nodes is this object itself, as
192+ // LINQ's own iterators do, so the common Nodes.Any(...) costs one object, not two.
193+ public IEnumerator < Entity > GetEnumerator ( )
194+ {
195+ if ( ! handedOut && ownerThread == Environment . CurrentManagedThreadId )
196+ {
197+ handedOut = true ;
198+ return this ;
199+ }
200+ return new NodeWalk ( root ) { handedOut = true } ;
201+ }
202+
203+ IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
204+
205+ public Entity Current => walk . Current ;
206+
207+ object IEnumerator . Current => Current ;
208+
209+ public bool MoveNext ( ) => walk . MoveNext ( ) ;
210+
211+ public void Dispose ( ) => walk . Release ( ) ;
132212
133213 public void Reset ( ) => throw new NotSupportedException ( ) ;
134214 }
0 commit comments