source: trunk/Include/Classes/System/Collections/ArrayList.ab@ 440

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

戻り値の型が定義されていない箇所を修正。

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