'Classes/System/Collections/Generic/Dictionary.ab

'#require <Classes/System/Collections/Generic/EqualityComparer.ab>

Namespace System
Namespace Collections
Namespace Generic

Namespace Detail
	Class Pair
	Public
		Sub Pair(key As Object, value As Object)
			Key = key
			Value = value
		End Sub

		Key As Object
		Value As Object
	End Class
End Namespace 'Detail

Class Dictionary<Key, T>
Public
	'Constructor
	Sub Dictionary()
		initialize(23)
	End Sub
'	Sub Dictionary(d As IDictionary<Key, T>)
'	Sub Dictionary(comparer As IEqualityComparer<Key>)
	Sub Dictionary(capacity As Long)
		initialize(capacity)
	End Sub
'	Sub Dictionary(d As IDictionary<Key, T>, comparer As IEqualityComparer<Key>)
'	Sub Dictionary(capacity As Long, comparer As IEqualityComparer<Key>)

	'Properties
'	Function Comparer() As IEqualityComparer<Key>
'	Virtual Function Count() As Long
'	End Function

	Virtual Function Item(key As Key) As T
		If Object.ReferenceEquals(key, Nothing) Then
			Throw New ArgumentNullException("key")
		End If

		Dim hash = getHash(key)
		Dim a = al[hash] As ArrayList

		If Not Object.ReferenceEquals(a, Nothing) Then
			Dim i As Long
			For i = 0 To ELM(a.Count)
				Dim pair = a[i] As Detail.Pair
				If pair.Key.Equals(key) Then
					Return pair.Value
				End If
			Next
		End If

		Throw New KeyNotFoundException
	End Function

	Virtual Sub Item(key As Key, value As T)
		If Object.ReferenceEquals(key, Nothing) Then
			'Throw ArgumentNullError
			Exit Sub
		End If

		Dim hash = getHash(key)
		Dim a = al[hash] As ArrayList

		If Object.ReferenceEquals(a, Nothing) Then
			a = New ArrayList
			al[hash] = a
		Else
			Dim i As Long
			For i = 0 To ELM(a.Count)
				Dim pair = a[i] As Detail.Pair
				If pair.Key.Equals(key) Then
					pair.Value = value
				End If
			Next
		End If

		a.Add(New Detail.Pair(key, value))
	End Sub

'	Function Keys() As KeyCollection
'	Function Values() As ValuesCollection
/*
	'Operators
'	Function Operator [](key As Key) As T
	Function Operator [](key As Key) As Object
		Return Item[key]
	End Function

	Sub Operator []=(key As Key, value As T)
'		Item[key] = vaule
	End Sub
*/
	'Methods
	Sub Add(key As Key, value As T)
		If Object.ReferenceEquals(key, Nothing) Then
			'Throw ArgumentNullError
			Exit Sub
		End If

		Dim hash = getHash(key)
		Dim a = al[hash] As ArrayList

		If Object.ReferenceEquals(a, Nothing) Then
			a = New ArrayList
			al[hash] = a
		Else
			Dim i As Long
			For i = 0 To ELM(a.Count)
				Dim pair = a[i] As Detail.Pair
				If pair.Key.Equals(key) Then
					'Throw ArgumentError
				End If
			Next
		End If

		a.Add(New Detail.Pair(key, value))

		count++
	End Sub

	Sub Clear()
		Dim i As Long
		For i = 0 To ELM(al.Count)
			al[i] = Nothing
		Next
	End Sub

	Function Count() As Long
		Return count
	End Function

'	Function ContainsKey(key As Key) As Boolean
'	End Function
'	Function ContainsValue(value As T) As Boolean
'	End Function
'	Function GetEnumerator() As Enumerator
'	Function Remove(key As Key) As Boolean
'	End Function
'	Function TryGetValue(key As Key, ByRef value As T) As Boolean
'	End Function

	'Classses
'	Class KeyCollection
'	Class ValuesCollection
'	Class Enumerator

Private

'	comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>

	Sub initialize(c As Long)
		al = ArrayList.Repeat(Nothing, c)
		count = 0
	End Sub

	Function getHash(key As Object) As Long
		Return (key.GetHashCode As DWord) Mod al.Count
	End Function

	al As ArrayList
	count As Long

End Class

End Namespace 'Generic
End Namespace 'Collections
End Namespace 'System
