|
|
写完了就没有意思了,没有仔细测,很明显的两处bug也没有改 :)
Option Public Option Explicit Option Compare Nocase
Const ArrayTop=400
Public Class MyCollection
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 本lib实现collection功能 ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private mCount As Integer '所有的已经分配空间数量 Private mCountUnused As Integer '已分配空间中被回收部分数量 Private mCountUsed As Integer '已分配空间中使用部分数量 Private mIndexUsed() As Integer 'Index序列号 ' Private mKeys() As String 'key键值,使用keys方式,主要为建立dictionary方式 Private mObjects() As Variant '存储分配空间 Private mIndexUnused() As Integer '被回收的序列号空间
Public Sub New() mCount=0 mCountUnused=0 Redim mIndex(0) Redim mKeys(0) Redim mObjects(0) Redim mIndexUnused(0) End Sub
Public Property Get Member(intIndex As Integer) Set Member=mObjects(mIndexUsed(intIndex)) End Property
Public Property Set Member(intIndex As Integer) Set mObjects(mIndexUsed(intIndex))=Member End Property
Public Function AddMember(vMember,intIndex As Integer) Dim CurPos As Integer '如果待插入点在当前的范围之外,那么,将它插入在最后 Dim i If intIndex<1 Or intIndex>mCountUsed Then CurPos=mCountUsed+1 Else CurPos=intIndex End If If mCountUnused>0 Then '从回收的空间分配 Set mObjects(mIndexUnused(mCountUnused))=vMember For i=mCountUsed To CurPos Step -1 mIndexUsed(i+1)=mIndexUsed(i) Next mIndexUsed(CurPos)=mIndexUnused(mCountUnused) mCountUnused=mCountUnused-1 mCountUsed=mCountUsed+1 Else '新分配空间 If mCount Mod ArrayTop=0 Then '如果空间已经使用完毕,创建新的空间 Redim Preserve mIndexUsed(mCount+ArrayTop) Redim Preserve mObjects(mCount+ArrayTop) Redim Preserve mIndexUnused(mCount+ArrayTop) End If For i=mCountUsed To CurPos Step -1 mIndexUsed(i+1)=mIndexUsed(i) Next mCountUsed=mCountUsed+1 mCount=mCount+1 Set mObjects(mCountUsed)=vMember mIndexUsed(CurPos)=mCountUsed End If End Function
Public Function DelMember(intIndex As Integer) If intIndex<1 Or intIndex>mCountUsed Then '如果要删除的序列值在范围之外,则取消本操作 Exit Function End If Dim i mCountUnused=mCountUnused+1 mIndexUnused(mCountUnused)=mIndexUsed(intIndex) For i= intIndex To mCountUsed mIndexUsed(i)=mIndexUsed(i+1) Next mCountUsed=mCountUsed-1 End Function
Public Property Get Count As Integer Count=mCountUsed End Property End Class
|
|