source: Include/Classes/System/Collections/ArrayList.ab@ 195

Last change on this file since 195 was 195, checked in by dai, 17 years ago

オブジェクトの循環参照を許容した(構造体はダメ)。
抽象クラスをメンバの型に指定できるようにした。
メンバがオブジェクトだったとき、自動的にNewするのをやめ、初期値としてNothingを指定するようにした。

【ArrayListの改良】
・ArrayList_Elementを廃止し、実装をArrayListのprivateに置いた。
・一通りのパラメータを*ObjectからObjectへ変更した。

【その他】
・TypeInfo改良中...
・Objectクラスに実行時型情報用のtypeInfoメンバを追加した。

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