1 | #Region "Microsoft.VisualBasic::61ebdc47db0f1d4716597be0889e90ec, Microsoft.VisualBasic.Core\Extensions\IO\Path\PathMatch.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Structure PathMatch |
35 | ' |
36 | ' Function: __pairs, Pairs, ToString |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports Microsoft.VisualBasic.Serialization.JSON |
44 | |
45 | Namespace FileIO.Path |
46 | |
47 | Public Structure PathMatch |
48 | |
49 | Dim Pair1 As String |
50 | Dim Pair2 As String |
51 | |
52 | Public Overrides Function ToString() As String |
53 | Return Me.GetJson |
54 | End Function |
55 | |
56 | Public Shared Iterator Function Pairs(paths1 As IEnumerable(Of String), path2 As IEnumerable(Of String), Optional process As Func(Of String, String) = Nothing) As IEnumerable(Of PathMatch) |
57 | Dim pas1 As String() = paths1.ToArray |
58 | Dim pas2 As String() = path2.ToArray |
59 | |
60 | If process Is Nothing Then |
61 | process = Function(s) s |
62 | End If |
63 | |
64 | If pas1.Length >= pas2.Length Then |
65 | For Each x As PathMatch In __pairs(pas1, pas2, process) |
66 | Yield x |
67 | Next |
68 | Else |
69 | For Each x As PathMatch In __pairs(pas2, pas1, process) |
70 | Yield x |
71 | Next |
72 | End If |
73 | End Function |
74 | |
75 | ''' <summary> |
76 | ''' <paramref name="paths1"/>的元素要比<paramref name="path2"/>多 |
77 | ''' </summary> |
78 | ''' <param name="paths1"></param> |
79 | ''' <param name="path2"></param> |
80 | ''' <returns></returns> |
81 | Private Shared Iterator Function __pairs(paths1 As String(), path2 As String(), process As Func(Of String, String)) As IEnumerable(Of PathMatch) |
82 | Dim pls = (From p As String In path2 Select name = process(p.BaseName), p).ToArray |
83 | |
84 | For Each path As String In paths1 |
85 | Dim q As String = process(path.BaseName) |
86 | |
87 | For Each S In pls |
88 | If InStr(q, S.name, CompareMethod.Text) = 1 OrElse |
89 | InStr(S.name, q, CompareMethod.Text) = 1 Then |
90 | Yield New PathMatch With { |
91 | .Pair1 = path, |
92 | .Pair2 = S.p |
93 | } |
94 | Exit For |
95 | End If |
96 | Next |
97 | Next |
98 | End Function |
99 | End Structure |
100 | End Namespace |