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