Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / Visual Basic

ArrayLists, Generic Lists, Is There A Way To Insert Items Faster?

Rate me:
Please Sign up or sign in to vote.
4.83/5 (37 votes)
26 Nov 2014CPOL6 min read 111.3K   588   101  
In this article, I try to highlight some issues in the .NET Framework generic list and how to circumvent them
Namespace Generic
    Public Class LinkedArrayEnumerator(Of type)
        Implements IEnumerator(Of type)

        ' Fields
        Private mCurrentElement As Object
        Private mCurrentList As List(Of type)
        Private Shared mDummyObject As Object = New Object
        Private mIndex1 As Integer
        Private mIndex2 As Integer
        Private mLinkedArray As LinkedArray(Of type)
        Private mVersion As Integer

        ' Methods
        Friend Sub New(ByVal list As LinkedArray(Of type))
            mLinkedArray = list
            mIndex1 = -1
            mIndex2 = -1
            mVersion = list.mVersion
            mCurrentList = Nothing
            mCurrentElement = LinkedArrayEnumerator(Of type).mDummyObject
        End Sub

        Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
            If (mVersion <> mLinkedArray.mVersion) Then
                Throw New InvalidOperationException("InvalidOperation_EnumFailedVersion")
            End If
            If mCurrentList Is Nothing OrElse mIndex2 >= mCurrentList.Count Then
                If mIndex1 < mLinkedArray.mLists.Count - 1 Then
                    mIndex1 += 1
                    mCurrentList = mLinkedArray.mLists(mIndex1)
                    mIndex2 = -1
                End If
            End If
            mIndex2 += 1
            If mCurrentList Is Nothing OrElse mIndex2 >= mCurrentList.Count Then
                mCurrentElement = LinkedArrayEnumerator(Of type).mDummyObject
                mIndex1 = Int32.MaxValue
                mIndex2 = Int32.MaxValue
                Return False
            Else
                mCurrentElement = mCurrentList(mIndex2)
                Return True
            End If
        End Function

        Public Sub Reset() Implements System.Collections.IEnumerator.Reset
            If (mVersion <> mLinkedArray.mVersion) Then
                Throw New InvalidOperationException("InvalidOperation_EnumFailedVersion")
            End If
            mCurrentElement = LinkedArrayEnumerator(Of type).mDummyObject
            mIndex1 = -1
        End Sub


        ' Properties
        Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
            Get
                Dim currentElement As Object = mCurrentElement
                If (Not LinkedArrayEnumerator(Of type).mDummyObject Is currentElement) Then
                    Return currentElement
                End If
                If (mIndex1 = -1) Then
                    Throw New InvalidOperationException("InvalidOperation_EnumNotStarted")
                End If
                Throw New InvalidOperationException("InvalidOperation_EnumEnded")
            End Get
        End Property

        Public ReadOnly Property Current1() As type Implements System.Collections.Generic.IEnumerator(Of type).Current
            Get

            End Get
        End Property

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If

                ' TODO: free your own state (unmanaged objects).
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' 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(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class


End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions