📄 arraylist.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 1 'Persistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ArrayList"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2004 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: ArrayList
'
''
' A list that dynamically increases in size to hold a number of items.
'
' @remarks This class contains an internal array of Variants. As new items are
' added to the list, the capacity will increase as necessary.
' <p>The capacity of the list is the current number of elements in the internal-array.</p>
' <p>To lower the capacity to save memory, use the TrimToSize method, or set Capacity directly.</p>
' <p>The default capacity is 16. The Item index is zero based.</p>
' <p>This class is persistable.</p>
'
' @see Constructors
' @see ArrayListStatic
' @see IList
'
Option Explicit
Implements IObject
Implements ICollection
Implements IEnumerable
Implements ICloneable
Implements IList
Private Const PROP_COUNT As String = "Count"
Private Const PROP_CAPACITY As String = "Capacity"
Private Const PROP_ITEMPREFIX As String = "Item"
Private Const PROP_SUBTYPE As String = "SubType"
Private Const PROP_DEFAULTCOMPARER As String = "DefaultComparer"
Private Const PROP_COMPARER As String = "Comparer"
Private Const SUBTYPE_NORMAL As Long = 0
Private Const SUBTYPE_EMPTY As Long = 1
Private Const SUBTYPE_NOTHING As Long = 2
Private Const DEF_CAPACITY As Long = 16
Private mItems() As Variant
Private mCount As Long
Private mCapacity As Long
Private mVersion As Long
Private mComparer As IComparer
''
' Adds a new item to the end of the list.
'
' @param value The item to be added to the list.
' @return The index at which the item was added.
' @remarks As items are added, the capacity is increased as necessary. The items
' are appended to the end of the list and maintain the order they were added,
' provided no other method is used to change the order, such as Insert or Remove.
' @include "..\..\Includes\ArrayListAdd.txt"
Public Function Add(ByRef Value As Variant) As Long
Add = mCount
Call Insert(mCount, Value)
End Function
''
' Adds the items from a collection to the list.
'
' @param c The collection of items to add.
' The collection can be a <b>VBA.Collection</b>, <b>ICollection</b> object,
' or an <b>Array</b>.
' @remarks The elements in the collection added by using For..Each to
' iterate each element. The elements are added to the end of the list in the
' same order as the For..Each loop produces them.
' <p>If the number of elements to be added is more than the available
' capacity in the <b>ArrayList</b>, then the capacity is increased to accommodate
' the collection of elements.</p>
' @see ICollection
' @include "..\..\Includes\ArrayListAddRange.txt"
Public Sub AddRange(ByRef c As Variant)
Call InsertRange(mCount, c)
End Sub
''
' Performs a binary search for the value in the internal list.
'
' @param value The value to search for.
' @param Index The starting index to begin the search.
' @param count The number of elements to search in from the startindex.
' @param comparer A comparer to perform user-defined comparison logic.
' @return The index at which the value was found, or a negative value indicating
' the value was not found.
' @remarks The search assumes the <b>ArrayList</b> is sorted.
' <p>The default comparison method requires the values to be of the same
' intrinsic Visual Basic datatype. A vbLong will fail to compare against a vbInteger,
' for instance. Objects must implement the <b>IComparable</b> interface or
' an exception will be thrown.</p>
' <p>The comparison behaviour can be overridden by supplying a custom <b>IComparer</b>
' compatible object.</P>
' <p>The search can be limited to a specific range within the list instead
' of over the entire list.</p>
' <p>If the return value is negative, then the value was not found in the list. To
' determine where the value should have been found, negate (Not) the return value.</p>
' @see IComparer
' @see IComparable
' @include "..\..\Includes\ArrayListBinarySearch.txt"
Public Function BinarySearch(ByRef Value As Variant, Optional ByRef Index 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, Index, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, ElemIndex, "Index", ElemCount, "Count", IsMissing(Index))
If mCount = 0 Then
BinarySearch = -1
Exit Function
End If
If Comparer Is Nothing Then Set Comparer = mComparer
BinarySearch = cArray.BinarySearch(mItems, Value, ElemIndex, ElemCount, Comparer)
End Function
''
' Returns the total number of elements in the the internal array.
'
' @return The number of elements allocated internally.
' @remarks This is not the same as <b>Count</b>. <b>Count</b> returns
' the number of items that have been added to the list.
'
Public Property Get Capacity() As Long
Capacity = mCapacity
End Property
''
' Sets the number of allocated elements in the array.
'
' @param RHS The number of elements to set the array to.
' @remarks The capacity cannot be set less than the number of items in the list.
' If the capacity is set to zero, then it is set to the default capacity (16).
' @include "..\..\Includes\ArrayListCapacity.txt"
Public Property Let Capacity(ByVal RHS As Long)
If RHS < mCount Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_SmallCapacity), "Capacity", Capacity)
If RHS = 0 Then RHS = DEF_CAPACITY
If RHS = mCapacity Then Exit Property
mCapacity = RHS
ReDim Preserve mItems(0 To mCapacity - 1)
End Property
''
' Clears the list of all values.
'
' @remarks The internal array is cleared of values. The count is reset
' to zero, but the capacity remains the same. To lower the number of
' elements in the array, use TrimToSize or set Capacity directly.
'
Public Sub Clear()
ReDim mItems(0 To mCapacity - 1)
mCount = 0
mVersion = mVersion + 1
End Sub
''
' Returns an ArrayList object containing a copy of the array in
' the original ArrayList.
'
' @return A copy of the original object.
' @remarks When the list of elements is cloned, elements that are
' reference types are not cloned. The cloned list will contain reference
' pointers to the same objects as the original list has.
'
Public Function Clone() As ArrayList
Set Clone = New ArrayList
Call Clone.CloneHelper(mItems, mCount)
End Function
''
' Returns if the list contains a specific value.
'
' @param value The value to search for.
' @param comparer A user-defined comparer to performs custom
' compare logic on the array elements.
' @return A boolean value indicating the value was found in the list.
' @remarks This method performs a linear search of all elements in the list
' comparing each element with the value being searched for. If the value is
' an object, then if it implements the <b>cObject</b> interface, then the
' <b>Equals</b> method is used to test equality, otherwise the <b>Is</b> operator
' is use to compare two objects.
' <p>If there is special compare logic necessary, then a comparer
' object can be supplied. The compare object must implement the IComparer interface.</p>
'
Public Function Contains(ByRef Value As Variant, Optional ByVal Comparer As IComparer) As Boolean
Contains = (IndexOf(Value, , , Comparer) >= 0)
End Function
''
' Copies all of the items to an array.
'
' @param dstArray The array to copy the items to.
' @param arrayindex The index to start copying items to in the destination array.
' @remarks The destination array must be large enough to hold all of the items. If an
' arrayindex is specified, then the destination array must be large enough to hold all of
' the items from that index on.
' @include "..\..\Includes\ArrayListCopyTo.txt"
Public Sub CopyTo(ByRef DstArray As Variant, Optional ByRef ArrayIndex As Variant)
If cArray.IsNull(DstArray) Then _
Throw Cor.NewArgumentNullException(Environment.GetResourceString(ArgumentNull_Array), "dstArray")
Call cArray.CopyEx(mItems, 0, DstArray, GetOptionalLong(ArrayIndex, LBound(DstArray)), mCount)
End Sub
''
' Copies a specified number of the items to an Array.
'
' @param index The starting index in the list to start copying from.
' @param dstArray The array to copy the items to.
' @param arrayindex The index in the destination array to start copying items to.
' @param count The number of items to copy.
' @remarks The destination array must be large enough to hold the number of elements being copied.
'
Public Sub CopyToEx(ByVal Index As Long, ByRef DstArray As Variant, ByVal ArrayIndex As Long, ByVal Count As Long)
Call cArray.CopyEx(mItems, Index, DstArray, ArrayIndex, Count)
End Sub
''
' Returns the number of items in the list.
'
' @return The number of items in the list.
'
Public Property Get Count() As Long
Count = mCount
End Property
''
' Returns a boolean indicating if the value and this object
' instance are the same instance.
'
' @param value The value to test equality on.
' @return Boolean indicating equality.
' @see IObject
'
Public Function Equals(ByRef Value As Variant) As Boolean
Equals = Object.Equals(Me, Value)
End Function
''
' Returns an enumerator for an ArrayList.
'
' @param startindex The index in the list to being enumeration.
' @param Count The number of items in the list to enumerate over.
' @return An ArrayList enumerator
' @remarks This enumerator can be used in For..Each loops. To access the optional
' parameters, the GetEnumerator must be called instead of simply passing the object
' to the For..Each. This allows for a portion of the list to be enumerated.
' @include "..\..\Includes\ArrayListGetEnumerator.txt"
Public Function GetEnumerator(Optional ByRef StartIndex As Variant, Optional ByRef Count As Variant) As Object
Dim ElemCount As Long
Dim ElemIndex As Long
Dim Result As Long
Result = GetOptionalListRange(mCount, StartIndex, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, ElemIndex, "StartIndex", ElemCount, "Count", IsMissing(StartIndex))
Dim Ret As New ArrayListEnumerator
Call Ret.Init(Me, ElemIndex, ElemCount)
Set GetEnumerator = Ret
End Function
''
' Returns a pseudo-unique number identifying this instance.
'
' @return Pseudo-unique number identifying this instance.
' @see IObject
'
Public Function GetHashCode() As Long
GetHashCode = ObjPtr(CUnk(Me))
End Function
''
' Returns an ArrayList object that is a windowed view into the original ArrayList.
'
' @param index The index in the original list to start the new view.
' @param count The number of elements in the original list to show in the view.
' @return An ArrayList that represents a subset view of the original list.
' @remarks This windowed view is the only way to then modify the original
' ArrayList. If the original list is modified some other way, then an exception
' is thrown the next time the ranged view is accessed.
'
Public Function GetRange(ByVal Index As Long, ByVal Count As Long) As ArrayList
Dim Result As Long
Result = VerifyListRange(mCount, Index, Count)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, Index, "Index", Count, "Count")
Dim Ret As New RangedArrayList
Call Ret.Init(Me, Index, Count)
Set GetRange = Ret
End Function
''
' Returns the index of the first occurrence of the value in the list.
'
' @param value The value to search the list for.
' @param startindex The index to begin the search in the list.
' @param count The number of items in the list to search.
' @param comparer A custom comparer to perform any special compare logic.
' @return The index of the first occurrence of value, or -1 if the value was not found.
' @remarks If there is special compare logic necessary, then a comparer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -