[310] | 1 | 'Classes/System/Collections/Generic/Dictionary.ab
|
---|
| 2 |
|
---|
| 3 | '#require <Classes/System/Collections/Generic/EqualityComparer.ab>
|
---|
| 4 |
|
---|
| 5 | Namespace System
|
---|
| 6 | Namespace Collections
|
---|
| 7 | Namespace Generic
|
---|
| 8 |
|
---|
| 9 | Namespace Detail
|
---|
[313] | 10 | Class Pair
|
---|
[310] | 11 | Public
|
---|
[313] | 12 | Sub Pair(key As Object, value As Object)
|
---|
[310] | 13 | Key = key
|
---|
| 14 | Value = value
|
---|
| 15 | End Sub
|
---|
| 16 |
|
---|
[313] | 17 | Key As Object
|
---|
| 18 | Value As Object
|
---|
[310] | 19 | End Class
|
---|
| 20 | End Namespace 'Detail
|
---|
| 21 |
|
---|
| 22 | Class Dictionary<Key, T>
|
---|
| 23 | Public
|
---|
| 24 | 'Constructor
|
---|
| 25 | Sub Dictionary()
|
---|
| 26 | initialize(23)
|
---|
| 27 | End Sub
|
---|
| 28 | ' Sub Dictionary(d As IDictionary<Key, T>)
|
---|
| 29 | ' Sub Dictionary(comparer As IEqualityComparer<Key>)
|
---|
| 30 | Sub Dictionary(capacity As Long)
|
---|
| 31 | initialize(capacity)
|
---|
| 32 | End Sub
|
---|
| 33 | ' Sub Dictionary(d As IDictionary<Key, T>, comparer As IEqualityComparer<Key>)
|
---|
| 34 | ' Sub Dictionary(capacity As Long, comparer As IEqualityComparer<Key>)
|
---|
| 35 |
|
---|
| 36 | 'Properties
|
---|
| 37 | ' Function Comparer() As IEqualityComparer<Key>
|
---|
| 38 | ' Virtual Function Count() As Long
|
---|
| 39 | ' End Function
|
---|
| 40 |
|
---|
[312] | 41 | Virtual Function Item(key As Key) As T
|
---|
[310] | 42 | If Object.ReferenceEquals(key, Nothing) Then
|
---|
| 43 | 'Throw ArgumentNullError
|
---|
[366] | 44 | debug
|
---|
| 45 | Exit Function
|
---|
[310] | 46 | End If
|
---|
| 47 |
|
---|
[393] | 48 | Dim hash = getHash(key)
|
---|
[310] | 49 | Dim a = al[hash] As ArrayList
|
---|
| 50 |
|
---|
| 51 | If Not Object.ReferenceEquals(a, Nothing) Then
|
---|
| 52 | Dim i As Long
|
---|
| 53 | For i = 0 To ELM(a.Count)
|
---|
[393] | 54 | Dim pair = a[i] As Detail.Pair
|
---|
[310] | 55 | If pair.Key.Equals(key) Then
|
---|
| 56 | Return pair.Value
|
---|
| 57 | End If
|
---|
| 58 | Next
|
---|
| 59 | End If
|
---|
[403] | 60 |
|
---|
| 61 | Throw New KeyNotFoundException
|
---|
| 62 |
|
---|
[310] | 63 | Return Nothing
|
---|
| 64 | End Function
|
---|
| 65 |
|
---|
| 66 | Virtual Sub Item(key As Key, value As T)
|
---|
| 67 | If Object.ReferenceEquals(key, Nothing) Then
|
---|
| 68 | 'Throw ArgumentNullError
|
---|
| 69 | Exit Sub
|
---|
| 70 | End If
|
---|
| 71 |
|
---|
[393] | 72 | Dim hash = getHash(key)
|
---|
[310] | 73 | Dim a = al[hash] As ArrayList
|
---|
| 74 |
|
---|
| 75 | If Object.ReferenceEquals(a, Nothing) Then
|
---|
| 76 | a = New ArrayList
|
---|
| 77 | al[hash] = a
|
---|
| 78 | Else
|
---|
| 79 | Dim i As Long
|
---|
| 80 | For i = 0 To ELM(a.Count)
|
---|
[393] | 81 | Dim pair = a[i] As Detail.Pair
|
---|
[310] | 82 | If pair.Key.Equals(key) Then
|
---|
| 83 | pair.Value = value
|
---|
| 84 | End If
|
---|
| 85 | Next
|
---|
| 86 | End If
|
---|
| 87 |
|
---|
[393] | 88 | a.Add(New Detail.Pair(key, value))
|
---|
[310] | 89 | End Sub
|
---|
| 90 |
|
---|
| 91 | ' Function Keys() As KeyCollection
|
---|
| 92 | ' Function Values() As ValuesCollection
|
---|
| 93 | /*
|
---|
| 94 | 'Operators
|
---|
[313] | 95 | ' Function Operator [](key As Key) As T
|
---|
| 96 | Function Operator [](key As Key) As Object
|
---|
[310] | 97 | Return Item[key]
|
---|
| 98 | End Function
|
---|
| 99 |
|
---|
| 100 | Sub Operator []=(key As Key, value As T)
|
---|
| 101 | ' Item[key] = vaule
|
---|
| 102 | End Sub
|
---|
| 103 | */
|
---|
| 104 | 'Methods
|
---|
| 105 | Sub Add(key As Key, value As T)
|
---|
| 106 | If Object.ReferenceEquals(key, Nothing) Then
|
---|
| 107 | 'Throw ArgumentNullError
|
---|
| 108 | Exit Sub
|
---|
| 109 | End If
|
---|
| 110 |
|
---|
[393] | 111 | Dim hash = getHash(key)
|
---|
[310] | 112 | Dim a = al[hash] As ArrayList
|
---|
| 113 |
|
---|
| 114 | If Object.ReferenceEquals(a, Nothing) Then
|
---|
| 115 | a = New ArrayList
|
---|
| 116 | al[hash] = a
|
---|
| 117 | Else
|
---|
| 118 | Dim i As Long
|
---|
| 119 | For i = 0 To ELM(a.Count)
|
---|
[393] | 120 | Dim pair = a[i] As Detail.Pair
|
---|
[310] | 121 | If pair.Key.Equals(key) Then
|
---|
| 122 | 'Throw ArgumentError
|
---|
| 123 | End If
|
---|
| 124 | Next
|
---|
| 125 | End If
|
---|
| 126 |
|
---|
[393] | 127 | a.Add(New Detail.Pair(key, value))
|
---|
[366] | 128 |
|
---|
[393] | 129 | count++
|
---|
[310] | 130 | End Sub
|
---|
| 131 |
|
---|
| 132 | Sub Clear()
|
---|
| 133 | Dim i As Long
|
---|
| 134 | For i = 0 To ELM(al.Count)
|
---|
| 135 | al[i] = Nothing
|
---|
| 136 | Next
|
---|
| 137 | End Sub
|
---|
| 138 |
|
---|
[366] | 139 | Function Count() As Long
|
---|
| 140 | Return count
|
---|
| 141 | End Function
|
---|
| 142 |
|
---|
[310] | 143 | ' Function ContainsKey(key As Key) As Boolean
|
---|
| 144 | ' End Function
|
---|
| 145 | ' Function ContainsValue(value As T) As Boolean
|
---|
| 146 | ' End Function
|
---|
| 147 | ' Function GetEnumerator() As Enumerator
|
---|
| 148 | ' Function Remove(key As Key) As Boolean
|
---|
| 149 | ' End Function
|
---|
| 150 | ' Function TryGetValue(key As Key, ByRef value As T) As Boolean
|
---|
| 151 | ' End Function
|
---|
| 152 |
|
---|
| 153 | 'Classses
|
---|
| 154 | ' Class KeyCollection
|
---|
| 155 | ' Class ValuesCollection
|
---|
| 156 | ' Class Enumerator
|
---|
| 157 |
|
---|
| 158 | Private
|
---|
| 159 |
|
---|
| 160 | ' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
|
---|
| 161 |
|
---|
| 162 | Sub initialize(c As Long)
|
---|
| 163 | al = ArrayList.Repeat(Nothing, c)
|
---|
[366] | 164 | count = 0
|
---|
[310] | 165 | End Sub
|
---|
| 166 |
|
---|
[393] | 167 | Function getHash(key As Object) As Long
|
---|
| 168 | Return (key.GetHashCode As DWord) Mod al.Count
|
---|
| 169 | End Function
|
---|
| 170 |
|
---|
[310] | 171 | al As ArrayList
|
---|
[366] | 172 | count As Long
|
---|
[310] | 173 |
|
---|
| 174 | End Class
|
---|
| 175 |
|
---|
| 176 | End Namespace 'Generic
|
---|
| 177 | End Namespace 'Collections
|
---|
| 178 | End Namespace 'System
|
---|