source: trunk/Include/Classes/System/Collections/Generic/List.ab@ 396

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

Foreachを試験的に実装。
ジェネリクスインターフェイスをサポートした。

File size: 4.2 KB
Line 
1
2Namespace System
3Namespace Collections
4Namespace Generic
5
6Interface IEnumerable<T>
7 ' Method
8 Function GetEnumerator() As IEnumerator<T>
9End Interface
10
11Interface IEnumerator<T>
12 ' Methods
13 Function MoveNext() As Boolean
14 Sub Reset()
15 ' Property
16 Function Current() As T
17End Interface
18
19Class List<T>
20 Implements IEnumerable<T>, IEnumerator<T>
21 items As *T
22 size As Long
23
24 currentIndexForEnumerator As Long
25
26 Sub Realloc( allocateSize As Long )
27 items = realloc( items, allocateSize * SizeOf(VoidPtr) )
28 End Sub
29
30Public
31 Sub List()
32 items = GC_malloc( 1 )
33
34 ' 列挙子の位置を初期化
35 Reset()
36 End Sub
37 Sub ~List()
38 End Sub
39
40 /*!
41 @brief インデクサ
42 @author Daisuke Yamamoto
43 @date 2007/08/22
44 @param インデックス
45 */
46 Function Operator[] ( index As Long ) As T
47 Return items[index]
48 End Function
49
50
51 '----------------------------------------------------------------
52 ' パブリック プロパティ
53 '----------------------------------------------------------------
54
55 /*!
56 @brief Listに格納されている要素の数を取得する
57 @author Daisuke Yamamoto
58 @date 2007/08/22
59 */
60 Function Count() As Long
61 Return size
62 End Function
63
64
65 '----------------------------------------------------------------
66 ' パブリック メソッド
67 '----------------------------------------------------------------
68
69 /*!
70 @brief Listの末端に要素を追加する
71 @author Daisuke Yamamoto
72 @date 2007/08/22
73 @param 追加するオブジェクト
74 */
75 Sub Add( item As T )
76 Realloc( size + 1 )
77
78 items[size] = item
79 size++
80 End Sub
81
82 /*!
83 @brief Listからすべての要素を削除する
84 @author Daisuke Yamamoto
85 @date 2007/08/22
86 */
87 Sub Clear()
88 size = 0
89 End Sub
90
91 /*!
92 @brief List内から、最初に出現する値のインデックスを取得する
93 @author Daisuke Yamamoto
94 @date 2007/08/22
95 @return インデックス(見つからなかったときは-1)
96 */
97 Function IndexOf( item As T ) As Long
98 Dim i As Long
99 For i = 0 To ELM(size)
100 If items[i].Equals(item) Then
101 Return i
102 End If
103 Next
104 Return -1
105 End Function
106
107 /*!
108 @brief List内の指定したインデックスの位置に要素を挿入する
109 @author Daisuke Yamamoto
110 @date 2007/08/22
111 */
112 Sub Insert( index As Long, item As T )
113 Realloc( size + 1 )
114 memmove( items + (index+1)*SizeOf(VoidPtr), items + index*SizeOf(VoidPtr), (size-index)*SizeOf(VoidPtr) )
115 items[index] = item
116 size++
117 End Sub
118
119 /*!
120 @brief List内で最初に出現する値を削除する
121 @author Daisuke Yamamoto
122 @date 2007/09/28
123 @return 値が削除されたときはTrue、されなかったときはFlaseが返る
124 */
125 Function Remove( item As T ) As Boolean
126 Dim index = IndexOf( item )
127 If index = -1 Then
128 Return False
129 End If
130
131 RemoveAt( index )
132 Return True
133 End Function
134
135 /*!
136 @brief List内の指定したインデックスの要素を削除する
137 @author Daisuke Yamamoto
138 @date 2007/10/03
139 @return 値が削除されたときはTrue、されなかったときはFlaseが返る
140 */
141 Sub RemoveAt( index As Long )
142 memmove( items + index*SizeOf(VoidPtr), items + (index+1)*SizeOf(VoidPtr), (size-(index+1))*SizeOf(VoidPtr) )
143 Realloc( size - 1 )
144 size--
145 End Sub
146
147 /*!
148 @brief IEnumeratorインターフェイスを取得する
149 @author Daisuke Yamamoto
150 @date 2007/11/19
151 @return IEnumeratorインターフェイスが返る
152 */
153 Function GetEnumerator() As IEnumerator<T>
154 Return This
155 End Function
156
157 /*!
158 @brief イテレータのカレントインデックスを増加させる
159 @author Daisuke Yamamoto
160 @date 2007/11/19
161 @return 上限に達していなければTrueが、達していればFalseが返る
162 */
163 Function MoveNext() As Boolean
164 If currentIndexForEnumerator + 1 >= size Then
165 ' 上限に達した
166 Return False
167 End If
168
169 currentIndexForEnumerator ++
170 Return True
171 End Function
172
173 /*!
174 @brief イテレータをリセットする
175 @author Daisuke Yamamoto
176 @date 2007/11/19
177 */
178 Sub Reset()
179 currentIndexForEnumerator = -1
180 End Sub
181
182 /*!
183 @brief イテレータのカレントオブジェクトを取得する
184 @author Daisuke Yamamoto
185 @date 2007/11/19
186 @return カレントオブジェクトが返る
187 */
188 Function Current() As T
189 If currentIndexForEnumerator = -1 Then
190 ' MoveNextメソッドがReset後、一度も呼び出されなかった
191 Return Nothing
192 End If
193 Return items[currentIndexForEnumerator]
194 End Function
195End Class
196
197End Namespace
198End Namespace
199End Namespace
Note: See TracBrowser for help on using the repository browser.