@@ -184,248 +184,5 @@ private static int GetNumNewLineCharacters(string text, out string[] lines)
184184
185185 return numCharDiff / ( lines . Length - 1 ) ;
186186 }
187-
188- private class TextLines : IList < String >
189- {
190- private LinkedList < String > lines ;
191- private int lastAccessedIndex ;
192- private LinkedListNode < String > lastAccessedNode ;
193-
194- private void ValidateIndex ( int index )
195- {
196- if ( index >= Count || index < 0 )
197- {
198- throw new ArgumentOutOfRangeException ( nameof ( index ) ) ;
199- }
200- }
201-
202- private void SetLastAccessed ( int index , LinkedListNode < String > node )
203- {
204- lastAccessedIndex = index ;
205- lastAccessedNode = node ;
206- }
207-
208- private void InvalidateLastAccessed ( )
209- {
210- lastAccessedIndex = - 1 ;
211- lastAccessedNode = null ;
212- }
213-
214- private bool IsLastAccessedValid ( )
215- {
216- return lastAccessedIndex != - 1 ;
217- }
218-
219- private LinkedListNode < String > GetNodeAt ( int index )
220- {
221- if ( index == 0 )
222- {
223- return lines . First ;
224- }
225-
226- if ( index == Count - 1 )
227- {
228- return lines . Last ;
229- }
230-
231- LinkedListNode < string > node ;
232- int searchDirection ;
233- int count ;
234- GetClosestReference ( index , out node , out count , out searchDirection ) ;
235- while ( node != null )
236- {
237- if ( count == index )
238- {
239- SetLastAccessed ( index , node ) ;
240- return node ;
241- }
242-
243- count += searchDirection ;
244- if ( searchDirection > 0 )
245- {
246- node = node . Next ;
247- }
248- else
249- {
250- node = node . Previous ;
251- }
252- }
253-
254- throw new InvalidOperationException ( ) ;
255- }
256-
257- private void GetClosestReference (
258- int index ,
259- out LinkedListNode < string > refNode ,
260- out int refIndex ,
261- out int searchDirection )
262- {
263- var delta = index - lastAccessedIndex ;
264- var deltaAbs = Math . Abs ( delta ) ;
265-
266- // lastAccessedIndex is closer to index than that to 0
267- if ( IsLastAccessedValid ( ) && deltaAbs < index )
268- {
269- // lastAccessedIndex is closer to index than to (Count - 1)
270- if ( deltaAbs < ( Count - 1 - index ) )
271- {
272- refNode = lastAccessedNode ;
273- refIndex = lastAccessedIndex ;
274- searchDirection = Math . Sign ( delta ) ;
275- }
276- else
277- {
278- refNode = lines . Last ;
279- refIndex = Count - 1 ;
280- searchDirection = - 1 ;
281- }
282- }
283- else
284- {
285- refNode = lines . First ;
286- refIndex = 0 ;
287- searchDirection = 1 ;
288- }
289- }
290-
291- public string this [ int index ]
292- {
293- get
294- {
295- ValidateIndex ( index ) ;
296- return GetNodeAt ( index ) . Value ;
297- }
298- set
299- {
300- ValidateIndex ( index ) ;
301- Insert ( index , value ) ;
302- RemoveAt ( index ) ;
303- }
304- }
305-
306- public int Count { get ; private set ; }
307-
308- public bool IsReadOnly => false ;
309-
310- public TextLines ( )
311- {
312- lines = new LinkedList < String > ( ) ;
313- Count = 0 ;
314- InvalidateLastAccessed ( ) ;
315- }
316-
317- public TextLines ( IEnumerable < String > inputLines ) : this ( )
318- {
319- if ( inputLines == null )
320- {
321- throw new ArgumentNullException ( nameof ( inputLines ) ) ;
322- }
323-
324- if ( inputLines . Any ( line => line == null ) )
325- {
326- // todo localize
327- throw new ArgumentException ( "Line element cannot be null." ) ;
328- }
329-
330- lines = new LinkedList < String > ( inputLines ) ;
331- Count = lines . Count ;
332- }
333-
334- public void Add ( string item )
335- {
336- if ( item == null )
337- {
338- throw new ArgumentNullException ( nameof ( item ) ) ;
339- }
340-
341- Insert ( Count - 1 , item ) ;
342- }
343-
344- public void Clear ( )
345- {
346- lines . Clear ( ) ;
347- }
348-
349- public bool Contains ( string item )
350- {
351- return lines . Contains ( item ) ;
352- }
353-
354- public void CopyTo ( string [ ] array , int arrayIndex )
355- {
356- lines . CopyTo ( array , arrayIndex ) ;
357- }
358-
359- public IEnumerator < string > GetEnumerator ( )
360- {
361- return lines . GetEnumerator ( ) ;
362- }
363-
364- public int IndexOf ( string item )
365- {
366- var llNode = lines . First ;
367- int count = 0 ;
368- while ( llNode != null )
369- {
370- if ( llNode . Value . Equals ( item ) )
371- {
372- return count ;
373- }
374-
375- llNode = llNode . Next ;
376- count ++ ;
377- }
378-
379- return - 1 ;
380- }
381-
382- public void Insert ( int index , string item )
383- {
384- ValidateIndex ( index ) ;
385- SetLastAccessed ( index , lines . AddBefore ( GetNodeAt ( index ) , item ) ) ;
386- Count ++ ;
387- }
388-
389- public bool Remove ( string item )
390- {
391- if ( lines . Remove ( item ) )
392- {
393- Count -- ;
394- return true ;
395- }
396-
397- return false ;
398- }
399-
400- public void RemoveAt ( int index )
401- {
402- var node = GetNodeAt ( index ) ;
403- if ( node . Next != null )
404- {
405- SetLastAccessed ( index , node . Next ) ;
406- }
407- else if ( node . Previous != null )
408- {
409- SetLastAccessed ( index - 1 , node . Previous ) ;
410- }
411- else
412- {
413- InvalidateLastAccessed ( ) ;
414- }
415-
416- lines . Remove ( node ) ;
417- Count -- ;
418- }
419-
420- IEnumerator IEnumerable . GetEnumerator ( )
421- {
422- return lines . GetEnumerator ( ) ;
423- }
424-
425- public override string ToString ( )
426- {
427- return String . Join ( Environment . NewLine , lines ) ;
428- }
429- }
430187 }
431188}
0 commit comments