/*
Quote from
Accelerated C# 2005
# By Trey Nash
# ISBN: 1-59059-717-6
# 432 pp.
# Published: Aug 2006
*/
using System;
using System.Collections;
using System.Collections.Generic;
public class MainClass
{
static void Main() {
LinkedList<int> intList = new LinkedList<int>();
for( int i = 1; i < 6; ++i ) {
intList.AddLast( i );
}
CircularIterator<int> iter = new CircularIterator<int>(intList, intList.First);
int counter = 0;
foreach( int n in iter ) {
Console.WriteLine( n );
if( counter++ == 100 ) {
iter.Stop();
}
}
}
}
public class CircularIterator<T> : IEnumerable<T>
{
public CircularIterator( LinkedList<T> list, LinkedListNode<T> start ) {
enumerator = CreateEnumerator( list, start, false ).GetEnumerator();
enumType = enumerator.GetType();
}
public void Stop() {
enumType.GetField("stop").SetValue( enumerator, true );
}
private IEnumerator<T> enumerator;
private Type enumType;
private IEnumerable<T> CreateEnumerator( LinkedList<T> list, LinkedListNode<T> start, bool stop ) {
LinkedListNode<T> current = null;
do {
if( current == null ) {
current = start;
} else {
current = current.Next;
if( current == null ) {
current = start;
}
}
yield return current.Value;
} while( !stop );
}
public IEnumerator<T> GetEnumerator() {
return enumerator;
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
|