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