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