Index: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab
===================================================================
--- trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab	(revision 560)
+++ trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab	(revision 579)
@@ -4,32 +4,64 @@
 Namespace Generic
 
+Namespace Detail
+	Class PointerEnumerator<T>
+		Implements IEnumerator<T>
+	Public
+		Sub PointerEnumerator(ptr As *T, count As Long)
+			p = ptr
+			n = count
+			cur = -1
+		End Sub
+
+		Function MoveNext() As Boolean
+			cur++
+			MoveNext = (cur <> n)
+		End Function
+
+		Sub Reset()
+			n = -1
+		End Sub
+
+		Function Current() As T
+			Current = p[cur]
+		End Function
+
+		Virtual Sub Dispose()
+			p = NULL
+			n = 0
+			cur = -1
+		End Sub
+	Private
+		p As *T
+		n As Long
+		cur As Long
+	End Class
+End Namespace
+
 Class List<T>
 	Inherits IList<T>
 
 	items As *T
-	size As Long
+	capacity As Long
 	count As Long
-
-	currentIndexForEnumerator As Long
 
 	Sub Realloc( allocateSize As Long )
 		items = realloc( items, allocateSize * SizeOf(T) )
-	End Sub
-
-	Sub _Initialize( capacity = 0 As Long )
-		items = GC_malloc( 1 )
-
-		' 列挙子の位置を初期化
-		Reset()
-
-		If capacity > 0 Then
-			This.size = capacity
+		capacity = allocateSize
+	End Sub
+
+	Sub _Initialize( capacity As Long )
+		This.items = GC_malloc( capacity * SizeOf(T) )
+		This.capacity = capacity
+	End Sub
+
+	Sub SetLeastCapacity( capacity As Long )
+		If This.capacity < capacity Then
 			Realloc(capacity)
 		End If
 	End Sub
-
 Public
 	Sub List()
-		_Initialize()
+		_Initialize(16)
 	End Sub
 
@@ -57,7 +89,4 @@
 	End Sub
 
-	Sub ~List()
-	End Sub
-
 	/*!
 	@brief	インデクサ
@@ -67,8 +96,16 @@
 	*/
 	Override Sub Operator[] ( index As Long, item As T )
-		items[index]=item
+		If 0 <= index And index < capacity Then
+			items[index]=item
+		Else
+			Throw New ArgumentOutOfRangeException("index")
+		End If
 	End Sub
 	Override Function Operator[] ( index As Long ) As T
-		Return items[index]
+		If 0 <= index And index < capacity Then
+			Return items[index]
+		Else
+			Throw New ArgumentOutOfRangeException("index")
+		End If
 	End Function
 
@@ -97,4 +134,18 @@
 	End Function
 
+	Const Function Capacity() As Long
+		Return This.capacity
+	End Function
+
+	Sub Capacity(c As Long)
+		If c <> capacity Then
+			If c < capacity Then
+				Throw New ArgumentOutOfRangeException("capacity")
+			Else
+				Realloc(c)
+			End If
+		End If
+	End Sub
+	
 	/*!
 	@brief	Listが読み込み専用かどうかを取得する
@@ -117,8 +168,5 @@
 	*/
 	Override Sub Add( item As T )
-		If This.size < (This.count + 1) Then
-			This.size++
-			Realloc( This.size )
-		End If
+		SetLeastCapacity(This.count + 1)
 		This.items[ This.count ] = item
 		This.count++
@@ -162,8 +210,5 @@
 	*/
 	Override Sub Insert( index As Long, item As T )
-		If This.size < (This.count + 1) Then
-			This.size++
-			Realloc( This.size )
-		End If
+		SetLeastCapacity(This.count + 1)
 		memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (This.count - index)*SizeOf(T) )
 		items[index] = item
@@ -183,5 +228,5 @@
 		End If
 
-		RemoveAt( index )
+		removeAtImpl( index )
 		Return True
 	End Function
@@ -193,7 +238,17 @@
 	*/
 	Override Sub RemoveAt( index As Long )
+		If 0 <= index And index < capacity Then
+			removeAtImpl( index )
+		Else
+			Throw New ArgumentOutOfRangeException("index")
+		End If
+	End Sub
+Private
+	Sub removeAtImpl( index As Long )
 		memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (This.count - (index+1))*SizeOf(T) )
 		This.count--
-	End Sub
+	End Sub 
+
+Public
 
 	/*!
@@ -204,10 +259,10 @@
 	*/
 	Override Function ToString() As String
-		Dim string As String
+		Dim string = New Text.StringBuilder
 		Dim i As Long
 		For i = 0 To ELM( This.count )
-			string += This[i].ToString() + Ex"\r\n"
+			string.Append(This[i]).Append(Ex"\r\n")
 		Next
-		Return string + This[i].ToString()
+		Return string.ToString
 	End Function
 
@@ -218,8 +273,6 @@
 	*/
 	Sub TrimExcess ()
-		Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
-		If ( This.size ) * 0.9 > This.count Then
-			This.size = This.count
-			Realloc( This.size )
+		If ( This.capacity ) * 0.9 > This.count Then
+			Realloc( This.count )
 		End If
 	End Sub
@@ -232,44 +285,5 @@
 	*/
 	Override Function GetEnumerator() As IEnumerator<T>
-		Return This
-	End Function
-
-	/*!
-	@brief	イテレータのカレントインデックスを増加させる
-	@author	Daisuke Yamamoto
-	@date	2007/11/19
-	@return	上限に達していなければTrueが、達していればFalseが返る
-	*/
-	Override 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
-	*/
-	Override Sub Reset()
-		currentIndexForEnumerator = -1
-	End Sub
-
-	/*!
-	@brief	イテレータのカレントオブジェクトを取得する
-	@author	Daisuke Yamamoto
-	@date	2007/11/19
-	@return	カレントオブジェクトが返る
-	*/
-	Override Function Current() As T
-		If currentIndexForEnumerator = -1 Then
-			' MoveNextメソッドがReset後、一度も呼び出されなかった
-			Return Nothing
-		End If
-		Return items[currentIndexForEnumerator]
+		Return New Detail.PointerEnumerator(items, count)
 	End Function
 End Class
