1 #Region "Microsoft.VisualBasic::30597085f169577662e128cc9d9bf8e7, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Threads\Groups\DataGroup.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 TaggedGroupData
35     
36     '         Properties: Tag
37     
38     '         FunctionToString
39     
40     '     Class GroupListNode
41     
42     '         Properties: Count, Group, InitReads
43     
44     '         FunctionGetEnumerator, IEnumerable_GetEnumerator, ToString
45     
46     '     Class GroupResult
47     
48     '         Properties: Count, Group, Tag
49     
50     '         Constructor: (+2 OverloadsSub New
51     
52     '         FunctionGetEnumerator, IEnumerable_GetEnumerator
53     
54     '         Sub: (+2 Overloads) Add
55     
56     
57     ' /********************************************************************************/
58
59 #End Region
60
61 Namespace Parallel
62
63     Public MustInherit Class TaggedGroupData(Of T_TAG)
64         Public Overridable Property Tag As T_TAG
65
66         Public Overrides Function ToString() As String
67             Return Tag.ToString
68         End Function
69     End Class
70
71     Public Class GroupListNode(Of T, T_TAG) : Inherits TaggedGroupData(Of T_TAG)
72         Implements IEnumerable(Of T)
73
74         Dim _Group As List(Of T)
75
76         Public Property Group As List(Of T)
77             Get
78                 Return _Group
79             End Get
80             Set(value As List(Of T))
81                 _Group = value
82                 If value.IsNullOrEmpty Then
83                     _InitReads = 0
84                 Else
85                     _InitReads = value.Count
86                 End If
87             End Set
88         End Property
89
90         Public ReadOnly Property Count As Integer
91             Get
92                 Return Group.Count
93             End Get
94         End Property
95
96         ''' <summary>
97         ''' 由于<see cref="Group"/>在分组之后的后续的操作的过程之中元素会发生改变,
98         ''' 所以在这个属性之中存储了在初始化<see cref="Group"/>列表的时候的原始的列表之中的元素的个数以满足一些其他的算法操作
99         ''' </summary>
100         ''' <returns></returns>
101         Public ReadOnly Property InitReads As Integer
102
103         Public Overrides Function ToString() As String
104             Return MyBase.ToString & $" // {NameOf(InitReads)}:={InitReads},  current:={Count}"
105         End Function
106
107         Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
108             For Each obj In Group
109                 Yield obj
110             Next
111         End Function
112
113         Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
114             Yield GetEnumerator()
115         End Function
116     End Class
117
118     ''' <summary>
119     ''' 分组操作的结果
120     ''' </summary>
121     ''' <typeparam name="T">Group的元素的类型</typeparam>
122     ''' <typeparam name="Itag">Group的Key的类型</typeparam>
123     Public Class GroupResult(Of T, Itag) : Inherits TaggedGroupData(Of Itag)
124         Implements IEnumerable(Of T)
125         Implements IGrouping(Of Itag, T)
126
127         Public Overrides Property Tag As Itag Implements IGrouping(Of Itag, T).Key
128         Public Property Group As T()
129             Get
130                 Return __list.ToArray
131             End Get
132             Set(value As T())
133                 Call __list.Clear()
134
135                 If Not value.IsNullOrEmpty Then
136                     Call __list.AddRange(value)
137                 End If
138             End Set
139         End Property
140
141         ReadOnly __list As New List(Of T)
142
143         Public ReadOnly Property Count As Integer
144             Get
145                 Return Group.Length
146             End Get
147         End Property
148
149         Sub New()
150         End Sub
151
152         Sub New(tag As Itag, data As IEnumerable(Of T))
153             Me.Tag = tag
154             Me.Group = data.ToArray
155         End Sub
156
157         Public Sub Add(x As T)
158             __list.Add(x)
159         End Sub
160
161         Public Sub Add(source As IEnumerable(Of T))
162             __list.AddRange(source)
163         End Sub
164
165         Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
166             For i As Integer = 0 To Group.Length - 1
167                 Yield Group(i)
168             Next
169         End Function
170
171         Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
172             Yield GetEnumerator()
173         End Function
174     End Class
175 End Namespace