1 |
#Region "Microsoft.VisualBasic::5b6f63c84f05a1517436f476a4735f98, Microsoft.VisualBasic.Core\ComponentModel\System.Collections.Generic\MapsHelper.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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
#End Region
|
68 |
|
69 |
Imports System.Runtime.CompilerServices
|
70 |
Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
|
71 |
Imports Microsoft.VisualBasic.Language
|
72 |
Imports Microsoft.VisualBasic.Language.Default
|
73 |
Imports Microsoft.VisualBasic.Serialization.JSON
|
74 |
Imports Microsoft.VisualBasic.Text
|
75 |
|
76 |
Namespace ComponentModel
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
Public Class MapsHelper(Of T) : Implements IEnumerable(Of Map(Of String, T))
|
83 |
|
84 |
Protected ReadOnly __default As T
|
85 |
Protected ReadOnly __maps As IDictionary(Of String, T)
|
86 |
|
87 |
Default Public ReadOnly Property Value(key$) As T
|
88 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
89 |
Get
|
90 |
Return GetValue(key)
|
91 |
End Get
|
92 |
End Property
|
93 |
|
94 |
Public ReadOnly Property [Default] As T
|
95 |
Get
|
96 |
Return __default
|
97 |
End Get
|
98 |
End Property
|
99 |
|
100 |
Sub New(map As IReadOnlyDictionary(Of String, T), Optional [default] As T = Nothing)
|
101 |
__default = [default]
|
102 |
__maps = map
|
103 |
End Sub
|
104 |
|
105 |
Public Function GetValue(key As String) As T
|
106 |
If __maps.ContainsKey(key) Then
|
107 |
Return __maps(key)
|
108 |
Else
|
109 |
Return __default
|
110 |
End If
|
111 |
End Function
|
112 |
|
113 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
114 |
Public Overrides Function ToString() As String
|
115 |
Return __maps.ToDictionary.GetJson
|
116 |
End Function
|
117 |
|
118 |
Public Iterator Function GetEnumerator() As IEnumerator(Of Map(Of String, T)) Implements IEnumerable(Of Map(Of String, T)).GetEnumerator
|
119 |
For Each tuple As KeyValuePair(Of String, T) In __maps
|
120 |
Yield New Map(Of String, T)(tuple.Key, tuple.Value)
|
121 |
Next
|
122 |
End Function
|
123 |
|
124 |
Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
|
125 |
Yield GetEnumerator()
|
126 |
End Function
|
127 |
End Class
|
128 |
|
129 |
Public Class NameMapping : Inherits MapsHelper(Of String)
|
130 |
|
131 |
Shared ReadOnly emptyMaps As DefaultValue(Of Dictionary(Of String, String)) = New Dictionary(Of String, String)()
|
132 |
|
133 |
Sub New(Optional dictionary As Dictionary(Of String, String) = Nothing,
|
134 |
Optional default$ = Nothing)
|
135 |
Call MyBase.New(dictionary Or emptyMaps, [default])
|
136 |
End Sub
|
137 |
|
138 |
Public Sub Add(key$, map$)
|
139 |
Call __maps.Add(key, map)
|
140 |
End Sub
|
141 |
|
142 |
Public Shared Widening Operator CType(table$(,)) As NameMapping
|
143 |
Dim dictionary As New Dictionary(Of String, String)
|
144 |
|
145 |
For Each map In table.RowIterator
|
146 |
Call dictionary.Add(map(0), map(1))
|
147 |
Next
|
148 |
|
149 |
Return New NameMapping(dictionary, "")
|
150 |
End Operator
|
151 |
|
152 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
153 |
Public Shared Narrowing Operator CType(maps As NameMapping) As Dictionary(Of String, String)
|
154 |
If maps Is Nothing Then
|
155 |
Return Nothing
|
156 |
Else
|
157 |
Return New Dictionary(Of String, String)(dictionary:=maps.__maps)
|
158 |
End If
|
159 |
End Operator
|
160 |
End Class
|
161 |
|
162 |
Public Structure Map(Of T1, V)
|
163 |
Implements IMap
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
Public Property Key As T1 Implements IMap.Key
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
Public Property Maps As V Implements IMap.Maps
|
175 |
|
176 |
Sub New(x As T1, y As V)
|
177 |
Key = x
|
178 |
Maps = y
|
179 |
End Sub
|
180 |
|
181 |
Public Overrides Function ToString() As String
|
182 |
Return $"map[{Key}, {Maps}]"
|
183 |
End Function
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
Public Interface IMap
|
189 |
Property Key As T1
|
190 |
Property Maps As V
|
191 |
End Interface
|
192 |
|
193 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
194 |
Public Shared Narrowing Operator CType(map As Map(Of T1, V)) As (key As T1, mapAs As V)
|
195 |
Return (map.Key, map.Maps)
|
196 |
End Operator
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
205 |
Public Shared Narrowing Operator CType(map As Map(Of T1, V)) As V
|
206 |
Return map.Maps
|
207 |
End Operator
|
208 |
End Structure
|
209 |
|
210 |
|
211 |
字符串类型的映射
|
212 |
|
213 |
Public Structure IDMap : Implements Map(Of String, String).IMap
|
214 |
Implements INamedValue
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
<remarks>
|
221 |
虽然说这个对象也实现了<see cref="INamedValue"/>接口,但是Key也可能是会出现重复的
|
222 |
</remarks>
|
223 |
Public Property Key As String Implements Map(Of String, String).IMap.Key, INamedValue.Key
|
224 |
Public Property Maps As String Implements Map(Of String, String).IMap.Maps
|
225 |
|
226 |
|
227 |
将这个映射转换为tsv文件之中的一行数据
|
228 |
|
229 |
|
230 |
|
231 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
232 |
Public Function TSV() As String
|
233 |
Return Key & vbTab & Maps
|
234 |
End Function
|
235 |
|
236 |
Public Shared Function ParseFromTsv(line$) As IDMap
|
237 |
Dim t = line.Split(ASCII.TAB)
|
238 |
|
239 |
If t.Length = 0 Then
|
240 |
Return New IDMap
|
241 |
ElseIf t.Length = 1 Then
|
242 |
t.Add("")
|
243 |
End If
|
244 |
|
245 |
Return New IDMap With {
|
246 |
.Key = t(0),
|
247 |
.Maps = t(1)
|
248 |
}
|
249 |
End Function
|
250 |
|
251 |
|
252 |
将TSV文件之中的数据行解析为IDMapping结果
|
253 |
|
254 |
<param name="path$"></param>
|
255 |
<param name="haveHeader"></param>
|
256 |
<param name="header"></param>
|
257 |
|
258 |
Public Shared Function ParseTsvFile(path$, Optional haveHeader As Boolean = False, Optional ByRef header As IDMap = Nothing) As IDMap()
|
259 |
Dim lines$() = path.ReadAllLines
|
260 |
Dim out As New List(Of IDMap)
|
261 |
|
262 |
If haveHeader Then
|
263 |
header = ParseFromTsv(lines(Scan0))
|
264 |
End If
|
265 |
|
266 |
For Each line As String In lines.Skip(If(haveHeader, 1, 0))
|
267 |
out += IDMap.ParseFromTsv(line)
|
268 |
Next
|
269 |
|
270 |
Return out
|
271 |
End Function
|
272 |
|
273 |
Public Overrides Function ToString() As String
|
274 |
Return $"{Key} --> {Maps}"
|
275 |
End Function
|
276 |
|
277 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
278 |
Public Shared Widening Operator CType(map As Map(Of String, String)) As IDMap
|
279 |
Return New IDMap With {
|
280 |
.Key = map.Key,
|
281 |
.Maps = map.Maps
|
282 |
}
|
283 |
End Operator
|
284 |
|
285 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
286 |
Public Shared Widening Operator CType(map As IDMap) As Map(Of String, String)
|
287 |
Return New Map(Of String, String) With {
|
288 |
.Key = map.Key,
|
289 |
.Maps = map.Maps
|
290 |
}
|
291 |
End Operator
|
292 |
End Structure
|
293 |
End Namespace
|