Last active
August 29, 2015 14:06
-
-
Save pierre3/c7a352c30cce47150980 to your computer and use it in GitHub Desktop.
Iterator class for debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace IteratorTest | |
{ | |
public static class IteratorHelper | |
{ | |
//インスタンス生成を楽するためのショートカット | |
public static Iterator<T> CreateIterator<T>(params T[] source) | |
{ | |
return new Iterator<T>(source); | |
} | |
} | |
public class Iterator<T>:IEnumerable<T>,IEnumerator<T> | |
{ | |
IEnumerable<T> source; | |
IEnumerator<T> enumerator; | |
bool canEnumerate; | |
public Iterator(IEnumerable<T> source) | |
{ | |
Console.WriteLine("Constructor()"); | |
this.source = source; | |
this.enumerator = this.source.GetEnumerator(); | |
this.canEnumerate = true; | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
Console.WriteLine("GetEnumerator()"); | |
if (canEnumerate) | |
{ return this; } | |
return new Iterator<T>(source); | |
} | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | |
{ | |
Console.Write("IEnumerable."); | |
return GetEnumerator(); | |
} | |
public T Current | |
{ | |
get { return enumerator.Current; } | |
} | |
public void Dispose() | |
{ | |
Console.WriteLine("Dispose()"); | |
enumerator.Dispose(); | |
canEnumerate = false; | |
} | |
object System.Collections.IEnumerator.Current | |
{ | |
get {return Current; } | |
} | |
public bool MoveNext() | |
{ | |
canEnumerate = enumerator.MoveNext(); | |
Console.WriteLine("MoveNext() == " + canEnumerate); | |
return canEnumerate; | |
} | |
public void Reset() | |
{ | |
throw new NotSupportedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment