source: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab @ 526

Last change on this file since 526 was 526, checked in by OverTaker, 15 years ago

List,TimeSpan?.ToString?()実装。
Console.WriteのObject型のオーバーロードを追加。

File size: 4.6 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(T) )
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    @brief  ポインタ型へのキャスト
52    @author Daisuke Yamamoto
53    @date   2007/08/22
54    @param  インデックス
55    */
56    Function Operator() As *T
57        Return items
58    End Function
59
60
61    '----------------------------------------------------------------
62    ' パブリック プロパティ
63    '----------------------------------------------------------------
64
65    /*!
66    @brief  Listに格納されている要素の数を取得する
67    @author Daisuke Yamamoto
68    @date   2007/08/22
69    */
70    Function Count() As Long
71        Return size
72    End Function
73
74
75    '----------------------------------------------------------------
76    ' パブリック メソッド
77    '----------------------------------------------------------------
78
79    /*!
80    @brief  Listの末端に要素を追加する
81    @author Daisuke Yamamoto
82    @date   2007/08/22
83    @param  追加するオブジェクト
84    */
85    Sub Add( item As T )
86        Realloc( size + 1 )
87
88        items[size] = item
89        size++
90    End Sub
91
92    /*!
93    @brief  Listからすべての要素を削除する
94    @author Daisuke Yamamoto
95    @date   2007/08/22
96    */
97    Sub Clear()
98        size = 0
99    End Sub
100
101    /*!
102    @brief  List内から、最初に出現する値のインデックスを取得する
103    @author Daisuke Yamamoto
104    @date   2007/08/22
105    @return インデックス(見つからなかったときは-1)
106    */
107    Function IndexOf( item As T ) As Long
108        Dim i As Long
109        For i = 0 To ELM(size)
110            If items[i].Equals(item) Then
111                Return i
112            End If
113        Next
114        Return -1
115    End Function
116
117    /*!
118    @brief  List内の指定したインデックスの位置に要素を挿入する
119    @author Daisuke Yamamoto
120    @date   2007/08/22
121    */
122    Sub Insert( index As Long, item As T )
123        Realloc( size + 1 )
124        memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (size-index)*SizeOf(T) )
125        items[index] = item
126        size++
127    End Sub
128
129    /*!
130    @brief  List内で最初に出現する値を削除する
131    @author Daisuke Yamamoto
132    @date   2007/09/28
133    @return 値が削除されたときはTrue、されなかったときはFlaseが返る
134    */
135    Function Remove( item As T ) As Boolean
136        Dim index = IndexOf( item )
137        If index = -1 Then
138            Return False
139        End If
140
141        RemoveAt( index )
142        Return True
143    End Function
144
145    /*!
146    @brief  List内の指定したインデックスの要素を削除する
147    @author Daisuke Yamamoto
148    @date   2007/10/03
149    @return 値が削除されたときはTrue、されなかったときはFlaseが返る
150    */
151    Sub RemoveAt( index As Long )
152        memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (size-(index+1))*SizeOf(T) )
153        Realloc( size - 1 )
154        size--
155    End Sub
156
157    /*!
158    @brief  Listの要素を文字列で返す
159    @author OverTaker
160    @date   2008/6/26
161    @return 文字列
162    */
163    Override Function ToString() As String
164        Dim object As Object
165        Dim s As String
166        Foreach object In This
167            s += object.ToString() + Ex"\r\n"
168        Next
169        Return s
170    End Function
171
172    /*!
173    @brief  IEnumeratorインターフェイスを取得する
174    @author Daisuke Yamamoto
175    @date   2007/11/19
176    @return IEnumeratorインターフェイスが返る
177    */
178    Function GetEnumerator() As IEnumerator<T>
179        Return This
180    End Function
181
182    /*!
183    @brief  イテレータのカレントインデックスを増加させる
184    @author Daisuke Yamamoto
185    @date   2007/11/19
186    @return 上限に達していなければTrueが、達していればFalseが返る
187    */
188    Function MoveNext() As Boolean
189        If currentIndexForEnumerator + 1 >= size Then
190            ' 上限に達した
191            Return False
192        End If
193
194        currentIndexForEnumerator ++
195        Return True
196    End Function
197
198    /*!
199    @brief  イテレータをリセットする
200    @author Daisuke Yamamoto
201    @date   2007/11/19
202    */
203    Sub Reset()
204        currentIndexForEnumerator = -1
205    End Sub
206
207    /*!
208    @brief  イテレータのカレントオブジェクトを取得する
209    @author Daisuke Yamamoto
210    @date   2007/11/19
211    @return カレントオブジェクトが返る
212    */
213    Function Current() As T
214        If currentIndexForEnumerator = -1 Then
215            ' MoveNextメソッドがReset後、一度も呼び出されなかった
216            Return Nothing
217        End If
218        Return items[currentIndexForEnumerator]
219    End Function
220End Class
221
222End Namespace
223End Namespace
224End Namespace
Note: See TracBrowser for help on using the repository browser.