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

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

ArrayList?.Add(x As Object)メソッドを追加
ArrayList?.Cloneメソッドの中のNewをオブジェクトポインタに対して行うようにした(これはDeleteしないといけないので、ホントはダメ。要修正)。
ArrayList?.GetEnumerator?()メソッドでエラーが起きていたので、一時的にコメントアウト。
ArrayList?.Containsメソッドでエラーが起きていたので、一時的にコメントアウト。
・ArrayList_Elementクラスの初期化コードをInitメソッドに出した。
(※これはArrayListのみではありませんが、オブジェクトが参照型になったのでユーザに見せるメソッドの引数と戻り値でポインタは出さない方向でいきましょう)

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