@@ -114,27 +114,31 @@ public ref
114114#else
115115 public
116116#endif
117- struct TakeRange < TEnumerator , TSource >
117+ struct TakeRange < TEnumerator , TSource > ( TEnumerator source , Range range )
118118 : IValueEnumerator < TSource >
119119 where TEnumerator : struct , IValueEnumerator < TSource >
120120#if NET9_0_OR_GREATER
121121 , allows ref struct
122122#endif
123123 {
124- TEnumerator source ;
125- readonly Range range ;
124+ TEnumerator source = source ;
125+ readonly Range range = range ;
126126
127127 int index ;
128128 int remains ;
129- readonly int skipIndex ;
130- readonly int fromEndQueueCount ; // 0 is not use q
131- Queue < TSource > ? q ; // TODO:RefBox<ValueQUeue>>
129+ int skipIndex ;
130+ int fromEndQueueCount ; // 0 is not use q
131+ RefBox < ValueQueue < TSource > > ? q ;
132+ bool isInitialized ;
132133
133- public TakeRange ( TEnumerator source , Range range )
134+ void Init ( )
134135 {
135- // initialize before run.
136- this . source = source ;
137- this . range = range ;
136+ if ( isInitialized )
137+ {
138+ return ;
139+ }
140+ isInitialized = true ;
141+
138142 this . fromEndQueueCount = 0 ;
139143 this . remains = - 1 ; // unknown
140144
@@ -172,15 +176,15 @@ public TakeRange(TEnumerator source, Range range)
172176 }
173177
174178 this . fromEndQueueCount = int . MaxValue ; // unknown queue count
175- this . q = new ( ) ;
179+ this . q = new ( new ( 4 ) ) ;
176180 }
177181 else if ( range . Start . IsFromEnd && ! range . End . IsFromEnd ) // start-fromend
178182 {
179183 // unknown skipIndex and remains
180184 this . skipIndex = 0 ;
181185 this . fromEndQueueCount = range . Start . Value ; //queue size is fixed from end-of-start
182186 if ( this . fromEndQueueCount == 0 ) fromEndQueueCount = 1 ;
183- this . q = new ( ) ;
187+ this . q = new ( new ( 4 ) ) ;
184188 }
185189 else if ( range . Start . IsFromEnd && range . End . IsFromEnd ) // both fromend
186190 {
@@ -195,13 +199,15 @@ public TakeRange(TEnumerator source, Range range)
195199 }
196200 this . fromEndQueueCount = range . Start . Value ;
197201 if ( this . fromEndQueueCount == 0 ) fromEndQueueCount = 1 ;
198- this . q = new ( ) ;
202+ this . q = new ( new ( 4 ) ) ;
199203 }
200204 }
201205 }
202206
203207 public bool TryGetNonEnumeratedCount ( out int count )
204208 {
209+ Init ( ) ;
210+
205211 if ( source . TryGetNonEnumeratedCount ( out _ ) )
206212 {
207213 count = remains ;
@@ -213,6 +219,8 @@ public bool TryGetNonEnumeratedCount(out int count)
213219
214220 public bool TryGetSpan ( out ReadOnlySpan < TSource > span )
215221 {
222+ Init ( ) ;
223+
216224 if ( source . TryGetSpan ( out span ) )
217225 {
218226 span = span . Slice ( skipIndex , remains ) ;
@@ -225,6 +233,8 @@ public bool TryGetSpan(out ReadOnlySpan<TSource> span)
225233
226234 public bool TryCopyTo ( Span < TSource > destination , Index offset )
227235 {
236+ Init ( ) ;
237+
228238 if ( source . TryGetNonEnumeratedCount ( out var totalCount ) )
229239 {
230240 var effectiveRemains = skipIndex < totalCount
@@ -262,13 +272,15 @@ public bool TryCopyTo(Span<TSource> destination, Index offset)
262272
263273 public bool TryGetNext ( out TSource current )
264274 {
275+ Init ( ) ;
276+
265277 if ( remains == 0 )
266278 {
267279 goto END ;
268280 }
269281
270282 DEQUEUE :
271- if ( q != null && q . Count != 0 )
283+ if ( q != null && q . GetValueRef ( ) . Count != 0 )
272284 {
273285 if ( remains == - 1 )
274286 {
@@ -284,18 +296,18 @@ public bool TryGetNext(out TSource current)
284296 }
285297
286298 // q.Count is fromEnd
287- var offset = count - q . Count ;
299+ var offset = count - q . GetValueRef ( ) . Count ;
288300 var skipIndex = Math . Max ( 0 , start - offset ) ;
289301 while ( skipIndex > 0 )
290302 {
291- q . Dequeue ( ) ;
303+ q . GetValueRef ( ) . Dequeue ( ) ;
292304 skipIndex -- ;
293305 }
294306 }
295307
296308 if ( remains -- > 0 )
297309 {
298- current = q . Dequeue ( ) ;
310+ current = q . GetValueRef ( ) . Dequeue ( ) ;
299311 return true ;
300312 }
301313 else
@@ -329,15 +341,15 @@ public bool TryGetNext(out TSource current)
329341 continue ;
330342 }
331343
332- if ( q . Count == fromEndQueueCount )
344+ if ( q . GetValueRef ( ) . Count == fromEndQueueCount )
333345 {
334- q . Dequeue ( ) ;
346+ q . GetValueRef ( ) . Dequeue ( ) ;
335347 }
336- q . Enqueue ( current ) ;
348+ q . GetValueRef ( ) . Enqueue ( current ) ;
337349 }
338350 }
339351
340- if ( q != null && q . Count != 0 )
352+ if ( q != null && q . GetValueRef ( ) . Count != 0 )
341353 {
342354 goto DEQUEUE ;
343355 }
@@ -350,6 +362,7 @@ public bool TryGetNext(out TSource current)
350362
351363 public void Dispose ( )
352364 {
365+ q ? . Dispose ( ) ;
353366 source . Dispose ( ) ;
354367 }
355368 }
0 commit comments