📄 arraylist.cls
字号:
' object can be supplied. The compare object must implement the IComparer interface.
'
Public Function IndexOf(ByRef Value As Variant, Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant, Optional ByVal Comparer As IComparer) As Long
Dim ElemCount As Long
Dim ElemIndex As Long
Dim Result As Long
Result = GetOptionalListRange(mCount, StartIndex, ElemIndex, Count, ElemCount)
If Result <> 0 Then Call ThrowListRangeException(Result, ElemIndex, "StartIndex", ElemCount, "Count", IsMissing(StartIndex))
If mCount = 0 Then
IndexOf = -1
Exit Function
End If
If Comparer Is Nothing Then Set Comparer = mComparer
IndexOf = cArray.IndexOf(mItems, Value, ElemIndex, ElemCount, Comparer)
End Function
''
' Inserts a value into the list at the specified index.
'
' @param index The index in which the value is to be inserted.
' @param value The value to be inserted into the list.
' @remarks When a value is inserted, all items starting at the insertion point
' are moved out by one space. If capacity is reached, the list will be reallocated.
'
Public Sub Insert(ByVal Index As Long, ByRef Value As Variant)
If Index < 0 Or Index > mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_ArrayListInsert), "index", Index)
If mCount = mCapacity Then Call EnsureCapacity(mCount + 1)
Call InsertSpace(Index, 1)
Call VariantCopyInd(mItems(Index), Value)
mCount = mCount + 1
mVersion = mVersion + 1
End Sub
''
' Inserts a collection of items into the list.
'
' @param index The index at which the items will be inserted.
' @param c The collection of items to be inserted into the list.
' @remarks When the items are inserted, the items starting at the
' index will be moved out a number of spaced equal to the number of
' items to be inserted. If the capacity is reached, the list will
' be reallocated.<br><br>
'
' The collection can be a <b>VBA.Collection</b>,<b>ICollection</b> object,
' or an <b>Array</b>.
'
Public Sub InsertRange(ByVal Index As Long, ByRef c As Variant)
If Index < 0 Or Index > mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_ArrayListInsert), "index", Index)
Call WriteRange(Index, c, True)
mVersion = mVersion + 1
End Sub
''
' Returns if the list is fixed-size.
'
' @return Value indicating if the list is fixed-size.
' @remarks All operations on the list can be executed as long as the number
' of items don't change, this includes the capacity of the underlying array.
'
Public Property Get IsFixedSize() As Boolean
IsFixedSize = False
End Property
''
' Returns if the list is read-only.
'
' @return Value indicating if the list is read-only.
' @remarks All operations on the list can be executed as long as the number
' of items don't change, and the items aren't changed in the list.
'
Public Property Get IsReadOnly() As Boolean
IsReadOnly = False
End Property
''
' Returns an item from the list.
'
' @param index The index in the list from which to return the item.
' @return The item as the specified index.
' @remarks This property is set as the <i>Default</i> property for
' this class. Items can be accessed parentheses:<br><br>
' var = list(index)<br><br>
' The index is zero-based.
'
Public Property Get Item(ByVal Index As Long) As Variant
Attribute Item.VB_UserMemId = 0
If Index < 0 Or Index >= mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index))
Call VariantCopy(Item, mItems(Index))
End Property
''
' Sets an item in the list to a value.
'
' @param index The index in the list to set the value.
' @param RHS The value to set in the list.
' @remarks This property is set as the <i>Default</i> property for
' this class. Items can be accessed using parentheses:<br><br>
' list(index) = value<br><br>
' The index is zero-based.
'
Public Property Let Item(ByVal Index As Long, ByRef RHS As Variant)
If Index < 0 Or Index >= mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index))
mItems(Index) = RHS
mVersion = mVersion + 1
End Property
''
' Sets an item in the list to a value.
'
' @param index The index in the list to set the value.
' @param RHS The value to set in the list.
' @remarks This property is set as the <i>Default</i> property for
' this class. Items can be accessed parentheses:<br><br>
' Set list(index) = value<br><br>
' The index is zero-based.
'
Public Property Set Item(ByVal Index As Long, ByRef RHS As Variant)
If Index < 0 Or Index >= mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index))
Set mItems(Index) = RHS
mVersion = mVersion + 1
End Property
''
' Returns the last occurrence of a value in the list.
'
' @param value The value to search for in the list.
' @param startindex The index to start searching from.
' @param count The number of items to be searched.
' @param comparer A custom comparer to perform any special compare logic.
' @return The index of the last occurrence of the value, or -1 if not found.
' @remarks The search is performed from the end of the list towards the
' beginning. If a startindex
'
Public Function LastIndexOf(ByRef Value As Variant, Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant, Optional ByVal Comparer As IComparer) As Long
Dim ElemIndex As Long
Dim ElemCount As Long
If GetOptionalLongPair(StartIndex, mCount - 1, ElemIndex, Count, mCount, ElemCount) = Argument_ParamRequired Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_ParamRequired), IIf(IsMissing(StartIndex), "StartIndex", "Count"))
If ElemIndex >= mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_LargerThanCollection), "StartIndex", ElemIndex)
If ElemCount < 0 Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_NeedNonNegNum), "Count", ElemCount)
If ElemIndex - ElemCount + 1 < 0 Then _
Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_InvalidCountOffset), "Count")
If mCount = 0 Then
LastIndexOf = -1
Exit Function
End If
If Comparer Is Nothing Then Set Comparer = mComparer
LastIndexOf = cArray.LastIndexOf(mItems, Value, ElemIndex, ElemCount, Comparer)
End Function
''
' Returns an enumerator
'
' @return The enumerator
' @see IEnumerator
'
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = CreateEnumerator(GetEnumerator)
End Function
''
' Removes a value from the list.
'
' @param value The value to be removed.
' @param comparer A custom comparer used to find the value.
' @remarks If the value is an object, then it must be the same instance of
' the object to be removed. This behaviour can be overriden with a custom comparer.
' <p>When removing an item, the remaining items maintain there current order
' relative to the other items.</p>
'
Public Sub Remove(ByRef Value As Variant, Optional ByVal Comparer As IComparer)
Dim Index As Long
If Comparer Is Nothing Then Set Comparer = mComparer
Index = IndexOf(Value, , , Comparer)
If Index >= 0 Then Call RemoveAt(Index)
End Sub
''
' Removes a value from the list at a specific index.
'
' @param index The index in the list to remove the item.<br>
' The index is zero-based.
' @remarks When removing an item, the remaining items maintain there current order
' relative to the other items.
'
Public Sub RemoveAt(ByVal Index As Long)
If Index < 0 Or Index >= mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_Index), "Index", Index)
mItems(Index) = Empty
Call RemoveSpace(Index, 1)
mCount = mCount - 1
mVersion = mVersion + 1
End Sub
''
' Remove a set of items from the list.
'
' @param index The index to start removing items.
' @param count The number of items to remove.
' @remarks The index is zero-based.
'
Public Sub RemoveRange(ByVal Index As Long, ByVal Count As Long)
Dim Result As Long
Result = VerifyListRange(mCount, Index, Count)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, Index, "Index", Count, "Count")
Call cArray.Clear(mItems, Index, Count)
Call RemoveSpace(Index, Count)
mCount = mCount - Count
mVersion = mVersion + 1
End Sub
''
' Reverses the list of items in the list.
'
' @param index The index to start reversing items.
' @param count The number of items to include in the reverse.
' @remarks If no parameters are supplied, then then
' entire list will be reversed.
'
Public Sub Reverse(Optional ByRef Index As Variant, Optional ByRef Count As Variant)
Dim ElemCount As Long
Dim ElemIndex As Long
Dim Result As Long
Result = GetOptionalListRange(mCount, Index, ElemIndex, Count, ElemCount)
If Result <> 0 Then Call ThrowListRangeException(Result, ElemIndex, "Index", ElemCount, "Count", IsMissing(Index))
Call cArray.Reverse(mItems, ElemIndex, ElemCount)
mVersion = mVersion + 1
End Sub
''
' Sets the items in the list to a collection of items.
'
' @param index The index at which to start setting items.
' @param c A collection of items to set in the list.
' @remarks The number of items in the list is not changed, nor is the size
' of the list increased. Only existing items in the list can be set to
' the items in the collection. This basically overlays new items onto
' existing items.
'
Public Sub SetRange(ByVal Index As Long, ByRef c As Variant)
Call WriteRange(Index, c, False)
mVersion = mVersion + 1
End Sub
''
' Sorts the items in the list.
'
' @param startindex The index to start sorting the items.
' @param Count The number of items to include in the sort.
' @param comparer A custom comparer used to compare items in the list.
' @remarks The list is sorted in ascending order by default. By providing
' a custom comaprer, the list can be sorted in special ways, such as descending order.
'
Public Sub Sort(Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant, Optional ByVal Comparer As IComparer)
Dim ElemCount As Long
Dim ElemIndex As Long
Dim Result As Long
Result = GetOptionalListRange(mCount, StartIndex, ElemIndex, Count, ElemCount)
If Result <> 0 Then Call ThrowListRangeException(Result, ElemIndex, "StartIndex", ElemCount, "Count", IsMissing(StartIndex))
If Comparer Is Nothing Then Set Comparer = mComparer
Call cArray.SortEx(mItems, ElemIndex, ElemCount, Comparer)
mVersion = mVersion + 1
End Sub
''
' Returns an array of the items in the list.
'
' @param arraytype The specific array type to return.
' @return An array of items in the list.
' @remarks <p>If an array type other than variant is specified, then the
' list attempts to convert each item in the list to the specific array type
' requested. If an item cannot be converted, then an exception is thrown.</p>
' <p>No items will be narrowed in the conversion. For example, a Double will
' not be converted to a Long. An exception will be thrown.</p>
'
Public Function ToArray(Optional ByVal ArrayType As ciArrayTypes = ciVariant) As Variant
Dim Ret As Variant
Ret = cArray.CreateInstance(ArrayType, mCount)
If mCount > 0 Then Call cArray.CopyEx(mItems, 0, Ret, 0, mCount)
ToArray = Ret
End Function
''
' Sets the capacity to the number of items in the list.
'
' @remarks This will decrease the size of the internal array. This can save
' memory if you know that no more items will be added to the list.
'
Public Sub TrimToSize()
Capacity = mCount
End Sub
''
' Returns a string representation of this object instance.
'
' @return String representing this instance.
' @see IObject
'
Public Function ToString() As String
ToString = Object.ToString(Me, App)
End Function
''
' Returns the current version of the instance.
'
' @return Version count.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -