Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 12 Dec 2007
In this article, I try to highlight some issues in the .NET Framework generic list and how to circumvent them
linkedarraycollection.zip
LinkedArrayCollection
Article
Benchmark.ods
snapshot.jpg
LinkedArrayCollection
LinkedArrayCollection.suo
LinkedArrayCollection.vsmdi
bin
Debug
LinkedArrayCollection.exe
LinkedArrayCollection.vshost.exe
LinkedArrayCollection.vshost.exe.manifest
Generic
My Project
Application.myapp
Settings.settings
UnTyped
LocalTestRun.testrunconfig
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)

Share

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 12 Dec 2007
Article Copyright 2007 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid