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( 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 |
---|
39 | capacity = size |
---|
40 | End Sub |
---|
41 | |
---|
42 | Sub SetLeastCapacity( capacity As Long ) |
---|
43 | If This.capacity < capacity Then |
---|
44 | This.capacity = capacity |
---|
45 | Realloc(capacity) |
---|
46 | End If |
---|
47 | End Sub |
---|
48 | |
---|
49 | Public |
---|
50 | |
---|
51 | '---------------------------------------------------------------- |
---|
52 | ' Properties |
---|
53 | '---------------------------------------------------------------- |
---|
54 | |
---|
55 | /*Const*/ Virtual Function Capacity() As Long |
---|
56 | Return capacity |
---|
57 | End Function |
---|
58 | |
---|
59 | Virtual Sub Capacity(c As Long) |
---|
60 | If c <> capacity Then |
---|
61 | If c < size Then |
---|
62 | 'Throw ArgumentOutOfRangeException |
---|
63 | Debug |
---|
64 | Else |
---|
65 | Realloc(c) |
---|
66 | End If |
---|
67 | End If |
---|
68 | End Sub |
---|
69 | |
---|
70 | /*Const*/ /*Override*/ Virtual Function Count() As Long |
---|
71 | Return size |
---|
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 |
---|
88 | /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object |
---|
89 | If 0 <= index And index < size Then |
---|
90 | Return pObject[index] |
---|
91 | Else |
---|
92 | 'Throw OutOfRangeException? |
---|
93 | Debug |
---|
94 | End If |
---|
95 | End Function |
---|
96 | |
---|
97 | /*Override*/ Virtual Sub Operator []=(index As Long, object As Object) |
---|
98 | If 0 <= index And index < size Then |
---|
99 | pObject[index] = object |
---|
100 | Else |
---|
101 | 'Throw OutOfRangeException? |
---|
102 | Debug |
---|
103 | End If |
---|
104 | End Sub |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | '---------------------------------------------------------------- |
---|
109 | ' Methods |
---|
110 | '---------------------------------------------------------------- |
---|
111 | |
---|
112 | ' Constractors |
---|
113 | Sub ArrayList() |
---|
114 | Init( 0 ) |
---|
115 | End Sub |
---|
116 | /* |
---|
117 | Sub ArrayList(c As ICollection) |
---|
118 | ' 未実装 |
---|
119 | Debug |
---|
120 | End Sub |
---|
121 | */ |
---|
122 | Sub ArrayList(c As Long) |
---|
123 | Init( c ) |
---|
124 | End Sub |
---|
125 | |
---|
126 | ' Destractor |
---|
127 | Virtual Sub ~ArrayList() |
---|
128 | End Sub |
---|
129 | |
---|
130 | ' Sub Operator =(a As ArrayList) |
---|
131 | |
---|
132 | /*Override*/ Virtual Function Add( object As Object ) As Long |
---|
133 | SetLeastCapacity( size + 1 ) |
---|
134 | pObject[size] = object |
---|
135 | size++ |
---|
136 | Return size - 1 |
---|
137 | End Function |
---|
138 | |
---|
139 | Virtual Sub AddRange(c As ICollection) |
---|
140 | ' TODO: 実装 |
---|
141 | End Sub |
---|
142 | |
---|
143 | Const Virtual Function BinarySearch(x As Object) |
---|
144 | ' TODO: 実装 |
---|
145 | End Function |
---|
146 | |
---|
147 | Const Virtual Function BinarySearch(x As Object, c As IComparer) As Long |
---|
148 | Return BinarySearch(0, size, x, c) |
---|
149 | End Function |
---|
150 | |
---|
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 |
---|
180 | End Function |
---|
181 | |
---|
182 | /*Override*/ Virtual Sub Clear() |
---|
183 | size = 0 |
---|
184 | End Sub |
---|
185 | |
---|
186 | Virtual Function Clone() As ArrayList |
---|
187 | Dim arrayList = New ArrayList( size ) |
---|
188 | memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size ) |
---|
189 | Return arrayList |
---|
190 | End Function |
---|
191 | |
---|
192 | /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean |
---|
193 | Dim i As Long |
---|
194 | For i = 0 To ELM(size) |
---|
195 | If x->Equals(data.Elm[i]) Then |
---|
196 | Return True |
---|
197 | End If |
---|
198 | Next |
---|
199 | End Function*/ |
---|
200 | |
---|
201 | ' CopyTo |
---|
202 | |
---|
203 | ' |
---|
204 | ' /*Const*/ /*Override*/ Virtual Function GetEnumerator() As IEnumerator |
---|
205 | ' 'Return GetEnumerator(index, count) |
---|
206 | ' End Function |
---|
207 | |
---|
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 |
---|
213 | ' TODO: 実装 |
---|
214 | End Function |
---|
215 | |
---|
216 | /*Const*/ Override Function IndexOf(object As Object) As Long |
---|
217 | Return IndexOf(object, 0, size) |
---|
218 | End Function |
---|
219 | |
---|
220 | /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long |
---|
221 | Return IndexOf(object, startIndex, size - startIndex) |
---|
222 | End Function |
---|
223 | |
---|
224 | /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long |
---|
225 | Dim i As Long |
---|
226 | Dim last = System.Math.Min(startIndex + count - 1, size) |
---|
227 | For i = startIndex To last |
---|
228 | If object.Equals( pObject[i] ) Then |
---|
229 | Return i |
---|
230 | End If |
---|
231 | Next |
---|
232 | Return -1 |
---|
233 | End Function |
---|
234 | |
---|
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++ |
---|
240 | End Sub |
---|
241 | |
---|
242 | Virtual Sub InsertRange(index As Long, c As ICollection) |
---|
243 | ' TODO: 実装 |
---|
244 | End Sub |
---|
245 | |
---|
246 | /*Const*/ Virtual Function LastIndexOf(object As Object) As Long |
---|
247 | LastIndexOf(object, 0, size) |
---|
248 | End Function |
---|
249 | |
---|
250 | /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long |
---|
251 | Return LastIndexOf(object, startIndex, size - startIndex) |
---|
252 | End Function |
---|
253 | |
---|
254 | /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long |
---|
255 | ' TODO: 実装 |
---|
256 | End Function |
---|
257 | |
---|
258 | Override Sub Remove(object As Object) |
---|
259 | Dim i = IndexOf(object) |
---|
260 | If i > 0 Then |
---|
261 | RemoveAt(i) |
---|
262 | size-- |
---|
263 | End If |
---|
264 | End Sub |
---|
265 | |
---|
266 | Override Sub RemoveAt(i As Long) |
---|
267 | RemoveRange(i, 1) |
---|
268 | size-- |
---|
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 | Return FixedSize(Adapter(l)) |
---|
322 | End Function |
---|
323 | |
---|
324 | Static Function ReadOnly(l As ArrayList) As ArrayList |
---|
325 | ' TODO: 実装 |
---|
326 | End Function |
---|
327 | |
---|
328 | Static Function ReadOnly(l As IList) As IList |
---|
329 | Return ReadOnly(Adapter(l)) |
---|
330 | End Function |
---|
331 | |
---|
332 | Static Function Repeat(x As Object, c As Long) As ArrayList |
---|
333 | Repeat = New ArrayList(c) |
---|
334 | Dim i As Long |
---|
335 | For i = 0 To ELM(c) |
---|
336 | Repeat->Add(x) |
---|
337 | Next |
---|
338 | End Function |
---|
339 | |
---|
340 | Static Function Synchronized(l As ArrayList) As ArrayList |
---|
341 | ' TODO: 実装 |
---|
342 | End Function |
---|
343 | |
---|
344 | Static Function Synchronized(l As IList) As IList |
---|
345 | Return Synchronized(Adapter(l)) |
---|
346 | End Function |
---|
347 | |
---|
348 | End Class |
---|
349 | |
---|
350 | /* |
---|
351 | Class ArrayList_Element |
---|
352 | Public |
---|
353 | Sub ArrayList_Element() |
---|
354 | Init(0) |
---|
355 | End Sub |
---|
356 | Sub ArrayList_Element(c As Long) |
---|
357 | Init(c) |
---|
358 | End Sub |
---|
359 | |
---|
360 | Sub ~ArrayList_Element() |
---|
361 | free(Elm) |
---|
362 | End Sub |
---|
363 | |
---|
364 | Sub Init(c As Long) |
---|
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 |
---|
395 | End Class |
---|
396 | */ |
---|