' Classes/System/Collections/ArrayList.ab

Namespace System
Namespace Collections

Class ArrayList
	'Implements IList /*, ICollection, IEnumerable, ICloneable*/

	pObject As *Object
	size As Long
	capacity As Long

	Sub Init( capacity As Long )
		If size < 0 Then
			' Error
			debug
		End If

		This.size = 0
		This.capacity = size

		pObject = GC_malloc( SizeOf(*Object) * size )
		If pObject = 0 Then
			' OutOfMemoryException
			Debug
		End If
	End Sub

	Sub Realloc( newSize As Long )
'		If newSize > capacity Then
'			' miss!
'			Debug
'		End If

		Dim p = realloc( pObject, SizeOf(*Object) * newSize  )
		If p = 0 Then
			' OutOfMemoryException
			Debug
		Else
			pObject = p
		End If
		capacity = newSize
	End Sub

	Sub SetLeastCapacity( capacity As Long )
		If This.capacity < capacity Then
			Realloc(capacity)
		End If
	End Sub

Public

	'----------------------------------------------------------------
	' Properties
	'----------------------------------------------------------------

	/*Const*/ Virtual Function Capacity() As Long
		Return capacity
	End Function

	Virtual Sub Capacity(c As Long)
		If c <> capacity Then
			If c < size Then
				'Throw ArgumentOutOfRangeException
				Debug
			Else
				Realloc(c)
			End If
		End If
	End Sub

	/*Const*/ /*Override*/ Virtual Function Count() As Long
		Return size
	End Function

	/*Const*/ Function IsFixedSize() As Boolean
		Return False
	End Function

	/*Const*/ Function IsReadOnly() As Boolean
		Return False
	End Function

	/*Const*/ /*Override*/ Virtual Function IsSynchronized() As Boolean
		Return False
	End Function
	' SyncRoot

	' Operators
	/*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object
		If 0 <= index And index < size Then
			Return pObject[index]
		Else
			'Throw OutOfRangeException?
			Debug
		End If
	End Function
	
	/*Override*/ Virtual Sub Operator []=(index As Long, object As Object)
		If 0 <= index And index < size Then
			pObject[index] = object
		Else
			'Throw OutOfRangeException?
			Debug
		End If
	End Sub



	'----------------------------------------------------------------
	' Methods
	'----------------------------------------------------------------

	'   Constractors
	Sub ArrayList()
		Init( 16 )
	End Sub
/*
	Sub ArrayList(c As ICollection)
		' –¢ŽÀ‘•
		Debug
	End Sub
*/
	Sub ArrayList(c As Long)
		Init( c )
	End Sub

	'   Destractor
	Virtual Sub ~ArrayList()
	End Sub

'	Sub Operator =(a As ArrayList)

	/*Override*/ Virtual Function Add( object As Object ) As Long
		SetLeastCapacity( size + 1 )
		pObject[size] = object
		Add = size
		size++
	End Function
/*
	Const Virtual Function BinarySearch(x As Object) As Long
		' TODO: ŽÀ‘•
	End Function

	Const Virtual Function BinarySearch(x As Object, c As IComparer) As Long
		Return BinarySearch(0, size, x, c)
	End Function

	Const Virtual Function BinarySearch(index As Long, count As Long, x As Object, c As IComparer) As Long
		Dim l = index
		Dim r = index + ELM(count)
		While l <= r
			Dim mid = (l + r) >> 2
			Dim ret = c.Compare(pObject[mid], x)
			If ret = 0 Then
				Return mid
			Else If ret < 0 Then 'pObject[mid] < x
				If l = r Then
					If mid + 1 <= index + ELM(count) Then
						Return Not (mid + 1)
					Else
						Return count
					End If
				End If
				l = mid + 1
			Else 'pObject[mid] > x
				If l = r And mid + 1 <= index + ELM(count) Then
					Return Not mid
				End If
				r = mid - 1
			End If
		Wend
		If l = index Then
			Return Not index
		Else 'r = index + ELM(count)
			Return Not count
		End If
	End Function
*/
	/*Override*/ Virtual Sub Clear()
		size = 0
	End Sub

	Virtual Function Clone() As ArrayList
		Dim arrayList = New ArrayList( size )
		memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size )
		arrayList.size = This.size
		Return arrayList
	End Function

	/*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean
		Dim i As Long
		For i = 0 To ELM(size)
			If x->Equals(data.Elm[i]) Then
				Return True
			End If
		Next
	End Function*/

	' CopyTo

'	
'	/*Const*/ /*Override*/ Virtual Function GetEnumerator() As IEnumerator
'		'Return GetEnumerator(index, count)
'	End Function

'	Const Virtual Function GetEnumerator(index As Long, count As Long) As IEnumerator
'		' TODO: ŽÀ‘•
'	End Function

	Virtual Function GetRange(index As Long, count As Long) As ArrayList
		' TODO: ŽÀ‘•
	End Function

	/*Const*/ Function IndexOf(object As Object) As Long
		Return IndexOf(object, 0, size)
	End Function

	/*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long
		Return IndexOf(object, startIndex, size - startIndex)
	End Function

	/*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long
		Dim i As Long
		Dim last = System.Math.Min(startIndex + count - 1, size)
		For i = startIndex To last
			If object.Equals( pObject[i] ) Then
				Return i
			End If
		Next
		Return -1
	End Function

	Sub Insert(i As Long, object As Object)
		SetLeastCapacity(size + 1)
		memmove(VarPtr(pObject[i + 1]), VarPtr(pObject[i]), SizeOf (*Object) * (size - i))
		pObject[i] = object
		size++
	End Sub

	/*Const*/ Virtual Function LastIndexOf(object As Object) As Long
		LastIndexOf(object, 0, size)
	End Function

	/*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long
		Return LastIndexOf(object, startIndex, size - startIndex)
	End Function

	/*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long
		' TODO: ŽÀ‘•
	End Function

	Sub Remove(object As Object)
		Dim i = IndexOf(object)
		If i > 0 Then
			RemoveAt(i)
		End If
	End Sub

	Sub RemoveAt(i As Long)
		RemoveRange(i, 1)
	End Sub
	
	Virtual Sub RemoveRange(i As Long, count As Long)
		Dim removeLastPos = i + count
		memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos))
		size -= count
	End Sub

	Virtual Sub Reverse()
		Reverse(0, size)
	End Sub

	Virtual Sub Reverse(startIndex As Long, count As Long)
		' TODO: ŽÀ‘•
	End Sub

	Virtual Sub Sort()
		' TODO: ŽÀ‘•
	End Sub

	' ToArray
	Virtual Sub TrimToSize()
		Realloc(size)
	End Sub

	Override Function ToString() As String
		Return "System.Collections.ArrayList"
	End Function

	Static Function Repeat(x As Object, c As Long) As ArrayList
		Repeat = New ArrayList(c)
		Dim i As Long
		For i = 0 To ELM(c)
			Repeat.Add(x)
		Next
	End Function
End Class

End Namespace 'Collections
End Namespace 'System
