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

Last change on this file since 118 was 118, checked in by イグトランス (egtra), 17 years ago

とりあえずおおよそ実装した(未テスト)

File size: 7.2 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 ArrrayList()
65 data.ArrayList(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.ArrayList_Element(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 Virtual Sub AddRange(ByRef c As ICollection)
91 ' TODO: 実装
92 End Sub
93
94 Const Virtual Function BinarySearch(x As *Object)
95 ' TODO: 実装
96 End Function
97
98 Const Virtual Function BinarySearch(x As *Object, ByRef c As IComparer) As Long
99 ' TODO: 実装
100 End Function
101
102 Const Virtual Function BinarySearch(index As Long, count As Long, x As *Object, ByRef c As IComparer) As Long
103 ' TODO: 実装
104 End Function
105
106 /*Override*/ Virtual Sub Clear()
107 data.Size = 0
108 End Sub
109
110 Virtual Function Clone() As *ArrayList
111 Dim p = New ArrayList(data.Size)
112 p->data.Size = data.Size
113 memcpy(p->data.Elm, This.data.Elm, SizeOf (*Object) * data.Size)
114 Return p
115 End Function
116
117 /*Const*/ /*Override*/ Virtual Function Contains(x As *Object) As Boolean
118 Dim i As Long
119 For i = 0 To ELM(data.Size)
120 If x->Equals(data.Elm[i]) Then
121 Return True
122 End If
123 Next
124 End Function
125
126 ' CopyTo
127 /*Const*/ /*Override*/ Virtual Function GetEnumerator() As *IEnumerator
128 Return GetEnumerator(index, count)
129 End Function
130
131 Const Virtual Function GetEnumerator(index As Long, count As Long) As *IEnumerator
132 ' TODO: 実装
133 End Function
134
135 Virtual Function GetRange(index As Long, count As Long) As *ArrayList
136 ' TODO: 実装
137 End Function
138
139 /*Const*/ Override Function IndexOf(x As *Object) As Long
140 Return IndexOf(x, 0, data.Size)
141 End Function
142
143 /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long) As Long
144 Return IndexOf(x, startIndex, data.Size - startIndex)
145 End Function
146
147 /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long, count As Long) As Long
148 Dim i As Long
149 Dim last = Math.Min(startIndex + count - 1, data.Size)
150 For i = startIndex To last
151 If x->Equals(ByVal data.Elm[i]) Then
152 Return i
153 End If
154 Next
155 End Function
156
157 Override Sub Insert(i As Long, x As *Object)
158 setLeastCapacity(data.Size + 1)
159 memmove(VarPtr(data.Elm[i + 1]), VarPtr(data.Elm[i]), SizeOf (*Object) * (data.Size - i))
160 data.Elm[i] = x
161 data.Size++
162 End Sub
163
164 Virtual Sub InsertRange(index As Long, ByRef c As ICollection)
165 ' TODO: 実装
166 End Sub
167
168 /*Const*/ Virtual Function LastIndexOf(x As *Object) As Long
169 LastIndexOf(x, 0, data.Size)
170 End Function
171
172 /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long) As Long
173 Return LastIndexOf(x, startIndex, data.Size - startIndex)
174 End Function
175
176 /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long, count As Long) As Long
177 ' TODO: 実装
178 End Function
179
180 Override Sub Remove(x As *Object)
181 Dim i = IndexOf(x)
182 If i > 0 Then
183 RemoveAt(i)
184 End If
185 End Sub
186
187 Override Sub RemoveAt(i As Long)
188 RemoveRange(i, 1)
189 End Sub
190
191 Virtual Sub RemoveRange(i As Long, count As Long)
192 Dim removeLastPos = i + count
193 memmove(VarPtr(data.Elm[i]), VarPtr(data.Elm[removeLastPos]), SizeOf (*Object) * (data.Size - removeLastPos))
194 End Sub
195
196 Virtual Sub Reverse()
197 Reverse(0, data.Size)
198 End Sub
199
200 Virtual Sub Reverse(startIndex As Long, count As Long)
201 ' TODO: 実装
202 End Sub
203
204 Virtual Sub SetRange(index As Long, ByRef c As ICollection)
205 ' TODO: 実装
206 End Sub
207
208 Virtual Sub Sort()
209 ' TODO: 実装
210 End Sub
211
212 Virtual Sub Sort(ByRef c As IComparer)
213 Sort(0, data.Size, c)
214 End Sub
215
216 Virtual Sub Sort(index As Long, count As Long, ByRef c As IComparer)
217 ' TODO: 実装
218 End Sub
219
220 ' ToArray
221 Virtual Sub TrimToSize()
222 reallocBuffer(data.Size)
223 End Sub
224
225 /*Override*/ Virtual Function ToString() As String
226 Return "System.Collections.ArrayList"
227 End Function
228
229 ' --------------------------------
230 ' static methods
231 Static Function Adapter(ByRef l As IList) As *ArrayList
232 ' TODO: 実装
233 End Function
234
235 Static Function FixedSize(ByRef l As ArrayList) As *ArrayList
236 ' TODO: 実装
237 End Function
238
239 Static Function FixedSize(ByRef l As IList) As *IList
240 Return FixedSize(Adapter(VarPtr(i)))
241 End Function
242
243 Static Function ReadOnly(ByRef l As ArrayList) As *ArrayList
244 ' TODO: 実装
245 End Function
246
247 Static Function ReadOnly(ByRef l As IList) As *IList
248 Return ReadOnly(Adapter(VarPtr(i)))
249 End Function
250
251 Static Function Repeat(ByRef x As Object, c As Long) As *ArrayList
252 Repeat = New ArrayList(c)
253 Dim i As Long
254 For i = 0 To ELM(c)
255 Repeat->Add(VarPtr(x))
256 Next
257 End Function
258
259 Static Function Synchronized(ByRef l As ArrayList) As *ArrayList
260 ' TODO: 実装
261 End Function
262
263 Static Function Synchronized(ByRef l As IList) As *IList
264 Return Synchronized(Adapter(VarPtr(i)))
265 End Function
266
267Private
268 Sub setLeastCapacity(c As Long)
269 If data.Capacity < c Then
270 reallocBuffer(c)
271 End If
272 End Sub
273
274 Sub reallocBuffer(c As Long)
275 Dim newBuf As ArrayList_Element(c)
276 newBuf.Size = data.Size
277 memcpy(newBuf.Elm, data.Elm, SizeOf (*Object) * newBuf.Size)
278 newBuf.Swap(data)
279 End Sub
280
281 data As ArrayList_Element
282End Class
283
284Class ArrayList_Element
285Public
286 Sub ArrayList_Element(c As Long)
287 If c > 0 Then
288 Elm = malloc(SizeOf (*Object) * c)
289 If Elm = 0 Then
290 ' OutOfMemoryException
291 Debug
292 End If
293 Else
294 Elm = 0
295 End If
296 Size = 0
297 Capacity = c
298 End Sub
299
300 Sub ~ArrayList_Element()
301 free(Elm)
302 End Sub
303
304 Sub Swap(ByRef x As ArrayList_Element)
305 Dim tmpElm = x.Elm
306 x.Elm = This.Elm
307 This.Elm = tmpElm
308
309 Dim tmpSize = x.Size
310 x.Size = This.Size
311 This.Size = x.Size
312
313 Dim tmpCap = x.Capacity
314 x.Capacity = This.Capacity
315 This.Capacity = tmpCap
316 End Sub
317
318 Elm As **Object
319 Size As Long
320 Capacity As Long
321End Class
Note: See TracBrowser for help on using the repository browser.