Namespace System Namespace Collections Namespace Generic Interface IEnumerable ' Method Function GetEnumerator() As IEnumerator End Interface Interface IEnumerator ' Methods Function MoveNext() As Boolean Sub Reset() ' Property Function Current() As T End Interface Class List Implements IEnumerable, IEnumerator items As *T size As Long currentIndexForEnumerator As Long Sub Realloc( allocateSize As Long ) items = realloc( items, allocateSize * SizeOf(T) ) End Sub Public Sub List() items = GC_malloc( 1 ) ' 列挙子の位置を初期化 Reset() End Sub Sub ~List() End Sub /*! @brief インデクサ @author Daisuke Yamamoto @date 2007/08/22 @param インデックス */ Function Operator[] ( index As Long ) As T Return items[index] End Function /*! @brief ポインタ型へのキャスト @author Daisuke Yamamoto @date 2007/08/22 @param インデックス */ Function Operator() As *T Return items End Function '---------------------------------------------------------------- ' パブリック プロパティ '---------------------------------------------------------------- /*! @brief Listに格納されている要素の数を取得する @author Daisuke Yamamoto @date 2007/08/22 */ Function Count() As Long Return size End Function '---------------------------------------------------------------- ' パブリック メソッド '---------------------------------------------------------------- /*! @brief Listの末端に要素を追加する @author Daisuke Yamamoto @date 2007/08/22 @param 追加するオブジェクト */ Sub Add( item As T ) Realloc( size + 1 ) items[size] = item size++ End Sub /*! @brief Listからすべての要素を削除する @author Daisuke Yamamoto @date 2007/08/22 */ Sub Clear() size = 0 End Sub /*! @brief List内から、最初に出現する値のインデックスを取得する @author Daisuke Yamamoto @date 2007/08/22 @return インデックス(見つからなかったときは-1) */ Function IndexOf( item As T ) As Long Dim i As Long For i = 0 To ELM(size) If items[i].Equals(item) Then Return i End If Next Return -1 End Function /*! @brief List内の指定したインデックスの位置に要素を挿入する @author Daisuke Yamamoto @date 2007/08/22 */ Sub Insert( index As Long, item As T ) Realloc( size + 1 ) memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (size-index)*SizeOf(T) ) items[index] = item size++ End Sub /*! @brief List内で最初に出現する値を削除する @author Daisuke Yamamoto @date 2007/09/28 @return 値が削除されたときはTrue、されなかったときはFlaseが返る */ Function Remove( item As T ) As Boolean Dim index = IndexOf( item ) If index = -1 Then Return False End If RemoveAt( index ) Return True End Function /*! @brief List内の指定したインデックスの要素を削除する @author Daisuke Yamamoto @date 2007/10/03 @return 値が削除されたときはTrue、されなかったときはFlaseが返る */ Sub RemoveAt( index As Long ) memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (size-(index+1))*SizeOf(T) ) Realloc( size - 1 ) size-- End Sub /*! @brief Listの要素を文字列で返す @author OverTaker @date 2008/6/26 @return 文字列 */ Override Function ToString() As String Dim string As String Dim i As Long For i = 0 To This.Count-2 string += This[i].ToString() + Ex"\r\n" Next Return string + This[i].ToString() End Function /*! @brief IEnumeratorインターフェイスを取得する @author Daisuke Yamamoto @date 2007/11/19 @return IEnumeratorインターフェイスが返る */ Function GetEnumerator() As IEnumerator Return This End Function /*! @brief イテレータのカレントインデックスを増加させる @author Daisuke Yamamoto @date 2007/11/19 @return 上限に達していなければTrueが、達していればFalseが返る */ Function MoveNext() As Boolean If currentIndexForEnumerator + 1 >= size Then ' 上限に達した Return False End If currentIndexForEnumerator ++ Return True End Function /*! @brief イテレータをリセットする @author Daisuke Yamamoto @date 2007/11/19 */ Sub Reset() currentIndexForEnumerator = -1 End Sub /*! @brief イテレータのカレントオブジェクトを取得する @author Daisuke Yamamoto @date 2007/11/19 @return カレントオブジェクトが返る */ Function Current() As T If currentIndexForEnumerator = -1 Then ' MoveNextメソッドがReset後、一度も呼び出されなかった Return Nothing End If Return items[currentIndexForEnumerator] End Function End Class End Namespace End Namespace End Namespace