| 1 |
#Region "Microsoft.VisualBasic::f2b1fa2d773400739822c560a014c5af, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\HashDictionary.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#End Region
|
| 50 |
|
| 51 |
Imports System.Collections.ObjectModel
|
| 52 |
Imports Microsoft.VisualBasic.Language
|
| 53 |
|
| 54 |
Namespace ComponentModel.Collection.Generic
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
Public Class HashDictionary(Of T) : Implements IDisposable
|
| 63 |
Implements IDictionary(Of String, T)
|
| 64 |
Implements IReadOnlyDictionary(Of String, T)
|
| 65 |
|
| 66 |
Protected ReadOnly _hashBuffer As Dictionary(Of String, T)
|
| 67 |
Protected ReadOnly _keysHash As Dictionary(Of String, String)
|
| 68 |
|
| 69 |
Sub New(data As IDictionary(Of String, T))
|
| 70 |
Call Me.New
|
| 71 |
|
| 72 |
If Not data Is Nothing Then
|
| 73 |
For Each ItemObject In data
|
| 74 |
Call Add(ItemObject)
|
| 75 |
Next
|
| 76 |
End If
|
| 77 |
End Sub
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
Sub New()
|
| 85 |
_hashBuffer = New Dictionary(Of String, T)
|
| 86 |
_keysHash = New Dictionary(Of String, String)
|
| 87 |
End Sub
|
| 88 |
|
| 89 |
#Region "Implements System.IDisposable, Generic.IDictionary(Of String, T)"
|
| 90 |
|
| 91 |
Public Sub Add(item As KeyValuePair(Of String, T)) Implements ICollection(Of KeyValuePair(Of String, T)).Add
|
| 92 |
Dim Key As String = item.Key
|
| 93 |
Me(Key) = item.Value
|
| 94 |
End Sub
|
| 95 |
|
| 96 |
Public Sub Clear() Implements ICollection(Of KeyValuePair(Of String, T)).Clear
|
| 97 |
Call _keysHash.Clear()
|
| 98 |
Call _hashBuffer.Clear()
|
| 99 |
End Sub
|
| 100 |
|
| 101 |
Public Function Contains(item As KeyValuePair(Of String, T)) As Boolean Implements ICollection(Of KeyValuePair(Of String, T)).Contains
|
| 102 |
Dim value = Me(item.Key)
|
| 103 |
If item.Value Is Nothing AndAlso value Is Nothing Then
|
| 104 |
Return True
|
| 105 |
ElseIf value Is Nothing Then
|
| 106 |
Return False
|
| 107 |
ElseIf Object.Equals(item.Value, value) Then
|
| 108 |
Return True
|
| 109 |
End If
|
| 110 |
|
| 111 |
Return False
|
| 112 |
End Function
|
| 113 |
|
| 114 |
#If NET_40 = 1 Then
|
| 115 |
Public ReadOnly Property Count As Integer Implements ICollection(Of KeyValuePair(Of String, T)).Count
|
| 116 |
#Else
|
| 117 |
Public ReadOnly Property Count As Integer Implements ICollection(Of KeyValuePair(Of String, T)).Count, IReadOnlyCollection(Of KeyValuePair(Of String, T)).Count
|
| 118 |
#End If
|
| 119 |
Get
|
| 120 |
Return _keysHash.Count
|
| 121 |
End Get
|
| 122 |
End Property
|
| 123 |
|
| 124 |
Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of KeyValuePair(Of String, T)).IsReadOnly
|
| 125 |
Get
|
| 126 |
Return False
|
| 127 |
End Get
|
| 128 |
End Property
|
| 129 |
|
| 130 |
Public Function Remove(item As KeyValuePair(Of String, T)) As Boolean Implements ICollection(Of KeyValuePair(Of String, T)).Remove
|
| 131 |
Dim Key As String = item.Key.ToLower
|
| 132 |
Call _hashBuffer.Remove(Key)
|
| 133 |
Return _keysHash.Remove(Key)
|
| 134 |
End Function
|
| 135 |
|
| 136 |
Public Sub Add(key As String, value As T) Implements IDictionary(Of String, T).Add
|
| 137 |
Call Add(New KeyValuePair(Of String, T)(key, value))
|
| 138 |
End Sub
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
<param name="key">大小写不敏感</param>
|
| 144 |
<returns></returns>
|
| 145 |
|
| 146 |
|
| 147 |
#If NET_40 = 1 Then
|
| 148 |
Public Function ContainsKey(key As String) As Boolean Implements IDictionary(Of String, T).ContainsKey
|
| 149 |
#Else
|
| 150 |
Public Function ContainsKey(key As String) As Boolean Implements IDictionary(Of String, T).ContainsKey, IReadOnlyDictionary(Of String, T).ContainsKey
|
| 151 |
#End If
|
| 152 |
Return _keysHash.ContainsKey(key.ToLower)
|
| 153 |
End Function
|
| 154 |
|
| 155 |
|
| 156 |
添加<see cref="Add"></see>和替换操作主要在这里进行
|
| 157 |
|
| 158 |
<param name="key">大小写不敏感</param>
|
| 159 |
<value></value>
|
| 160 |
<returns></returns>
|
| 161 |
|
| 162 |
|
| 163 |
#If NET_40 = 1 Then
|
| 164 |
Default Public Property Item(key As String) As T Implements IDictionary(Of String, T).Item
|
| 165 |
#Else
|
| 166 |
Default Public Property Item(key As String) As T Implements IDictionary(Of String, T).Item, IReadOnlyDictionary(Of String, T).Item
|
| 167 |
#End If
|
| 168 |
|
| 169 |
Get
|
| 170 |
key = key.ToLower
|
| 171 |
If _keysHash.ContainsKey(key) Then
|
| 172 |
Return _hashBuffer(key)
|
| 173 |
Else
|
| 174 |
Return Nothing
|
| 175 |
End If
|
| 176 |
End Get
|
| 177 |
Set(value As T)
|
| 178 |
Dim query As String = key.ToLower
|
| 179 |
|
| 180 |
If _keysHash.ContainsKey(query) Then
|
| 181 |
_hashBuffer(query) = value
|
| 182 |
Else
|
| 183 |
Call _hashBuffer.Add(query, value)
|
| 184 |
Call _keysHash.Add(query, key)
|
| 185 |
End If
|
| 186 |
End Set
|
| 187 |
End Property
|
| 188 |
|
| 189 |
Public Sub CopyTo(array() As KeyValuePair(Of String, T), arrayIndex As Integer) Implements ICollection(Of KeyValuePair(Of String, T)).CopyTo
|
| 190 |
Dim LQuery = (From item In _keysHash Select New KeyValuePair(Of String, T)(item.Value, _hashBuffer(item.Key))).ToArray
|
| 191 |
Call System.Array.ConstrainedCopy(LQuery, 0, array, arrayIndex, array.Length - arrayIndex)
|
| 192 |
End Sub
|
| 193 |
|
| 194 |
Public ReadOnly Property Keys As ICollection(Of String) Implements IDictionary(Of String, T).Keys
|
| 195 |
Get
|
| 196 |
Return _keysHash.Values
|
| 197 |
End Get
|
| 198 |
End Property
|
| 199 |
|
| 200 |
#If NET_40 = 0 Then
|
| 201 |
Private ReadOnly Property __keys As IEnumerable(Of String) Implements IReadOnlyDictionary(Of String, T).Keys
|
| 202 |
Get
|
| 203 |
Return Keys
|
| 204 |
End Get
|
| 205 |
End Property
|
| 206 |
#End If
|
| 207 |
|
| 208 |
Public Function Remove(key As String) As Boolean Implements IDictionary(Of String, T).Remove
|
| 209 |
key = key.ToLower
|
| 210 |
If Me._keysHash.ContainsKey(key) Then
|
| 211 |
Call _keysHash.Remove(key)
|
| 212 |
Call _hashBuffer.Remove(key)
|
| 213 |
|
| 214 |
Return True
|
| 215 |
Else
|
| 216 |
Return False
|
| 217 |
End If
|
| 218 |
End Function
|
| 219 |
|
| 220 |
#If NET_40 = 1 Then
|
| 221 |
Public Function TryGetValue(key As String, ByRef value As T) As Boolean Implements IDictionary(Of String, T).TryGetValue
|
| 222 |
#Else
|
| 223 |
Public Function TryGetValue(key As String, ByRef value As T) As Boolean Implements IDictionary(Of String, T).TryGetValue, IReadOnlyDictionary(Of String, T).TryGetValue
|
| 224 |
#End If
|
| 225 |
|
| 226 |
value = Me(key)
|
| 227 |
Return _keysHash.ContainsKey(key.ToLower)
|
| 228 |
End Function
|
| 229 |
|
| 230 |
Public ReadOnly Property Values As ICollection(Of T) Implements IDictionary(Of String, T).Values
|
| 231 |
Get
|
| 232 |
Return _hashBuffer.Values
|
| 233 |
End Get
|
| 234 |
End Property
|
| 235 |
|
| 236 |
#If NET_40 = 0 Then
|
| 237 |
Private ReadOnly Property __values As IEnumerable(Of T) Implements IReadOnlyDictionary(Of String, T).Values
|
| 238 |
Get
|
| 239 |
Return Values
|
| 240 |
End Get
|
| 241 |
End Property
|
| 242 |
#End If
|
| 243 |
Public Iterator Function GetEnumerator1() As IEnumerator(Of KeyValuePair(Of String, T)) Implements IEnumerable(Of KeyValuePair(Of String, T)).GetEnumerator
|
| 244 |
For Each ItemObject In Me._hashBuffer
|
| 245 |
Yield New KeyValuePair(Of String, T)(Me._keysHash(ItemObject.Key), ItemObject.Value)
|
| 246 |
Next
|
| 247 |
End Function
|
| 248 |
|
| 249 |
Public Iterator Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
|
| 250 |
Yield GetEnumerator1()
|
| 251 |
End Function
|
| 252 |
|
| 253 |
#Region "IDisposable Support"
|
| 254 |
Private disposedValue As Boolean
|
| 255 |
|
| 256 |
|
| 257 |
Protected Overridable Sub Dispose(disposing As Boolean)
|
| 258 |
If Not Me.disposedValue Then
|
| 259 |
If disposing Then
|
| 260 |
|
| 261 |
Call Me.Clear()
|
| 262 |
End If
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
End If
|
| 267 |
Me.disposedValue = True
|
| 268 |
End Sub
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
Public Sub Dispose() Implements IDisposable.Dispose
|
| 279 |
|
| 280 |
Dispose(True)
|
| 281 |
GC.SuppressFinalize(Me)
|
| 282 |
End Sub
|
| 283 |
#End Region
|
| 284 |
#End Region
|
| 285 |
End Class
|
| 286 |
End Namespace
|