' Classes/System/Collections/ArrayList.ab #require Class ArrayList Inherits IList /*, ICollection, IEnumerable, ICloneable*/ pObject As *Object size As Long capacity As Long Sub Init( size As Long ) If size < 0 Then ' Error debug End If This.size = size This.capacity = size pObject = GC_malloc( SizeOf(*Object) * size + 1 ) If pObject = 0 Then ' OutOfMemoryException Debug End If End Sub Sub Realloc( size As Long ) If size > capacity Then ' miss! Debug End If pObject = realloc( pObject, SizeOf(*Object) * size + 1 ) If pObject = 0 Then ' OutOfMemoryException Debug End If End Sub Sub SetLeastCapacity( capacity As Long ) If This.capacity < capacity Then This.capacity = capacity Realloc( capacity ) End If End Sub Public '---------------------------------------------------------------- ' Properties '---------------------------------------------------------------- /*Const*/ Virtual Function Capacity() As Long Return capacity End Function Virtual Sub Capacity(c As Long) If c <> capacity Then If c < size Then 'Throw ArgumentOutOfRangeException Debug Else Realloc(c) End If End If End Sub /*Const*/ /*Override*/ Virtual Function Count() As Long Return size End Function /*Const*/ Override Function IsFixedSize() As Boolean Return False End Function /*Const*/ Override Function IsReadOnly() As Boolean Return False End Function /*Const*/ /*Override*/ Virtual Function IsSynchronized() As Boolean Return False End Function ' SyncRoot ' Operators /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object If 0 <= index And index < size Then Return pObject[index] Else 'Throw OutOfRangeException? Debug End If End Function /*Override*/ Virtual Sub Operator []=(index As Long, object As Object) If 0 <= index And index < size Then pObject[i] = object Else 'Throw OutOfRangeException? Debug End If End Sub '---------------------------------------------------------------- ' Methods '---------------------------------------------------------------- ' Constractors Sub ArrayList() Init( 0 ) End Sub /* Sub ArrayList(ByRef c As ICollection) ' –¢ŽÀ‘• Debug End Sub */ Sub ArrayList(c As Long) Init( c ) End Sub ' Destractor Virtual Sub ~ArrayList() End Sub ' Sub Operator =(ByRef a As ArrayList) /*Override*/ Virtual Function Add( object As Object ) As Long SetLeastCapacity( size + 1 ) pObject[size] = object size++ Return size - 1 End Function Virtual Sub AddRange(ByRef c As ICollection) ' TODO: ŽÀ‘• End Sub Const Virtual Function BinarySearch(x As *Object) ' TODO: ŽÀ‘• End Function Const Virtual Function BinarySearch(x As *Object, ByRef c As IComparer) As Long ' TODO: ŽÀ‘• End Function Const Virtual Function BinarySearch(index As Long, count As Long, x As *Object, ByRef c As IComparer) As Long ' TODO: ŽÀ‘• End Function /*Override*/ Virtual Sub Clear() size = 0 End Sub Virtual Function Clone() As ArrayList Dim arrayList = New ArrayList( size ) memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size ) Return arrayList End Function /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean Dim i As Long For i = 0 To ELM(size) If x->Equals(data.Elm[i]) Then Return True End If Next End Function*/ ' CopyTo /*Const*/ /*Override*/ Virtual Function GetEnumerator() As *IEnumerator 'Return GetEnumerator(index, count) End Function Const Virtual Function GetEnumerator(index As Long, count As Long) As *IEnumerator ' TODO: ŽÀ‘• End Function Virtual Function GetRange(index As Long, count As Long) As *ArrayList ' TODO: ŽÀ‘• End Function /*Const*/ Override Function IndexOf(object As Object) As Long Return IndexOf(object, 0, size) End Function /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long Return IndexOf(object, startIndex, size - startIndex) End Function /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long Dim i As Long Dim last = Math.Min(startIndex + count - 1, size) For i = startIndex To last If object.Equals( pObject[i] ) Then Return i End If Next Return -1 End Function Override Sub Insert(i As Long, object As Object) SetLeastCapacity(size + 1) memmove(VarPtr(pObject[i + 1]), VarPtr(pObject[i]), SizeOf (*Object) * (size - i)) pObject[i] = object size++ End Sub Virtual Sub InsertRange(index As Long, ByRef c As ICollection) ' TODO: ŽÀ‘• End Sub /*Const*/ Virtual Function LastIndexOf(object As Object) As Long LastIndexOf(object, 0, size) End Function /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long Return LastIndexOf(object, startIndex, size - startIndex) End Function /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long ' TODO: ŽÀ‘• End Function Override Sub Remove(object As Object) Dim i = IndexOf(object) If i > 0 Then RemoveAt(i) size-- End If End Sub Override Sub RemoveAt(i As Long) RemoveRange(i, 1) size-- End Sub Virtual Sub RemoveRange(i As Long, count As Long) Dim removeLastPos = i + count memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos)) size -= count End Sub Virtual Sub Reverse() Reverse(0, size) End Sub Virtual Sub Reverse(startIndex As Long, count As Long) ' TODO: ŽÀ‘• End Sub Virtual Sub SetRange(index As Long, ByRef c As ICollection) ' TODO: ŽÀ‘• End Sub Virtual Sub Sort() ' TODO: ŽÀ‘• End Sub Virtual Sub Sort(ByRef c As IComparer) Sort(0, size, c) End Sub Virtual Sub Sort(index As Long, count As Long, ByRef c As IComparer) ' TODO: ŽÀ‘• End Sub ' ToArray Virtual Sub TrimToSize() Realloc(size) End Sub /*Override*/ Virtual Function ToString() As String Return "System.Collections.ArrayList" End Function ' -------------------------------- ' static methods Static Function Adapter(ByRef l As IList) As *ArrayList ' TODO: ŽÀ‘• End Function Static Function FixedSize(ByRef l As ArrayList) As *ArrayList ' TODO: ŽÀ‘• End Function Static Function FixedSize(ByRef l As IList) As *IList Return FixedSize(Adapter(VarPtr(i))) End Function Static Function ReadOnly(ByRef l As ArrayList) As *ArrayList ' TODO: ŽÀ‘• End Function Static Function ReadOnly(ByRef l As IList) As *IList Return ReadOnly(Adapter(VarPtr(i))) End Function Static Function Repeat(ByRef x As Object, c As Long) As *ArrayList Repeat = New ArrayList(c) Dim i As Long For i = 0 To ELM(c) Repeat->Add(VarPtr(x)) Next End Function Static Function Synchronized(ByRef l As ArrayList) As *ArrayList ' TODO: ŽÀ‘• End Function Static Function Synchronized(ByRef l As IList) As *IList Return Synchronized(Adapter(VarPtr(i))) End Function End Class /* Class ArrayList_Element Public Sub ArrayList_Element() Init(0) End Sub Sub ArrayList_Element(c As Long) Init(c) End Sub Sub ~ArrayList_Element() free(Elm) End Sub Sub Init(c As Long) If c > 0 Then Elm = malloc(SizeOf (*Object) * c) If Elm = 0 Then ' OutOfMemoryException Debug End If Else Elm = 0 End If Size = 0 Capacity = c End Sub Sub Swap(ByRef x As ArrayList_Element) Dim tmpElm = x.Elm x.Elm = This.Elm This.Elm = tmpElm Dim tmpSize = x.Size x.Size = This.Size This.Size = x.Size Dim tmpCap = x.Capacity x.Capacity = This.Capacity This.Capacity = tmpCap End Sub Elm As **Object Size As Long Capacity As Long End Class */