vbAccelerator - Contents of code file: cCollectionArrayList.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cCollectionArrayList"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit


'
' cIndexCollection
'
' Implements a collection intended to be accessed by
' Index using a VB collection.  Adding items is
' quick but reading is slow, also inserting and
' deleting are only quick if we do not specify
' an index at the start or end of the collection...
'

Private m_c As New Collection

Public Property Get Item(ByVal Index As Long) As Object
   ' We would like to do the lookup for the item
   ' using a key rather than an Index.  But this
   ' would not work with a collection.  Say for
   ' example we added three items:
   '   Add 1, "C1"
   '   Add 2, "C2"
   '   Add 3, "C3"
   ' Then we could initially lookup "C2" to get the
   ' second item.  But this would stop working if
   ' we removed the second item or inserted an item
   ' before it.
   ' You could rebuild the collection every time
   ' an item is inserted or deleted to fix this
   ' problem, but this is, erm, computationally
   ' expensive...
   Set Item = m_c.Item(Index)
End Property
Public Property Let Item(ByVal Index As Long, Value As Object)
   Set m_c.Item(Index) = Value
End Property
Public Sub Add(Value As Object, Optional ByVal Index As Long = -1)
   If Index > 0 And Index <= m_c.Count Then
      m_c.Add Value, , Index
   Else
      m_c.Add Value
   End If
End Sub
Public Sub Remove(Optional ByVal Index As Long = -1)
   If Index > 0 Then
      m_c.Remove Index
   Else
      m_c.Remove m_c.Count
   End If
End Sub
Public Property Get Count() As Long
   Count = m_c.Count
End Property
Public Property Get Exists(ByVal Index As Long)
   Exists = (Index > 0 And Index <= m_c.Count)
End Property
Public Sub HeapMinimize()
   ' nothing to do: VB does it automatically
   ' during delete.
   ' (which has relevance to the appalling
   ' performance of a collection containing
   ' objects during deletion...)
End Sub
Public Sub Clear()
   ' Recreate the collection to
   ' clear it...
   Set m_c = Nothing
   Set m_c = New Collection
End Sub