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?

, 26 Nov 2014 CPOL
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
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)

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 | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 27 Nov 2014
Article Copyright 2007 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid