source: trunk/ab5.0/ablib/src/Classes/System/Collections/ArrayList.ab

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

非Genericコレクションインタフェースの扱いを大幅に縮小。Queue/Stackの実装インタフェースの修正。

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