[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
|
---|
[366] | 60 | debug
|
---|
[310] | 61 | 'KeyNotFoundException
|
---|
| 62 | Return Nothing
|
---|
| 63 | End Function
|
---|
| 64 |
|
---|
| 65 | Virtual Sub Item(key As Key, value As T)
|
---|
| 66 | If Object.ReferenceEquals(key, Nothing) Then
|
---|
| 67 | 'Throw ArgumentNullError
|
---|
| 68 | Exit Sub
|
---|
| 69 | End If
|
---|
| 70 |
|
---|
[393] | 71 | Dim hash = getHash(key)
|
---|
[310] | 72 | Dim a = al[hash] As ArrayList
|
---|
| 73 |
|
---|
| 74 | If Object.ReferenceEquals(a, Nothing) Then
|
---|
| 75 | a = New ArrayList
|
---|
| 76 | al[hash] = a
|
---|
| 77 | Else
|
---|
| 78 | Dim i As Long
|
---|
| 79 | For i = 0 To ELM(a.Count)
|
---|
[393] | 80 | Dim pair = a[i] As Detail.Pair
|
---|
[310] | 81 | If pair.Key.Equals(key) Then
|
---|
| 82 | pair.Value = value
|
---|
| 83 | End If
|
---|
| 84 | Next
|
---|
| 85 | End If
|
---|
| 86 |
|
---|
[393] | 87 | a.Add(New Detail.Pair(key, value))
|
---|
[310] | 88 | End Sub
|
---|
| 89 |
|
---|
| 90 | ' Function Keys() As KeyCollection
|
---|
| 91 | ' Function Values() As ValuesCollection
|
---|
| 92 | /*
|
---|
| 93 | 'Operators
|
---|
[313] | 94 | ' Function Operator [](key As Key) As T
|
---|
| 95 | Function Operator [](key As Key) As Object
|
---|
[310] | 96 | Return Item[key]
|
---|
| 97 | End Function
|
---|
| 98 |
|
---|
| 99 | Sub Operator []=(key As Key, value As T)
|
---|
| 100 | ' Item[key] = vaule
|
---|
| 101 | End Sub
|
---|
| 102 | */
|
---|
| 103 | 'Methods
|
---|
| 104 | Sub Add(key As Key, value As T)
|
---|
| 105 | If Object.ReferenceEquals(key, Nothing) Then
|
---|
| 106 | 'Throw ArgumentNullError
|
---|
| 107 | Exit Sub
|
---|
| 108 | End If
|
---|
| 109 |
|
---|
[393] | 110 | Dim hash = getHash(key)
|
---|
[310] | 111 | Dim a = al[hash] As ArrayList
|
---|
| 112 |
|
---|
| 113 | If Object.ReferenceEquals(a, Nothing) Then
|
---|
| 114 | a = New ArrayList
|
---|
| 115 | al[hash] = a
|
---|
| 116 | Else
|
---|
| 117 | Dim i As Long
|
---|
| 118 | For i = 0 To ELM(a.Count)
|
---|
[393] | 119 | Dim pair = a[i] As Detail.Pair
|
---|
[310] | 120 | If pair.Key.Equals(key) Then
|
---|
| 121 | 'Throw ArgumentError
|
---|
| 122 | End If
|
---|
| 123 | Next
|
---|
| 124 | End If
|
---|
| 125 |
|
---|
[393] | 126 | a.Add(New Detail.Pair(key, value))
|
---|
[366] | 127 |
|
---|
[393] | 128 | count++
|
---|
[310] | 129 | End Sub
|
---|
| 130 |
|
---|
| 131 | Sub Clear()
|
---|
| 132 | Dim i As Long
|
---|
| 133 | For i = 0 To ELM(al.Count)
|
---|
| 134 | al[i] = Nothing
|
---|
| 135 | Next
|
---|
| 136 | End Sub
|
---|
| 137 |
|
---|
[366] | 138 | Function Count() As Long
|
---|
| 139 | Return count
|
---|
| 140 | End Function
|
---|
| 141 |
|
---|
[310] | 142 | ' Function ContainsKey(key As Key) As Boolean
|
---|
| 143 | ' End Function
|
---|
| 144 | ' Function ContainsValue(value As T) As Boolean
|
---|
| 145 | ' End Function
|
---|
| 146 | ' Function GetEnumerator() As Enumerator
|
---|
| 147 | ' Function Remove(key As Key) As Boolean
|
---|
| 148 | ' End Function
|
---|
| 149 | ' Function TryGetValue(key As Key, ByRef value As T) As Boolean
|
---|
| 150 | ' End Function
|
---|
| 151 |
|
---|
| 152 | 'Classses
|
---|
| 153 | ' Class KeyCollection
|
---|
| 154 | ' Class ValuesCollection
|
---|
| 155 | ' Class Enumerator
|
---|
| 156 |
|
---|
| 157 | Private
|
---|
| 158 |
|
---|
| 159 | ' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
|
---|
| 160 |
|
---|
| 161 | Sub initialize(c As Long)
|
---|
| 162 | al = ArrayList.Repeat(Nothing, c)
|
---|
[366] | 163 | count = 0
|
---|
[310] | 164 | End Sub
|
---|
| 165 |
|
---|
[393] | 166 | Function getHash(key As Object) As Long
|
---|
| 167 | Return (key.GetHashCode As DWord) Mod al.Count
|
---|
| 168 | End Function
|
---|
| 169 |
|
---|
[310] | 170 | al As ArrayList
|
---|
[366] | 171 | count As Long
|
---|
[310] | 172 |
|
---|
| 173 | End Class
|
---|
| 174 |
|
---|
| 175 | End Namespace 'Generic
|
---|
| 176 | End Namespace 'Collections
|
---|
| 177 | End Namespace 'System
|
---|