Click here to Skip to main content
11,642,216 members (63,328 online)
Click here to Skip to main content
Add your own
alternative version

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

, 26 Nov 2014 CPOL 59.5K 388 96
In this article, I try to highlight some issues in the .NET Framework generic list and how to circumvent them
My Project
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
                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
                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

            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.
        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.


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


About the Author

Pascal Ganaye
Software Developer (Senior)
United Kingdom United Kingdom
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 27 Nov 2014
Article Copyright 2007 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid