Changeset 195 for Include/Classes/System/Collections/ArrayList.ab
- Timestamp:
- Mar 30, 2007, 4:22:30 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Include/Classes/System/Collections/ArrayList.ab
r188 r195 5 5 Class ArrayList 6 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 7 48 Public 8 ' -------------------------------- 49 50 '---------------------------------------------------------------- 9 51 ' Properties 52 '---------------------------------------------------------------- 53 10 54 /*Const*/ Virtual Function Capacity() As Long 11 Return data.Capacity55 Return capacity 12 56 End Function 13 57 14 58 Virtual Sub Capacity(c As Long) 15 If c <> data.Capacity Then16 If c < data.Size Then59 If c <> capacity Then 60 If c < size Then 17 61 'Throw ArgumentOutOfRangeException 18 62 Debug 19 63 Else 20 reallocBuffer(c)64 Realloc(c) 21 65 End If 22 66 End If … … 24 68 25 69 /*Const*/ /*Override*/ Virtual Function Count() As Long 26 Return data.Size70 Return size 27 71 End Function 28 72 … … 41 85 42 86 ' Operators 43 /*Const*/ /*Override*/ Virtual Function Operator [](i As Long) As *Object44 If 0 <= i And i < data.Size Then45 Return data.Elm[i]87 /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object 88 If 0 <= index And index < size Then 89 Return pObject[index] 46 90 Else 47 91 'Throw OutOfRangeException? … … 50 94 End Function 51 95 52 /*Override*/ Virtual Sub Operator []=(i As Long, x As *Object)53 If 0 <= i And i < data.Size Then54 data.Elm[i] = 096 /*Override*/ Virtual Sub Operator []=(index As Long, object As Object) 97 If 0 <= index And index < size Then 98 pObject[i] = object 55 99 Else 56 100 'Throw OutOfRangeException? … … 59 103 End Sub 60 104 61 ' -------------------------------- 105 106 107 '---------------------------------------------------------------- 62 108 ' Methods 109 '---------------------------------------------------------------- 110 63 111 ' Constractors 64 112 Sub ArrayList() 65 data.Init(16)113 Init( 0 ) 66 114 End Sub 67 115 /* … … 72 120 */ 73 121 Sub ArrayList(c As Long) 74 data.Init(c)122 Init( c ) 75 123 End Sub 76 124 … … 81 129 ' Sub Operator =(ByRef a As ArrayList) 82 130 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++ 131 /*Override*/ Virtual Function Add( object As Object ) As Long 132 SetLeastCapacity( size + 1 ) 133 pObject[size] = object 134 size++ 135 Return size - 1 95 136 End Function 96 137 … … 112 153 113 154 /*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 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 123 162 End Function 124 163 125 164 /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean 126 165 Dim i As Long 127 For i = 0 To ELM( data.Size)166 For i = 0 To ELM(size) 128 167 If x->Equals(data.Elm[i]) Then 129 168 Return True … … 145 184 End Function 146 185 147 /*Const*/ Override Function IndexOf( x As *Object) As Long148 Return IndexOf( x, 0, data.Size)149 End Function 150 151 /*Const*/ Virtual Function IndexOf( x As *Object, startIndex As Long) As Long152 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 Long186 /*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 156 195 Dim i As Long 157 Dim last = Math.Min(startIndex + count - 1, data.Size)196 Dim last = Math.Min(startIndex + count - 1, size) 158 197 For i = startIndex To last 159 If x->Equals(ByVal data.Elm[i]) Then198 If object.Equals( pObject[i] ) Then 160 199 Return i 161 200 End If 162 201 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++ 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++ 170 210 End Sub 171 211 … … 174 214 End Sub 175 215 176 /*Const*/ Virtual Function LastIndexOf( x As *Object) As Long177 LastIndexOf( x, 0, data.Size)178 End Function 179 180 /*Const*/ Virtual Function LastIndexOf( x As *Object, startIndex As Long) As Long181 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 Long185 ' TODO: 実装 186 End Function 187 188 Override Sub Remove( x As *Object)189 Dim i = IndexOf( x)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) 190 230 If i > 0 Then 191 231 RemoveAt(i) 232 size-- 192 233 End If 193 234 End Sub … … 195 236 Override Sub RemoveAt(i As Long) 196 237 RemoveRange(i, 1) 238 size-- 197 239 End Sub 198 240 199 241 Virtual Sub RemoveRange(i As Long, count As Long) 200 242 Dim removeLastPos = i + count 201 memmove(VarPtr(data.Elm[i]), VarPtr(data.Elm[removeLastPos]), SizeOf (*Object) * (data.Size - removeLastPos)) 243 memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos)) 244 size -= count 202 245 End Sub 203 246 204 247 Virtual Sub Reverse() 205 Reverse(0, data.Size)248 Reverse(0, size) 206 249 End Sub 207 250 … … 219 262 220 263 Virtual Sub Sort(ByRef c As IComparer) 221 Sort(0, data.Size, c)264 Sort(0, size, c) 222 265 End Sub 223 266 … … 228 271 ' ToArray 229 272 Virtual Sub TrimToSize() 230 reallocBuffer(data.Size)273 Realloc(size) 231 274 End Sub 232 275 … … 273 316 End Function 274 317 275 Private276 Sub setLeastCapacity(c As Long)277 If data.Capacity < c Then278 reallocBuffer(c)279 End If280 End Sub281 282 Sub reallocBuffer(c As Long)283 Dim newBuf As ArrayList_Element(c)284 newBuf.Size = data.Size285 memcpy(newBuf.Elm, data.Elm, SizeOf (*Object) * newBuf.Size)286 newBuf.Swap(data)287 End Sub288 289 data As ArrayList_Element290 318 End Class 291 319 320 /* 292 321 Class ArrayList_Element 293 322 Public … … 335 364 Capacity As Long 336 365 End Class 366 */
Note:
See TracChangeset
for help on using the changeset viewer.