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

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

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

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

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

File size: 7.9 KB
Line 
1' Classes/System/Collections/ArrayList.ab
2
3#require <Classes/System/Collections/misc.ab>
4
5Class 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    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
48Public
49
50    '----------------------------------------------------------------
51    ' Properties
52    '----------------------------------------------------------------
53
54    /*Const*/ Virtual Function Capacity() As Long
55        Return capacity
56    End Function
57
58    Virtual Sub Capacity(c As Long)
59        If c <> capacity Then
60            If c < size Then
61                'Throw ArgumentOutOfRangeException
62                Debug
63            Else
64                Realloc(c)
65            End If
66        End If
67    End Sub
68
69    /*Const*/ /*Override*/ Virtual Function Count() As Long
70        Return size
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
87    /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object
88        If 0 <= index And index < size Then
89            Return pObject[index]
90        Else
91            'Throw OutOfRangeException?
92            Debug
93        End If
94    End Function
95   
96    /*Override*/ Virtual Sub Operator []=(index As Long, object As Object)
97        If 0 <= index And index < size Then
98            pObject[i] = object
99        Else
100            'Throw OutOfRangeException?
101            Debug
102        End If
103    End Sub
104
105
106
107    '----------------------------------------------------------------
108    ' Methods
109    '----------------------------------------------------------------
110
111    '   Constractors
112    Sub ArrayList()
113        Init( 0 )
114    End Sub
115/*
116    Sub ArrayList(ByRef c As ICollection)
117        ' 未実装
118        Debug
119    End Sub
120*/
121    Sub ArrayList(c As Long)
122        Init( c )
123    End Sub
124
125    '   Destractor
126    Virtual Sub ~ArrayList()
127    End Sub
128
129'   Sub Operator =(ByRef a As ArrayList)
130
131    /*Override*/ Virtual Function Add( object As Object ) As Long
132        SetLeastCapacity( size + 1 )
133        pObject[size] = object
134        size++
135        Return size - 1
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()
155        size = 0
156    End Sub
157
158    Virtual Function Clone() As ArrayList
159        Dim arrayList = New ArrayList( size )
160        memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size )
161        Return arrayList
162    End Function
163
164    /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean
165        Dim i As Long
166        For i = 0 To ELM(size)
167            If x->Equals(data.Elm[i]) Then
168                Return True
169            End If
170        Next
171    End Function*/
172
173    ' CopyTo
174    /*Const*/ /*Override*/ Virtual Function GetEnumerator() As *IEnumerator
175        'Return GetEnumerator(index, count)
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
186    /*Const*/ Override Function IndexOf(object As Object) As Long
187        Return IndexOf(object, 0, size)
188    End Function
189
190    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long
191        Return IndexOf(object, startIndex, size - startIndex)
192    End Function
193
194    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long
195        Dim i As Long
196        Dim last = Math.Min(startIndex + count - 1, size)
197        For i = startIndex To last
198            If object.Equals( pObject[i] ) Then
199                Return i
200            End If
201        Next
202        Return -1
203    End Function
204
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++
210    End Sub
211
212    Virtual Sub InsertRange(index As Long, ByRef c As ICollection)
213        ' TODO: 実装
214    End Sub
215
216    /*Const*/ Virtual Function LastIndexOf(object As Object) As Long
217        LastIndexOf(object, 0, size)
218    End Function
219
220    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long
221        Return LastIndexOf(object, startIndex, size - startIndex)
222    End Function
223
224    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long
225        ' TODO: 実装
226    End Function
227
228    Override Sub Remove(object As Object)
229        Dim i = IndexOf(object)
230        If i > 0 Then
231            RemoveAt(i)
232            size--
233        End If
234    End Sub
235
236    Override Sub RemoveAt(i As Long)
237        RemoveRange(i, 1)
238        size--
239    End Sub
240   
241    Virtual Sub RemoveRange(i As Long, count As Long)
242        Dim removeLastPos = i + count
243        memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos))
244        size -= count
245    End Sub
246
247    Virtual Sub Reverse()
248        Reverse(0, size)
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)
264        Sort(0, size, c)
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()
273        Realloc(size)
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
320/*
321Class ArrayList_Element
322Public
323    Sub ArrayList_Element()
324        Init(0)
325    End Sub
326    Sub ArrayList_Element(c As Long)
327        Init(c)
328    End Sub
329
330    Sub ~ArrayList_Element()
331        free(Elm)
332    End Sub
333
334    Sub Init(c As Long)
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
365End Class
366*/
Note: See TracBrowser for help on using the repository browser.