Skip to content

Instantly share code, notes, and snippets.

@AdamSpeight2008
Created January 13, 2016 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamSpeight2008/1200ca239abcdb1d81c5 to your computer and use it in GitHub Desktop.
Save AdamSpeight2008/1200ca239abcdb1d81c5 to your computer and use it in GitHub Desktop.
NestedEnumerable(Of T)
Public Class RootEnumerator(Of T)
Implements IEnumerator(Of T)
Dim stk As New Stack(Of NestedEnumerator(Of T))
Public Sub New(e As NestedEnumerator(Of T))
stk.Push(e)
End Sub
Public ReadOnly Property Current As T Implements IEnumerator(Of T).Current
Get
If ((stk.Count = 0) OrElse (stk.Peek.pc = 0)) Then Throw New InvalidOperationException()
Return stk.Peek.CurrentVAlue
End Get
End Property
Private ReadOnly Property _Current As Object Implements IEnumerator.Current
Get
Return Current()
End Get
End Property
Public Sub Reset() Implements IEnumerator.Reset
Throw New NotImplementedException()
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
While True
If stk.Count = 0 Then Return False
Select Case stk.Peek.NestedMoveNext()
Case NestedEnumState.Value
Return True
Case NestedEnumState.Enumerator
stk.Push(stk.Peek.currentEnumerator)
Case NestedEnumState.Done
stk.Pop()
Case NestedEnumState.TailEnumerator
Dim e = stk.Peek.currentEnumerator
stk.Pop()
stk.Push(e)
End Select
End While
End Function
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
' TODO: uncomment the following line if Finalize() is overridden above.
' GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
Public Class EnumeratorAdapter(Of T)
Inherits NestedEnumerator(Of T)
Dim e As IEnumerator(Of T)
Public Sub New(e As IEnumerable(Of T))
Me.e = e.GetEnumerator
End Sub
Public Overrides Function NestedMoveNext() As NestedEnumState
If e.MoveNext = False Then Return NestedEnumState.Done
CurrentVAlue = e.Current
Return NestedEnumState.Value
End Function
End Class
Public Enum NestedEnumState
Done
Value
Enumerator
TailEnumerator
End Enum
Public MustInherit Class NestedEnumerable(Of T)
Implements IEnumerable(Of T)
Public MustOverride Function GetNestedEnumerator() As NestedEnumerator(Of T)
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Return New RootEnumerator(Of T)(GetNestedEnumerator)
End Function
Private Function _GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Throw New NotImplementedException()
End Function
End Class
Public MustInherit Class NestedEnumerator(Of T)
Friend pc As Integer
Friend CurrentVAlue As T
Friend currentEnumerator As NestedEnumerator(Of T)
Public MustOverride Function NestedMoveNext() As NestedEnumState
Public Shared Function GetNestedEnumerator(e As IEnumerable(Of T))
Dim ne = TryCast(e, NestedEnumerable(Of T))
Return If(ne Is Nothing, New EnumeratorAdapter(Of T)(e), ne.GetNestedEnumerator)
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment