1 #Region "Microsoft.VisualBasic::51299ecbb99130ea2e39faed48d3df7a, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\base\Combination\Comb.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     '     Class Comb
35     
36     '         Properties: CombList, EOL, NewLine
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: __internalQuery, CreateCompleteObjectPairs, CreateObject, GetObjectPair, ToString
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports Microsoft.VisualBasic.Language
47 Imports Microsoft.VisualBasic.Linq
48
49 Namespace ComponentModel.Algorithm.base
50
51     ''' <summary>
52     ''' 对象类型的组合输出工具,即目标类型的集合之中的元素两两组合配对
53     ''' </summary>
54     ''' <remarks></remarks>
55     Public Class Comb(Of T)
56
57         Dim source As List(Of T)
58         Dim p As Integer = 1
59
60         ''' <summary>
61         ''' 对象列表是否已经完全组合输出
62         ''' </summary>
63         ''' <value></value>
64         ''' <returns></returns>
65         ''' <remarks></remarks>
66         Public ReadOnly Property EOL As Boolean
67             Get
68                 Return Not source.Count >= 2
69             End Get
70         End Property
71
72         ''' <summary>
73         ''' 是否已经开始读取新的一行数据
74         ''' </summary>
75         ''' <value></value>
76         ''' <returns></returns>
77         ''' <remarks></remarks>
78         Public ReadOnly Property NewLine As Boolean = True
79
80         Public ReadOnly Property CombList As Tuple(Of T, T)()()
81             Get
82                 Dim out As Tuple(Of T, T)()() = __internalQuery.ToArray
83                 Return out
84             End Get
85         End Property
86
87         Private Function __internalQuery() As IEnumerable(Of Tuple(Of T, T)())
88             Return From index As Integer
89                    In source.Sequence _
90                        .Take(source.Count - 1) _
91                        .AsParallel
92                    Let combTuples = {
93  _
94                        From offset As Integer
95                        In source.Skip(index + 1).Sequence
96                        Let b = index + 1
97                        Select New Tuple(Of T, T)(source(index), source(b + offset))
98                    }
99                    Select array = combTuples _
100                        .IteratesALL _
101                        .ToArray
102                    Order By array.Length Descending
103         End Function
104
105         Public Function GetObjectPair() As (T, T)
106             If source.Count = 1 Then
107                 Return Nothing
108             End If
109
110             If p < source.Count Then
111                 Dim o As T = source(p)
112                 _NewLine = False
113                 p += 1
114                 Return (source(0), o)
115             Else
116                 source.RemoveAt(0)
117                 p = 1
118                 Dim pair As (T, T) = GetObjectPair()
119                 _NewLine = True
120
121                 Return pair
122             End If
123         End Function
124
125         Friend Sub New()
126         End Sub
127
128         Public Shared Function CreateObject(source As IEnumerable(Of T)) As Comb(Of T)
129             Return New Comb(Of T) With {
130                 .source = source.AsList
131             }
132         End Function
133
134         Public Overrides Function ToString() As String
135             Return String.Format("Comb(Of {0})::There is {1} object last."GetType(T).FullName, source.Count)
136         End Function
137
138         Public Shared Widening Operator CType(source As T()) As Comb(Of T)
139             Return New Comb(Of T) With {
140                 .source = source.AsList
141             }
142         End Operator
143
144         Public Shared Widening Operator CType(source As List(Of T)) As Comb(Of T)
145             Return New Comb(Of T) With {
146                 .source = source
147             }
148         End Operator
149
150         ''' <summary>
151         ''' Creates the completely combination of the elements in the target input collection source.
152         ''' (创建完完全全的两两配对)
153         ''' </summary>
154         ''' <param name="source"></param>
155         ''' <returns></returns>
156         ''' <remarks></remarks>
157         Public Shared Iterator Function CreateCompleteObjectPairs(source As IEnumerable(Of T)) As IEnumerable(Of Tuple(Of T, T)())
158             Dim array As T() = source.ToArray
159
160             For Each i As T In array
161                 Dim tmp As New List(Of Tuple(Of T, T))
162
163                 For Each j As T In array
164                     tmp += New Tuple(Of T, T)(i, j)
165                 Next
166
167                 Yield tmp.ToArray
168             Next
169         End Function
170     End Class
171 End Namespace