'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
		Imports System.Collections.Generic.Detail

		If Object.ReferenceEquals(key, Nothing) Then
			'Throw ArgumentNullError
			Exit Sub
		End If

		Dim hash = key.GetHashCode Mod al.Count
		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 Pair
				If pair.Key.Equals(key) Then
					Return pair.Value
				End If
			Next
		End If
		'KeyNotFoundException
		Return Nothing

	End Function

	Virtual Sub Item(key As Key, value As T)
		Imports System.Collections.Generic.Detail

		If Object.ReferenceEquals(key, Nothing) Then
			'Throw ArgumentNullError
			Exit Sub
		End If

		Dim hash = key.GetHashCode Mod al.Count
		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 Pair
				If pair.Key.Equals(key) Then
					pair.Value = value
				End If
			Next
		End If

		a.Add(New 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)
		Imports System.Collections.Generic.Detail

		If Object.ReferenceEquals(key, Nothing) Then
			'Throw ArgumentNullError
			Exit Sub
		End If

		Dim hash = key.GetHashCode Mod al.Count
		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 Pair
				If pair.Key.Equals(key) Then
					'Throw ArgumentError
				End If
			Next
		End If

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

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

'	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)
		Dim e = al[0]
		Dim p = ObjPtr(e) As ULONG_PTR
	End Sub

	al As ArrayList

End Class

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