Click here to Skip to main content
15,893,588 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.9K   588   101  
In this article, I try to highlight some issues in the .NET Framework generic list and how to circumvent them
Public Class LinkedArrayEnumerator
    Implements IEnumerator

    ' Fields
    Private mCurrentElement As Object
    Private mCurrentList As ArrayList
    Private Shared mDummyObject As Object = New Object
    Private mIndex1 As Integer
    Private mIndex2 As Integer
    Private mList As LinkedArray
    Private mVersion As Integer

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

    Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
        If (mVersion <> mList.mVersion) Then
            Throw New InvalidOperationException("InvalidOperation_EnumFailedVersion")
        End If
        If mCurrentList Is Nothing OrElse mIndex2 >= mCurrentList.Count Then
            If mIndex1 < mList.mLists.Count - 1 Then
                mIndex1 += 1
                mCurrentList = mList.mLists(mIndex1)
                mIndex2 = -1
            End If
        End If
        mIndex2 += 1
        If mCurrentList Is Nothing OrElse mIndex2 >= mCurrentList.Count Then
            mCurrentElement = LinkedArrayEnumerator.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 <> mList.mVersion) Then
            Throw New InvalidOperationException("InvalidOperation_EnumFailedVersion")
        End If
        mCurrentElement = LinkedArrayEnumerator.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.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

    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

End Class



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