| 1 | ' Classes/System/Collections/ArrayList.ab
|
|---|
| 2 |
|
|---|
| 3 | #require <Classes/System/Collections/misc.ab>
|
|---|
| 4 |
|
|---|
| 5 | Namespace System
|
|---|
| 6 | Namespace Collections
|
|---|
| 7 |
|
|---|
| 8 | Class ArrayList
|
|---|
| 9 | Implements IList /*, ICollection, IEnumerable, ICloneable*/
|
|---|
| 10 |
|
|---|
| 11 | pObject As *Object
|
|---|
| 12 | size As Long
|
|---|
| 13 | capacity As Long
|
|---|
| 14 |
|
|---|
| 15 | Sub Init( capacity As Long )
|
|---|
| 16 | If size < 0 Then
|
|---|
| 17 | ' Error
|
|---|
| 18 | debug
|
|---|
| 19 | End If
|
|---|
| 20 |
|
|---|
| 21 | This.size = 0
|
|---|
| 22 | This.capacity = size
|
|---|
| 23 |
|
|---|
| 24 | pObject = GC_malloc( SizeOf(*Object) * size )
|
|---|
| 25 | If pObject = 0 Then
|
|---|
| 26 | ' OutOfMemoryException
|
|---|
| 27 | Debug
|
|---|
| 28 | End If
|
|---|
| 29 | End Sub
|
|---|
| 30 |
|
|---|
| 31 | Sub Realloc( newSize As Long )
|
|---|
| 32 | ' If newSize > capacity Then
|
|---|
| 33 | ' ' miss!
|
|---|
| 34 | ' Debug
|
|---|
| 35 | ' End If
|
|---|
| 36 |
|
|---|
| 37 | Dim p = realloc( pObject, SizeOf(*Object) * newSize )
|
|---|
| 38 | If p = 0 Then
|
|---|
| 39 | ' OutOfMemoryException
|
|---|
| 40 | Debug
|
|---|
| 41 | Else
|
|---|
| 42 | pObject = p
|
|---|
| 43 | End If
|
|---|
| 44 | capacity = newSize
|
|---|
| 45 | End Sub
|
|---|
| 46 |
|
|---|
| 47 | Sub SetLeastCapacity( capacity As Long )
|
|---|
| 48 | If This.capacity < capacity Then
|
|---|
| 49 | Realloc(capacity)
|
|---|
| 50 | End If
|
|---|
| 51 | End Sub
|
|---|
| 52 |
|
|---|
| 53 | Public
|
|---|
| 54 |
|
|---|
| 55 | '----------------------------------------------------------------
|
|---|
| 56 | ' Properties
|
|---|
| 57 | '----------------------------------------------------------------
|
|---|
| 58 |
|
|---|
| 59 | /*Const*/ Virtual Function Capacity() As Long
|
|---|
| 60 | Return capacity
|
|---|
| 61 | End Function
|
|---|
| 62 |
|
|---|
| 63 | Virtual Sub Capacity(c As Long)
|
|---|
| 64 | If c <> capacity Then
|
|---|
| 65 | If c < size Then
|
|---|
| 66 | 'Throw ArgumentOutOfRangeException
|
|---|
| 67 | Debug
|
|---|
| 68 | Else
|
|---|
| 69 | Realloc(c)
|
|---|
| 70 | End If
|
|---|
| 71 | End If
|
|---|
| 72 | End Sub
|
|---|
| 73 |
|
|---|
| 74 | /*Const*/ /*Override*/ Virtual Function Count() As Long
|
|---|
| 75 | Return size
|
|---|
| 76 | End Function
|
|---|
| 77 |
|
|---|
| 78 | /*Const*/ Override Function IsFixedSize() As Boolean
|
|---|
| 79 | Return False
|
|---|
| 80 | End Function
|
|---|
| 81 |
|
|---|
| 82 | /*Const*/ Override Function IsReadOnly() As Boolean
|
|---|
| 83 | Return False
|
|---|
| 84 | End Function
|
|---|
| 85 |
|
|---|
| 86 | /*Const*/ /*Override*/ Virtual Function IsSynchronized() As Boolean
|
|---|
| 87 | Return False
|
|---|
| 88 | End Function
|
|---|
| 89 | ' SyncRoot
|
|---|
| 90 |
|
|---|
| 91 | ' Operators
|
|---|
| 92 | /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object
|
|---|
| 93 | If 0 <= index And index < size Then
|
|---|
| 94 | Return pObject[index]
|
|---|
| 95 | Else
|
|---|
| 96 | 'Throw OutOfRangeException?
|
|---|
| 97 | Debug
|
|---|
| 98 | End If
|
|---|
| 99 | End Function
|
|---|
| 100 |
|
|---|
| 101 | /*Override*/ Virtual Sub Operator []=(index As Long, object As Object)
|
|---|
| 102 | If 0 <= index And index < size Then
|
|---|
| 103 | pObject[index] = object
|
|---|
| 104 | Else
|
|---|
| 105 | 'Throw OutOfRangeException?
|
|---|
| 106 | Debug
|
|---|
| 107 | End If
|
|---|
| 108 | End Sub
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | '----------------------------------------------------------------
|
|---|
| 113 | ' Methods
|
|---|
| 114 | '----------------------------------------------------------------
|
|---|
| 115 |
|
|---|
| 116 | ' Constractors
|
|---|
| 117 | Sub ArrayList()
|
|---|
| 118 | Init( 16 )
|
|---|
| 119 | End Sub
|
|---|
| 120 | /*
|
|---|
| 121 | Sub ArrayList(c As ICollection)
|
|---|
| 122 | ' 未実装
|
|---|
| 123 | Debug
|
|---|
| 124 | End Sub
|
|---|
| 125 | */
|
|---|
| 126 | Sub ArrayList(c As Long)
|
|---|
| 127 | Init( c )
|
|---|
| 128 | End Sub
|
|---|
| 129 |
|
|---|
| 130 | ' Destractor
|
|---|
| 131 | Virtual Sub ~ArrayList()
|
|---|
| 132 | End Sub
|
|---|
| 133 |
|
|---|
| 134 | ' Sub Operator =(a As ArrayList)
|
|---|
| 135 |
|
|---|
| 136 | /*Override*/ Virtual Function Add( object As Object ) As Long
|
|---|
| 137 | SetLeastCapacity( size + 1 )
|
|---|
| 138 | pObject[size] = object
|
|---|
| 139 | Add = size
|
|---|
| 140 | size++
|
|---|
| 141 | End Function
|
|---|
| 142 |
|
|---|
| 143 | Virtual Sub AddRange(c As ICollection)
|
|---|
| 144 | ' TODO: 実装
|
|---|
| 145 | End Sub
|
|---|
| 146 |
|
|---|
| 147 | Const Virtual Function BinarySearch(x As Object)
|
|---|
| 148 | ' TODO: 実装
|
|---|
| 149 | End Function
|
|---|
| 150 |
|
|---|
| 151 | Const Virtual Function BinarySearch(x As Object, c As IComparer) As Long
|
|---|
| 152 | Return BinarySearch(0, size, x, c)
|
|---|
| 153 | End Function
|
|---|
| 154 |
|
|---|
| 155 | Const Virtual Function BinarySearch(index As Long, count As Long, x As Object, c As IComparer) As Long
|
|---|
| 156 | Dim l = index
|
|---|
| 157 | Dim r = index + ELM(count)
|
|---|
| 158 | While l <= r
|
|---|
| 159 | Dim mid = (l + r) >> 2
|
|---|
| 160 | Dim ret = c.Compare(pObject[mid], x)
|
|---|
| 161 | If ret = 0 Then
|
|---|
| 162 | Return mid
|
|---|
| 163 | Else If ret < 0 Then 'pObject[mid] < x
|
|---|
| 164 | If l = r Then
|
|---|
| 165 | If mid + 1 <= index + ELM(count) Then
|
|---|
| 166 | Return Not (mid + 1)
|
|---|
| 167 | Else
|
|---|
| 168 | Return count
|
|---|
| 169 | End If
|
|---|
| 170 | End If
|
|---|
| 171 | l = mid + 1
|
|---|
| 172 | Else 'pObject[mid] > x
|
|---|
| 173 | If l = r And mid + 1 <= index + ELM(count) Then
|
|---|
| 174 | Return Not mid
|
|---|
| 175 | End If
|
|---|
| 176 | r = mid - 1
|
|---|
| 177 | End If
|
|---|
| 178 | Wend
|
|---|
| 179 | If l = index Then
|
|---|
| 180 | Return Not index
|
|---|
| 181 | Else 'r = index + ELM(count)
|
|---|
| 182 | Return Not count
|
|---|
| 183 | End If
|
|---|
| 184 | End Function
|
|---|
| 185 |
|
|---|
| 186 | /*Override*/ Virtual Sub Clear()
|
|---|
| 187 | size = 0
|
|---|
| 188 | End Sub
|
|---|
| 189 |
|
|---|
| 190 | Virtual Function Clone() As ArrayList
|
|---|
| 191 | Dim arrayList = New ArrayList( size )
|
|---|
| 192 | memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size )
|
|---|
| 193 | arrayList.size = This.size
|
|---|
| 194 | Return arrayList
|
|---|
| 195 | End Function
|
|---|
| 196 |
|
|---|
| 197 | /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean
|
|---|
| 198 | Dim i As Long
|
|---|
| 199 | For i = 0 To ELM(size)
|
|---|
| 200 | If x->Equals(data.Elm[i]) Then
|
|---|
| 201 | Return True
|
|---|
| 202 | End If
|
|---|
| 203 | Next
|
|---|
| 204 | End Function*/
|
|---|
| 205 |
|
|---|
| 206 | ' CopyTo
|
|---|
| 207 |
|
|---|
| 208 | '
|
|---|
| 209 | ' /*Const*/ /*Override*/ Virtual Function GetEnumerator() As IEnumerator
|
|---|
| 210 | ' 'Return GetEnumerator(index, count)
|
|---|
| 211 | ' End Function
|
|---|
| 212 |
|
|---|
| 213 | ' Const Virtual Function GetEnumerator(index As Long, count As Long) As IEnumerator
|
|---|
| 214 | ' ' TODO: 実装
|
|---|
| 215 | ' End Function
|
|---|
| 216 |
|
|---|
| 217 | Virtual Function GetRange(index As Long, count As Long) As ArrayList
|
|---|
| 218 | ' TODO: 実装
|
|---|
| 219 | End Function
|
|---|
| 220 |
|
|---|
| 221 | /*Const*/ Override Function IndexOf(object As Object) As Long
|
|---|
| 222 | Return IndexOf(object, 0, size)
|
|---|
| 223 | End Function
|
|---|
| 224 |
|
|---|
| 225 | /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long
|
|---|
| 226 | Return IndexOf(object, startIndex, size - startIndex)
|
|---|
| 227 | End Function
|
|---|
| 228 |
|
|---|
| 229 | /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long
|
|---|
| 230 | Dim i As Long
|
|---|
| 231 | Dim last = System.Math.Min(startIndex + count - 1, size)
|
|---|
| 232 | For i = startIndex To last
|
|---|
| 233 | If object.Equals( pObject[i] ) Then
|
|---|
| 234 | Return i
|
|---|
| 235 | End If
|
|---|
| 236 | Next
|
|---|
| 237 | Return -1
|
|---|
| 238 | End Function
|
|---|
| 239 |
|
|---|
| 240 | Override Sub Insert(i As Long, object As Object)
|
|---|
| 241 | SetLeastCapacity(size + 1)
|
|---|
| 242 | memmove(VarPtr(pObject[i + 1]), VarPtr(pObject[i]), SizeOf (*Object) * (size - i))
|
|---|
| 243 | pObject[i] = object
|
|---|
| 244 | size++
|
|---|
| 245 | End Sub
|
|---|
| 246 |
|
|---|
| 247 | Virtual Sub InsertRange(index As Long, c As ICollection)
|
|---|
| 248 | ' TODO: 実装
|
|---|
| 249 | End Sub
|
|---|
| 250 |
|
|---|
| 251 | /*Const*/ Virtual Function LastIndexOf(object As Object) As Long
|
|---|
| 252 | LastIndexOf(object, 0, size)
|
|---|
| 253 | End Function
|
|---|
| 254 |
|
|---|
| 255 | /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long
|
|---|
| 256 | Return LastIndexOf(object, startIndex, size - startIndex)
|
|---|
| 257 | End Function
|
|---|
| 258 |
|
|---|
| 259 | /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long
|
|---|
| 260 | ' TODO: 実装
|
|---|
| 261 | End Function
|
|---|
| 262 |
|
|---|
| 263 | Override Sub Remove(object As Object)
|
|---|
| 264 | Dim i = IndexOf(object)
|
|---|
| 265 | If i > 0 Then
|
|---|
| 266 | RemoveAt(i)
|
|---|
| 267 | End If
|
|---|
| 268 | End Sub
|
|---|
| 269 |
|
|---|
| 270 | Override Sub RemoveAt(i As Long)
|
|---|
| 271 | RemoveRange(i, 1)
|
|---|
| 272 | End Sub
|
|---|
| 273 |
|
|---|
| 274 | Virtual Sub RemoveRange(i As Long, count As Long)
|
|---|
| 275 | Dim removeLastPos = i + count
|
|---|
| 276 | memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos))
|
|---|
| 277 | size -= count
|
|---|
| 278 | End Sub
|
|---|
| 279 |
|
|---|
| 280 | Virtual Sub Reverse()
|
|---|
| 281 | Reverse(0, size)
|
|---|
| 282 | End Sub
|
|---|
| 283 |
|
|---|
| 284 | Virtual Sub Reverse(startIndex As Long, count As Long)
|
|---|
| 285 | ' TODO: 実装
|
|---|
| 286 | End Sub
|
|---|
| 287 |
|
|---|
| 288 | Virtual Sub SetRange(index As Long, c As ICollection)
|
|---|
| 289 | ' TODO: 実装
|
|---|
| 290 | End Sub
|
|---|
| 291 |
|
|---|
| 292 | Virtual Sub Sort()
|
|---|
| 293 | ' TODO: 実装
|
|---|
| 294 | End Sub
|
|---|
| 295 |
|
|---|
| 296 | Virtual Sub Sort(c As IComparer)
|
|---|
| 297 | Sort(0, size, c)
|
|---|
| 298 | End Sub
|
|---|
| 299 |
|
|---|
| 300 | Virtual Sub Sort(index As Long, count As Long, c As IComparer)
|
|---|
| 301 | ' TODO: 実装
|
|---|
| 302 | End Sub
|
|---|
| 303 |
|
|---|
| 304 | ' ToArray
|
|---|
| 305 | Virtual Sub TrimToSize()
|
|---|
| 306 | Realloc(size)
|
|---|
| 307 | End Sub
|
|---|
| 308 |
|
|---|
| 309 | /*Override*/ Override Function ToString() As String
|
|---|
| 310 | Return "System.Collections.ArrayList"
|
|---|
| 311 | End Function
|
|---|
| 312 |
|
|---|
| 313 | ' --------------------------------
|
|---|
| 314 | ' static methods
|
|---|
| 315 | Static Function Adapter(l As IList) As ArrayList
|
|---|
| 316 | ' TODO: 実装
|
|---|
| 317 | End Function
|
|---|
| 318 |
|
|---|
| 319 | Static Function FixedSize(l As ArrayList) As ArrayList
|
|---|
| 320 | ' TODO: 実装
|
|---|
| 321 | End Function
|
|---|
| 322 |
|
|---|
| 323 | Static Function FixedSize(l As IList) As IList
|
|---|
| 324 | ' TODO: 実装
|
|---|
| 325 | 'Return FixedSize(Adapter(l))
|
|---|
| 326 | End Function
|
|---|
| 327 |
|
|---|
| 328 | Static Function ReadOnly(l As ArrayList) As ArrayList
|
|---|
| 329 | ' TODO: 実装
|
|---|
| 330 | End Function
|
|---|
| 331 |
|
|---|
| 332 | Static Function ReadOnly(l As IList) As IList
|
|---|
| 333 | ' TODO: 実装
|
|---|
| 334 | 'Return ReadOnly(Adapter(l))
|
|---|
| 335 | End Function
|
|---|
| 336 |
|
|---|
| 337 | Static Function Repeat(x As Object, c As Long) As ArrayList
|
|---|
| 338 | Repeat = New ArrayList(c)
|
|---|
| 339 | Dim i As Long
|
|---|
| 340 | For i = 0 To ELM(c)
|
|---|
| 341 | Repeat.Add(x)
|
|---|
| 342 | Next
|
|---|
| 343 | End Function
|
|---|
| 344 |
|
|---|
| 345 | Static Function Synchronized(l As ArrayList) As ArrayList
|
|---|
| 346 | ' TODO: 実装
|
|---|
| 347 | End Function
|
|---|
| 348 |
|
|---|
| 349 | Static Function Synchronized(l As IList) As IList
|
|---|
| 350 | ' TODO: 実装
|
|---|
| 351 | 'Return Synchronized(Adapter(l))
|
|---|
| 352 | End Function
|
|---|
| 353 |
|
|---|
| 354 | End Class
|
|---|
| 355 |
|
|---|
| 356 | End Namespace 'Collections
|
|---|
| 357 | End Namespace 'System
|
|---|