Changeset 579
- Timestamp:
- Aug 8, 2008, 12:15:30 AM (16 years ago)
- Location:
- trunk/ab5.0
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab
r560 r579 4 4 Namespace Generic 5 5 6 Namespace Detail 7 Class PointerEnumerator<T> 8 Implements IEnumerator<T> 9 Public 10 Sub PointerEnumerator(ptr As *T, count As Long) 11 p = ptr 12 n = count 13 cur = -1 14 End Sub 15 16 Function MoveNext() As Boolean 17 cur++ 18 MoveNext = (cur <> n) 19 End Function 20 21 Sub Reset() 22 n = -1 23 End Sub 24 25 Function Current() As T 26 Current = p[cur] 27 End Function 28 29 Virtual Sub Dispose() 30 p = NULL 31 n = 0 32 cur = -1 33 End Sub 34 Private 35 p As *T 36 n As Long 37 cur As Long 38 End Class 39 End Namespace 40 6 41 Class List<T> 7 42 Inherits IList<T> 8 43 9 44 items As *T 10 sizeAs Long45 capacity As Long 11 46 count As Long 12 13 currentIndexForEnumerator As Long14 47 15 48 Sub Realloc( allocateSize As Long ) 16 49 items = realloc( items, allocateSize * SizeOf(T) ) 17 End Sub18 19 Sub _Initialize( capacity = 0 As Long ) 20 items = GC_malloc( 1)21 22 ' 列挙子の位置を初期化23 Reset()24 25 If capacity > 0 Then26 This.size = capacity50 capacity = allocateSize 51 End Sub 52 53 Sub _Initialize( capacity As Long ) 54 This.items = GC_malloc( capacity * SizeOf(T) ) 55 This.capacity = capacity 56 End Sub 57 58 Sub SetLeastCapacity( capacity As Long ) 59 If This.capacity < capacity Then 27 60 Realloc(capacity) 28 61 End If 29 62 End Sub 30 31 63 Public 32 64 Sub List() 33 _Initialize( )65 _Initialize(16) 34 66 End Sub 35 67 … … 57 89 End Sub 58 90 59 Sub ~List()60 End Sub61 62 91 /*! 63 92 @brief インデクサ … … 67 96 */ 68 97 Override Sub Operator[] ( index As Long, item As T ) 69 items[index]=item 98 If 0 <= index And index < capacity Then 99 items[index]=item 100 Else 101 Throw New ArgumentOutOfRangeException("index") 102 End If 70 103 End Sub 71 104 Override Function Operator[] ( index As Long ) As T 72 Return items[index] 105 If 0 <= index And index < capacity Then 106 Return items[index] 107 Else 108 Throw New ArgumentOutOfRangeException("index") 109 End If 73 110 End Function 74 111 … … 97 134 End Function 98 135 136 Const Function Capacity() As Long 137 Return This.capacity 138 End Function 139 140 Sub Capacity(c As Long) 141 If c <> capacity Then 142 If c < capacity Then 143 Throw New ArgumentOutOfRangeException("capacity") 144 Else 145 Realloc(c) 146 End If 147 End If 148 End Sub 149 99 150 /*! 100 151 @brief Listが読み込み専用かどうかを取得する … … 117 168 */ 118 169 Override Sub Add( item As T ) 119 If This.size < (This.count + 1) Then 120 This.size++ 121 Realloc( This.size ) 122 End If 170 SetLeastCapacity(This.count + 1) 123 171 This.items[ This.count ] = item 124 172 This.count++ … … 162 210 */ 163 211 Override Sub Insert( index As Long, item As T ) 164 If This.size < (This.count + 1) Then 165 This.size++ 166 Realloc( This.size ) 167 End If 212 SetLeastCapacity(This.count + 1) 168 213 memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (This.count - index)*SizeOf(T) ) 169 214 items[index] = item … … 183 228 End If 184 229 185 RemoveAt( index )230 removeAtImpl( index ) 186 231 Return True 187 232 End Function … … 193 238 */ 194 239 Override Sub RemoveAt( index As Long ) 240 If 0 <= index And index < capacity Then 241 removeAtImpl( index ) 242 Else 243 Throw New ArgumentOutOfRangeException("index") 244 End If 245 End Sub 246 Private 247 Sub removeAtImpl( index As Long ) 195 248 memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (This.count - (index+1))*SizeOf(T) ) 196 249 This.count-- 197 End Sub 250 End Sub 251 252 Public 198 253 199 254 /*! … … 204 259 */ 205 260 Override Function ToString() As String 206 Dim string As String261 Dim string = New Text.StringBuilder 207 262 Dim i As Long 208 263 For i = 0 To ELM( This.count ) 209 string += This[i].ToString() + Ex"\r\n"264 string.Append(This[i]).Append(Ex"\r\n") 210 265 Next 211 Return string + This[i].ToString()266 Return string.ToString 212 267 End Function 213 268 … … 218 273 */ 219 274 Sub TrimExcess () 220 Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items ) 221 If ( This.size ) * 0.9 > This.count Then 222 This.size = This.count 223 Realloc( This.size ) 275 If ( This.capacity ) * 0.9 > This.count Then 276 Realloc( This.count ) 224 277 End If 225 278 End Sub … … 232 285 */ 233 286 Override Function GetEnumerator() As IEnumerator<T> 234 Return This 235 End Function 236 237 /*! 238 @brief イテレータのカレントインデックスを増加させる 239 @author Daisuke Yamamoto 240 @date 2007/11/19 241 @return 上限に達していなければTrueが、達していればFalseが返る 242 */ 243 Override Function MoveNext() As Boolean 244 If currentIndexForEnumerator + 1 >= size Then 245 ' 上限に達した 246 Return False 247 End If 248 249 currentIndexForEnumerator ++ 250 Return True 251 End Function 252 253 /*! 254 @brief イテレータをリセットする 255 @author Daisuke Yamamoto 256 @date 2007/11/19 257 */ 258 Override Sub Reset() 259 currentIndexForEnumerator = -1 260 End Sub 261 262 /*! 263 @brief イテレータのカレントオブジェクトを取得する 264 @author Daisuke Yamamoto 265 @date 2007/11/19 266 @return カレントオブジェクトが返る 267 */ 268 Override Function Current() As T 269 If currentIndexForEnumerator = -1 Then 270 ' MoveNextメソッドがReset後、一度も呼び出されなかった 271 Return Nothing 272 End If 273 Return items[currentIndexForEnumerator] 287 Return New Detail.PointerEnumerator(items, count) 274 288 End Function 275 289 End Class -
trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Queue.ab
r577 r579 18 18 Sub Queue () 19 19 This.items = GC_malloc( 1 ) 20 Reset()21 20 End Sub 22 21 23 22 Sub Queue ( capacity As Long ) 24 23 This.items = GC_malloc( 1 ) 25 Reset()26 24 If capacity > 0 Then 27 25 This.size = capacity … … 64 62 */ 65 63 Override Function GetEnumerator () As IEnumerator<T> 66 Return This64 Return New Detail.PointerEnumerator(items, count) 67 65 End Function 68 66 … … 116 114 End If 117 115 End Sub 118 119 120 '------------------------121 ' IEnumeratorの実装122 '------------------------123 currentIndexForEnumerator As Long124 125 Override Function Current () As T126 If This.currentIndexForEnumerator = -1 Then127 ' MoveNextメソッドがReset後、一度も呼び出されなかった128 Return Nothing129 End If130 Return This.items[ This.currentIndexForEnumerator ]131 End Function132 133 Override Function MoveNext() As Boolean134 If This.currentIndexForEnumerator + 1 >= This.size Then135 ' 上限に達した136 Return False137 End If138 139 This.currentIndexForEnumerator ++140 Return True141 End Function142 143 Override Sub Reset ()144 This.currentIndexForEnumerator = -1145 End Sub146 116 End Class 147 117 -
trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Stack.ab
r577 r579 18 18 Sub Stack () 19 19 This.items = GC_malloc( 1 ) 20 Reset()21 20 End Sub 22 21 23 22 Sub Stack ( capacity As Long ) 24 23 This.items = GC_malloc( 1 ) 25 Reset()26 24 If capacity > 0 Then 27 25 This.size = capacity … … 64 62 */ 65 63 Override Function GetEnumerator () As IEnumerator<T> 66 Return This64 Return New Detail.PointerEnumerator(items, count) 67 65 End Function 68 66 … … 117 115 End If 118 116 End Sub 119 120 121 '------------------------122 ' IEnumeratorの実装123 '------------------------124 currentIndexForEnumerator As Long125 126 Override Function Current () As T127 If This.currentIndexForEnumerator = -1 Then128 ' MoveNextメソッドがReset後、一度も呼び出されなかった129 Return Nothing130 End If131 Return This.items[ This.currentIndexForEnumerator ]132 End Function133 134 Override Function MoveNext() As Boolean135 If This.currentIndexForEnumerator + 1 >= This.count Then136 ' 上限に達した137 Return False138 End If139 140 This.currentIndexForEnumerator ++141 Return True142 End Function143 144 Override Sub Reset ()145 This.currentIndexForEnumerator = -1146 End Sub147 117 End Class 148 118 -
trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/misc.ab
r577 r579 10 10 11 11 Interface IEnumerator<T> 12 Inherits IDisposable 12 13 ' Methods 13 14 Function MoveNext () As Boolean … … 19 20 20 21 Class ICollection<T> 21 Implements IEnumerable<T> , IEnumerator<T>22 Implements IEnumerable<T> 22 23 Public 23 24 ' Property
Note:
See TracChangeset
for help on using the changeset viewer.